Commit eb0e29a6 by Paktalin

More refactoring. Null safety

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