Commit 38842264 by Paktalin

Refactoring in saveWords method

parent 646fc562
...@@ -9,6 +9,7 @@ import com.paktalin.vocabularynotebook.firestoreitems.UserPojo ...@@ -9,6 +9,7 @@ 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.Label
import java.util.* import java.util.*
class FirestoreManager { class FirestoreManager {
...@@ -46,19 +47,14 @@ class FirestoreManager { ...@@ -46,19 +47,14 @@ class FirestoreManager {
} }
} }
fun saveWords(wordMap: MutableMap<WordPojo, Boolean>) { fun saveWords(wordMap: MutableMap<Label, MutableList<WordPojo>>) {
if (wordMap.isEmpty()) if (wordMap.isEmpty()) return
return
val batch = db.batch() val batch = db.batch()
wordMap[Label.DELETED]?.forEach { w -> batch.delete(wordsCollection.document(w.id!!)) }
wordMap.forEach { wordMap[Label.EDITED]?.forEach { w -> batch.set(wordsCollection.document(w.id!!), w) }
entry -> run { wordMap[Label.DELETED]?.clear()
if (entry.value) batch.delete(wordsCollection.document(entry.key.id!!)) wordMap[Label.EDITED]?.clear()
else batch.set(wordsCollection.document(entry.key.id!!), entry.key) batch.commit()
}}
wordMap.clear()
batch.commit().addOnCompleteListener { Log.d(TAG, "words are successfully pushed to Firestore") }
} }
fun extractVocabulary(onComplete: (querySnapshot: QuerySnapshot?) -> Unit) { fun extractVocabulary(onComplete: (querySnapshot: QuerySnapshot?) -> Unit) {
......
...@@ -4,30 +4,39 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordPojo ...@@ -4,30 +4,39 @@ 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, Boolean>()
var wordMap = mutableMapOf(
Pair(Label.DELETED, mutableListOf<WordPojo>()),
Pair(Label.EDITED, mutableListOf()))
private val maxPermitted = 9 private val maxPermitted = 9
private var currentSize = 0
override fun addWord(newWord: WordPojo) { override fun addWord(newWord: WordPojo) {
add(newWord, false) add(newWord, Label.EDITED)
} }
override fun updateWord(updatedWord: WordPojo) { override fun updateWord(updatedWord: WordPojo) {
add(updatedWord, false) add(updatedWord, Label.EDITED)
} }
override fun deleteWord(wordPojo: WordPojo) { override fun deleteWord(wordPojo: WordPojo) {
add(wordPojo, true) add(wordPojo, Label.DELETED)
} }
override fun addAll(words: MutableList<WordPojo>) { } override fun addAll(words: MutableList<WordPojo>) { }
private fun add(wordPojo: WordPojo, deleted: Boolean) { private fun add(wordPojo: WordPojo, label: Label) {
wordMap[wordPojo] = deleted wordMap[label]?.add(wordPojo)
if (wordMap.size == maxPermitted) currentSize++
if (currentSize > maxPermitted) {
currentSize = 0
FirestoreManager().saveWords(wordMap) FirestoreManager().saveWords(wordMap)
} }
}
fun get(): MutableMap<WordPojo, Boolean> { fun get(): MutableMap<Label, MutableList<WordPojo>> {
return wordMap return wordMap
} }
enum class Label { DELETED, EDITED }
} }
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