Commit e6d52044 by Paktalin

Isolated firestore logic from EditWordFragment and AddWordFragment

parent 8a981912
......@@ -63,7 +63,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
if (it.itemId == R.id.option_edit) { editWord(v, displayedVocabulary.getAt(position)) }
true
}
if (EditWordFragment.notInEditMode) popup.show()
if (!mainActivity.inEditMode) popup.show()
}
private fun deleteWord(position: Int) {
......@@ -96,7 +96,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
@SuppressLint("ResourceType")
private fun editWord(container: View, wordItem: WordItem) {
if (EditWordFragment.notInEditMode) {
if (!mainActivity.inEditMode) {
//set container id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
container.id = View.generateViewId()
......
......@@ -20,6 +20,7 @@ class MainActivity : AppCompatActivity() {
lateinit var vocabularyFragment: VocabularyFragment
lateinit var searchView: SearchView
var inEditMode = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -59,7 +60,7 @@ class MainActivity : AppCompatActivity() {
private fun extractVocabularyData() {
addProgressBar()
FirestoreManager().extractVocabularyData(
FirestoreManager().extractVocabularyId(
{ id-> addVocabularyFragment(id) },
{ showToastNoWords() },
{ removeProgressBar() })
......
package com.paktalin.vocabularynotebook.ui.fragments
import android.util.Log
import android.view.View
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.WORDS
import com.paktalin.vocabularynotebook.utils.FirestoreManager
import com.paktalin.vocabularynotebook.utils.shortToast
import kotlinx.android.synthetic.main.fragment_editable_word.*
......@@ -14,19 +11,17 @@ class AddWordFragment : WordFragment() {
override fun saveToFirestore(word:String, translation:String, vocabularyId: String) {
val wordPojo = WordItem.Pojo(word, translation, null)
ConfiguredFirestore.instance
.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).add(wordPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully added a new word")
clearFields()
mainActivity.removeProgressBar()
val wordItem = WordItem(wordPojo, it.id, vocabularyId)
updateRecycleView(wordItem) }
.addOnFailureListener {
Log.w(TAG, "addNewWordToDb:failure", it.fillInStackTrace())
shortToast(mainActivity, getString(R.string.toast_new_word_fail))
}
FirestoreManager().saveNewWord(
{ documentId -> onSuccessfulSave(wordPojo, vocabularyId, documentId) },
{ shortToast(mainActivity, getString(R.string.toast_new_word_fail)) },
wordPojo, vocabularyId)
}
private fun onSuccessfulSave(wordPojo: WordItem.Pojo, vocabularyId: String, documentId: String) {
clearFields()
mainActivity.removeProgressBar()
val wordItem = WordItem(wordPojo, documentId, vocabularyId)
updateRecycleView(wordItem)
}
override fun updateRecycleView(wordItem: WordItem) {
......
......@@ -2,21 +2,15 @@ package com.paktalin.vocabularynotebook.ui.fragments
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.WORDS
import com.paktalin.vocabularynotebook.utils.disableScrolling
import com.paktalin.vocabularynotebook.utils.enableScrolling
import com.paktalin.vocabularynotebook.utils.removeFragment
import com.paktalin.vocabularynotebook.utils.shortToast
import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.fragment_editable_word.*
import kotlinx.android.synthetic.main.content_main.*
import kotlinx.android.synthetic.main.word_item.view.*
......@@ -25,8 +19,8 @@ class EditWordFragment : WordFragment() {
private lateinit var wordItem: WordItem
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
notInEditMode = false
mainActivity = activity as MainActivity
mainActivity.inEditMode = true
hidePreviousViews(container)
wordItem = arguments!!["wordItem"] as WordItem
return super.onCreateView(inflater, container, savedInstanceState)
......@@ -42,7 +36,7 @@ class EditWordFragment : WordFragment() {
private fun setWordItemData() {
word.setText(wordItem.pojo.word)
translation.setText(wordItem.pojo.translation)
editable_word.setBackgroundColor(resources.getColor(R.color.green_highlight))
editable_word.setBackgroundColor(ContextCompat.getColor(mainActivity, R.color.green_highlight))
}
private fun setFocusOnWord() {
......@@ -61,22 +55,23 @@ class EditWordFragment : WordFragment() {
override fun saveToFirestore(word:String, translation:String, vocabularyId: String) {
val wordPojo = WordItem.Pojo(word, translation, wordItem.pojo.time)
ConfiguredFirestore.instance
.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).document(wordItem.id).set(wordPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully updated the word")
hideSubmitButton()
mainActivity.removeProgressBar()
wordItem.pojo = wordPojo
updateRecycleView(wordItem)
stop()
}
.addOnFailureListener {
Log.w(TAG, "updateExistingWord:failure", it.fillInStackTrace())
shortToast(mainActivity, getString(R.string.toast_update_word_failed))
stop()
}
FirestoreManager().updateWord(
{ onSuccessfulSave(wordPojo) },
{ onFailure() },
wordItem, wordPojo, vocabularyId)
}
private fun onSuccessfulSave(wordPojo: WordItem.Pojo) {
hideSubmitButton()
mainActivity.removeProgressBar()
wordItem.pojo = wordPojo
updateRecycleView(wordItem)
stop()
}
private fun onFailure() {
shortToast(mainActivity, getString(R.string.toast_update_word_failed))
stop()
}
private fun stop() {
......@@ -84,7 +79,7 @@ class EditWordFragment : WordFragment() {
mainActivity.btnSubmit.setOnClickListener { (mainActivity.fragmentAddWord as AddWordFragment).submitWord() }
enableScrolling(mainActivity)
removeFragment(mainActivity.supportFragmentManager, this)
notInEditMode = true
mainActivity.inEditMode = false
}
override fun updateRecycleView(wordItem: WordItem) {
......@@ -93,6 +88,5 @@ class EditWordFragment : WordFragment() {
companion object {
private val TAG = "VN/" + EditWordFragment::class.java.simpleName
var notInEditMode = true
}
}
\ No newline at end of file
......@@ -9,30 +9,34 @@ import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import java.util.*
class FirestoreManager {
private val db: FirebaseFirestore = ConfiguredFirestore.instance
private val vocabularyCollection = db.collection(VOCABULARIES)
fun extractVocabularyData(onSuccess: (vocabularyId: String) -> Unit,
showToastNoWords: () -> Unit, removeProgressBar: () -> Unit) {
fun extractVocabularyId(onSuccess: (vocabularyId: String) -> Unit,
showToastNoWords: () -> Unit, removeProgressBar: () -> Unit) {
val userId = FirebaseAuth.getInstance()!!.currentUser!!.uid
userDocument(userId).get()
.addOnSuccessListener { task ->
.addOnSuccessListener { snapshot ->
removeProgressBar()
if (task.get(VOCABULARIES) != null) {
setVocabularyID(task, db)
if (snapshot.get(VOCABULARIES) != null) {
setVocabularyID(snapshot)
onSuccess(vocabularyId)
} else {
Log.w(TAG, "There's no collection \"vocabularies\"")
showToastNoWords() } }
showToastNoWords()
}
}
}
fun addNewUserToDb(firebaseUser: FirebaseUser, logInActivity: LogInActivity) {
fun addNewUser(firebaseUser: FirebaseUser, logInActivity: LogInActivity) {
//todo add condition to writing to the db in Firebase Console (request.auth.uid)
db.collection(VOCABULARIES).add(Vocabulary.Pojo(null))
vocabularyCollection.add(Vocabulary.Pojo(null))
.addOnSuccessListener { firstVocabularyRef ->
Log.d(TAG, "VocabularyPojo successfully created: " + firstVocabularyRef.path)
setNewUserWithVocabularyData(firebaseUser, firstVocabularyRef, logInActivity)
......@@ -43,8 +47,31 @@ class FirestoreManager {
}
}
private fun userDocument(userId: String): DocumentReference {
return db.collection(USERS).document(userId)
fun saveNewWord(onSuccess: (documentId: String) -> Unit, onFailure: () -> Unit,
wordPojo: WordItem.Pojo, vocabularyId: String) {
vocabularyDocument(vocabularyId)
.collection(WORDS).add(wordPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully added a new word")
onSuccess(it.id)
}
.addOnFailureListener {
Log.w(TAG, "addNewWordToDb:failure", it.fillInStackTrace())
onFailure() }
}
fun updateWord(onSuccess: () -> Unit, onFailure: () -> Unit,
wordItem: WordItem, wordPojo: WordItem.Pojo, vocabularyId: String) {
vocabularyDocument(vocabularyId)
.collection(WORDS).document(wordItem.id).set(wordPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully updated the word")
onSuccess()
}
.addOnFailureListener {
Log.w(TAG, "updateExistingWord:failure", it.fillInStackTrace())
onFailure()
}
}
private fun setNewUserWithVocabularyData(firebaseUser: FirebaseUser,
......@@ -62,12 +89,20 @@ class FirestoreManager {
}
}
private fun setVocabularyID(task: DocumentSnapshot, db: FirebaseFirestore) {
val vocabularies: List<DocumentReference> = task.get(VOCABULARIES) as List<DocumentReference>
val vocabulary = db.collection(VOCABULARIES).document(vocabularies[0].id)
private fun setVocabularyID(snapshot: DocumentSnapshot) {
val vocabularies: List<DocumentReference> = snapshot.get(VOCABULARIES) as List<DocumentReference>
val vocabulary = vocabularyDocument(vocabularies[0].id)
vocabularyId = vocabulary.id
}
private fun userDocument(userId: String): DocumentReference {
return db.collection(USERS).document(userId)
}
private fun vocabularyDocument(id: String): DocumentReference {
return vocabularyCollection.document(id)
}
companion object {
const val USERS = "users"
const val WORDS = "words"
......
......@@ -33,7 +33,7 @@ fun mSignUp(activity: LogInActivity, email: String, password: String) {
.addOnCompleteListener { removeProgressBar(activity.supportFragmentManager) }
.addOnSuccessListener {
Log.d(TAG, "Successfully signed up a new user")
FirestoreManager().addNewUserToDb(mAuth!!.currentUser!!, activity)
FirestoreManager().addNewUser(mAuth!!.currentUser!!, activity)
activity.login()
}
.addOnFailureListener {
......
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