Commit 5934185c by Paktalin

Refactoring in LogInActivity

parent 82cb4ebe
package com.paktalin.vocabularynotebook.entities
class VocabularyPojo(var title: String?, val id: String?) {
import java.util.*
class VocabularyPojo(var title: String?, var id: String? = null) {
init {
if (title == null) title = "Untitled vocabulary"
title = title ?: "Untitled vocabulary"
id = id ?: UUID.randomUUID().toString()
}
}
......@@ -45,7 +45,7 @@ class FirestoreManager {
}
.addOnFailureListener {
Log.w(TAG, "Couldn't addAll user to the database", it.cause)
deleteUser(firebaseUser)
MyUserManager.deleteUser(firebaseUser)
}
}
......@@ -69,7 +69,7 @@ class FirestoreManager {
private fun setNewUserWithVocabularyData(firebaseUser: FirebaseUser,
firstVocabularyRef: DocumentReference,
logInActivity: LogInActivity) {
val user = UserPojo(firebaseUser.email)
val user = UserPojo(firebaseUser.email, firebaseUser.uid)
user.vocabularies = Collections.singletonList(firstVocabularyRef)
userDocument(firebaseUser.uid).set(user)
.addOnSuccessListener {
......
......@@ -3,23 +3,25 @@ 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.R
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import com.paktalin.vocabularynotebook.utils.Log
import com.paktalin.vocabularynotebook.utils.removeProgressBar
import com.paktalin.vocabularynotebook.utils.shortToast
private const val TAG = "VN/UserManager"
private var mAuth: FirebaseAuth? = FirebaseAuth.getInstance()
class MyUserManager {
fun deleteUser(user: FirebaseUser) {
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) }
}
}
fun logIn(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
username: String, password: String) {
fun logIn(username: String, password: String,
onComplete: () -> Unit,
onSuccess: () -> Unit,
onFailure: () -> Unit) {
mAuth?.signInWithEmailAndPassword(constructEmail(username), password)
?.addOnCompleteListener { onComplete() }
?.addOnSuccessListener {
......@@ -30,34 +32,39 @@ fun logIn(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
Log.w(TAG, "signInWithEmail:failure", it)
onFailure()
}
}
}
internal fun isEmail(string: String) = Patterns.EMAIL_ADDRESS.matcher(string).matches()
private fun isEmail(string: String) = Patterns.EMAIL_ADDRESS.matcher(string).matches()
fun signUp(activity: LogInActivity, username: String, password: String) {
fun signUp(username: String, password: String,
onComplete: () -> Unit,
onSuccess: () -> Unit,
onFailure: () -> Unit) {
mAuth?.createUserWithEmailAndPassword(constructEmail(username), password)
?.addOnCompleteListener { removeProgressBar(activity.supportFragmentManager) }
?.addOnCompleteListener { onComplete() }
?.addOnSuccessListener {
Log.d(TAG, "Successfully signed up a new user")
mAuth?.currentUser?.let { it1 -> FirestoreManager().addNewUser(it1, activity) }
shortToast(activity, activity.resources.getString(R.string.toast_successful_sign_up))
activity.login()
onSuccess()
}
?.addOnFailureListener { e ->
Log.d(TAG, "createUserWithEmail:failure", e.fillInStackTrace())
onFailure()
}
}
?.addOnFailureListener { msg ->
Log.d(TAG, "createUserWithEmail:failure", msg.fillInStackTrace())
msg.message?.let { it1 -> shortToast(activity, it1) }
} }
fun userLoggedIn(): Boolean {
fun userLoggedIn(): Boolean {
return mAuth?.currentUser != null
}
}
fun logOut() {
fun logOut() {
mAuth?.signOut()
Log.i(TAG, "User logged out")
}
}
private fun constructEmail(username: String): String {
private fun constructEmail(username: String): String {
return if (isEmail(username)) username
else username.toLowerCase() + "@wordbook.com"
}
}
}
\ No newline at end of file
......@@ -3,9 +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.logIn
import com.paktalin.vocabularynotebook.firestore.signUp
import com.paktalin.vocabularynotebook.firestore.userLoggedIn
import com.paktalin.vocabularynotebook.firestore.MyUserManager
import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.activity_log_in.*
......@@ -14,36 +12,32 @@ class LogInActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_log_in)
btnLogIn!!.setOnClickListener { login() }
btnSignUp!!.setOnClickListener { signUp() }
btnLogIn?.setOnClickListener { login() }
btnSignUp?.setOnClickListener { signUp() }
}
override fun onStart() {
super.onStart()
if (userLoggedIn()) {
startUserActivity()
}
if (MyUserManager.userLoggedIn()) startActivity(this, MainActivity::class.java)
}
fun login() {
val (username, password) = getUsernameAndPassword()
if (fieldsNotEmpty(username, password, this)) {
addProgressBar()
logIn(
{ removeProgressBar(supportFragmentManager) },
{ startUserActivity() },
{ shortToast(this@LogInActivity, getString(R.string.toast_auth_failed)) },
username, password)
private fun login() {
getValidUsernameAndPassword()?.let { (u, p) ->
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)) })
}
}
private fun signUp() {
val (username, password) = getUsernameAndPassword()
if (fieldsNotEmpty(username, password, this) && validUsername(username, this)) {
addProgressBar()
signUp(this, username, password)
getValidUsernameAndPassword()?.let { (u, p) ->
addProgressBar(supportFragmentManager, R.id.container_login)
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)) })
}
}
......@@ -52,11 +46,13 @@ class LogInActivity : AppCompatActivity() {
startActivity(this@LogInActivity, MainActivity::class.java)
}
private fun addProgressBar() {
com.paktalin.vocabularynotebook.utils.addProgressBar(supportFragmentManager, R.id.container_login)
private fun getValidUsernameAndPassword(): Pair<String, String>? {
val username = etUsername!!.text.toString()
val password = etPassword!!.text.toString()
return Pair(username, password).takeIf {
(fieldsNotEmpty(username, password, this) && validUsername(username, this))
}
}
private fun getUsernameAndPassword() = Pair(etUsername!!.text.toString(), etPassword!!.text.toString())
companion object {
private val TAG = "VN/" + LogInActivity::class.simpleName
......
......@@ -24,4 +24,5 @@
<string name="toast_invalid_username">Please, create a username using letters a–z, numbers 0–9 and characters -_.</string>
<string name="toast_select_words_to_tag">Select words to tag</string>
<string name="toast_tag_cannot_be_empty">Please, enter the tag</string>
<string name="toast_sign_up_failed">Sign up failed</string>
</resources>
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