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