Commit 638c1d44 by Paktalin

Moved login and signUp actions from LoginActivity to UserManager

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