Commit 38404f70 by Paktalin

Refactoring

parent 60bd38c2
package com.paktalin.vocabularynotebook.ui.recycler_view
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -54,8 +53,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.word_item, parent, false)
return ViewHolder(view)
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.word_item, parent, false))
}
override fun getItemCount(): Int {
......@@ -72,6 +70,32 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
// 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) {
if (state == State.NONE) {
val popup = PopupMenu(mainActivity, v)
......@@ -89,8 +113,6 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
}
}
fun refresh() {}
private fun deleteWord(wordPojo: WordPojo, position: Int) {
vocabulary.deleteWord(wordPojo)
recyclerView.removeViewAt(position)
......@@ -98,23 +120,11 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
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() {
vocabulary.sort(sort)
this.notifyDataSetChanged()
}
@SuppressLint("ResourceType")
private fun editWord(container: View, wordPosition: Int) {
addSubmitFragment(mainActivity.supportFragmentManager,
SubmitEditedFragment().apply {
......@@ -127,22 +137,5 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
R.id.main_activity_container)
}
fun filter(query: String) {
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
}
val TAG = "VN/" + VocabularyAdapter::class.java.simpleName
}
\ No newline at end of file
......@@ -65,12 +65,25 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary {
displayedVocabulary.sort(sort)
}
fun displayAll() {
displayedVocabulary.addAll(wordList)
fun getModified(): MutableMap<WordPojo, Boolean> {
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) {
displayedVocabulary.byQuery(query.toLowerCase(), wordList)
clearDisplayed()
if (query.isEmpty()) displayAll()
else displayedVocabulary.byQuery(query.toLowerCase(), wordList)
}
fun displayedAt(position: Int): WordPojo {
......@@ -81,22 +94,11 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary {
return displayedVocabulary.size()
}
fun clearDisplayed() {
private fun clearDisplayed() {
displayedVocabulary.clear()
}
fun getModified(): MutableMap<WordPojo, Boolean> {
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
private fun displayAll() {
displayedVocabulary.addAll(wordList)
}
}
\ 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