Commit 51f65fec by Paktalin

Options sortByWord and sortByTranslation are available

parent a0a60ff8
......@@ -11,10 +11,12 @@ import android.widget.LinearLayout
import android.widget.TextView
import com.paktalin.vocabularynotebook.ui.EditWordFragment
import com.paktalin.vocabularynotebook.ui.MainActivity
import java.util.*
class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView
private var sortByWord = true
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
......@@ -68,6 +70,26 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
this.notifyDataSetChanged()
}
private fun sortByTranslation() {
wordItems.sortWith(Comparator { item1, item2 ->
item1.pojo!!.translation!!.compareTo(item2.pojo!!.translation!!)
})
this.notifyDataSetChanged()
}
private fun sortByWord() {
wordItems.sortWith(Comparator { item1, item2 ->
item1.pojo!!.word!!.compareTo(item2.pojo!!.word!!)
})
this.notifyDataSetChanged()
}
fun sort() {
sortByWord = !sortByWord
if (sortByWord) sortByWord()
else sortByTranslation()
}
@SuppressLint("ResourceType")
private fun editWordItem(container:View, wordItem: WordItem) {
//set container id
......
......@@ -2,7 +2,7 @@ package com.paktalin.vocabularynotebook.pojo;
public class VocabularyPojo {
String title;
private String title;
public VocabularyPojo() {
title = "First vocabulary";
......
......@@ -15,7 +15,9 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_vocabulary.*
class MainActivity : AppCompatActivity() {
......@@ -38,7 +40,7 @@ class MainActivity : AppCompatActivity() {
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item!!.itemId == R.id.sort)
Log.d(TAG, "sort")
(recyclerView.adapter as VocabularyAdapter).sort()
return super.onOptionsItemSelected(item)
}
......
......@@ -43,7 +43,7 @@ class VocabularyFragment : Fragment() {
}
fun retrieveWordsData(vocabularyId: String) {
db.collection(VOCABULARIES).document(vocabularyId).collection(WORDS).get()
db.collection(VOCABULARIES).document(vocabularyId).collection(WORDS).orderBy("word").get()
.addOnSuccessListener { setVocabularyAdapter(it.documents, vocabularyId) }
}
......
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