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