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
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.EditWordFragment
import com.paktalin.vocabularynotebook.ui.MainActivity
import java.util.*
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
//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) {
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, vocabulary.words[position]) }
if (it.itemId == R.id.option_delete) { deleteWord(position) }
if (it.itemId == R.id.option_edit) { editWord(v, vocabulary.words[position]) }
true
}
popup.show()
}
private fun deleteWordItem(position: Int) {
vocabulary.words[position].delete()
vocabulary.words.removeAt(position)
private fun deleteWord(position: Int) {
vocabulary.deleteWord(position)
// update recyclerView
recyclerView.removeViewAt(position)
this.notifyItemRemoved(position)
this.notifyItemRangeChanged(position, vocabulary.words.size)
this.notifyItemRangeChanged(position, vocabulary.size())
}
fun addWordItem(newWordItem: WordItem) {
vocabulary.words.add(0, newWordItem)
fun addWord(newWord: WordItem) {
vocabulary.addWord(newWord)
this.sort()
}
fun updateWordItem(updatedWordItem: WordItem) {
fun updateWord(updatedWordItem: WordItem) {
val updatedItemId = vocabulary.words.indexOf(updatedWordItem)
vocabulary.words[updatedItemId] = updatedWordItem
this.sort()
......@@ -82,7 +81,7 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
}
@SuppressLint("ResourceType")
private fun editWordItem(container:View, wordItem: WordItem) {
private fun editWord(container:View, wordItem: WordItem) {
//set container id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
container.id = View.generateViewId()
......
package com.paktalin.vocabularynotebook.firestoreitems
class Vocabulary(var words: MutableList<WordItem>) {
class Vocabulary(words: MutableList<WordItem>) {
companion object {
private val TAG = "VN/" + Vocabulary::class.java.simpleName
......@@ -11,6 +11,9 @@ class Vocabulary(var words: MutableList<WordItem>) {
var pojo:Pojo
//todo set private
var words: MutableList<WordItem>
class Pojo(var title:String?) {
init {
if (title == null) title = "Untitled vocabulary"
......@@ -18,7 +21,8 @@ class Vocabulary(var words: MutableList<WordItem>) {
}
init {
pojo = Pojo(null)
this.pojo = Pojo(null)
this.words = words
}
fun sort(sortOrder:Int) {
......@@ -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() {
words.sortWith(Comparator { item1, item2 ->
-item1.pojo.time!!.compareTo(item2.pojo.time) })
......@@ -41,5 +57,5 @@ class Vocabulary(var words: MutableList<WordItem>) {
words.sortWith(Comparator { item1, item2 ->
item1.pojo.translation.compareTo(item2.pojo.translation) })
}
//endregion
}
......@@ -78,10 +78,10 @@ class VocabularyFragment : Fragment() {
}
fun addWordItem(newWordItem: WordItem) {
(recyclerView.adapter as VocabularyAdapter).addWordItem(newWordItem)
(recyclerView.adapter as VocabularyAdapter).addWord(newWordItem)
}
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