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