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