Commit 6a28bf07 by Paktalin

Words can be sorted by time

parent 38b6f6d7
......@@ -4,8 +4,8 @@ import android.util.Log
import com.google.firebase.auth.FirebaseUser
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.ui.LogInActivity
import com.paktalin.vocabularynotebook.pojo.UserPojo
import com.paktalin.vocabularynotebook.pojo.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import java.util.*
class UserManager {
......
......@@ -6,9 +6,11 @@ import android.os.Build
import android.os.Bundle
import android.support.v7.widget.PopupMenu
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.*
import android.widget.LinearLayout
import android.widget.TextView
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.EditWordFragment
import com.paktalin.vocabularynotebook.ui.MainActivity
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>() {
private lateinit var recyclerView: RecyclerView
private var sortByWord = true
private var sortOrder:Int = 0
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
......@@ -31,8 +33,8 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val wordItem = wordItems[position]
holder.tvWord.text = wordItem.pojo!!.word
holder.tvTranslation.text = wordItem.pojo!!.translation
holder.tvWord.text = wordItem.pojo.word
holder.tvTranslation.text = wordItem.pojo.translation
holder.itemView.setOnClickListener { showPopupMenu(holder.itemView, position) }
//todo set click listener to menu
}
......@@ -72,20 +74,30 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
private fun sortByTranslation() {
wordItems.sortWith(Comparator { item1, item2 ->
item1.pojo!!.translation!!.compareTo(item2.pojo!!.translation!!)
})
item1.pojo.translation.compareTo(item2.pojo.translation) })
}
private fun sortByWord() {
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() {
sortByWord = !sortByWord
if (sortByWord) sortByWord()
else sortByTranslation()
if (sortOrder == 2) sortOrder = 0
else sortOrder++
when(sortOrder) {
0 -> sortByTime()
1 -> sortByWord()
2 -> sortByTranslation()
}
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;
......
package com.paktalin.vocabularynotebook.pojo
package com.paktalin.vocabularynotebook.firestoreitems
class Vocabulary {
var pojo:Pojo
......
......@@ -8,7 +8,7 @@ import android.view.ViewGroup
import android.widget.Toast
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
class AddWordFragment : WordFragment() {
......
......@@ -11,7 +11,7 @@ import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_new_word.*
......
......@@ -8,8 +8,10 @@ import android.view.View
import android.view.ViewGroup
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.Query
import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.fragment_vocabulary.*
class VocabularyFragment : Fragment() {
......@@ -43,7 +45,9 @@ class VocabularyFragment : Fragment() {
}
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 {
if (it.documents.size != 0)
setVocabularyAdapter(it.documents, vocabularyId)
......
......@@ -8,7 +8,7 @@ import android.view.View
import android.widget.FrameLayout
import android.widget.ImageButton
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.fragment_new_word.*
abstract class WordFragment : Fragment() {
......@@ -70,7 +70,7 @@ abstract class WordFragment : Fragment() {
val word = word.text.toString()
val translation = translation.text.toString()
val vocabularyId = (activity as MainActivity).vocabularyId
val wordPojo = WordItem.Pojo(word, translation)
val wordPojo = WordItem.Pojo(word, translation, null)
saveToFirestore(wordPojo, vocabularyId)
}
......@@ -80,6 +80,6 @@ abstract class WordFragment : Fragment() {
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)
}
\ 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