Commit eb0e29a6 by Paktalin

More refactoring. Null safety

parent 5934185c
......@@ -23,8 +23,8 @@ class FirestoreManager {
fun extractVocabularyId(onSuccess: () -> Unit,
showToastNoWords: () -> Unit, removeProgressBar: () -> Unit, mainActivity: Context) {
val userId = FirebaseAuth.getInstance()!!.currentUser!!.uid
userDocument(userId).get()
.addOnSuccessListener { snapshot ->
userDocument(userId)?.get()
?.addOnSuccessListener { snapshot ->
removeProgressBar()
if (snapshot.get(VOCABULARIES) != null) {
setVocabularyID(snapshot, mainActivity)
......@@ -36,7 +36,7 @@ class FirestoreManager {
}
}
fun addNewUser(firebaseUser: FirebaseUser, logInActivity: LogInActivity) {
fun addNewUser(firebaseUser: FirebaseUser?, logInActivity: LogInActivity) {
//todo addAll condition to writing to the db in Firebase Console (request.auth.uid)
vocabularyCollection.add(VocabularyPojo(null))
.addOnSuccessListener { firstVocabularyRef ->
......@@ -66,17 +66,17 @@ class FirestoreManager {
.addOnCompleteListener { t -> onComplete(t.result) }
}
private fun setNewUserWithVocabularyData(firebaseUser: FirebaseUser,
private fun setNewUserWithVocabularyData(firebaseUser: FirebaseUser?,
firstVocabularyRef: DocumentReference,
logInActivity: LogInActivity) {
val user = UserPojo(firebaseUser.email, firebaseUser.uid)
val user = UserPojo(firebaseUser?.email, firebaseUser?.uid)
user.vocabularies = Collections.singletonList(firstVocabularyRef)
userDocument(firebaseUser.uid).set(user)
.addOnSuccessListener {
userDocument(firebaseUser?.uid)?.set(user)
?.addOnSuccessListener {
Log.i(TAG, "Successfully added user to the collection")
logInActivity.startUserActivity()
}
.addOnFailureListener { exception ->
?.addOnFailureListener { exception ->
Log.w(TAG, "addUser:failure", exception)
}
}
......@@ -88,8 +88,8 @@ class FirestoreManager {
vocabularyId = vocabulary.id
}
private fun userDocument(userId: String): DocumentReference {
return db.collection(USERS).document(userId)
private fun userDocument(userId: String?): DocumentReference? {
return userId?.let { db.collection(USERS).document(it) }
}
private fun vocabularyDocument(): DocumentReference {
......
......@@ -3,6 +3,7 @@ package com.paktalin.vocabularynotebook.firestore
import android.util.Patterns
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import com.paktalin.vocabularynotebook.utils.Log
class MyUserManager {
......@@ -10,19 +11,14 @@ class MyUserManager {
companion object {
private const val TAG = "VN/MyUserManager"
private var mAuth: FirebaseAuth? = FirebaseAuth.getInstance()
fun deleteUser(user: FirebaseUser) {
user.delete()
.addOnSuccessListener { Log.i(TAG, "UserPojo was successfully deleted") }
.addOnFailureListener { Log.i(TAG, "deleteUser:failure", it.cause) }
}
private var auth: FirebaseAuth? = FirebaseAuth.getInstance()
private var user: FirebaseUser? = auth?.currentUser
fun logIn(username: String, password: String,
onComplete: () -> Unit,
onSuccess: () -> Unit,
onFailure: () -> Unit) {
mAuth?.signInWithEmailAndPassword(constructEmail(username), password)
auth?.signInWithEmailAndPassword(constructEmail(username), password)
?.addOnCompleteListener { onComplete() }
?.addOnSuccessListener {
Log.d(TAG, "Successfully signed in")
......@@ -34,17 +30,16 @@ class MyUserManager {
}
}
private fun isEmail(string: String) = Patterns.EMAIL_ADDRESS.matcher(string).matches()
fun signUp(username: String, password: String,
onComplete: () -> Unit,
onSuccess: () -> Unit,
onFailure: () -> Unit) {
mAuth?.createUserWithEmailAndPassword(constructEmail(username), password)
onFailure: () -> Unit,
logInActivity: LogInActivity) {
auth?.createUserWithEmailAndPassword(constructEmail(username), password)
?.addOnCompleteListener { onComplete() }
?.addOnSuccessListener {
Log.d(TAG, "Successfully signed up a new user")
mAuth?.currentUser?.let { it1 -> FirestoreManager().addNewUser(it1, activity) }
FirestoreManager().addNewUser(user, logInActivity)
onSuccess()
}
?.addOnFailureListener { e ->
......@@ -53,15 +48,23 @@ class MyUserManager {
}
}
fun deleteUser(user: FirebaseUser?) {
user?.delete()
?.addOnSuccessListener { Log.i(TAG, "UserPojo was successfully deleted") }
?.addOnFailureListener { Log.i(TAG, "deleteUser:failure", it.cause) }
}
fun userLoggedIn(): Boolean {
return mAuth?.currentUser != null
return user != null
}
fun logOut() {
mAuth?.signOut()
auth?.signOut()
Log.i(TAG, "User logged out")
}
private fun isEmail(string: String) = Patterns.EMAIL_ADDRESS.matcher(string).matches()
private fun constructEmail(username: String): String {
return if (isEmail(username)) username
else username.toLowerCase() + "@wordbook.com"
......
......@@ -37,7 +37,8 @@ class LogInActivity : AppCompatActivity() {
MyUserManager.signUp(u, p,
onComplete = { removeProgressBar(supportFragmentManager) },
onSuccess = { shortToast(this, resources.getString(R.string.toast_successful_sign_up)); login() },
onFailure = { shortToast(this, resources.getString(R.string.toast_sign_up_failed)) })
onFailure = { shortToast(this, resources.getString(R.string.toast_sign_up_failed)) },
logInActivity = this)
}
}
......
......@@ -12,6 +12,7 @@ import androidx.appcompat.widget.SearchView
import androidx.recyclerview.selection.Selection
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestore.FirestoreManager
import com.paktalin.vocabularynotebook.firestore.MyUserManager
import com.paktalin.vocabularynotebook.ui.fragments.SubmitAddedFragment
import com.paktalin.vocabularynotebook.ui.fragments.TagFragment
import com.paktalin.vocabularynotebook.ui.recycler_view.State
......@@ -61,11 +62,6 @@ class MainActivity : AppCompatActivity() {
return super.onOptionsItemSelected(item)
}
private fun logOut() {
com.paktalin.vocabularynotebook.firestore.logOut()
startActivity(this@MainActivity, LogInActivity::class.java)
}
private fun refreshVocabulary() {
vocabularyAdapter?.refresh()
swipeRefresh.isRefreshing = false
......@@ -74,8 +70,10 @@ class MainActivity : AppCompatActivity() {
private fun setUpNavigationView() {
navigationView.setNavigationItemSelectedListener { menuItem ->
menuItem.isChecked = true
if (menuItem.itemId == R.id.logOut)
logOut()
if (menuItem.itemId == R.id.logOut) {
MyUserManager.logOut()
startActivity(this@MainActivity, LogInActivity::class.java)
}
drawerLayout!!.closeDrawers()
true
}
......
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