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