Commit 4968f1b6 by Paktalin

Transformed WordItem to WordPojo, fixed duplicated modified words bug

parent a91bfbe1
package com.paktalin.vocabularynotebook.firestoreitems
import java.io.Serializable
import java.util.Date
class WordItem(word: String, translation: String, time: Date?, var id: String?) : Serializable {
var pojo: Pojo = Pojo(word, translation, time)
class Pojo(var word: String, var translation: String, var time:Date? = null) : Serializable {
init {
if (time == null) time = Date(System.currentTimeMillis())
}
}
constructor(pojo: Pojo, id: String?)
: this(pojo.word, pojo.translation, pojo.time, id)
constructor(pojo: Pojo) : this(pojo, null)
fun contains(string:String):Boolean {
return pojo.word.toLowerCase().contains(string) ||
pojo.translation.toLowerCase().contains(string)
}
companion object { private val TAG = "VN/" + WordItem::class.java.simpleName }
}
package com.paktalin.vocabularynotebook.firestoreitems
import java.io.Serializable
import java.util.Date
class WordPojo(var word: String, var translation: String, var time:Date? = null, var id: String? = null) : Serializable {
init {
if (time == null)
time = Date(System.currentTimeMillis())
}
}
......@@ -4,7 +4,7 @@ import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import kotlinx.android.synthetic.main.word_item.view.*
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
......@@ -12,9 +12,9 @@ class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val tvTranslation: TextView = itemView.translation
val layout: LinearLayout = itemView.layout
fun init(wordItem: WordItem, position: Int, showPopupMenu: (View, Int) -> Unit) {
tvWord.text = wordItem.pojo.word
tvTranslation.text = wordItem.pojo.translation
fun init(wordPojo: WordPojo, position: Int, showPopupMenu: (View, Int) -> Unit) {
tvWord.text = wordPojo.word
tvTranslation.text = wordPojo.translation
itemView.setOnClickListener { showPopupMenu(itemView, position) }
}
}
......@@ -8,7 +8,7 @@ import android.support.v7.widget.RecyclerView
import android.view.*
import com.paktalin.vocabularynotebook.OnQueryTextListener
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.fragments.EditWordFragment
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.Log
......@@ -69,20 +69,20 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
}*/
}
private fun deleteWord(wordItem: WordItem, position: Int) {
vocabulary.deleteWord(wordItem)
private fun deleteWord(wordPojo: WordPojo, position: Int) {
vocabulary.deleteWord(wordPojo)
recyclerView.removeViewAt(position)
this.notifyItemRemoved(position)
this.notifyItemRangeChanged(position, vocabulary.displayedSize())
}
fun addWord(newWord: WordItem) {
fun addWord(newWord: WordPojo) {
Log.d(TAG, "vocabularyAdapter addWord")
vocabulary.addWord(newWord)
this.sort()
}
fun updateWord(updatedWord: WordItem) {
fun updateWord(updatedWord: WordPojo) {
vocabulary.updateWord(updatedWord)
this.sort()
}
......@@ -93,7 +93,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
}
@SuppressLint("ResourceType")
private fun startEditFragment(container: View, wordItem: WordItem) {
private fun startEditFragment(container: View, wordPojo: WordPojo) {
//set container id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
container.id = View.generateViewId()
......@@ -101,7 +101,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
// start EditWordFragment
val arguments = Bundle()
arguments.putSerializable("wordItem", wordItem)
arguments.putSerializable("wordPojo", wordPojo)
addFragment(mainActivity.supportFragmentManager, EditWordFragment(), container.id, arguments, "edit_fragment")
}
......@@ -114,7 +114,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
notifyDataSetChanged()
}
fun getModifiedWords(): MutableMap<WordItem, ModifiedVocabulary.Label> {
fun getModifiedWords(): MutableMap<WordPojo, ModifiedVocabulary.Label> {
return vocabulary.getModified()
}
......
......@@ -3,7 +3,7 @@ package com.paktalin.vocabularynotebook.ui.fragments
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.content_main.*
import kotlinx.android.synthetic.main.fragment_editable_word.*
......@@ -39,7 +39,7 @@ class AddWordFragment : WordFragment() {
override fun save(word:String, translation:String) {
// TODO addAll entry point
clearFields()
mainActivity.vocabularyAdapter.addWord(WordItem(WordItem.Pojo(word, translation, null)))
mainActivity.vocabularyAdapter.addWord(WordPojo(word, translation))
this.word.requestFocus()
}
......
......@@ -6,9 +6,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.FirestoreManager
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.removeFragment
import com.paktalin.vocabularynotebook.utils.visible
......@@ -17,7 +16,7 @@ import kotlinx.android.synthetic.main.fragment_editable_word.*
import kotlinx.android.synthetic.main.word_item.view.*
class EditWordFragment : WordFragment() {
private lateinit var wordItem: WordItem
private lateinit var wordPojo: WordPojo
private var container: ViewGroup? = null
override fun cancelEditing() {
......@@ -29,7 +28,7 @@ class EditWordFragment : WordFragment() {
mainActivity = activity as MainActivity
this.container = container
hidePreviousViews()
wordItem = arguments!!["wordItem"] as WordItem
wordPojo = arguments!!["wordPojo"] as WordPojo
//(mainActivity.recyclerView.layoutManager as LockableLayoutManager).isScrollEnabled = false
return super.onCreateView(inflater, container, savedInstanceState)
}
......@@ -42,8 +41,8 @@ class EditWordFragment : WordFragment() {
}
private fun setWordItemData() {
word.setText(wordItem.pojo.word)
translation.setText(wordItem.pojo.translation)
word.setText(wordPojo.word)
translation.setText(wordPojo.translation)
}
private fun setFocusOnWord() {
......@@ -65,9 +64,9 @@ class EditWordFragment : WordFragment() {
}
override fun save(word: String, translation: String) {
// TODO edit entry point
wordItem.pojo = WordItem.Pojo(word, translation, wordItem.pojo.time)
mainActivity.vocabularyAdapter.updateWord(wordItem)
wordPojo.word = word
wordPojo.translation = translation
mainActivity.vocabularyAdapter.updateWord(wordPojo)
stop()
}
......
......@@ -7,7 +7,7 @@ import com.google.firebase.firestore.*
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import com.paktalin.vocabularynotebook.firestoreitems.VocabularyPojo
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary
import com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary.Label
......@@ -48,7 +48,7 @@ class FirestoreManager {
}
}
fun saveWords(wordMap: MutableMap<WordItem, ModifiedVocabulary.Label>) {
fun saveWords(wordMap: MutableMap<WordPojo, ModifiedVocabulary.Label>) {
if (wordMap.isEmpty())
return
val batch = db.batch()
......@@ -58,10 +58,11 @@ class FirestoreManager {
if (entry.value == Label.DELETED)
batch.delete(wordsCollection.document(entry.key.id!!))
else if (entry.value == Label.UPDATED && entry.key.id != null)
batch.set(wordsCollection.document(entry.key.id!!), entry.key.pojo)
batch.set(wordsCollection.document(entry.key.id!!), entry.key)
else
batch.set(wordsCollection.document(), entry.key.pojo)
batch.set(wordsCollection.document(), entry.key)
}}
wordMap.clear()
batch.commit().addOnCompleteListener { Log.d(TAG, "words are successfully pushed to Firestore") }
}
......
package com.paktalin.vocabularynotebook.vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
open class BasicVocabulary(var wordList: MutableList<WordItem>): Vocabulary {
open class BasicVocabulary(var wordList: MutableList<WordPojo>): Vocabulary {
init {
wordList = wordList.toMutableList()
}
override fun addAll(words: MutableList<WordItem>) {
override fun addAll(words: MutableList<WordPojo>) {
wordList.addAll(words)
}
override fun deleteWord(wordItem: WordItem) {
wordList.remove(wordItem)
override fun deleteWord(wordPojo: WordPojo) {
wordList.remove(wordPojo)
}
override fun addWord(newWord: WordItem) {
override fun addWord(newWord: WordPojo) {
wordList.add(0, newWord)
}
override fun updateWord(updatedWord: WordItem) {
override fun updateWord(updatedWord: WordPojo) {
wordList[wordList.indexOf(updatedWord)] = updatedWord
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
class DisplayedVocabulary(wordList: MutableList<WordItem>): BasicVocabulary(wordList) {
class DisplayedVocabulary(wordList: MutableList<WordPojo>): BasicVocabulary(wordList) {
var sort: Sort = Sort.BY_TIME
fun clear() {
......@@ -13,12 +13,12 @@ class DisplayedVocabulary(wordList: MutableList<WordItem>): BasicVocabulary(word
return wordList.size
}
fun at(index: Int): WordItem {
fun at(index: Int): WordPojo {
return wordList[index]
}
fun byQuery(query: String, allWords: MutableList<WordItem>) {
allWords.filter { wordItem -> wordItem.contains(query) }.toCollection(wordList)
fun byQuery(query: String, allWords: MutableList<WordPojo>) {
allWords.filter { wordItem -> contains(wordItem, query) }.toCollection(wordList)
}
fun sort(sort: Sort) {
......@@ -26,19 +26,25 @@ class DisplayedVocabulary(wordList: MutableList<WordItem>): BasicVocabulary(word
when (sort) {
Sort.BY_TIME -> {
wordList.sortWith(Comparator { item1, item2 ->
-item1.pojo.time!!.compareTo(item2.pojo.time)
-item1.time!!.compareTo(item2.time)
})
}
Sort.BY_WORD -> {
wordList.sortWith(Comparator { item1, item2 ->
item1.pojo.word.toLowerCase().compareTo(item2.pojo.word.toLowerCase())
item1.word.toLowerCase().compareTo(item2.word.toLowerCase())
})
}
Sort.BY_TRANSLATION -> {
wordList.sortWith(Comparator { item1, item2 ->
item1.pojo.translation.toLowerCase().compareTo(item2.pojo.translation.toLowerCase())
item1.translation.toLowerCase().compareTo(item2.translation.toLowerCase())
})
}
}
}
// TODO convert to a lambda expression
fun contains(wordPojo: WordPojo, string:String):Boolean {
return wordPojo.word.toLowerCase().contains(string) ||
wordPojo.translation.toLowerCase().contains(string)
}
}
package com.paktalin.vocabularynotebook.vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.utils.FirestoreManager
class ModifiedVocabulary : Vocabulary {
var wordMap = mutableMapOf<WordItem, Label>()
var wordMap = mutableMapOf<WordPojo, Label>()
private val maxPermitted = 9
override fun addWord(newWord: WordItem) {
override fun addWord(newWord: WordPojo) {
add(newWord, Label.ADDED)
}
override fun updateWord(updatedWord: WordItem) {
override fun updateWord(updatedWord: WordPojo) {
add(updatedWord, Label.UPDATED)
}
override fun deleteWord(wordItem: WordItem) {
add(wordItem, Label.DELETED)
override fun deleteWord(wordPojo: WordPojo) {
add(wordPojo, Label.DELETED)
}
override fun addAll(words: MutableList<WordItem>) { }
override fun addAll(words: MutableList<WordPojo>) { }
fun add(wordItem: WordItem, label: Label) {
wordMap[wordItem] = label
if (wordMap.size == maxPermitted) {
fun add(wordPojo: WordPojo, label: Label) {
wordMap[wordPojo] = label
if (wordMap.size == maxPermitted)
FirestoreManager().saveWords(wordMap)
wordMap.clear()
}
}
fun get(): MutableMap<WordItem, Label> {
fun get(): MutableMap<WordPojo, Label> {
return wordMap
}
......
......@@ -3,9 +3,9 @@ package com.paktalin.vocabularynotebook.vocabulary
import com.google.firebase.Timestamp
import com.google.firebase.firestore.QueryDocumentSnapshot
import com.google.firebase.firestore.QuerySnapshot
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
class VocabSet(var wordList: MutableList<WordItem>) : Vocabulary {
class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary {
var fullVocabulary = BasicVocabulary(wordList)
private var displayedVocabulary = DisplayedVocabulary(wordList)
......@@ -13,13 +13,13 @@ class VocabSet(var wordList: MutableList<WordItem>) : Vocabulary {
companion object {
fun createFromSnapshot(querySnapshot: QuerySnapshot): VocabSet {
val wordList = mutableListOf<WordItem>()
val wordList = mutableListOf<WordPojo>()
querySnapshot.forEach { s -> wordList.add(toWordItem(s)) }
return VocabSet(wordList)
}
private fun toWordItem(snapshot: QueryDocumentSnapshot): WordItem {
return WordItem(
private fun toWordItem(snapshot: QueryDocumentSnapshot): WordPojo {
return WordPojo(
snapshot["word"].toString(),
snapshot["translation"].toString(),
(snapshot["time"] as Timestamp).toDate(),
......@@ -27,22 +27,22 @@ class VocabSet(var wordList: MutableList<WordItem>) : Vocabulary {
}
}
override fun addAll(words: MutableList<WordItem>) {
override fun addAll(words: MutableList<WordPojo>) {
listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
.forEach { v -> v.addAll(words) }
}
override fun updateWord(updatedWord: WordItem) {
override fun updateWord(updatedWord: WordPojo) {
listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
.forEach { v -> v.updateWord(updatedWord) }
}
override fun deleteWord(wordItem: WordItem) {
override fun deleteWord(wordPojo: WordPojo) {
listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
.forEach { v -> v.deleteWord(wordItem) }
.forEach { v -> v.deleteWord(wordPojo) }
}
override fun addWord(newWord: WordItem) {
override fun addWord(newWord: WordPojo) {
listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
.forEach { v -> v.addWord(newWord) }
}
......@@ -59,7 +59,7 @@ class VocabSet(var wordList: MutableList<WordItem>) : Vocabulary {
displayedVocabulary.byQuery(query, wordList)
}
fun displayedAt(position: Int): WordItem {
fun displayedAt(position: Int): WordPojo {
return displayedVocabulary.at(position)
}
......@@ -67,5 +67,5 @@ class VocabSet(var wordList: MutableList<WordItem>) : Vocabulary {
fun clearDisplayed() { displayedVocabulary.clear() }
fun getModified(): MutableMap<WordItem, ModifiedVocabulary.Label> { return modifiedVocabulary.get() }
fun getModified(): MutableMap<WordPojo, ModifiedVocabulary.Label> { return modifiedVocabulary.get() }
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
interface Vocabulary {
fun addAll(words: MutableList<WordItem>)
fun addAll(words: MutableList<WordPojo>)
fun deleteWord(wordItem: WordItem)
fun deleteWord(wordPojo: WordPojo)
fun addWord(newWord: WordItem)
fun addWord(newWord: WordPojo)
fun updateWord(updatedWord: WordItem)
fun updateWord(updatedWord: WordPojo)
}
\ 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