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