Commit 632ea2a0 by Paktalin

Removed saving to Firestore right after editing/adding

parent 9f8de0f3
...@@ -56,7 +56,7 @@ class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val main ...@@ -56,7 +56,7 @@ class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val main
} }
private fun deleteWord(position: Int) { private fun deleteWord(position: Int) {
//TODO delete entry point //TODO delete from fullVocabulary
displayedVocabulary.deleteWord(position) displayedVocabulary.deleteWord(position)
// update recyclerView // update recyclerView
recyclerView.removeViewAt(position) recyclerView.removeViewAt(position)
......
...@@ -4,7 +4,7 @@ import com.paktalin.vocabularynotebook.utils.FirestoreManager ...@@ -4,7 +4,7 @@ import com.paktalin.vocabularynotebook.utils.FirestoreManager
import java.io.Serializable import java.io.Serializable
import java.util.Date import java.util.Date
class WordItem(word: String, translation: String, time: Date?, var id: String, private val vocabularyId: String) : Serializable { class WordItem(word: String, translation: String, time: Date?, var id: String?, private val vocabularyId: String?) : Serializable {
var pojo: Pojo = Pojo(word, translation, time) var pojo: Pojo = Pojo(word, translation, time)
class Pojo(var word: String, var translation: String, var time:Date?) : Serializable { class Pojo(var word: String, var translation: String, var time:Date?) : Serializable {
...@@ -13,11 +13,13 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p ...@@ -13,11 +13,13 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
} }
} }
constructor(pojo: Pojo, id: String, vocabularyId: String) constructor(pojo: Pojo, id: String?, vocabularyId: String?)
: this(pojo.word, pojo.translation, pojo.time, id, vocabularyId) : this(pojo.word, pojo.translation, pojo.time, id, vocabularyId)
constructor(pojo: Pojo) : this(pojo, null, null)
fun delete() { fun delete() {
FirestoreManager().deleteWord(id) FirestoreManager().deleteWord(id!!)
} }
fun contains(string:String):Boolean { fun contains(string:String):Boolean {
......
...@@ -3,7 +3,6 @@ package com.paktalin.vocabularynotebook.ui.fragments ...@@ -3,7 +3,6 @@ package com.paktalin.vocabularynotebook.ui.fragments
import android.os.Bundle import android.os.Bundle
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.utils.* import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.content_main.* import kotlinx.android.synthetic.main.content_main.*
...@@ -37,14 +36,11 @@ class AddWordFragment : WordFragment() { ...@@ -37,14 +36,11 @@ class AddWordFragment : WordFragment() {
mainActivity.hideKeyboardNotFromActivity(mainActivity) mainActivity.hideKeyboardNotFromActivity(mainActivity)
} }
override fun saveToFirestore(word:String, translation:String, vocabularyId: String) { override fun save(word:String, translation:String) {
// TODO add entry point // TODO add entry point
clearFields()
val wordPojo = WordItem.Pojo(word, translation, null) mainActivity.vocabularyAdapter.addWord(WordItem(WordItem.Pojo(word, translation, null)))
FirestoreManager().saveNewWord( this.word.requestFocus()
{ documentId -> onSuccessfulSave(wordPojo, vocabularyId, documentId) },
{ shortToast(mainActivity, getString(R.string.toast_new_word_fail)) },
wordPojo)
} }
private fun textWatcher(setEmpty: () -> Unit): TextWatcher { private fun textWatcher(setEmpty: () -> Unit): TextWatcher {
...@@ -60,13 +56,5 @@ class AddWordFragment : WordFragment() { ...@@ -60,13 +56,5 @@ class AddWordFragment : WordFragment() {
if (wordEmpty && translationEmpty) invisible(btnClear) if (wordEmpty && translationEmpty) invisible(btnClear)
} }
private fun onSuccessfulSave(wordPojo: WordItem.Pojo, vocabularyId: String, documentId: String) {
clearFields()
mainActivity.removeProgressBar()
val wordItem = WordItem(wordPojo, documentId, vocabularyId)
mainActivity.vocabularyAdapter.addWord(wordItem)
word.requestFocus()
}
companion object { private val TAG = "VN/" + AddWordFragment::class.java.simpleName } companion object { private val TAG = "VN/" + AddWordFragment::class.java.simpleName }
} }
\ No newline at end of file
...@@ -64,28 +64,13 @@ class EditWordFragment : WordFragment() { ...@@ -64,28 +64,13 @@ class EditWordFragment : WordFragment() {
visible(container!!.translation) visible(container!!.translation)
} }
override fun saveToFirestore(word: String, translation: String, vocabularyId: String) { override fun save(word: String, translation: String) {
// TODO edit entry point // TODO edit entry point
val wordPojo = WordItem.Pojo(word, translation, wordItem.pojo.time) wordItem.pojo = WordItem.Pojo(word, translation, wordItem.pojo.time)
FirestoreManager().updateWord(
{ onSuccessfulSave(wordPojo) },
{ onFailure() },
wordItem, wordPojo)
}
private fun onSuccessfulSave(wordPojo: WordItem.Pojo) {
gone(mainActivity.btnSubmitLayout)
mainActivity.removeProgressBar()
wordItem.pojo = wordPojo
mainActivity.vocabularyAdapter.updateWord(wordItem) mainActivity.vocabularyAdapter.updateWord(wordItem)
stop() stop()
} }
private fun onFailure() {
/*shortToast(mainActivity, getString(R.string.toast_update_word_failed))
stop()*/
}
private fun stop() { private fun stop() {
// set onClickListener from AddWordFragment // set onClickListener from AddWordFragment
mainActivity.btnSubmit.setOnClickListener { (mainActivity.fragmentAddWord as AddWordFragment).submitWord() } mainActivity.btnSubmit.setOnClickListener { (mainActivity.fragmentAddWord as AddWordFragment).submitWord() }
......
...@@ -2,17 +2,13 @@ package com.paktalin.vocabularynotebook.ui.fragments ...@@ -2,17 +2,13 @@ package com.paktalin.vocabularynotebook.ui.fragments
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.activities.MainActivity import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.FirestoreManager import com.paktalin.vocabularynotebook.utils.FirestoreManager
import com.paktalin.vocabularynotebook.utils.gone import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.visible
import kotlinx.android.synthetic.main.fragment_editable_word.* import kotlinx.android.synthetic.main.fragment_editable_word.*
import kotlinx.android.synthetic.main.content_main.* import kotlinx.android.synthetic.main.content_main.*
...@@ -34,8 +30,7 @@ abstract class WordFragment : Fragment() { ...@@ -34,8 +30,7 @@ abstract class WordFragment : Fragment() {
fun submitWord() { fun submitWord() {
gone(mainActivity.btnSubmitLayout) gone(mainActivity.btnSubmitLayout)
mainActivity.addProgressBar() save(word.text.toString(), translation.text.toString())
saveToFirestore(word.text.toString(), translation.text.toString(), FirestoreManager.vocabularyId!!)
return return
} }
...@@ -45,5 +40,5 @@ abstract class WordFragment : Fragment() { ...@@ -45,5 +40,5 @@ abstract class WordFragment : Fragment() {
} }
protected abstract fun cancelEditing() protected abstract fun cancelEditing()
protected abstract fun saveToFirestore(word:String, translation:String, vocabularyId:String) protected abstract fun save(word:String, translation:String)
} }
\ No newline at end of file
...@@ -63,7 +63,7 @@ class FirestoreManager { ...@@ -63,7 +63,7 @@ class FirestoreManager {
fun updateWord(onSuccess: () -> Unit, onFailure: () -> Unit, wordItem: WordItem, wordPojo: WordItem.Pojo) { fun updateWord(onSuccess: () -> Unit, onFailure: () -> Unit, wordItem: WordItem, wordPojo: WordItem.Pojo) {
vocabularyDocument() vocabularyDocument()
.collection(WORDS).document(wordItem.id).set(wordPojo) .collection(WORDS).document(wordItem.id!!).set(wordPojo)
.addOnSuccessListener { .addOnSuccessListener {
Log.i(TAG, "Successfully updated the word") Log.i(TAG, "Successfully updated the word")
onSuccess() onSuccess()
......
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