Commit 7bd71fc9 by Paktalin

Now it's possible to sign up with either a username or an email

parent d0c5f749
...@@ -8,8 +8,6 @@ import com.paktalin.vocabularynotebook.utils.* ...@@ -8,8 +8,6 @@ import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.activity_log_in.* import kotlinx.android.synthetic.main.activity_log_in.*
class LogInActivity : AppCompatActivity() { class LogInActivity : AppCompatActivity() {
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)
...@@ -24,25 +22,21 @@ class LogInActivity : AppCompatActivity() { ...@@ -24,25 +22,21 @@ class LogInActivity : AppCompatActivity() {
} }
fun login() { fun login() {
processUser(resources.getString(R.string.toast_incorrect_username_password)) { val (username, password) = getUsernameAndPassword()
if (fieldsNotEmpty(username, password, this))
addProgressBar()
mLogIn( mLogIn(
{ removeProgressBar() }, { removeProgressBar(supportFragmentManager) },
{ startUserActivity() }, { startUserActivity() },
{ showToastFailure() }, { shortToast(this@LogInActivity, getString(R.string.toast_auth_failed)) },
email!!, password!!) username, password)
}
}
private fun signUp() { processUser(resources.getString(R.string.toast_username_characters)) { mSignUp(this, email!!, password!!) }
} }
private fun processUser(invalidUsername:String, authAction: () -> Unit) { private fun signUp() {
email = etEmail!!.text.toString() val (username, password) = getUsernameAndPassword()
password = etPassword!!.text.toString() if (fieldsNotEmpty(username, password, this) && validUsername(username, this)) {
if (validInput(email!!, password!!, this, invalidUsername)) {
addProgressBar() addProgressBar()
authAction() mSignUp(this, username, password)
} }
} }
...@@ -55,11 +49,7 @@ class LogInActivity : AppCompatActivity() { ...@@ -55,11 +49,7 @@ class LogInActivity : AppCompatActivity() {
com.paktalin.vocabularynotebook.utils.addProgressBar(supportFragmentManager, R.id.container_login) com.paktalin.vocabularynotebook.utils.addProgressBar(supportFragmentManager, R.id.container_login)
} }
private fun removeProgressBar() { private fun getUsernameAndPassword() = Pair(etEmail!!.text.toString(), etPassword!!.text.toString())
com.paktalin.vocabularynotebook.utils.removeProgressBar(supportFragmentManager)
}
private fun showToastFailure() { shortToast(this@LogInActivity, getString(R.string.toast_auth_failed)) }
companion object { private val TAG = "VN/" + LogInActivity::class.simpleName } companion object { private val TAG = "VN/" + LogInActivity::class.simpleName }
} }
\ No newline at end of file
...@@ -5,12 +5,11 @@ import android.content.Intent ...@@ -5,12 +5,11 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentManager
import android.text.TextUtils
import android.view.View import android.view.View
import android.widget.Toast import android.widget.Toast
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.ui.fragments.ProgressFragment import com.paktalin.vocabularynotebook.ui.fragments.ProgressFragment
import org.apache.commons.lang3.StringUtils.containsOnly import org.apache.commons.lang3.StringUtils
val progressFragment: Fragment = ProgressFragment() val progressFragment: Fragment = ProgressFragment()
...@@ -31,20 +30,21 @@ fun removeProgressBar(fragmentManager: FragmentManager) { ...@@ -31,20 +30,21 @@ fun removeProgressBar(fragmentManager: FragmentManager) {
removeFragment(fragmentManager, progressFragment) removeFragment(fragmentManager, progressFragment)
} }
fun validInput(username: String, password: String, context: Context, invalidUsername:String): Boolean { fun fieldsNotEmpty(username: String, password: String, context: Context): Boolean {
if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) { if (username.isEmpty() || password.isEmpty()) {
shortToast(context, context.resources.getString(R.string.toast_enter_username_password)) shortToast(context, context.resources.getString(R.string.toast_enter_username_password))
return false return false
} }
else if (!appropriateCharacters(username)) {
shortToast(context, invalidUsername)
return false
}
return true return true
} }
private fun appropriateCharacters(username: String): Boolean { fun validUsername(username: String, context: Context): Boolean {
return containsOnly(username.toLowerCase(), "abcdefghijklmnopqrstuvwxyz0123456789-_.") return if (StringUtils.containsOnly(username.toLowerCase(), "abcdefghijklmnopqrstuvwxyz0123456789-_."))
true
else {
shortToast(context, context.resources.getString(R.string.toast_invalid_username))
false
}
} }
fun shortToast(context: Context, text: String) { fun shortToast(context: Context, text: String) {
......
package com.paktalin.vocabularynotebook.utils package com.paktalin.vocabularynotebook.utils
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.R
...@@ -28,6 +29,10 @@ fun mLogIn(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit, ...@@ -28,6 +29,10 @@ fun mLogIn(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
} }
} }
private fun isEmail(string: String) = Patterns.EMAIL_ADDRESS.matcher(string).matches()
fun mSignUp(activity: LogInActivity, username: String, password: String) { fun mSignUp(activity: LogInActivity, username: String, password: String) {
mAuth!!.createUserWithEmailAndPassword(email(username), password) mAuth!!.createUserWithEmailAndPassword(email(username), password)
.addOnCompleteListener { removeProgressBar(activity.supportFragmentManager) } .addOnCompleteListener { removeProgressBar(activity.supportFragmentManager) }
...@@ -52,6 +57,10 @@ fun mLogOut() { ...@@ -52,6 +57,10 @@ fun mLogOut() {
} }
private fun email(username: String): String { private fun email(username: String): String {
return if (isEmail(username))
username
else {
val dummyDomain = "@wordbook.com" val dummyDomain = "@wordbook.com"
return username.toLowerCase() + dummyDomain username.toLowerCase() + dummyDomain
}
} }
\ No newline at end of file
...@@ -24,6 +24,6 @@ ...@@ -24,6 +24,6 @@
<string name="toast_update_word_failed">Couldn\'t update the word</string> <string name="toast_update_word_failed">Couldn\'t update the word</string>
<string name="toast_successful_sign_up">Congratulations! You can now access your vocabulary from any device</string> <string name="toast_successful_sign_up">Congratulations! You can now access your vocabulary from any device</string>
<string name="toast_enter_username_password">Please, enter username and password</string> <string name="toast_enter_username_password">Please, enter username and password</string>
<string name="toast_username_characters">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_incorrect_username_password">Username or password is incorrect</string> <string name="toast_incorrect_username_password">Username or password is incorrect</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