Commit 39d2e21a by Paktalin

Words are stored in Vocabulary class

parent 3134bda9
...@@ -6,16 +6,16 @@ import android.os.Build ...@@ -6,16 +6,16 @@ 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.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem 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.*
class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() { class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView private lateinit var recyclerView: RecyclerView
private var sortOrder:Int = 0 private var sortOrder:Int = 0
...@@ -32,14 +32,14 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va ...@@ -32,14 +32,14 @@ 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 = vocabulary.words[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
} }
override fun getItemCount(): Int { return wordItems.size } override fun getItemCount(): Int { return vocabulary.words.size }
private fun showPopupMenu(v: View, position: Int) { private fun showPopupMenu(v: View, position: Int) {
val popup = PopupMenu(activity, v) val popup = PopupMenu(activity, v)
...@@ -47,43 +47,43 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va ...@@ -47,43 +47,43 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
inflater.inflate(R.menu.word_item_menu, popup.menu) inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener { popup.setOnMenuItemClickListener {
if (it.itemId == R.id.option_delete) { deleteWordItem(position) } if (it.itemId == R.id.option_delete) { deleteWordItem(position) }
if (it.itemId == R.id.option_edit) { editWordItem(v, wordItems[position]) } if (it.itemId == R.id.option_edit) { editWordItem(v, vocabulary.words[position]) }
true true
} }
popup.show() popup.show()
} }
private fun deleteWordItem(position: Int) { private fun deleteWordItem(position: Int) {
wordItems[position].delete() vocabulary.words[position].delete()
wordItems.removeAt(position) vocabulary.words.removeAt(position)
recyclerView.removeViewAt(position) recyclerView.removeViewAt(position)
this.notifyItemRemoved(position) this.notifyItemRemoved(position)
this.notifyItemRangeChanged(position, wordItems.size) this.notifyItemRangeChanged(position, vocabulary.words.size)
} }
fun addWordItem(newWordItem: WordItem) { fun addWordItem(newWordItem: WordItem) {
wordItems.add(0, newWordItem) vocabulary.words.add(0, newWordItem)
this.sort() this.sort()
} }
fun updateWordItem(updatedWordItem: WordItem) { fun updateWordItem(updatedWordItem: WordItem) {
val updatedItemId = wordItems.indexOf(updatedWordItem) val updatedItemId = vocabulary.words.indexOf(updatedWordItem)
wordItems[updatedItemId] = updatedWordItem vocabulary.words[updatedItemId] = updatedWordItem
this.sort() this.sort()
} }
private fun sortByTranslation() { private fun sortByTranslation() {
wordItems.sortWith(Comparator { item1, item2 -> vocabulary.words.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 -> vocabulary.words.sortWith(Comparator { item1, item2 ->
item1.pojo.word.compareTo(item2.pojo.word) }) item1.pojo.word.compareTo(item2.pojo.word) })
} }
private fun sortByTime() { private fun sortByTime() {
wordItems.sortWith(Comparator { item1, item2 -> vocabulary.words.sortWith(Comparator { item1, item2 ->
-item1.pojo.time!!.compareTo(item2.pojo.time) }) -item1.pojo.time!!.compareTo(item2.pojo.time) })
} }
......
package com.paktalin.vocabularynotebook.firestoreitems package com.paktalin.vocabularynotebook.firestoreitems
class Vocabulary { class Vocabulary(var words: MutableList<WordItem>) {
companion object {
private const val SORT_BY_TIME = 0
private const val SORT_BY_WORD = 1
private const val SORT_BY_TRANSLATION = 2
}
var pojo:Pojo var pojo:Pojo
private lateinit var words:MutableList<WordItem>
class Pojo(var title:String?) { class Pojo(var title:String?) {
init { init {
...@@ -14,5 +19,16 @@ class Vocabulary { ...@@ -14,5 +19,16 @@ class Vocabulary {
pojo = Pojo(null) pojo = Pojo(null)
} }
fun sort(index:Int) {
when(index) {
SORT_BY_TIME -> sortByTime()
SORT_BY_WORD -> sortByWord()
SORT_BY_TRANSLATION -> sortByTranslation()
}
}
private fun sortByTime() { }
private fun sortByWord() { }
private fun sortByTranslation() { }
} }
...@@ -24,7 +24,7 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p ...@@ -24,7 +24,7 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
fun delete() { fun delete() {
ConfiguredFirestore.instance.collection("vocabularies").document(vocabularyId) ConfiguredFirestore.instance.collection("vocabularies").document(vocabularyId)
.collection("words").document(id).delete() .collection("WORDS").document(id).delete()
.addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") } .addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) } .addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
} }
......
...@@ -20,8 +20,8 @@ class AddWordFragment : WordFragment() { ...@@ -20,8 +20,8 @@ class AddWordFragment : WordFragment() {
override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) { override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) {
mainActivity.showProgressBar() mainActivity.showProgressBar()
ConfiguredFirestore.instance ConfiguredFirestore.instance
.collection(vocabularies).document(vocabularyId) .collection(VOCABULARIES).document(vocabularyId)
.collection(words).add(wordPojo) .collection(WORDS).add(wordPojo)
.addOnSuccessListener { .addOnSuccessListener {
Log.i(TAG, "Successfully added a new word") Log.i(TAG, "Successfully added a new word")
clearFields() clearFields()
......
...@@ -53,8 +53,8 @@ class EditWordFragment : WordFragment() { ...@@ -53,8 +53,8 @@ class EditWordFragment : WordFragment() {
override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) { override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) {
mainActivity.showProgressBar() mainActivity.showProgressBar()
ConfiguredFirestore.instance ConfiguredFirestore.instance
.collection(vocabularies).document(vocabularyId) .collection(VOCABULARIES).document(vocabularyId)
.collection(words).document(wordItem.id).set(wordPojo) .collection(WORDS).document(wordItem.id).set(wordPojo)
.addOnSuccessListener { .addOnSuccessListener {
Log.i(TAG, "Successfully updated the word") Log.i(TAG, "Successfully updated the word")
hideSubmitButton() hideSubmitButton()
......
...@@ -80,7 +80,9 @@ class MainActivity : AppCompatActivity() { ...@@ -80,7 +80,9 @@ class MainActivity : AppCompatActivity() {
arguments.putString("vocabularyId", vocabularyId) arguments.putString("vocabularyId", vocabularyId)
vocabularyFragment.arguments = arguments vocabularyFragment.arguments = arguments
supportFragmentManager.beginTransaction().add(R.id.fragment_container, vocabularyFragment).commitNowAllowingStateLoss() supportFragmentManager.beginTransaction().add(R.id.fragment_container, vocabularyFragment).commitNowAllowingStateLoss()
} else { showToastNoWords() } } else {
Log.w(TAG, "There's no collection \"vocabularies\"")
showToastNoWords() }
} }
} }
...@@ -103,7 +105,7 @@ class MainActivity : AppCompatActivity() { ...@@ -103,7 +105,7 @@ class MainActivity : AppCompatActivity() {
fun showToastNoWords() { fun showToastNoWords() {
Toast.makeText(this@MainActivity, Toast.makeText(this@MainActivity,
"You don't have any words yet. Add your fist one!", Toast.LENGTH_SHORT).show() "You don't have any WORDS yet. Add your fist one!", Toast.LENGTH_SHORT).show()
} }
override fun onPause() { override fun onPause() {
......
...@@ -13,9 +13,9 @@ import com.google.firebase.firestore.DocumentSnapshot ...@@ -13,9 +13,9 @@ import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.Query 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.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.fragment_vocabulary.* import kotlinx.android.synthetic.main.fragment_vocabulary.*
import java.util.*
class VocabularyFragment : Fragment() { class VocabularyFragment : Fragment() {
companion object { companion object {
...@@ -44,7 +44,7 @@ class VocabularyFragment : Fragment() { ...@@ -44,7 +44,7 @@ class VocabularyFragment : Fragment() {
private fun setEmptyAdapter() { private fun setEmptyAdapter() {
val emptyList: MutableList<WordItem> = mutableListOf() val emptyList: MutableList<WordItem> = mutableListOf()
recyclerView.adapter = VocabularyAdapter(emptyList, activity!!) recyclerView.adapter = VocabularyAdapter(Vocabulary(emptyList), activity!!)
val mLayoutManager = LinearLayoutManager(activity) val mLayoutManager = LinearLayoutManager(activity)
recyclerView.layoutManager = mLayoutManager recyclerView.layoutManager = mLayoutManager
} }
...@@ -57,7 +57,7 @@ class VocabularyFragment : Fragment() { ...@@ -57,7 +57,7 @@ class VocabularyFragment : Fragment() {
if (it.documents.size != 0) if (it.documents.size != 0)
setVocabularyAdapter(it.documents, vocabularyId) setVocabularyAdapter(it.documents, vocabularyId)
else { else {
Log.i(TAG, "There are no documents in collection \"words\"") Log.i(TAG, "There are no documents in collection \"WORDS\"")
(activity as MainActivity).showToastNoWords() (activity as MainActivity).showToastNoWords()
}} }}
} }
...@@ -72,7 +72,8 @@ class VocabularyFragment : Fragment() { ...@@ -72,7 +72,8 @@ class VocabularyFragment : Fragment() {
wordItems.add(WordItem(word, translation, time.toDate(), ref.id, vocabularyId)) wordItems.add(WordItem(word, translation, time.toDate(), ref.id, vocabularyId))
} }
val adapter = VocabularyAdapter(wordItems, activity!!) val vocabulary = Vocabulary(wordItems)
val adapter = VocabularyAdapter(vocabulary, activity!!)
recyclerView.adapter = adapter recyclerView.adapter = adapter
} }
......
...@@ -12,8 +12,11 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordItem ...@@ -12,8 +12,11 @@ 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() {
protected val vocabularies = "vocabularies"
protected val words = "words" companion object {
const val VOCABULARIES = "vocabularies"
const val WORDS = "words"
}
protected lateinit var mainActivity: MainActivity protected lateinit var mainActivity: MainActivity
private var wordEmpty: Boolean = true private var wordEmpty: Boolean = true
......
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