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,39 +10,62 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary ...@@ -9,39 +10,62 @@ 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) {
user.delete()
private fun deleteUser(user: FirebaseUser) { .addOnSuccessListener { Log.i(TAG, "UserPojo was successfully deleted") }
user.delete() .addOnFailureListener { Log.i(TAG, "deleteUser:failure", it.cause)}
.addOnSuccessListener { Log.i(TAG, "UserPojo was successfully deleted") } }
.addOnFailureListener { Log.i(TAG, "deleteUser:failure", it.cause)}
} fun addNewUserToDb(newUser: FirebaseUser, logInActivity: LogInActivity) {
//todo add condition to writing to the db in Firebase Console (request.auth.uid)
fun addNewUserToDb(newUser: FirebaseUser, logInActivity: LogInActivity) { val db = ConfiguredFirestore.instance
//todo add condition to writing to the db in Firebase Console (request.auth.uid) val user = UserPojo(newUser.email)
val db = ConfiguredFirestore.instance
val user = UserPojo(newUser.email) db.collection(VOCABULARIES).add(Vocabulary.Pojo(null))
.addOnSuccessListener { firstVocabularyRef ->
db.collection(VOCABULARIES).add(Vocabulary.Pojo(null)) Log.d(TAG, "VocabularyPojo successfully created: " + firstVocabularyRef.path)
.addOnSuccessListener { firstVocabularyRef -> user.vocabularies = Collections.singletonList(firstVocabularyRef)
Log.d(TAG, "VocabularyPojo successfully created: " + firstVocabularyRef.path)
user.vocabularies = Collections.singletonList(firstVocabularyRef) db.collection("users").document(newUser.uid).set(user)
.addOnCompleteListener { task ->
db.collection("users").document(newUser.uid).set(user) if (task.isSuccessful) {
.addOnCompleteListener({ task -> Log.i(TAG, "Successfully added user to the collection")
if (task.isSuccessful) { logInActivity.startUserActivity()
Log.i(TAG, "Successfully added user to the collection") } else Log.w(TAG, "addUser:failure", task.exception)
logInActivity.startUserActivity() }
} }
else Log.w(TAG, "addUser:failure", task.exception) .addOnFailureListener {
}) Log.w(TAG, "Couldn't add user to the database", it.cause)
} deleteUser(newUser)
.addOnFailureListener { }
Log.w(TAG, "Couldn't add user to the database", it.cause) }
UserManager.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() } private fun signUp() { processUser { signUp(mAuth, this, email!!, password!!) } }
.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 processUser(authAction: () -> Unit) {
val email = etEmail!!.text.toString() email = etEmail!!.text.toString()
val password = etPassword!!.text.toString() password = etPassword!!.text.toString()
if (fieldsNotEmpty(email, password, "Please, enter email and password", this)) { if (fieldsNotEmpty(email!!, password!!, "Please, enter email and password", this)) {
//todo check if the password is good addProgressBar()
// todo verify email authAction()
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!!)
}
} }
} }
...@@ -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 removeProgressBar() { removeProgressBar(supportFragmentManager) }
private fun hideProgressBar() { 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