Commit 15af5219 by Paktalin

Finished isolating firebase logic

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