Commit af63849b by Paktalin

Sort is made inside Vocabulary class

parent 39d2e21a
......@@ -72,30 +72,12 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
this.sort()
}
private fun sortByTranslation() {
vocabulary.words.sortWith(Comparator { item1, item2 ->
item1.pojo.translation.compareTo(item2.pojo.translation) })
}
private fun sortByWord() {
vocabulary.words.sortWith(Comparator { item1, item2 ->
item1.pojo.word.compareTo(item2.pojo.word) })
}
private fun sortByTime() {
vocabulary.words.sortWith(Comparator { item1, item2 ->
-item1.pojo.time!!.compareTo(item2.pojo.time) })
}
fun sort() {
// update SortOrder
if (sortOrder == 2) sortOrder = 0
else sortOrder++
when(sortOrder) {
0 -> sortByTime()
1 -> sortByWord()
2 -> sortByTranslation()
}
vocabulary.sort(sortOrder)
this.notifyDataSetChanged()
}
......
......@@ -2,6 +2,8 @@ package com.paktalin.vocabularynotebook.firestoreitems
class Vocabulary(var words: MutableList<WordItem>) {
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
......@@ -19,16 +21,25 @@ class Vocabulary(var words: MutableList<WordItem>) {
pojo = Pojo(null)
}
fun sort(index:Int) {
when(index) {
fun sort(sortOrder:Int) {
when(sortOrder) {
SORT_BY_TIME -> sortByTime()
SORT_BY_WORD -> sortByWord()
SORT_BY_TRANSLATION -> sortByTranslation()
}
}
private fun sortByTime() { }
private fun sortByWord() { }
private fun sortByTranslation() { }
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) })
}
}
......@@ -69,6 +69,7 @@ class MainActivity : AppCompatActivity() {
userDocument.get()
.addOnSuccessListener { task ->
hideProgressBar()
//todo move Firestore logic and collections names to a separate class
if (task.get("vocabularies") != null) {
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
val vocabulary = db.collection("vocabularies").document(vocabularies[0].id)
......
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