Commit 5934185c by Paktalin

Refactoring in LogInActivity

parent 82cb4ebe
package com.paktalin.vocabularynotebook.entities 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 { init {
if (title == null) title = "Untitled vocabulary" title = title ?: "Untitled vocabulary"
id = id ?: UUID.randomUUID().toString()
} }
} }
...@@ -45,7 +45,7 @@ class FirestoreManager { ...@@ -45,7 +45,7 @@ class FirestoreManager {
} }
.addOnFailureListener { .addOnFailureListener {
Log.w(TAG, "Couldn't addAll user to the database", it.cause) Log.w(TAG, "Couldn't addAll user to the database", it.cause)
deleteUser(firebaseUser) MyUserManager.deleteUser(firebaseUser)
} }
} }
...@@ -69,7 +69,7 @@ class FirestoreManager { ...@@ -69,7 +69,7 @@ class FirestoreManager {
private fun setNewUserWithVocabularyData(firebaseUser: FirebaseUser, private fun setNewUserWithVocabularyData(firebaseUser: FirebaseUser,
firstVocabularyRef: DocumentReference, firstVocabularyRef: DocumentReference,
logInActivity: LogInActivity) { logInActivity: LogInActivity) {
val user = UserPojo(firebaseUser.email) 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 {
......
...@@ -3,61 +3,68 @@ package com.paktalin.vocabularynotebook.firestore ...@@ -3,61 +3,68 @@ 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.R
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import com.paktalin.vocabularynotebook.utils.Log import com.paktalin.vocabularynotebook.utils.Log
import com.paktalin.vocabularynotebook.utils.removeProgressBar
import com.paktalin.vocabularynotebook.utils.shortToast class MyUserManager {
private const val TAG = "VN/UserManager" companion object {
private var mAuth: FirebaseAuth? = FirebaseAuth.getInstance()
private const val TAG = "VN/MyUserManager"
fun deleteUser(user: FirebaseUser) { private var mAuth: FirebaseAuth? = FirebaseAuth.getInstance()
user.delete()
.addOnSuccessListener { Log.i(TAG, "UserPojo was successfully deleted") } fun deleteUser(user: FirebaseUser) {
.addOnFailureListener { Log.i(TAG, "deleteUser:failure", it.cause) } 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) {
mAuth?.signInWithEmailAndPassword(constructEmail(username), password) fun logIn(username: String, password: String,
?.addOnCompleteListener { onComplete() } onComplete: () -> Unit,
?.addOnSuccessListener { onSuccess: () -> Unit,
Log.d(TAG, "Successfully signed in") onFailure: () -> Unit) {
onSuccess() mAuth?.signInWithEmailAndPassword(constructEmail(username), password)
} ?.addOnCompleteListener { onComplete() }
?.addOnFailureListener { ?.addOnSuccessListener {
Log.w(TAG, "signInWithEmail:failure", it) Log.d(TAG, "Successfully signed in")
onFailure() onSuccess()
} }
} ?.addOnFailureListener {
Log.w(TAG, "signInWithEmail:failure", it)
internal fun isEmail(string: String) = Patterns.EMAIL_ADDRESS.matcher(string).matches() onFailure()
}
fun signUp(activity: LogInActivity, username: String, password: String) { }
mAuth?.createUserWithEmailAndPassword(constructEmail(username), password)
?.addOnCompleteListener { removeProgressBar(activity.supportFragmentManager) } private fun isEmail(string: String) = Patterns.EMAIL_ADDRESS.matcher(string).matches()
?.addOnSuccessListener {
Log.d(TAG, "Successfully signed up a new user") fun signUp(username: String, password: String,
mAuth?.currentUser?.let { it1 -> FirestoreManager().addNewUser(it1, activity) } onComplete: () -> Unit,
shortToast(activity, activity.resources.getString(R.string.toast_successful_sign_up)) onSuccess: () -> Unit,
activity.login() onFailure: () -> Unit) {
} mAuth?.createUserWithEmailAndPassword(constructEmail(username), password)
?.addOnFailureListener { msg -> ?.addOnCompleteListener { onComplete() }
Log.d(TAG, "createUserWithEmail:failure", msg.fillInStackTrace()) ?.addOnSuccessListener {
msg.message?.let { it1 -> shortToast(activity, it1) } Log.d(TAG, "Successfully signed up a new user")
} } mAuth?.currentUser?.let { it1 -> FirestoreManager().addNewUser(it1, activity) }
onSuccess()
fun userLoggedIn(): Boolean { }
return mAuth?.currentUser != null ?.addOnFailureListener { e ->
} Log.d(TAG, "createUserWithEmail:failure", e.fillInStackTrace())
onFailure()
fun logOut() { }
mAuth?.signOut() }
Log.i(TAG, "User logged out")
} fun userLoggedIn(): Boolean {
return mAuth?.currentUser != null
private fun constructEmail(username: String): String { }
return if (isEmail(username)) username
else username.toLowerCase() + "@wordbook.com" fun logOut() {
mAuth?.signOut()
Log.i(TAG, "User logged out")
}
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 ...@@ -3,9 +3,7 @@ package com.paktalin.vocabularynotebook.ui.activities
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestore.logIn import com.paktalin.vocabularynotebook.firestore.MyUserManager
import com.paktalin.vocabularynotebook.firestore.signUp
import com.paktalin.vocabularynotebook.firestore.userLoggedIn
import com.paktalin.vocabularynotebook.utils.* import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.activity_log_in.* import kotlinx.android.synthetic.main.activity_log_in.*
...@@ -14,36 +12,32 @@ class LogInActivity : AppCompatActivity() { ...@@ -14,36 +12,32 @@ class LogInActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_log_in) setContentView(R.layout.activity_log_in)
btnLogIn!!.setOnClickListener { login() } btnLogIn?.setOnClickListener { login() }
btnSignUp!!.setOnClickListener { signUp() } btnSignUp?.setOnClickListener { signUp() }
} }
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
if (userLoggedIn()) { if (MyUserManager.userLoggedIn()) startActivity(this, MainActivity::class.java)
startUserActivity()
}
} }
fun login() { private fun login() {
val (username, password) = getUsernameAndPassword() getValidUsernameAndPassword()?.let { (u, p) ->
if (fieldsNotEmpty(username, password, this)) { addProgressBar(supportFragmentManager, R.id.container_login)
addProgressBar() MyUserManager.logIn(u, p,
logIn( onComplete = { removeProgressBar(supportFragmentManager) },
{ removeProgressBar(supportFragmentManager) }, onSuccess = { startActivity(this, MainActivity::class.java) },
{ startUserActivity() }, onFailure = { shortToast(this, getString(R.string.toast_auth_failed)) })
{ shortToast(this@LogInActivity, getString(R.string.toast_auth_failed)) },
username, password)
} }
} }
private fun signUp() { private fun signUp() {
val (username, password) = getUsernameAndPassword() getValidUsernameAndPassword()?.let { (u, p) ->
if (fieldsNotEmpty(username, password, this) && validUsername(username, this)) { addProgressBar(supportFragmentManager, R.id.container_login)
addProgressBar() MyUserManager.signUp(u, p,
onComplete = { removeProgressBar(supportFragmentManager) },
onSuccess = { shortToast(this, resources.getString(R.string.toast_successful_sign_up)); login() },
signUp(this, username, password) onFailure = { shortToast(this, resources.getString(R.string.toast_sign_up_failed)) })
} }
} }
...@@ -52,12 +46,14 @@ class LogInActivity : AppCompatActivity() { ...@@ -52,12 +46,14 @@ class LogInActivity : AppCompatActivity() {
startActivity(this@LogInActivity, MainActivity::class.java) startActivity(this@LogInActivity, MainActivity::class.java)
} }
private fun addProgressBar() { private fun getValidUsernameAndPassword(): Pair<String, String>? {
com.paktalin.vocabularynotebook.utils.addProgressBar(supportFragmentManager, R.id.container_login) 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 { companion object {
private val TAG = "VN/" + LogInActivity::class.simpleName private val TAG = "VN/" + LogInActivity::class.simpleName
} }
......
...@@ -24,4 +24,5 @@ ...@@ -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_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_select_words_to_tag">Select words to tag</string>
<string name="toast_tag_cannot_be_empty">Please, enter the 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> </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