Commit 15af5219 by Paktalin

Finished isolating firebase logic

parent e6d52044
package com.paktalin.vocabularynotebook.firestoreitems
import android.util.Log
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.WORDS
import com.paktalin.vocabularynotebook.utils.FirestoreManager
import java.io.Serializable
import java.util.Date
......@@ -20,10 +17,7 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
: this(pojo.word, pojo.translation, pojo.time, id, vocabularyId)
fun delete() {
ConfiguredFirestore.instance.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).document(id).delete()
.addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
FirestoreManager().deleteWord(id)
}
fun contains(string:String):Boolean {
......
......@@ -8,15 +8,12 @@ import android.view.View
import android.view.ViewGroup
import com.google.firebase.Timestamp
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.Query
import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.WORDS
import com.paktalin.vocabularynotebook.utils.FirestoreManager
import kotlinx.android.synthetic.main.fragment_vocabulary.*
class VocabularyFragment : Fragment() {
......@@ -24,7 +21,6 @@ class VocabularyFragment : Fragment() {
private val TAG = "VN/" + VocabularyFragment::class.simpleName
}
private val db = ConfiguredFirestore.instance
private lateinit var mainActivity: MainActivity
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
......@@ -35,7 +31,7 @@ class VocabularyFragment : Fragment() {
super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
setEmptyAdapter()
retrieveWordsData(arguments!!["vocabularyId"] as String)
extractWordsData(arguments!!["vocabularyId"] as String)
}
private fun setEmptyAdapter() {
......@@ -46,17 +42,18 @@ class VocabularyFragment : Fragment() {
recyclerView.layoutManager = mLayoutManager
}
private fun retrieveWordsData(vocabularyId: String) {
db.collection(VOCABULARIES).document(vocabularyId).collection(WORDS)
.orderBy("time", Query.Direction.DESCENDING)
.get()
.addOnSuccessListener {
if (it.documents.size != 0)
setVocabularyAdapter(it.documents, vocabularyId)
private fun extractWordsData(vocabularyId: String) {
FirestoreManager().retrieveWordsData({ documents ->
onSuccessfulWordDataExtraction(documents, vocabularyId)}, vocabularyId)
}
private fun onSuccessfulWordDataExtraction(documents: MutableList<DocumentSnapshot>, vocabularyId: String) {
if (documents.isNotEmpty())
setVocabularyAdapter(documents, vocabularyId)
else {
Log.i(TAG, "There are no documents in collection \"words\"")
mainActivity.showToastNoWords()
}}
}
}
private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>, vocabularyId: String) {
......
......@@ -6,11 +6,13 @@ import com.google.firebase.auth.FirebaseUser
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
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 com.paktalin.vocabularynotebook.ui.fragments.VocabularyFragment
import java.util.*
class FirestoreManager {
......@@ -74,6 +76,20 @@ class FirestoreManager {
}
}
fun deleteWord(id: String) {
vocabularyDocument(vocabularyId)
.collection(WORDS).document(id).delete()
.addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
}
fun retrieveWordsData(onSuccess: (documents: MutableList<DocumentSnapshot>) -> Unit, vocabularyId: String) {
vocabularyDocument(vocabularyId).collection(WORDS)
.orderBy("time", Query.Direction.DESCENDING)
.get()
.addOnSuccessListener { onSuccess(it.documents)}
}
private fun setNewUserWithVocabularyData(firebaseUser: FirebaseUser,
firstVocabularyRef: DocumentReference,
logInActivity: LogInActivity) {
......@@ -99,14 +115,14 @@ class FirestoreManager {
return db.collection(USERS).document(userId)
}
private fun vocabularyDocument(id: String): DocumentReference {
return vocabularyCollection.document(id)
private fun vocabularyDocument(vocabularyId: String): DocumentReference {
return vocabularyCollection.document(vocabularyId)
}
companion object {
const val USERS = "users"
const val WORDS = "words"
const val VOCABULARIES = "vocabularies"
private const val USERS = "users"
private const val WORDS = "words"
private const val VOCABULARIES = "vocabularies"
private const val TAG = "VN/FirestoreManager"
lateinit var vocabularyId: String
......
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