Commit 63e5838b by Paktalin

When edit button is pressed EditWordFragment is started

parent bf2888fe
package com.paktalin.vocabularynotebook package com.paktalin.vocabularynotebook
import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.support.v7.widget.PopupMenu import android.support.v7.widget.PopupMenu
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.* import android.view.*
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import com.paktalin.vocabularynotebook.ui.EditWordFragment
import com.paktalin.vocabularynotebook.ui.MainActivity import com.paktalin.vocabularynotebook.ui.MainActivity
import com.paktalin.vocabularynotebook.ui.WordInfoFragment
class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() { class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
...@@ -30,24 +32,27 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va ...@@ -30,24 +32,27 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
val wordItem = wordItems[position] val wordItem = wordItems[position]
holder.tvWord.text = wordItem.pojo!!.word holder.tvWord.text = wordItem.pojo!!.word
holder.tvTranslation.text = wordItem.pojo!!.translation holder.tvTranslation.text = wordItem.pojo!!.translation
holder.itemView.setOnClickListener { openWordItemInfo(wordItem) } holder.itemView.setOnClickListener { showPopupMenu(holder.itemView, position) }
//todo set click listener to menu //todo set click listener to menu
} }
override fun getItemCount(): Int { return wordItems.size } override fun getItemCount(): Int { return wordItems.size }
private fun openWordItemInfo(wordItem: WordItem) { private fun showPopupMenu(v: View, position: Int) {
val wordInfoFragment = WordInfoFragment() val popup = PopupMenu(activity, v)
val arguments = Bundle() val inflater = popup.menuInflater
arguments.putSerializable("wordItem", wordItem) inflater.inflate(R.menu.word_item_menu, popup.menu)
wordInfoFragment.arguments = arguments popup.setOnMenuItemClickListener {
(activity as MainActivity).supportFragmentManager.beginTransaction().add(R.id.content, wordInfoFragment).commit() if (it.itemId == R.id.option_delete) { deleteWordItem(position) }
if (it.itemId == R.id.option_edit) { editWordItem(v, wordItems[position]) }
true
}
popup.show()
} }
fun deleteWordItem(wordItem: WordItem) { private fun deleteWordItem(position: Int) {
wordItem.delete() wordItems[position].delete()
val position = wordItems.indexOf(wordItem) wordItems.removeAt(position)
wordItems.remove(wordItem)
recyclerView.removeViewAt(position) recyclerView.removeViewAt(position)
this.notifyItemRemoved(position) this.notifyItemRemoved(position)
this.notifyItemRangeChanged(position, wordItems.size) this.notifyItemRangeChanged(position, wordItems.size)
...@@ -58,10 +63,29 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va ...@@ -58,10 +63,29 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
this.notifyItemInserted(0) this.notifyItemInserted(0)
} }
@SuppressLint("ResourceType")
private fun editWordItem(container:View, wordItem: WordItem) {
//hide textViews
container.findViewById<TextView>(R.id.word).visibility = View.GONE
container.findViewById<TextView>(R.id.translation).visibility = View.GONE
//set container id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
container.id = View.generateViewId()
} else container.id = 18071999
// start fragment
val wordInfoFragment = EditWordFragment()
val arguments = Bundle()
arguments.putSerializable("wordItem", wordItem)
wordInfoFragment.arguments = arguments
(activity as MainActivity).supportFragmentManager.beginTransaction().add(container.id, wordInfoFragment).commit()
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvWord: TextView = itemView.findViewById(R.id.etWord) val tvWord: TextView = itemView.findViewById(R.id.word)
val tvTranslation: TextView = itemView.findViewById(R.id.etTranslation) val tvTranslation: TextView = itemView.findViewById(R.id.translation)
val layout: LinearLayout = itemView.findViewById(R.id.tableLayout) val layout: LinearLayout = itemView.findViewById(R.id.layout)
} }
companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName } companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName }
......
package com.paktalin.vocabularynotebook.ui
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
class EditWordFragment : Fragment() {
private lateinit var wordItem: WordItem
private lateinit var etWord: EditText
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
wordItem = arguments!!["wordItem"] as WordItem
return inflater.inflate(R.layout.editable_word_item, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
etWord = view!!.findViewById(R.id.word)
setWordItemData()
setFocusOnWord()
}
private fun setWordItemData() {
etWord.setText(wordItem.pojo!!.word)
view!!.findViewById<EditText>(R.id.translation).setText(wordItem.pojo!!.translation)
}
private fun setFocusOnWord() {
activity!!.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
etWord.requestFocus()
val imm = activity!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm!!.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
}
...@@ -19,7 +19,7 @@ import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore ...@@ -19,7 +19,7 @@ import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
lateinit var vocabularyId: String lateinit var vocabularyId: String
lateinit var vocabularyFragment: VocabularyFragment private lateinit var vocabularyFragment: VocabularyFragment
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -55,11 +55,13 @@ class MainActivity : AppCompatActivity() { ...@@ -55,11 +55,13 @@ class MainActivity : AppCompatActivity() {
userDocument.get() userDocument.get()
.addOnSuccessListener { task -> .addOnSuccessListener { task ->
progress.visibility = View.GONE progress.visibility = View.GONE
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference> if (task.get("vocabularies") != null) {
//todo represent specific vocabulary instead of the first one val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
val vocabulary = db.collection("vocabularies").document(vocabularies[0].id) //todo represent specific vocabulary instead of the first one
vocabularyId = vocabulary.id val vocabulary = db.collection("vocabularies").document(vocabularies[0].id)
vocabularyFragment.retrieveWordsData(vocabularyId) vocabularyId = vocabulary.id
vocabularyFragment.retrieveWordsData(vocabularyId)
}
} }
} }
...@@ -67,7 +69,7 @@ class MainActivity : AppCompatActivity() { ...@@ -67,7 +69,7 @@ class MainActivity : AppCompatActivity() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
} }
fun hideKeyboard(activity: Activity) { fun hideKeyboardNotFromActivity(activity: Activity) {
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
var view = activity.currentFocus var view = activity.currentFocus
if (view == null) { if (view == null) {
...@@ -76,8 +78,9 @@ class MainActivity : AppCompatActivity() { ...@@ -76,8 +78,9 @@ class MainActivity : AppCompatActivity() {
imm.hideSoftInputFromWindow(view.windowToken, 0) imm.hideSoftInputFromWindow(view.windowToken, 0)
} }
fun deleteWordItem(wordItem: WordItem) { override fun onPause() {
vocabularyFragment.deleteWordItem(wordItem) super.onPause()
hideKeyboard()
} }
companion object { private val TAG = "VN/" + MainActivity::class.simpleName } companion object { private val TAG = "VN/" + MainActivity::class.simpleName }
......
...@@ -14,6 +14,7 @@ import android.widget.Toast ...@@ -14,6 +14,7 @@ import android.widget.Toast
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem import com.paktalin.vocabularynotebook.WordItem
import kotlinx.android.synthetic.main.editable_word_item.*
import kotlinx.android.synthetic.main.fragment_new_word.* import kotlinx.android.synthetic.main.fragment_new_word.*
class NewWordFragment : Fragment() { class NewWordFragment : Fragment() {
...@@ -30,15 +31,15 @@ class NewWordFragment : Fragment() { ...@@ -30,15 +31,15 @@ class NewWordFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
etWord.addTextChangedListener(textWatcher { word.addTextChangedListener(textWatcher {
wordEmpty = etWord.text.isEmpty() }) wordEmpty = word.text.isEmpty() })
etTranslation.addTextChangedListener(textWatcher { translation.addTextChangedListener(textWatcher {
translationEmpty = etTranslation.text.isEmpty() }) translationEmpty = translation.text.isEmpty() })
btnClear.setOnClickListener { btnClear.setOnClickListener {
etWord.text.clear() word.text.clear()
etTranslation.text.clear() translation.text.clear()
} }
activity!!.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener { addWord() } activity!!.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener { addWord() }
} }
...@@ -73,10 +74,10 @@ class NewWordFragment : Fragment() { ...@@ -73,10 +74,10 @@ class NewWordFragment : Fragment() {
private fun showClearButton() { btnClear.visibility = View.VISIBLE } private fun showClearButton() { btnClear.visibility = View.VISIBLE }
private fun addWord() { private fun addWord() {
(activity as MainActivity).hideKeyboard(activity as MainActivity) (activity as MainActivity).hideKeyboardNotFromActivity(activity as MainActivity)
val word = etWord.text.toString() val word = word.text.toString()
val translation = etTranslation.text.toString() val translation = translation.text.toString()
val vocabularyId = (activity as MainActivity).vocabularyId val vocabularyId = (activity as MainActivity).vocabularyId
val newWordItemPojo = WordItem.WordItemPojo(word, translation) val newWordItemPojo = WordItem.WordItemPojo(word, translation)
ConfiguredFirestore.instance ConfiguredFirestore.instance
...@@ -93,8 +94,8 @@ class NewWordFragment : Fragment() { ...@@ -93,8 +94,8 @@ class NewWordFragment : Fragment() {
} }
private fun clearFields() { private fun clearFields() {
etWord.text.clear() word.text.clear()
etTranslation.text.clear() translation.text.clear()
} }
private fun updateRecycleView(newWordItem: WordItem) { private fun updateRecycleView(newWordItem: WordItem) {
......
...@@ -60,10 +60,6 @@ class VocabularyFragment : Fragment() { ...@@ -60,10 +60,6 @@ class VocabularyFragment : Fragment() {
recyclerView.adapter = adapter recyclerView.adapter = adapter
} }
fun deleteWordItem(wordItem: WordItem) {
(recyclerView.adapter as VocabularyAdapter).deleteWordItem(wordItem)
}
fun addWordItem(newWordItem: WordItem) { fun addWordItem(newWordItem: WordItem) {
(recyclerView.adapter as VocabularyAdapter).addWordItem(newWordItem) (recyclerView.adapter as VocabularyAdapter).addWordItem(newWordItem)
} }
......
package com.paktalin.vocabularynotebook.ui
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
import kotlinx.android.synthetic.main.activity_word_info.*
class WordInfoFragment : Fragment() {
private lateinit var wordItem:WordItem
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
wordItem = arguments!!["wordItem"] as WordItem
return inflater.inflate(R.layout.activity_word_info, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setData()
btnClose.setOnClickListener { closeFragment() }
btnDelete.setOnClickListener { deleteItem() }
}
private fun setData() {
etWord.text = wordItem.pojo!!.word
etTranslation.text = wordItem.pojo!!.translation
}
private fun closeFragment() {
activity!!.supportFragmentManager.beginTransaction().remove(this).commit()
}
private fun deleteItem() {
(activity as MainActivity).deleteWordItem(wordItem)
closeFragment()
}
}
\ No newline at end of file
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
android:layout_gravity="center"> android:layout_gravity="center">
<TextView <TextView
android:id="@+id/etWord" android:id="@+id/word"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
...@@ -19,41 +19,54 @@ ...@@ -19,41 +19,54 @@
app:layout_constraintTop_toBottomOf="@+id/toolBar" /> app:layout_constraintTop_toBottomOf="@+id/toolBar" />
<TextView <TextView
android:id="@+id/etTranslation" android:id="@+id/translation"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etWord" /> app:layout_constraintTop_toBottomOf="@+id/word" />
<RelativeLayout <LinearLayout
android:id="@+id/toolBar" android:id="@+id/toolBar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="horizontal"
android:background="@color/colorPrimary"> android:background="@color/colorPrimary">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageButton <ImageButton
android:id="@+id/btnClose" android:id="@+id/btnEdit"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:layout_margin="5dp"
android:background="@android:color/transparent" app:srcCompat="@drawable/ic_edit_icon"
app:srcCompat="@drawable/ic_cancel_icon"
tools:ignore="ContentDescription" tools:ignore="ContentDescription"
android:layout_alignParentRight="true" /> android:background="@android:color/transparent" />
<ImageButton <ImageButton
android:id="@+id/btnDelete" android:id="@+id/btnDelete"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="5dp"
app:srcCompat="@drawable/ic_delete_icon" app:srcCompat="@drawable/ic_delete_icon"
tools:ignore="ContentDescription" tools:ignore="ContentDescription"
android:background="@android:color/transparent" />
<ImageButton
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:layout_toLeftOf="@+id/btnClose" /> app:srcCompat="@drawable/ic_cancel_icon"
tools:ignore="ContentDescription" />
</RelativeLayout> </LinearLayout>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:paddingRight="16dp"
android:paddingEnd="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="@android:color/transparent"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/word"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="@string/hint_new_word"
android:inputType="text"
android:textSize="22sp"
app:fontFamily="@font/neucha"
android:textColor="#000F55"
tools:ignore="LabelFor" />
<EditText
android:id="@+id/translation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="@string/hint_translation"
android:inputType="text"
android:textSize="22sp"
app:fontFamily="@font/neucha"
android:textColor="#000F55"
tools:ignore="LabelFor" />
</LinearLayout>
\ No newline at end of file
...@@ -20,44 +20,7 @@ ...@@ -20,44 +20,7 @@
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />
<LinearLayout <include layout="@layout/editable_word_item" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:paddingRight="16dp"
android:paddingEnd="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="@android:color/transparent">
<EditText
android:id="@+id/etWord"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="@string/hint_new_word"
android:inputType="text"
android:textSize="22sp"
app:fontFamily="@font/neucha"
android:textColor="#000F55"
tools:ignore="LabelFor" />
<EditText
android:id="@+id/etTranslation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="@string/hint_translation"
android:inputType="text"
android:textSize="22sp"
app:fontFamily="@font/neucha"
android:textColor="#000F55"
tools:ignore="LabelFor" />
</LinearLayout>
<ImageButton <ImageButton
android:id="@+id/btnClear" android:id="@+id/btnClear"
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />
<LinearLayout <LinearLayout
android:id="@+id/tableLayout" android:id="@+id/layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="horizontal"
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
<TextView <TextView
android:id="@+id/etWord" android:id="@+id/word"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
tools:ignore="LabelFor" /> tools:ignore="LabelFor" />
<TextView <TextView
android:id="@+id/etTranslation" android:id="@+id/translation"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
......
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Delete"
android:id="@+id/item_delete"
android:icon="@drawable/ic_delete_icon"
app:showAsAction="ifRoom"/>
<item android:title="Edit"
android:id="@+id/item_edit"
android:icon="@drawable/ic_edit_icon"
app:showAsAction="ifRoom"/>
</menu>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="@string/menu_option_delete"
android:id="@+id/option_delete"/>
<item android:title="@string/menu_option_edit"
android:id="@+id/option_edit"/>
<item android:title="Delete"
android:id="@+id/item_delete"
android:icon="@drawable/ic_delete_icon"
app:showAsAction="always"/>
</menu> </menu>
\ No newline at end of file
...@@ -11,4 +11,6 @@ ...@@ -11,4 +11,6 @@
<string name="btn_cancel">Cancel</string> <string name="btn_cancel">Cancel</string>
<string name="hint_new_word">new word</string> <string name="hint_new_word">new word</string>
<string name="hint_translation">translation</string> <string name="hint_translation">translation</string>
<string name="menu_option_delete">Delete</string>
<string name="menu_option_edit">Edit</string>
</resources> </resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment