Commit 63e5838b by Paktalin

When edit button is pressed EditWordFragment is started

parent bf2888fe
package com.paktalin.vocabularynotebook
import android.annotation.SuppressLint
import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.support.v7.widget.PopupMenu
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.*
import android.widget.LinearLayout
import android.widget.TextView
import com.paktalin.vocabularynotebook.ui.EditWordFragment
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>() {
......@@ -30,24 +32,27 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
val wordItem = wordItems[position]
holder.tvWord.text = wordItem.pojo!!.word
holder.tvTranslation.text = wordItem.pojo!!.translation
holder.itemView.setOnClickListener { openWordItemInfo(wordItem) }
holder.itemView.setOnClickListener { showPopupMenu(holder.itemView, position) }
//todo set click listener to menu
}
override fun getItemCount(): Int { return wordItems.size }
private fun openWordItemInfo(wordItem: WordItem) {
val wordInfoFragment = WordInfoFragment()
val arguments = Bundle()
arguments.putSerializable("wordItem", wordItem)
wordInfoFragment.arguments = arguments
(activity as MainActivity).supportFragmentManager.beginTransaction().add(R.id.content, wordInfoFragment).commit()
private fun showPopupMenu(v: View, position: Int) {
val popup = PopupMenu(activity, v)
val inflater = popup.menuInflater
inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener {
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) {
wordItem.delete()
val position = wordItems.indexOf(wordItem)
wordItems.remove(wordItem)
private fun deleteWordItem(position: Int) {
wordItems[position].delete()
wordItems.removeAt(position)
recyclerView.removeViewAt(position)
this.notifyItemRemoved(position)
this.notifyItemRangeChanged(position, wordItems.size)
......@@ -58,10 +63,29 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
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) {
val tvWord: TextView = itemView.findViewById(R.id.etWord)
val tvTranslation: TextView = itemView.findViewById(R.id.etTranslation)
val layout: LinearLayout = itemView.findViewById(R.id.tableLayout)
val tvWord: TextView = itemView.findViewById(R.id.word)
val tvTranslation: TextView = itemView.findViewById(R.id.translation)
val layout: LinearLayout = itemView.findViewById(R.id.layout)
}
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
class MainActivity : AppCompatActivity() {
lateinit var vocabularyId: String
lateinit var vocabularyFragment: VocabularyFragment
private lateinit var vocabularyFragment: VocabularyFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -55,6 +55,7 @@ class MainActivity : AppCompatActivity() {
userDocument.get()
.addOnSuccessListener { task ->
progress.visibility = View.GONE
if (task.get("vocabularies") != null) {
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
//todo represent specific vocabulary instead of the first one
val vocabulary = db.collection("vocabularies").document(vocabularies[0].id)
......@@ -62,12 +63,13 @@ class MainActivity : AppCompatActivity() {
vocabularyFragment.retrieveWordsData(vocabularyId)
}
}
}
private fun hideKeyboard() {
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
var view = activity.currentFocus
if (view == null) {
......@@ -76,8 +78,9 @@ class MainActivity : AppCompatActivity() {
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
fun deleteWordItem(wordItem: WordItem) {
vocabularyFragment.deleteWordItem(wordItem)
override fun onPause() {
super.onPause()
hideKeyboard()
}
companion object { private val TAG = "VN/" + MainActivity::class.simpleName }
......
......@@ -14,6 +14,7 @@ import android.widget.Toast
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
import kotlinx.android.synthetic.main.editable_word_item.*
import kotlinx.android.synthetic.main.fragment_new_word.*
class NewWordFragment : Fragment() {
......@@ -30,15 +31,15 @@ class NewWordFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
etWord.addTextChangedListener(textWatcher {
wordEmpty = etWord.text.isEmpty() })
word.addTextChangedListener(textWatcher {
wordEmpty = word.text.isEmpty() })
etTranslation.addTextChangedListener(textWatcher {
translationEmpty = etTranslation.text.isEmpty() })
translation.addTextChangedListener(textWatcher {
translationEmpty = translation.text.isEmpty() })
btnClear.setOnClickListener {
etWord.text.clear()
etTranslation.text.clear()
word.text.clear()
translation.text.clear()
}
activity!!.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener { addWord() }
}
......@@ -73,10 +74,10 @@ class NewWordFragment : Fragment() {
private fun showClearButton() { btnClear.visibility = View.VISIBLE }
private fun addWord() {
(activity as MainActivity).hideKeyboard(activity as MainActivity)
(activity as MainActivity).hideKeyboardNotFromActivity(activity as MainActivity)
val word = etWord.text.toString()
val translation = etTranslation.text.toString()
val word = word.text.toString()
val translation = translation.text.toString()
val vocabularyId = (activity as MainActivity).vocabularyId
val newWordItemPojo = WordItem.WordItemPojo(word, translation)
ConfiguredFirestore.instance
......@@ -93,8 +94,8 @@ class NewWordFragment : Fragment() {
}
private fun clearFields() {
etWord.text.clear()
etTranslation.text.clear()
word.text.clear()
translation.text.clear()
}
private fun updateRecycleView(newWordItem: WordItem) {
......
......@@ -60,10 +60,6 @@ class VocabularyFragment : Fragment() {
recyclerView.adapter = adapter
}
fun deleteWordItem(wordItem: WordItem) {
(recyclerView.adapter as VocabularyAdapter).deleteWordItem(wordItem)
}
fun addWordItem(newWordItem: WordItem) {
(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 @@
android:layout_gravity="center">
<TextView
android:id="@+id/etWord"
android:id="@+id/word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
......@@ -19,41 +19,54 @@
app:layout_constraintTop_toBottomOf="@+id/toolBar" />
<TextView
android:id="@+id/etTranslation"
android:id="@+id/translation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etWord" />
app:layout_constraintTop_toBottomOf="@+id/word" />
<RelativeLayout
<LinearLayout
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/colorPrimary">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageButton
android:id="@+id/btnClose"
android:id="@+id/btnEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_cancel_icon"
android:layout_margin="5dp"
app:srcCompat="@drawable/ic_edit_icon"
tools:ignore="ContentDescription"
android:layout_alignParentRight="true" />
android:background="@android:color/transparent" />
<ImageButton
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:srcCompat="@drawable/ic_delete_icon"
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:layout_toLeftOf="@+id/btnClose" />
app:srcCompat="@drawable/ic_cancel_icon"
tools:ignore="ContentDescription" />
</RelativeLayout>
</LinearLayout>
</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 @@
android:layout_marginStart="8dp"
tools:ignore="ContentDescription" />
<LinearLayout
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>
<include layout="@layout/editable_word_item" />
<ImageButton
android:id="@+id/btnClear"
......
......@@ -22,7 +22,7 @@
tools:ignore="ContentDescription" />
<LinearLayout
android:id="@+id/tableLayout"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
......@@ -35,7 +35,7 @@
<TextView
android:id="@+id/etWord"
android:id="@+id/word"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
......@@ -45,7 +45,7 @@
tools:ignore="LabelFor" />
<TextView
android:id="@+id/etTranslation"
android:id="@+id/translation"
android:layout_width="0dp"
android:layout_height="wrap_content"
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"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<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>
\ No newline at end of file
......@@ -11,4 +11,6 @@
<string name="btn_cancel">Cancel</string>
<string name="hint_new_word">new word</string>
<string name="hint_translation">translation</string>
<string name="menu_option_delete">Delete</string>
<string name="menu_option_edit">Edit</string>
</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