Commit 41f1256d by Paktalin

Added refresh by swipe

parent 36c1dfbf
package com.paktalin.vocabularynotebook
import com.google.firebase.Timestamp
import com.google.firebase.firestore.DocumentSnapshot
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.vocabularyId
class Vocabulary() {
private var words: MutableList<WordItem> = mutableListOf()
constructor(words: MutableList<WordItem>) : this() {
this.words = words
}
companion object {
private val TAG = "VN/" + Vocabulary::class.java.simpleName
private const val SORT_BY_TIME = 0
private const val SORT_BY_WORD = 1
private const val SORT_BY_TRANSLATION = 2
}
fun sort(sortOrder:Int) {
when(sortOrder) {
SORT_BY_TIME -> sortByTime()
SORT_BY_WORD -> sortByWord()
SORT_BY_TRANSLATION -> sortByTranslation()
}
}
fun deleteWord(position:Int) {
words[position].delete() // delete word from the database
words.removeAt(position) // delete word from the list
}
fun addWord(newWord: WordItem) {
words.add(0, newWord)
}
fun addWords(newWords:MutableList<WordItem>) {
words.addAll(newWords)
}
fun addWordsAsDocuments(documents: MutableList<DocumentSnapshot>) {
for (ref in documents) {
val word = ref["word"].toString()
val translation = ref["translation"].toString()
val time = ref["time"] as Timestamp
words.add(WordItem(word, translation, time.toDate(), ref.id, vocabularyId!!))
}
}
fun addWordsFittingQuery(newWords:MutableList<WordItem>, query:String) {
for (newWord in newWords) {
if (newWord.contains(query))
this.addWord(newWord)
}
}
fun updateWord(updatedWord: WordItem) {
val updatedItemIndex = words.indexOf(updatedWord)
words[updatedItemIndex] = updatedWord
}
fun getAt(position: Int): WordItem {
return words[position]
}
fun get():MutableList<WordItem> { return words }
fun size():Int { return words.size }
fun clear() { words.clear() }
private fun sortByTime() {
words.sortWith(Comparator { item1, item2 ->
-item1.pojo.time!!.compareTo(item2.pojo.time) })
}
private fun sortByWord() {
words.sortWith(Comparator { item1, item2 ->
item1.pojo.word.compareTo(item2.pojo.word) })
}
private fun sortByTranslation() {
words.sortWith(Comparator { item1, item2 ->
item1.pojo.translation.compareTo(item2.pojo.translation) })
}
}
\ 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