Commit 638c1d44 by Paktalin

Moved login and signUp actions from LoginActivity to UserManager

parent 472ba8eb
......@@ -25,7 +25,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
package com.paktalin.vocabularynotebook
import android.util.Log
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
......@@ -9,17 +10,15 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import java.util.*
class UserManager {
companion object {
private val TAG = "VN/" + UserManager::class.simpleName
private const val TAG = "VN/UserManager"
private fun deleteUser(user: FirebaseUser) {
private fun deleteUser(user: FirebaseUser) {
user.delete()
.addOnSuccessListener { Log.i(TAG, "UserPojo was successfully deleted") }
.addOnFailureListener { Log.i(TAG, "deleteUser:failure", it.cause)}
}
}
fun addNewUserToDb(newUser: FirebaseUser, logInActivity: LogInActivity) {
fun addNewUserToDb(newUser: FirebaseUser, logInActivity: LogInActivity) {
//todo add condition to writing to the db in Firebase Console (request.auth.uid)
val db = ConfiguredFirestore.instance
val user = UserPojo(newUser.email)
......@@ -30,18 +29,43 @@ class UserManager {
user.vocabularies = Collections.singletonList(firstVocabularyRef)
db.collection("users").document(newUser.uid).set(user)
.addOnCompleteListener({ task ->
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.i(TAG, "Successfully added user to the collection")
logInActivity.startUserActivity()
} else Log.w(TAG, "addUser:failure", task.exception)
}
else Log.w(TAG, "addUser:failure", task.exception)
})
}
.addOnFailureListener {
Log.w(TAG, "Couldn't add user to the database", it.cause)
UserManager.deleteUser(newUser)
deleteUser(newUser)
}
}
fun login(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
mAuth: FirebaseAuth?, email: String, password: String) {
mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { onComplete() }
.addOnSuccessListener {
Log.d(TAG, "Successfully signed in")
onSuccess()
}
.addOnFailureListener {
Log.w(TAG, "signInWithEmail:failure", it)
onFailure()
}
}
fun signUp(mAuth: FirebaseAuth?, activity: LogInActivity, email: String, password: String) {
mAuth!!.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { removeProgressBar(activity.supportFragmentManager) }
.addOnSuccessListener {
Log.d(TAG, "Successfully signed up a new user")
addNewUserToDb(mAuth.currentUser!!, activity)
activity.login()
}
.addOnFailureListener {
Log.d(TAG, "createUserWithEmail:failure", it.fillInStackTrace())
shortToast(activity, it.message!!)
}
}
......@@ -12,15 +12,17 @@ import kotlinx.android.synthetic.main.activity_log_in.*
class LogInActivity : AppCompatActivity() {
private var mAuth: FirebaseAuth? = null
private var email: String? = null
private var password: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_log_in)
mAuth = FirebaseAuth.getInstance()
btnLogIn!!.setOnClickListener({ logIn() })
btnSignUp!!.setOnClickListener({ signUp() })
btnRandomUser!!.setOnClickListener({ createRandomUser() })
btnLogIn!!.setOnClickListener { login() }
btnSignUp!!.setOnClickListener { signUp() }
btnRandomUser!!.setOnClickListener { createRandomUser() }
}
override fun onStart() {
......@@ -28,43 +30,22 @@ class LogInActivity : AppCompatActivity() {
if (mAuth!!.currentUser != null) { startUserActivity() }
}
private fun logIn() {
val email = etEmail!!.text.toString()
val password = etPassword!!.text.toString()
if (fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
showProgressBar()
mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { hideProgressBar() }
.addOnSuccessListener {
Log.d(TAG, "Successfully signed in")
startUserActivity()
}
.addOnFailureListener {
Log.w(TAG, "signInWithEmail:failure", it)
shortToast(this@LogInActivity, getString(R.string.toast_auth_failed))
}
}
}
fun login() {
processUser { login(
{ removeProgressBar() },
{ startUserActivity() },
{ showToastFailure() },
mAuth, email!!, password!!) } }
private fun signUp() {
val email = etEmail!!.text.toString()
val password = etPassword!!.text.toString()
if (fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
//todo check if the password is good
// todo verify email
showProgressBar()
mAuth!!.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { hideProgressBar() }
.addOnSuccessListener { _ ->
Log.d(TAG, "Successfully signed up a new user")
UserManager.addNewUserToDb(mAuth!!.currentUser!!, this)
}
.addOnFailureListener {
Log.d(TAG, "createUserWithEmail:failure", it.fillInStackTrace())
shortToast(this@LogInActivity, it.message!!)
}
private fun signUp() { processUser { signUp(mAuth, this, email!!, password!!) } }
private fun processUser(authAction: () -> Unit) {
email = etEmail!!.text.toString()
password = etPassword!!.text.toString()
if (fieldsNotEmpty(email!!, password!!, "Please, enter email and password", this)) {
addProgressBar()
authAction()
}
}
......@@ -74,15 +55,19 @@ class LogInActivity : AppCompatActivity() {
startActivity(userActivityIntent)
}
private fun showProgressBar() { addProgressBar(supportFragmentManager, R.id.container_login) }
private fun addProgressBar() { addProgressBar(supportFragmentManager, R.id.container_login) }
private fun hideProgressBar() { removeProgressBar(supportFragmentManager) }
private fun removeProgressBar() { removeProgressBar(supportFragmentManager) }
private fun showToastFailure() {
shortToast(this@LogInActivity, getString(R.string.toast_auth_failed))
}
@SuppressLint("SetTextI18n")
private fun createRandomUser() {
etEmail.setText("random@gmail.com")
etPassword.setText("123456")
signUp()
processUser { signUp(mAuth, this@LogInActivity, email!!, password!!) }
}
companion object { private val TAG = "VN/" + LogInActivity::class.simpleName }
......
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