Commit 39d2e21a by Paktalin

Words are stored in Vocabulary class

parent 3134bda9
......@@ -6,16 +6,16 @@ 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.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
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>() {
class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView
private var sortOrder:Int = 0
......@@ -32,14 +32,14 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val wordItem = wordItems[position]
val wordItem = vocabulary.words[position]
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
}
override fun getItemCount(): Int { return wordItems.size }
override fun getItemCount(): Int { return vocabulary.words.size }
private fun showPopupMenu(v: View, position: Int) {
val popup = PopupMenu(activity, v)
......@@ -47,43 +47,43 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener {
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
}
popup.show()
}
private fun deleteWordItem(position: Int) {
wordItems[position].delete()
wordItems.removeAt(position)
vocabulary.words[position].delete()
vocabulary.words.removeAt(position)
recyclerView.removeViewAt(position)
this.notifyItemRemoved(position)
this.notifyItemRangeChanged(position, wordItems.size)
this.notifyItemRangeChanged(position, vocabulary.words.size)
}
fun addWordItem(newWordItem: WordItem) {
wordItems.add(0, newWordItem)
vocabulary.words.add(0, newWordItem)
this.sort()
}
fun updateWordItem(updatedWordItem: WordItem) {
val updatedItemId = wordItems.indexOf(updatedWordItem)
wordItems[updatedItemId] = updatedWordItem
val updatedItemId = vocabulary.words.indexOf(updatedWordItem)
vocabulary.words[updatedItemId] = updatedWordItem
this.sort()
}
private fun sortByTranslation() {
wordItems.sortWith(Comparator { item1, item2 ->
vocabulary.words.sortWith(Comparator { item1, item2 ->
item1.pojo.translation.compareTo(item2.pojo.translation) })
}
private fun sortByWord() {
wordItems.sortWith(Comparator { item1, item2 ->
vocabulary.words.sortWith(Comparator { item1, item2 ->
item1.pojo.word.compareTo(item2.pojo.word) })
}
private fun sortByTime() {
wordItems.sortWith(Comparator { item1, item2 ->
vocabulary.words.sortWith(Comparator { item1, item2 ->
-item1.pojo.time!!.compareTo(item2.pojo.time) })
}
......
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
private lateinit var words:MutableList<WordItem>
class Pojo(var title:String?) {
init {
......@@ -14,5 +19,16 @@ class Vocabulary {
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
fun delete() {
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") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
}
......
......@@ -20,8 +20,8 @@ class AddWordFragment : WordFragment() {
override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) {
mainActivity.showProgressBar()
ConfiguredFirestore.instance
.collection(vocabularies).document(vocabularyId)
.collection(words).add(wordPojo)
.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).add(wordPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully added a new word")
clearFields()
......
......@@ -53,8 +53,8 @@ class EditWordFragment : WordFragment() {
override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) {
mainActivity.showProgressBar()
ConfiguredFirestore.instance
.collection(vocabularies).document(vocabularyId)
.collection(words).document(wordItem.id).set(wordPojo)
.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).document(wordItem.id).set(wordPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully updated the word")
hideSubmitButton()
......
......@@ -80,7 +80,9 @@ class MainActivity : AppCompatActivity() {
arguments.putString("vocabularyId", vocabularyId)
vocabularyFragment.arguments = arguments
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() {
fun showToastNoWords() {
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() {
......
......@@ -13,9 +13,9 @@ 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.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.fragment_vocabulary.*
import java.util.*
class VocabularyFragment : Fragment() {
companion object {
......@@ -44,7 +44,7 @@ class VocabularyFragment : Fragment() {
private fun setEmptyAdapter() {
val emptyList: MutableList<WordItem> = mutableListOf()
recyclerView.adapter = VocabularyAdapter(emptyList, activity!!)
recyclerView.adapter = VocabularyAdapter(Vocabulary(emptyList), activity!!)
val mLayoutManager = LinearLayoutManager(activity)
recyclerView.layoutManager = mLayoutManager
}
......@@ -57,7 +57,7 @@ class VocabularyFragment : Fragment() {
if (it.documents.size != 0)
setVocabularyAdapter(it.documents, vocabularyId)
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()
}}
}
......@@ -72,7 +72,8 @@ class VocabularyFragment : Fragment() {
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
}
......
......@@ -12,8 +12,11 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.fragment_new_word.*
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
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