Commit d14cc276 by Paktalin

All operations on words are moved to Vocabulary class

parent dacdb0e0
......@@ -17,7 +17,9 @@ import com.paktalin.vocabularynotebook.ui.MainActivity
class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView
private var sortOrder:Int = 0
set(value) { field = value; sort() }
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
......@@ -31,7 +33,7 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val wordItem = vocabulary.words[position]
val wordItem = vocabulary.getAt(position)
holder.tvWord.text = wordItem.pojo.word
holder.tvTranslation.text = wordItem.pojo.translation
holder.itemView.setOnClickListener { showPopupMenu(holder.itemView, position) }
......@@ -46,7 +48,7 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener {
if (it.itemId == R.id.option_delete) { deleteWord(position) }
if (it.itemId == R.id.option_edit) { editWord(v, vocabulary.words[position]) }
if (it.itemId == R.id.option_edit) { editWord(v, vocabulary.getAt(position)) }
true
}
popup.show()
......@@ -65,17 +67,17 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
this.sort()
}
fun updateWord(updatedWordItem: WordItem) {
val updatedItemId = vocabulary.words.indexOf(updatedWordItem)
vocabulary.words[updatedItemId] = updatedWordItem
fun updateWord(updatedWord: WordItem) {
vocabulary.updateWord(updatedWord)
this.sort()
}
fun sort() {
// update SortOrder
fun updateSortOrder() {
if (sortOrder == 2) sortOrder = 0
else sortOrder++
}
private fun sort() {
vocabulary.sort(sortOrder)
this.notifyDataSetChanged()
}
......
......@@ -10,9 +10,7 @@ class Vocabulary(words: MutableList<WordItem>) {
}
var pojo:Pojo
//todo set private
var words: MutableList<WordItem>
private var words: MutableList<WordItem>
class Pojo(var title:String?) {
init {
......@@ -42,6 +40,15 @@ class Vocabulary(words: MutableList<WordItem>) {
words.add(0, newWord)
}
fun updateWord(updatedWord:WordItem) {
val updatedItemIndex = words.indexOf(updatedWord)
words[updatedItemIndex] = updatedWord
}
fun getAt(position: Int):WordItem {
return words[position]
}
fun size():Int { return words.size }
//region Private methods
......@@ -58,4 +65,4 @@ class Vocabulary(words: MutableList<WordItem>) {
item1.pojo.translation.compareTo(item2.pojo.translation) })
}
//endregion
}
}
\ No newline at end of file
......@@ -40,7 +40,7 @@ class MainActivity : AppCompatActivity() {
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item!!.itemId == R.id.sort)
(recyclerView.adapter as VocabularyAdapter).sort()
(recyclerView.adapter as VocabularyAdapter).updateSortOrder()
return super.onOptionsItemSelected(item)
}
......
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