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.*
import kotlinx.android.synthetic.main.activity_log_in.*
class LogInActivity : AppCompatActivity() {
private var email: String? = null
private var password: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -24,25 +22,21 @@ class LogInActivity : AppCompatActivity() {
}
fun login() {
processUser(resources.getString(R.string.toast_incorrect_username_password)) {
val (username, password) = getUsernameAndPassword()
if (fieldsNotEmpty(username, password, this))
addProgressBar()
mLogIn(
{ removeProgressBar() },
{ removeProgressBar(supportFragmentManager) },
{ startUserActivity() },
{ showToastFailure() },
email!!, password!!)
}
}
private fun signUp() { processUser(resources.getString(R.string.toast_username_characters)) { mSignUp(this, email!!, password!!) }
{ shortToast(this@LogInActivity, getString(R.string.toast_auth_failed)) },
username, password)
}
private fun processUser(invalidUsername:String, authAction: () -> Unit) {
email = etEmail!!.text.toString()
password = etPassword!!.text.toString()
if (validInput(email!!, password!!, this, invalidUsername)) {
private fun signUp() {
val (username, password) = getUsernameAndPassword()
if (fieldsNotEmpty(username, password, this) && validUsername(username, this)) {
addProgressBar()
authAction()
mSignUp(this, username, password)
}
}
......@@ -55,11 +49,7 @@ class LogInActivity : AppCompatActivity() {
com.paktalin.vocabularynotebook.utils.addProgressBar(supportFragmentManager, R.id.container_login)
}
private fun removeProgressBar() {
com.paktalin.vocabularynotebook.utils.removeProgressBar(supportFragmentManager)
}
private fun showToastFailure() { shortToast(this@LogInActivity, getString(R.string.toast_auth_failed)) }
private fun getUsernameAndPassword() = Pair(etEmail!!.text.toString(), etPassword!!.text.toString())
companion object { private val TAG = "VN/" + LogInActivity::class.simpleName }
}
\ No newline at end of file
......@@ -5,12 +5,11 @@ import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.ui.fragments.ProgressFragment
import org.apache.commons.lang3.StringUtils.containsOnly
import org.apache.commons.lang3.StringUtils
val progressFragment: Fragment = ProgressFragment()
......@@ -31,20 +30,21 @@ fun removeProgressBar(fragmentManager: FragmentManager) {
removeFragment(fragmentManager, progressFragment)
}
fun validInput(username: String, password: String, context: Context, invalidUsername:String): Boolean {
if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
fun fieldsNotEmpty(username: String, password: String, context: Context): Boolean {
if (username.isEmpty() || password.isEmpty()) {
shortToast(context, context.resources.getString(R.string.toast_enter_username_password))
return false
}
else if (!appropriateCharacters(username)) {
shortToast(context, invalidUsername)
return false
}
return true
}
private fun appropriateCharacters(username: String): Boolean {
return containsOnly(username.toLowerCase(), "abcdefghijklmnopqrstuvwxyz0123456789-_.")
fun validUsername(username: String, context: Context): Boolean {
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) {
......
package com.paktalin.vocabularynotebook.utils
import android.util.Patterns
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.paktalin.vocabularynotebook.R
......@@ -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) {
mAuth!!.createUserWithEmailAndPassword(email(username), password)
.addOnCompleteListener { removeProgressBar(activity.supportFragmentManager) }
......@@ -52,6 +57,10 @@ fun mLogOut() {
}
private fun email(username: String): String {
return if (isEmail(username))
username
else {
val dummyDomain = "@wordbook.com"
return username.toLowerCase() + dummyDomain
username.toLowerCase() + dummyDomain
}
}
\ No newline at end of file
......@@ -24,6 +24,6 @@
<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_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>
</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