Commit 9843c6ba by Paktalin

wordItem id is generated by default

parent af51e854
package com.paktalin.vocabularynotebook.firestoreitems package com.paktalin.vocabularynotebook.firestoreitems
import java.io.Serializable import java.io.Serializable
import java.util.Date import java.util.*
class WordPojo(var word: String, class WordPojo(var word: String,
var translation: String, var translation: String,
...@@ -11,7 +11,7 @@ class WordPojo(var word: String, ...@@ -11,7 +11,7 @@ class WordPojo(var word: String,
: Serializable { : Serializable {
init { init {
if (time == null) time = time ?: Date(System.currentTimeMillis())
time = Date(System.currentTimeMillis()) id = id ?: UUID.randomUUID().toString()
} }
} }
...@@ -15,7 +15,6 @@ import com.paktalin.vocabularynotebook.ui.fragments.EditWordFragment ...@@ -15,7 +15,6 @@ import com.paktalin.vocabularynotebook.ui.fragments.EditWordFragment
import com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker.MySelectionTracker import com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker.MySelectionTracker
import com.paktalin.vocabularynotebook.utils.Log import com.paktalin.vocabularynotebook.utils.Log
import com.paktalin.vocabularynotebook.utils.addFragment import com.paktalin.vocabularynotebook.utils.addFragment
import com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary
import com.paktalin.vocabularynotebook.vocabulary.Sort import com.paktalin.vocabularynotebook.vocabulary.Sort
import com.paktalin.vocabularynotebook.vocabulary.VocabSet import com.paktalin.vocabularynotebook.vocabulary.VocabSet
...@@ -128,7 +127,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -128,7 +127,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
notifyDataSetChanged() notifyDataSetChanged()
} }
fun getModifiedWords(): MutableMap<WordPojo, ModifiedVocabulary.Label> { fun getModifiedWords(): MutableMap<WordPojo, Boolean> {
return vocabulary.getModified() return vocabulary.getModified()
} }
......
...@@ -9,8 +9,6 @@ import com.paktalin.vocabularynotebook.firestoreitems.UserPojo ...@@ -9,8 +9,6 @@ import com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import com.paktalin.vocabularynotebook.firestoreitems.VocabularyPojo import com.paktalin.vocabularynotebook.firestoreitems.VocabularyPojo
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary
import com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary.Label
import java.util.* import java.util.*
class FirestoreManager { class FirestoreManager {
...@@ -48,20 +46,17 @@ class FirestoreManager { ...@@ -48,20 +46,17 @@ class FirestoreManager {
} }
} }
fun saveWords(wordMap: MutableMap<WordPojo, ModifiedVocabulary.Label>) { fun saveWords(wordMap: MutableMap<WordPojo, Boolean>) {
if (wordMap.isEmpty()) if (wordMap.isEmpty())
return return
val batch = db.batch() val batch = db.batch()
wordMap.forEach { wordMap.forEach {
entry -> run { entry -> run {
if (entry.value == Label.DELETED) if (entry.value) batch.delete(wordsCollection.document(entry.key.id!!))
batch.delete(wordsCollection.document(entry.key.id!!)) else batch.set(wordsCollection.document(entry.key.id!!), entry.key)
else if (entry.value == Label.UPDATED && entry.key.id != null)
batch.set(wordsCollection.document(entry.key.id!!), entry.key)
else
batch.set(wordsCollection.document(), entry.key)
}} }}
wordMap.clear() wordMap.clear()
batch.commit().addOnCompleteListener { Log.d(TAG, "words are successfully pushed to Firestore") } batch.commit().addOnCompleteListener { Log.d(TAG, "words are successfully pushed to Firestore") }
} }
......
...@@ -4,34 +4,30 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordPojo ...@@ -4,34 +4,30 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.utils.FirestoreManager import com.paktalin.vocabularynotebook.utils.FirestoreManager
class ModifiedVocabulary : Vocabulary { class ModifiedVocabulary : Vocabulary {
var wordMap = mutableMapOf<WordPojo, Label>() var wordMap = mutableMapOf<WordPojo, Boolean>()
private val maxPermitted = 9 private val maxPermitted = 9
override fun addWord(newWord: WordPojo) { override fun addWord(newWord: WordPojo) {
add(newWord, Label.ADDED) add(newWord, false)
} }
override fun updateWord(updatedWord: WordPojo) { override fun updateWord(updatedWord: WordPojo) {
add(updatedWord, Label.UPDATED) add(updatedWord, false)
} }
override fun deleteWord(wordPojo: WordPojo) { override fun deleteWord(wordPojo: WordPojo) {
add(wordPojo, Label.DELETED) add(wordPojo, true)
} }
override fun addAll(words: MutableList<WordPojo>) { } override fun addAll(words: MutableList<WordPojo>) { }
fun add(wordPojo: WordPojo, label: Label) { private fun add(wordPojo: WordPojo, deleted: Boolean) {
wordMap[wordPojo] = label wordMap[wordPojo] = deleted
if (wordMap.size == maxPermitted) if (wordMap.size == maxPermitted)
FirestoreManager().saveWords(wordMap) FirestoreManager().saveWords(wordMap)
} }
fun get(): MutableMap<WordPojo, Label> { fun get(): MutableMap<WordPojo, Boolean> {
return wordMap return wordMap
} }
enum class Label {
ADDED, UPDATED, DELETED
}
} }
...@@ -67,5 +67,7 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary { ...@@ -67,5 +67,7 @@ class VocabSet(var wordList: MutableList<WordPojo>) : Vocabulary {
fun clearDisplayed() { displayedVocabulary.clear() } fun clearDisplayed() { displayedVocabulary.clear() }
fun getModified(): MutableMap<WordPojo, ModifiedVocabulary.Label> { return modifiedVocabulary.get() } fun getModified(): MutableMap<WordPojo, Boolean> {
return modifiedVocabulary.get()
}
} }
\ 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