Commit 9843c6ba by Paktalin

wordItem id is generated by default

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