Commit dacdb0e0 by Paktalin

Word addition and deletion is made inside Vocabulary class

parent af63849b
...@@ -13,7 +13,6 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary ...@@ -13,7 +13,6 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.EditWordFragment import com.paktalin.vocabularynotebook.ui.EditWordFragment
import com.paktalin.vocabularynotebook.ui.MainActivity import com.paktalin.vocabularynotebook.ui.MainActivity
import java.util.*
class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() { class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
...@@ -39,34 +38,34 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity ...@@ -39,34 +38,34 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
//todo set click listener to menu //todo set click listener to menu
} }
override fun getItemCount(): Int { return vocabulary.words.size } override fun getItemCount(): Int { return vocabulary.size() }
private fun showPopupMenu(v: View, position: Int) { private fun showPopupMenu(v: View, position: Int) {
val popup = PopupMenu(activity, v) val popup = PopupMenu(activity, v)
val inflater = popup.menuInflater val inflater = popup.menuInflater
inflater.inflate(R.menu.word_item_menu, popup.menu) inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener { popup.setOnMenuItemClickListener {
if (it.itemId == R.id.option_delete) { deleteWordItem(position) } if (it.itemId == R.id.option_delete) { deleteWord(position) }
if (it.itemId == R.id.option_edit) { editWordItem(v, vocabulary.words[position]) } if (it.itemId == R.id.option_edit) { editWord(v, vocabulary.words[position]) }
true true
} }
popup.show() popup.show()
} }
private fun deleteWordItem(position: Int) { private fun deleteWord(position: Int) {
vocabulary.words[position].delete() vocabulary.deleteWord(position)
vocabulary.words.removeAt(position) // update recyclerView
recyclerView.removeViewAt(position) recyclerView.removeViewAt(position)
this.notifyItemRemoved(position) this.notifyItemRemoved(position)
this.notifyItemRangeChanged(position, vocabulary.words.size) this.notifyItemRangeChanged(position, vocabulary.size())
} }
fun addWordItem(newWordItem: WordItem) { fun addWord(newWord: WordItem) {
vocabulary.words.add(0, newWordItem) vocabulary.addWord(newWord)
this.sort() this.sort()
} }
fun updateWordItem(updatedWordItem: WordItem) { fun updateWord(updatedWordItem: WordItem) {
val updatedItemId = vocabulary.words.indexOf(updatedWordItem) val updatedItemId = vocabulary.words.indexOf(updatedWordItem)
vocabulary.words[updatedItemId] = updatedWordItem vocabulary.words[updatedItemId] = updatedWordItem
this.sort() this.sort()
...@@ -82,7 +81,7 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity ...@@ -82,7 +81,7 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
} }
@SuppressLint("ResourceType") @SuppressLint("ResourceType")
private fun editWordItem(container:View, wordItem: WordItem) { private fun editWord(container:View, wordItem: WordItem) {
//set container id //set container id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
container.id = View.generateViewId() container.id = View.generateViewId()
......
package com.paktalin.vocabularynotebook.firestoreitems package com.paktalin.vocabularynotebook.firestoreitems
class Vocabulary(var words: MutableList<WordItem>) { class Vocabulary(words: MutableList<WordItem>) {
companion object { companion object {
private val TAG = "VN/" + Vocabulary::class.java.simpleName private val TAG = "VN/" + Vocabulary::class.java.simpleName
...@@ -11,6 +11,9 @@ class Vocabulary(var words: MutableList<WordItem>) { ...@@ -11,6 +11,9 @@ class Vocabulary(var words: MutableList<WordItem>) {
var pojo:Pojo var pojo:Pojo
//todo set private
var words: MutableList<WordItem>
class Pojo(var title:String?) { class Pojo(var title:String?) {
init { init {
if (title == null) title = "Untitled vocabulary" if (title == null) title = "Untitled vocabulary"
...@@ -18,7 +21,8 @@ class Vocabulary(var words: MutableList<WordItem>) { ...@@ -18,7 +21,8 @@ class Vocabulary(var words: MutableList<WordItem>) {
} }
init { init {
pojo = Pojo(null) this.pojo = Pojo(null)
this.words = words
} }
fun sort(sortOrder:Int) { fun sort(sortOrder:Int) {
...@@ -29,6 +33,18 @@ class Vocabulary(var words: MutableList<WordItem>) { ...@@ -29,6 +33,18 @@ class Vocabulary(var words: MutableList<WordItem>) {
} }
} }
fun deleteWord(position:Int) {
words[position].delete() // delete word from the database
words.removeAt(position) // delete word from the list
}
fun addWord(newWord:WordItem) {
words.add(0, newWord)
}
fun size():Int { return words.size }
//region Private methods
private fun sortByTime() { private fun sortByTime() {
words.sortWith(Comparator { item1, item2 -> words.sortWith(Comparator { item1, item2 ->
-item1.pojo.time!!.compareTo(item2.pojo.time) }) -item1.pojo.time!!.compareTo(item2.pojo.time) })
...@@ -41,5 +57,5 @@ class Vocabulary(var words: MutableList<WordItem>) { ...@@ -41,5 +57,5 @@ class Vocabulary(var words: MutableList<WordItem>) {
words.sortWith(Comparator { item1, item2 -> words.sortWith(Comparator { item1, item2 ->
item1.pojo.translation.compareTo(item2.pojo.translation) }) item1.pojo.translation.compareTo(item2.pojo.translation) })
} }
//endregion
} }
...@@ -78,10 +78,10 @@ class VocabularyFragment : Fragment() { ...@@ -78,10 +78,10 @@ class VocabularyFragment : Fragment() {
} }
fun addWordItem(newWordItem: WordItem) { fun addWordItem(newWordItem: WordItem) {
(recyclerView.adapter as VocabularyAdapter).addWordItem(newWordItem) (recyclerView.adapter as VocabularyAdapter).addWord(newWordItem)
} }
fun updateWordItem(updatedWordItem: WordItem) { fun updateWordItem(updatedWordItem: WordItem) {
(recyclerView.adapter as VocabularyAdapter).updateWordItem(updatedWordItem) (recyclerView.adapter as VocabularyAdapter).updateWord(updatedWordItem)
} }
} }
\ 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