Commit 646fc562 by Paktalin

Fixed the problem with replaced word when adding

parent f4a8ddf1
...@@ -62,10 +62,10 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -62,10 +62,10 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
viewHolder.bind( viewHolder.bind(
vocabulary.displayedAt(position), vocabulary.getDisplayedAt(position),
position, position,
(selectionTracker.selected(position.toLong())) && state.notAddOrEdit(), (selectionTracker.selected(position.toLong())) && state.notAddOrEdit(),
vocabulary.getColorForTag(vocabulary.displayedAt(position).tag) vocabulary.getColorForTag(vocabulary.getDisplayedAt(position).tag)
) { view, p -> showPopupMenu(view, p) } ) { view, p -> showPopupMenu(view, p) }
// TODO not allow to edit when tag is being selected // TODO not allow to edit when tag is being selected
} }
...@@ -73,13 +73,15 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -73,13 +73,15 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
fun refresh() {} fun refresh() {}
fun addWord(newWord: WordPojo) { fun addWord(newWord: WordPojo) {
this.sort = Sort.BY_TIME
vocabulary.addWord(newWord) vocabulary.addWord(newWord)
notifyDataSetChanged() recyclerView.smoothScrollToPosition(0)
notifyItemRangeInserted(0, 2)
} }
fun updateWord(wordPojo: WordPojo) { fun updateWord(wordPojo: WordPojo) {
vocabulary.updateWord(wordPojo) vocabulary.updateWord(wordPojo)
notifyDataSetChanged() notifyItemChanged(vocabulary.indexOfDisplayed(wordPojo))
} }
fun filter(query: String) { fun filter(query: String) {
...@@ -101,7 +103,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -101,7 +103,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
popup.menuInflater.inflate(R.menu.word_item_menu, popup.menu) popup.menuInflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener { popup.setOnMenuItemClickListener {
when (it.itemId) { when (it.itemId) {
R.id.option_delete -> deleteWord(vocabulary.displayedAt(position), position) R.id.option_delete -> deleteWord(vocabulary.getDisplayedAt(position), position)
R.id.option_edit -> editWord(v, position) R.id.option_edit -> editWord(v, position)
} }
true true
...@@ -124,12 +126,11 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -124,12 +126,11 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
SubmitEditedFragment().apply { SubmitEditedFragment().apply {
setData(container.word, setData(container.word,
container.translation, container.translation,
vocabulary.displayedAt(wordPosition), vocabulary.getDisplayedAt(wordPosition),
container.clickable_view, container.clickable_view,
wordPosition) wordPosition)
}, },
R.id.main_activity_container) R.id.main_activity_container)
} }
private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName
} }
\ No newline at end of file
...@@ -18,6 +18,10 @@ class DisplayedVocabulary(wordList: MutableList<WordPojo>): BasicVocabulary(word ...@@ -18,6 +18,10 @@ class DisplayedVocabulary(wordList: MutableList<WordPojo>): BasicVocabulary(word
return wordList[index] return wordList[index]
} }
fun indexOf(wordPojo: WordPojo): Int {
return wordList.indexOf(wordPojo)
}
fun byQuery(query: String, allWords: MutableList<WordPojo>) { fun byQuery(query: String, allWords: MutableList<WordPojo>) {
// TODO make tag search more flexible // TODO make tag search more flexible
if (query[0] == '#') { if (query[0] == '#') {
...@@ -37,7 +41,7 @@ class DisplayedVocabulary(wordList: MutableList<WordPojo>): BasicVocabulary(word ...@@ -37,7 +41,7 @@ class DisplayedVocabulary(wordList: MutableList<WordPojo>): BasicVocabulary(word
} }
// TODO convert to a lambda expression // TODO convert to a lambda expression
fun contains(wordPojo: WordPojo, string:String):Boolean { private fun contains(wordPojo: WordPojo, string: String): Boolean {
return wordPojo.word.toLowerCase().contains(string) || return wordPojo.word.toLowerCase().contains(string) ||
wordPojo.translation.toLowerCase().contains(string) wordPojo.translation.toLowerCase().contains(string)
} }
......
...@@ -47,8 +47,7 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary { ...@@ -47,8 +47,7 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary {
override fun updateWord(updatedWord: WordPojo) { override fun updateWord(updatedWord: WordPojo) {
listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary) listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
.forEach { v -> v.updateWord(updatedWord) } .forEach { v -> v.updateWord(updatedWord) }
// TODO change this displayedVocabulary.sort(displayedVocabulary.sort)
sort(displayedVocabulary.sort)
} }
override fun deleteWord(wordPojo: WordPojo) { override fun deleteWord(wordPojo: WordPojo) {
...@@ -59,41 +58,44 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary { ...@@ -59,41 +58,44 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary {
override fun addWord(newWord: WordPojo) { override fun addWord(newWord: WordPojo) {
listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary) listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
.forEach { v -> v.addWord(newWord) } .forEach { v -> v.addWord(newWord) }
// TODO change this displayedVocabulary.sort(displayedVocabulary.sort)
sort(displayedVocabulary.sort)
} }
fun sort(sort: Sort) { fun sort(sort: Sort) {
displayedVocabulary.sort(sort) displayedVocabulary.sort(sort)
} }
fun getModified(): MutableMap<WordPojo, Boolean> {
return modifiedVocabulary.get()
}
fun addTag(selectionList: MutableList<Long>, tag: String) { fun addTag(selectionList: MutableList<Long>, tag: String) {
selectionList.forEach { position -> selectionList.forEach { position ->
displayedAt(position.toInt()).tag = tag getDisplayedAt(position.toInt()).tag = tag
modifiedVocabulary.addWord(displayedAt(position.toInt())) modifiedVocabulary.addWord(getDisplayedAt(position.toInt()))
} }
} }
fun getColorForTag(tag: String?): Int {
return colorMap[tag] ?: Color.TRANSPARENT
}
fun displayByQuery(query: String) { fun displayByQuery(query: String) {
clearDisplayed() clearDisplayed()
if (query.isEmpty()) displayAll() if (query.isEmpty()) displayAll()
else displayedVocabulary.byQuery(query.toLowerCase(), wordList) else displayedVocabulary.byQuery(query.toLowerCase(), wordList)
} }
fun displayedAt(position: Int): WordPojo { fun displayedSize(): Int {
return displayedVocabulary.size()
}
fun getModified(): MutableMap<WordPojo, Boolean> {
return modifiedVocabulary.get()
}
fun getColorForTag(tag: String?): Int {
return colorMap[tag] ?: Color.TRANSPARENT
}
fun getDisplayedAt(position: Int): WordPojo {
return displayedVocabulary.at(position) return displayedVocabulary.at(position)
} }
fun displayedSize(): Int { fun indexOfDisplayed(wordPojo: WordPojo): Int {
return displayedVocabulary.size() return displayedVocabulary.indexOf(wordPojo)
} }
private fun clearDisplayed() { private fun clearDisplayed() {
......
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