Commit 38404f70 by Paktalin

Refactoring

parent 60bd38c2
package com.paktalin.vocabularynotebook.ui.recycler_view package com.paktalin.vocabularynotebook.ui.recycler_view
import android.annotation.SuppressLint
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
...@@ -54,8 +53,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -54,8 +53,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.word_item, parent, false) return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.word_item, parent, false))
return ViewHolder(view)
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
...@@ -72,6 +70,32 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -72,6 +70,32 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
// TODO not allow to edit when tag is being selected // TODO not allow to edit when tag is being selected
} }
fun refresh() {}
fun addWord(newWord: WordPojo) {
Log.d(TAG, "vocabularyAdapter addWord")
vocabulary.addWord(newWord)
this.sort()
}
fun updateWord(wordPojo: WordPojo) {
vocabulary.updateWord(wordPojo)
this.sort()
}
fun filter(query: String) {
vocabulary.displayByQuery(query)
notifyDataSetChanged()
}
fun getModifiedWords(): MutableMap<WordPojo, Boolean> {
return vocabulary.getModified()
}
fun addTagToSelected(tag: String, selectionList: MutableList<Long>) {
vocabulary.addTag(selectionList, tag)
}
private fun showPopupMenu(v: View, position: Int) { private fun showPopupMenu(v: View, position: Int) {
if (state == State.NONE) { if (state == State.NONE) {
val popup = PopupMenu(mainActivity, v) val popup = PopupMenu(mainActivity, v)
...@@ -89,8 +113,6 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -89,8 +113,6 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
} }
} }
fun refresh() {}
private fun deleteWord(wordPojo: WordPojo, position: Int) { private fun deleteWord(wordPojo: WordPojo, position: Int) {
vocabulary.deleteWord(wordPojo) vocabulary.deleteWord(wordPojo)
recyclerView.removeViewAt(position) recyclerView.removeViewAt(position)
...@@ -98,23 +120,11 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -98,23 +120,11 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
this.notifyItemRangeChanged(position, vocabulary.displayedSize()) this.notifyItemRangeChanged(position, vocabulary.displayedSize())
} }
fun addWord(newWord: WordPojo) {
Log.d(TAG, "vocabularyAdapter addWord")
vocabulary.addWord(newWord)
this.sort()
}
fun updateWord(wordPojo: WordPojo) {
vocabulary.updateWord(wordPojo)
this.sort()
}
private fun sort() { private fun sort() {
vocabulary.sort(sort) vocabulary.sort(sort)
this.notifyDataSetChanged() this.notifyDataSetChanged()
} }
@SuppressLint("ResourceType")
private fun editWord(container: View, wordPosition: Int) { private fun editWord(container: View, wordPosition: Int) {
addSubmitFragment(mainActivity.supportFragmentManager, addSubmitFragment(mainActivity.supportFragmentManager,
SubmitEditedFragment().apply { SubmitEditedFragment().apply {
...@@ -127,22 +137,5 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -127,22 +137,5 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
R.id.main_activity_container) R.id.main_activity_container)
} }
fun filter(query: String) { val TAG = "VN/" + VocabularyAdapter::class.java.simpleName
vocabulary.clearDisplayed()
if (query.isEmpty()) vocabulary.displayAll()
else vocabulary.displayByQuery(query)
notifyDataSetChanged()
}
fun getModifiedWords(): MutableMap<WordPojo, Boolean> {
return vocabulary.getModified()
}
fun addTagToSelected(tag: String, selectionList: MutableList<Long>) {
vocabulary.addTag(selectionList, tag)
}
companion object {
private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName
}
} }
\ No newline at end of file
...@@ -65,12 +65,25 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary { ...@@ -65,12 +65,25 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary {
displayedVocabulary.sort(sort) displayedVocabulary.sort(sort)
} }
fun displayAll() { fun getModified(): MutableMap<WordPojo, Boolean> {
displayedVocabulary.addAll(wordList) return modifiedVocabulary.get()
}
fun addTag(selectionList: MutableList<Long>, tag: String) {
selectionList.forEach { position ->
displayedAt(position.toInt()).tag = tag
modifiedVocabulary.addWord(displayedAt(position.toInt()))
}
}
fun getColorForTag(tag: String?): Int {
return colorMap[tag] ?: Color.TRANSPARENT
} }
fun displayByQuery(query: String) { fun displayByQuery(query: String) {
displayedVocabulary.byQuery(query.toLowerCase(), wordList) clearDisplayed()
if (query.isEmpty()) displayAll()
else displayedVocabulary.byQuery(query.toLowerCase(), wordList)
} }
fun displayedAt(position: Int): WordPojo { fun displayedAt(position: Int): WordPojo {
...@@ -81,22 +94,11 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary { ...@@ -81,22 +94,11 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary {
return displayedVocabulary.size() return displayedVocabulary.size()
} }
fun clearDisplayed() { private fun clearDisplayed() {
displayedVocabulary.clear() displayedVocabulary.clear()
} }
fun getModified(): MutableMap<WordPojo, Boolean> { private fun displayAll() {
return modifiedVocabulary.get() displayedVocabulary.addAll(wordList)
}
fun addTag(selectionList: MutableList<Long>, tag: String) {
selectionList.forEach { position ->
displayedAt(position.toInt()).tag = tag
modifiedVocabulary.addWord(displayedAt(position.toInt()))
}
}
fun getColorForTag(tag: String?): Int {
return colorMap[tag] ?: Color.TRANSPARENT
} }
} }
\ 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