Commit 51f65fec by Paktalin

Options sortByWord and sortByTranslation are available

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