Commit e5deb43a by Paktalin

Major refactoring in FirestoreManager

parent c370f256
package com.paktalin.vocabularynotebook.firestore
import android.content.Context
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.firestore.*
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.QuerySnapshot
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.entities.UserPojo
import com.paktalin.vocabularynotebook.entities.VocabularyPojo
import com.paktalin.vocabularynotebook.entities.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.Log
import com.paktalin.vocabularynotebook.utils.saveVocabularyId
import com.paktalin.vocabularynotebook.utils.startActivity
import com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary.Label
import java.util.*
class FirestoreManager {
private val db: FirebaseFirestore = ConfiguredFirestore.instance
private val vocabularyCollection = db.collection(VOCABULARIES)
private val wordsCollection = vocabularyCollection.document(vocabularyId!!).collection(WORDS)
private val wordsCollection = db.collection(VOCABULARIES).document(vocabularyId!!).collection(WORDS)
fun extractVocabularyId(onSuccess: () -> Unit,
showToastNoWords: () -> Unit, removeProgressBar: () -> Unit, mainActivity: Context) {
userDocument(Model.userId)?.get()
?.addOnSuccessListener { snapshot ->
removeProgressBar()
if (snapshot.get(VOCABULARIES) != null) {
setVocabularyID(snapshot, mainActivity)
onSuccess()
} else {
Log.w(TAG, "There's no collection \"vocabularies\"")
showToastNoWords()
fun extractVocabularyId(onSuccess: (String?) -> Unit) {
Log.d(TAG, "userId: $userId")
userDocument(userId)?.get()?.addOnSuccessListener { user ->
onSuccess((user.get(VOCABULARIES) as List<DocumentReference>?)?.get(0)?.id)
}
}
fun extractVocabulary(onComplete: (QuerySnapshot?) -> Unit) {
vocabularyId?.let { vocabId ->
db.collection(VOCABULARIES)
.document(vocabId)
.collection(WORDS)
.orderBy("time", Query.Direction.DESCENDING).get()
.addOnCompleteListener { t -> onComplete(t.result) }
}
}
fun addNewUser(firebaseUser: FirebaseUser?, logInActivity: LogInActivity) {
//todo addAll condition to writing to the db in Firebase Console (request.auth.uid)
vocabularyCollection.add(VocabularyPojo(null))
db.collection(VOCABULARIES).add(VocabularyPojo(null))
.addOnSuccessListener { firstVocabularyRef ->
Log.d(TAG, "VocabularyPojo successfully created: " + firstVocabularyRef.path)
setNewUserWithVocabularyData(firebaseUser, firstVocabularyRef, logInActivity)
......@@ -57,13 +61,6 @@ class FirestoreManager {
batch.commit()
}
fun extractVocabulary(onComplete: (querySnapshot: QuerySnapshot?) -> Unit) {
vocabularyDocument().collection(WORDS)
.orderBy("time", Query.Direction.DESCENDING)
.get()
.addOnCompleteListener { t -> onComplete(t.result) }
}
private fun setNewUserWithVocabularyData(firebaseUser: FirebaseUser?,
firstVocabularyRef: DocumentReference,
logInActivity: LogInActivity) {
......@@ -72,34 +69,24 @@ class FirestoreManager {
userDocument(firebaseUser?.uid)?.set(user)
?.addOnSuccessListener {
Log.i(TAG, "Successfully added user to the collection")
logInActivity.startUserActivity()
startActivity(logInActivity, MainActivity::class.java)
}
?.addOnFailureListener { exception ->
Log.w(TAG, "addUser:failure", exception)
}
}
private fun setVocabularyID(snapshot: DocumentSnapshot, context: Context) {
val vocabularies: List<DocumentReference> = snapshot.get(VOCABULARIES) as List<DocumentReference>
val vocabulary = vocabularyCollection.document(vocabularies[0].id)
saveVocabularyId(context, vocabulary.id)
vocabularyId = vocabulary.id
}
private fun userDocument(userId: String?): DocumentReference? {
return userId?.let { db.collection(USERS).document(it) }
}
private fun vocabularyDocument(): DocumentReference {
return vocabularyCollection.document(vocabularyId!!)
}
companion object {
private const val USERS = "users"
private const val WORDS = "words"
private const val VOCABULARIES = "vocabularies"
const val VOCABULARIES = "vocabularies"
private const val TAG = "VN/FirestoreManager"
var vocabularyId: String? = null
var userId: String? = null
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.firestore
import com.paktalin.vocabularynotebook.entities.UserPojo
import com.paktalin.vocabularynotebook.entities.VocabularyPojo
import com.paktalin.vocabularynotebook.entities.WordPojo
class Model {
lateinit var user: UserPojo
lateinit var vocabularyPojo: VocabularyPojo
lateinit var words: MutableList<WordPojo>
companion object {
var userId: String? = null
}
}
\ No newline at end of file
......@@ -11,8 +11,7 @@ class MyUserManager {
companion object {
private const val TAG = "VN/MyUserManager"
private var auth: FirebaseAuth? = FirebaseAuth.getInstance()
private var user: FirebaseUser? = auth?.currentUser
internal var auth: FirebaseAuth? = FirebaseAuth.getInstance()
fun logIn(username: String, password: String,
onComplete: () -> Unit,
......@@ -22,13 +21,13 @@ class MyUserManager {
?.addOnCompleteListener { onComplete() }
?.addOnSuccessListener {
Log.d(TAG, "Successfully signed in")
FirestoreManager.userId = auth?.currentUser?.uid
onSuccess()
}
?.addOnFailureListener { e ->
Log.w(TAG, "signInWithEmail:failure", e)
onFailure()
}
Model.userId = this.user?.uid
}
fun signUp(username: String, password: String,
......@@ -40,14 +39,14 @@ class MyUserManager {
?.addOnCompleteListener { onComplete() }
?.addOnSuccessListener {
Log.d(TAG, "Successfully signed up a new user")
FirestoreManager().addNewUser(user, logInActivity)
FirestoreManager().addNewUser(auth?.currentUser, logInActivity)
FirestoreManager.userId = auth?.currentUser?.uid
onSuccess()
}
?.addOnFailureListener { e ->
Log.d(TAG, "createUserWithEmail:failure", e.fillInStackTrace())
onFailure()
}
Model.userId = this.user?.uid
}
fun deleteUser(user: FirebaseUser?) {
......@@ -57,7 +56,7 @@ class MyUserManager {
}
fun userLoggedIn(): Boolean {
return user != null
return auth?.currentUser != null
}
fun logOut() {
......
......@@ -3,6 +3,7 @@ package com.paktalin.vocabularynotebook.ui.activities
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestore.FirestoreManager
import com.paktalin.vocabularynotebook.firestore.MyUserManager
import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.activity_log_in.*
......@@ -18,16 +19,20 @@ class LogInActivity : AppCompatActivity() {
override fun onStart() {
super.onStart()
if (MyUserManager.userLoggedIn()) startActivity(this, MainActivity::class.java)
if (MyUserManager.userLoggedIn()) {
startActivity(this@LogInActivity, MainActivity::class.java)
FirestoreManager.userId = MyUserManager.auth?.currentUser?.uid
}
}
private fun login() {
getValidUsernameAndPassword()?.let { (u, p) ->
Log.d(TAG, "Logging in: username $u")
addProgressBar(supportFragmentManager, R.id.container_login)
MyUserManager.logIn(u, p,
onComplete = { removeProgressBar(supportFragmentManager) },
onSuccess = { startActivity(this, MainActivity::class.java) },
onFailure = { shortToast(this, getString(R.string.toast_auth_failed)) })
onSuccess = { startActivity(this@LogInActivity, MainActivity::class.java) },
onFailure = { shortToast(this@LogInActivity, getString(R.string.toast_auth_failed)) })
}
}
......@@ -37,19 +42,14 @@ class LogInActivity : AppCompatActivity() {
MyUserManager.signUp(u, p,
onComplete = { removeProgressBar(supportFragmentManager) },
onSuccess = {
shortToast(this, resources.getString(R.string.toast_successful_sign_up))
startActivity(this, MainActivity::class.java)
shortToast(this@LogInActivity, resources.getString(R.string.toast_successful_sign_up))
startActivity(this@LogInActivity, MainActivity::class.java)
},
onFailure = { shortToast(this, resources.getString(R.string.toast_sign_up_failed)) },
logInActivity = this)
onFailure = { shortToast(this@LogInActivity, resources.getString(R.string.toast_sign_up_failed)) },
logInActivity = this@LogInActivity)
}
}
fun startUserActivity() {
Log.d(TAG, "Logged in successfully")
startActivity(this@LogInActivity, MainActivity::class.java)
}
private fun getValidUsernameAndPassword(): Pair<String, String>? {
val username = etUsername!!.text.toString()
val password = etPassword!!.text.toString()
......
......@@ -10,6 +10,7 @@ import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.selection.Selection
import com.google.firebase.firestore.QuerySnapshot
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestore.FirestoreManager
import com.paktalin.vocabularynotebook.firestore.MyUserManager
......@@ -33,6 +34,7 @@ class MainActivity : AppCompatActivity() {
private var tagFragment = TagFragment()
override fun onCreate(savedInstanceState: Bundle?) {
if (savedInstanceState == null) Log.d(TAG, "null state")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
FirestoreManager.vocabularyId = getSavedVocabularyIdFromPreferences(this@MainActivity)
......@@ -49,7 +51,8 @@ class MainActivity : AppCompatActivity() {
searchView = menu!!.findItem(R.id.search).actionView as SearchView
// extract vocabulary data only after searchView is initialized,
// since it needs to be called in the VocabularyAdapter initialization
setUpVocabularyAdapter()
Log.d(TAG, "onCreateOptionsMenu")
extractVocabularyId()
return true
}
......@@ -74,7 +77,7 @@ class MainActivity : AppCompatActivity() {
MyUserManager.logOut()
startActivity(this@MainActivity, LogInActivity::class.java)
}
drawerLayout!!.closeDrawers()
drawerLayout.closeDrawers()
true
}
}
......@@ -84,23 +87,34 @@ class MainActivity : AppCompatActivity() {
swipeRefresh.setColorSchemeResources(R.color.colorAccent)
}
private fun setUpVocabularyAdapter() {
private fun extractVocabularyId() {
Log.d(TAG, "extracting vocabulary id")
addProgressBar(supportFragmentManager, R.id.container_main)
FirestoreManager().extractVocabularyId(
onSuccess = {
FirestoreManager().extractVocabulary { querySnapshot ->
run {
if (querySnapshot != null && !querySnapshot.isEmpty) {
onSuccess = { vocabularyId -> extractVocabularyWithId(vocabularyId) })
}
private fun extractVocabularyWithId(vocabularyId: String?) {
Log.d(TAG, "vocabularyId: $vocabularyId")
removeProgressBar(supportFragmentManager)
if (vocabularyId == null) {
shortToast(this, getString(R.string.toast_empty_vocabulary))
return
}
saveVocabularyId(this, vocabularyId)
FirestoreManager.vocabularyId = vocabularyId
FirestoreManager().extractVocabulary(
onComplete = { querySnapshot -> setUpVocabularyAdapter(querySnapshot) })
}
private fun setUpVocabularyAdapter(querySnapshot: QuerySnapshot?) {
if (querySnapshot == null || querySnapshot.isEmpty) {
shortToast(this, getString(R.string.toast_empty_vocabulary))
return
}
vocabularyAdapter = VocabularyAdapter(VocabSet.createFromSnapshot(querySnapshot), this@MainActivity)
recyclerView.adapter = vocabularyAdapter
setUpSelectionTracker()
} else showToastNoWords()
}
}
},
showToastNoWords = { shortToast(this, getString(R.string.toast_empty_vocabulary)) },
removeProgressBar = { removeProgressBar(supportFragmentManager) },
mainActivity = this)
}
......@@ -125,10 +139,6 @@ class MainActivity : AppCompatActivity() {
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
private fun showToastNoWords() {
shortToast(this, getString(R.string.toast_empty_vocabulary))
}
private fun setUpSelectionTracker() {
vocabularyAdapter?.selectionTracker = MySelectionTracker().apply {
init(recyclerView)
......
......@@ -63,6 +63,7 @@ fun shortToast(context: Context, text: String) {
}
fun startActivity(context: Context, cls: Class<*>) {
Log.d("VN/ActivityUtil", "starting activity $cls")
context.startActivity(Intent(context, cls))
}
......
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