Commit 6a28bf07 by Paktalin

Words can be sorted by time

parent 38b6f6d7
...@@ -4,8 +4,8 @@ import android.util.Log ...@@ -4,8 +4,8 @@ import android.util.Log
import com.google.firebase.auth.FirebaseUser import com.google.firebase.auth.FirebaseUser
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.ui.LogInActivity import com.paktalin.vocabularynotebook.ui.LogInActivity
import com.paktalin.vocabularynotebook.pojo.UserPojo import com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import com.paktalin.vocabularynotebook.pojo.Vocabulary import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import java.util.* import java.util.*
class UserManager { class UserManager {
......
...@@ -6,9 +6,11 @@ import android.os.Build ...@@ -6,9 +6,11 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.support.v7.widget.PopupMenu import android.support.v7.widget.PopupMenu
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.* import android.view.*
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
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.* import java.util.*
...@@ -16,7 +18,7 @@ import java.util.* ...@@ -16,7 +18,7 @@ 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 private var sortOrder:Int = 0
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView) super.onAttachedToRecyclerView(recyclerView)
...@@ -31,8 +33,8 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va ...@@ -31,8 +33,8 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val wordItem = wordItems[position] val wordItem = wordItems[position]
holder.tvWord.text = wordItem.pojo!!.word holder.tvWord.text = wordItem.pojo.word
holder.tvTranslation.text = wordItem.pojo!!.translation holder.tvTranslation.text = wordItem.pojo.translation
holder.itemView.setOnClickListener { showPopupMenu(holder.itemView, position) } holder.itemView.setOnClickListener { showPopupMenu(holder.itemView, position) }
//todo set click listener to menu //todo set click listener to menu
} }
...@@ -72,20 +74,30 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va ...@@ -72,20 +74,30 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
private fun sortByTranslation() { private fun sortByTranslation() {
wordItems.sortWith(Comparator { item1, item2 -> wordItems.sortWith(Comparator { item1, item2 ->
item1.pojo!!.translation!!.compareTo(item2.pojo!!.translation!!) item1.pojo.translation.compareTo(item2.pojo.translation) })
})
} }
private fun sortByWord() { private fun sortByWord() {
wordItems.sortWith(Comparator { item1, item2 -> wordItems.sortWith(Comparator { item1, item2 ->
item1.pojo!!.word!!.compareTo(item2.pojo!!.word!!) item1.pojo.word.compareTo(item2.pojo.word) })
}
private fun sortByTime() {
wordItems.sortWith(Comparator { item1, item2 ->
-item1.pojo.time!!.compareTo(item2.pojo.time)
}) })
wordItems.forEach{ item -> Log.d(TAG, item.pojo.print())}
} }
fun sort() { fun sort() {
sortByWord = !sortByWord if (sortOrder == 2) sortOrder = 0
if (sortByWord) sortByWord() else sortOrder++
else sortByTranslation()
when(sortOrder) {
0 -> sortByTime()
1 -> sortByWord()
2 -> sortByTranslation()
}
this.notifyDataSetChanged() this.notifyDataSetChanged()
} }
......
package com.paktalin.vocabularynotebook
import android.util.Log
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import java.io.Serializable
class WordItem(word: String, translation: String, var id: String, private val vocabularyId: String) : Serializable {
var pojo: Pojo? = null
class Pojo(var word: String?, var translation: String?) : Serializable
init {
this.pojo = Pojo(word, translation)
}
constructor(pojo: Pojo, id: String, vocabularyId: String)
: this(pojo.word!!, pojo.translation!!, id, vocabularyId)
fun delete() {
ConfiguredFirestore.instance.collection("vocabularies").document(vocabularyId)
.collection("words").document(id!!).delete()
.addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
}
companion object { private val TAG = "VN/" + WordItem::class.java.simpleName }
}
package com.paktalin.vocabularynotebook.pojo; package com.paktalin.vocabularynotebook.firestoreitems;
import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentReference;
......
package com.paktalin.vocabularynotebook.pojo package com.paktalin.vocabularynotebook.firestoreitems
class Vocabulary { class Vocabulary {
var pojo:Pojo var pojo:Pojo
......
...@@ -8,7 +8,7 @@ import android.view.ViewGroup ...@@ -8,7 +8,7 @@ import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
class AddWordFragment : WordFragment() { class AddWordFragment : WordFragment() {
......
...@@ -11,7 +11,7 @@ import android.widget.ImageView ...@@ -11,7 +11,7 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_new_word.* import kotlinx.android.synthetic.main.fragment_new_word.*
......
...@@ -8,8 +8,10 @@ import android.view.View ...@@ -8,8 +8,10 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.google.firebase.auth.FirebaseAuth import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentSnapshot import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.Query
import com.paktalin.vocabularynotebook.* import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.fragment_vocabulary.* import kotlinx.android.synthetic.main.fragment_vocabulary.*
class VocabularyFragment : Fragment() { class VocabularyFragment : Fragment() {
...@@ -43,7 +45,9 @@ class VocabularyFragment : Fragment() { ...@@ -43,7 +45,9 @@ class VocabularyFragment : Fragment() {
} }
fun retrieveWordsData(vocabularyId: String) { fun retrieveWordsData(vocabularyId: String) {
db.collection(VOCABULARIES).document(vocabularyId).collection(WORDS).orderBy("word").get() db.collection(VOCABULARIES).document(vocabularyId).collection(WORDS)
.orderBy("time", Query.Direction.DESCENDING)
.get()
.addOnSuccessListener { .addOnSuccessListener {
if (it.documents.size != 0) if (it.documents.size != 0)
setVocabularyAdapter(it.documents, vocabularyId) setVocabularyAdapter(it.documents, vocabularyId)
......
...@@ -8,7 +8,7 @@ import android.view.View ...@@ -8,7 +8,7 @@ import android.view.View
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.ImageButton import android.widget.ImageButton
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.fragment_new_word.* import kotlinx.android.synthetic.main.fragment_new_word.*
abstract class WordFragment : Fragment() { abstract class WordFragment : Fragment() {
...@@ -70,7 +70,7 @@ abstract class WordFragment : Fragment() { ...@@ -70,7 +70,7 @@ abstract class WordFragment : Fragment() {
val word = word.text.toString() val word = word.text.toString()
val translation = translation.text.toString() val translation = translation.text.toString()
val vocabularyId = (activity as MainActivity).vocabularyId val vocabularyId = (activity as MainActivity).vocabularyId
val wordPojo = WordItem.Pojo(word, translation) val wordPojo = WordItem.Pojo(word, translation, null)
saveToFirestore(wordPojo, vocabularyId) saveToFirestore(wordPojo, vocabularyId)
} }
...@@ -80,6 +80,6 @@ abstract class WordFragment : Fragment() { ...@@ -80,6 +80,6 @@ abstract class WordFragment : Fragment() {
translation.text.clear() translation.text.clear()
} }
protected abstract fun saveToFirestore(wordPojo:WordItem.Pojo, vocabularyId:String) protected abstract fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId:String)
protected abstract fun updateRecycleView(wordItem: WordItem) protected abstract fun updateRecycleView(wordItem: WordItem)
} }
\ 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