Commit a91bfbe1 by Paktalin

Removed modifiedLabel from WordItem, fixed paddings and duplication bug in BasicVocabulary

parent 40c0da46
package com.paktalin.vocabularynotebook.firestoreitems
enum class ModifiedLabel {
ADDED, UPDATED, DELETED
}
\ No newline at end of file
......@@ -5,7 +5,6 @@ import java.util.Date
class WordItem(word: String, translation: String, time: Date?, var id: String?) : Serializable {
var pojo: Pojo = Pojo(word, translation, time)
var modifiedLabel: ModifiedLabel? = null
class Pojo(var word: String, var translation: String, var time:Date? = null) : Serializable {
init {
......
......@@ -13,6 +13,7 @@ import com.paktalin.vocabularynotebook.ui.fragments.EditWordFragment
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
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
......@@ -113,7 +114,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
notifyDataSetChanged()
}
fun getModifiedWords(): MutableSet<WordItem> {
fun getModifiedWords(): MutableMap<WordItem, ModifiedVocabulary.Label> {
return vocabulary.getModified()
}
......
......@@ -5,17 +5,19 @@ import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.firestore.*
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.ModifiedLabel
import com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import com.paktalin.vocabularynotebook.firestoreitems.VocabularyPojo
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
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 {
private val db: FirebaseFirestore = ConfiguredFirestore.instance
private val vocabularyCollection = db.collection(VOCABULARIES)
private val wordsCollection = vocabularyCollection.document(vocabularyId!!).collection(WORDS)
fun extractVocabularyId(onSuccess: () -> Unit,
showToastNoWords: () -> Unit, removeProgressBar: () -> Unit, mainActivity: Context) {
......@@ -46,17 +48,19 @@ class FirestoreManager {
}
}
fun saveWords(wordList: MutableSet<WordItem>) {
if (wordList.isEmpty())
fun saveWords(wordMap: MutableMap<WordItem, ModifiedVocabulary.Label>) {
if (wordMap.isEmpty())
return
val batch = db.batch()
wordList.forEach { w -> kotlin.run {
if (w.modifiedLabel == ModifiedLabel.DELETED)
batch.delete(vocabularyDocument().collection(WORDS).document(w.id!!))
else if (w.modifiedLabel == ModifiedLabel.UPDATED && w.id != null)
batch.set(vocabularyDocument().collection(WORDS).document(w.id!!), w.pojo)
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.pojo)
else
batch.set(vocabularyDocument().collection(WORDS).document(), w.pojo)
batch.set(wordsCollection.document(), entry.key.pojo)
}}
batch.commit().addOnCompleteListener { Log.d(TAG, "words are successfully pushed to Firestore") }
}
......
......@@ -4,6 +4,10 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordItem
open class BasicVocabulary(var wordList: MutableList<WordItem>): Vocabulary {
init {
wordList = wordList.toMutableList()
}
override fun addAll(words: MutableList<WordItem>) {
wordList.addAll(words)
}
......
package com.paktalin.vocabularynotebook.vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.ModifiedLabel
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.utils.FirestoreManager
class ModifiedVocabulary : Vocabulary {
//TODO wordMap<ModifiedLabel, WordItem>
var wordList = mutableListOf<WordItem>()
var wordMap = mutableMapOf<WordItem, Label>()
private val maxPermitted = 9
override fun addWord(newWord: WordItem) {
add(newWord, ModifiedLabel.ADDED)
add(newWord, Label.ADDED)
}
override fun updateWord(updatedWord: WordItem) {
add(updatedWord, ModifiedLabel.UPDATED)
add(updatedWord, Label.UPDATED)
}
override fun deleteWord(wordItem: WordItem) {
add(wordItem, ModifiedLabel.DELETED)
add(wordItem, Label.DELETED)
}
override fun addAll(words: MutableList<WordItem>) { }
fun add(wordItem: WordItem, modifiedLabel: ModifiedLabel) {
wordItem.modifiedLabel = modifiedLabel
wordList.add(wordItem)
if (wordList.size == maxPermitted) {
FirestoreManager().saveWords(wordList.toMutableSet())
wordList.clear()
fun add(wordItem: WordItem, label: Label) {
wordMap[wordItem] = label
if (wordMap.size == maxPermitted) {
FirestoreManager().saveWords(wordMap)
wordMap.clear()
}
}
fun get(): MutableSet<WordItem> {
return wordList.toMutableSet()
fun get(): MutableMap<WordItem, Label> {
return wordMap
}
enum class Label {
ADDED, UPDATED, DELETED
}
}
......@@ -67,5 +67,5 @@ class VocabSet(var wordList: MutableList<WordItem>) : Vocabulary {
fun clearDisplayed() { displayedVocabulary.clear() }
fun getModified(): MutableSet<WordItem> { return modifiedVocabulary.get() }
fun getModified(): MutableMap<WordItem, ModifiedVocabulary.Label> { return modifiedVocabulary.get() }
}
\ No newline at end of file
......@@ -45,9 +45,9 @@
android:textSize="@dimen/text_size"
app:fontFamily="@font/neucha"
android:textColor="@color/text_color"
android:layout_marginRight="@dimen/word_margin"
android:layout_marginEnd="@dimen/word_margin"
tools:ignore="LabelFor" />
android:paddingEnd="@dimen/word_margin"
android:paddingRight="@dimen/word_margin"
tools:ignore="LabelFor,RtlSymmetry" />
<EditText
android:id="@+id/translation"
......@@ -60,9 +60,9 @@
android:textSize="@dimen/text_size"
app:fontFamily="@font/neucha"
android:textColor="@color/text_color"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
tools:ignore="LabelFor" />
android:paddingEnd="16dp"
android:paddingRight="16dp"
tools:ignore="LabelFor,RtlSymmetry" />
</LinearLayout>
<ImageButton
......
......@@ -34,7 +34,6 @@
android:paddingTop="8dp"
android:paddingBottom="8dp">
<TextView
android:id="@+id/word"
android:layout_width="0dp"
......@@ -43,9 +42,9 @@
android:textSize="@dimen/text_size"
app:fontFamily="@font/neucha"
android:textColor="@color/text_color"
android:layout_marginRight="@dimen/word_margin"
android:layout_marginEnd="@dimen/word_margin"
tools:ignore="LabelFor" />
android:paddingEnd="@dimen/word_margin"
android:paddingRight="@dimen/word_margin"
tools:ignore="LabelFor,RtlSymmetry" />
<TextView
android:id="@+id/translation"
......
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