Commit bf2888fe by Paktalin

WordInfo can be closed. WordItem can be deleted from WordInfo

parent 93464da6
...@@ -44,22 +44,10 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va ...@@ -44,22 +44,10 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
(activity as MainActivity).supportFragmentManager.beginTransaction().add(R.id.content, wordInfoFragment).commit() (activity as MainActivity).supportFragmentManager.beginTransaction().add(R.id.content, wordInfoFragment).commit()
} }
private fun showPopupMenu(v: View, position: Int) { fun deleteWordItem(wordItem: WordItem) {
val popup = PopupMenu(activity, v) wordItem.delete()
val inflater = popup.menuInflater val position = wordItems.indexOf(wordItem)
inflater.inflate(R.menu.word_item_menu, popup.menu) wordItems.remove(wordItem)
popup.setOnMenuItemClickListener {
if (it.itemId == R.id.item_delete) {
deleteWordItem(position)
}
true
}
popup.show()
}
private fun deleteWordItem(position: Int) {
wordItems[position].delete()
wordItems.removeAt(position)
recyclerView.removeViewAt(position) recyclerView.removeViewAt(position)
this.notifyItemRemoved(position) this.notifyItemRemoved(position)
this.notifyItemRangeChanged(position, wordItems.size) this.notifyItemRangeChanged(position, wordItems.size)
......
...@@ -13,17 +13,20 @@ import android.view.WindowManager ...@@ -13,17 +13,20 @@ import android.view.WindowManager
import android.app.Activity import android.app.Activity
import android.view.View import android.view.View
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import com.paktalin.vocabularynotebook.WordItem
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
lateinit var vocabularyId: String lateinit var vocabularyId: String
lateinit var vocabularyFragment: VocabularyFragment
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
hideKeyboard() hideKeyboard()
setUpNavigationView() setUpNavigationView()
vocabularyFragment = supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment
extractVocabularyData() extractVocabularyData()
} }
...@@ -56,8 +59,7 @@ class MainActivity : AppCompatActivity() { ...@@ -56,8 +59,7 @@ class MainActivity : AppCompatActivity() {
//todo represent specific vocabulary instead of the first one //todo represent specific vocabulary instead of the first one
val vocabulary = db.collection("vocabularies").document(vocabularies[0].id) val vocabulary = db.collection("vocabularies").document(vocabularies[0].id)
vocabularyId = vocabulary.id vocabularyId = vocabulary.id
(supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment) vocabularyFragment.retrieveWordsData(vocabularyId)
.retrieveWordsData(vocabularyId)
} }
} }
...@@ -74,5 +76,9 @@ class MainActivity : AppCompatActivity() { ...@@ -74,5 +76,9 @@ class MainActivity : AppCompatActivity() {
imm.hideSoftInputFromWindow(view.windowToken, 0) imm.hideSoftInputFromWindow(view.windowToken, 0)
} }
fun deleteWordItem(wordItem: WordItem) {
vocabularyFragment.deleteWordItem(wordItem)
}
companion object { private val TAG = "VN/" + MainActivity::class.simpleName } companion object { private val TAG = "VN/" + MainActivity::class.simpleName }
} }
\ No newline at end of file
...@@ -60,6 +60,10 @@ class VocabularyFragment : Fragment() { ...@@ -60,6 +60,10 @@ 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)
} }
......
...@@ -2,7 +2,6 @@ package com.paktalin.vocabularynotebook.ui ...@@ -2,7 +2,6 @@ package com.paktalin.vocabularynotebook.ui
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
...@@ -22,6 +21,8 @@ class WordInfoFragment : Fragment() { ...@@ -22,6 +21,8 @@ class WordInfoFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
setData() setData()
btnClose.setOnClickListener { closeFragment() }
btnDelete.setOnClickListener { deleteItem() }
} }
private fun setData() { private fun setData() {
...@@ -29,4 +30,13 @@ class WordInfoFragment : Fragment() { ...@@ -29,4 +30,13 @@ class WordInfoFragment : Fragment() {
etTranslation.text = wordItem.pojo!!.translation 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
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
<android.support.constraint.ConstraintLayout <android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="250dp" xmlns:tools="http://schemas.android.com/tools"
android:layout_height="180dp" android:layout_width="300dp"
android:layout_height="200dp"
android:background="@android:color/white" android:background="@android:color/white"
android:layout_gravity="center" android:layout_gravity="center">
>
<TextView <TextView
android:id="@+id/etWord" android:id="@+id/etWord"
...@@ -15,9 +15,8 @@ ...@@ -15,9 +15,8 @@
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:text="Word"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toBottomOf="@+id/toolBar" />
<TextView <TextView
android:id="@+id/etTranslation" android:id="@+id/etTranslation"
...@@ -26,8 +25,35 @@ ...@@ -26,8 +25,35 @@
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:text="Translation"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etWord" /> app:layout_constraintTop_toBottomOf="@+id/etWord" />
<RelativeLayout
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/colorPrimary">
<ImageButton
android:id="@+id/btnClose"
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"
tools:ignore="ContentDescription"
android:layout_alignParentRight="true" />
<ImageButton
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_delete_icon"
tools:ignore="ContentDescription"
android:background="@android:color/transparent"
android:layout_toLeftOf="@+id/btnClose" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
\ No newline at end of file
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