Commit c370f256 by Paktalin

Refactoring

parent eb0e29a6
package com.paktalin.vocabularynotebook.firestore
import android.content.Context
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.firestore.*
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
......@@ -22,8 +21,7 @@ class FirestoreManager {
fun extractVocabularyId(onSuccess: () -> Unit,
showToastNoWords: () -> Unit, removeProgressBar: () -> Unit, mainActivity: Context) {
val userId = FirebaseAuth.getInstance()!!.currentUser!!.uid
userDocument(userId)?.get()
userDocument(Model.userId)?.get()
?.addOnSuccessListener { snapshot ->
removeProgressBar()
if (snapshot.get(VOCABULARIES) != null) {
......
......@@ -9,5 +9,9 @@ class Model {
lateinit var vocabularyPojo: VocabularyPojo
lateinit var words: MutableList<WordPojo>
companion object {
var userId: String? = null
}
}
\ No newline at end of file
......@@ -24,10 +24,11 @@ class MyUserManager {
Log.d(TAG, "Successfully signed in")
onSuccess()
}
?.addOnFailureListener {
Log.w(TAG, "signInWithEmail:failure", it)
?.addOnFailureListener { e ->
Log.w(TAG, "signInWithEmail:failure", e)
onFailure()
}
Model.userId = this.user?.uid
}
fun signUp(username: String, password: String,
......@@ -46,6 +47,7 @@ class MyUserManager {
Log.d(TAG, "createUserWithEmail:failure", e.fillInStackTrace())
onFailure()
}
Model.userId = this.user?.uid
}
fun deleteUser(user: FirebaseUser?) {
......
......@@ -36,7 +36,10 @@ class LogInActivity : AppCompatActivity() {
addProgressBar(supportFragmentManager, R.id.container_login)
MyUserManager.signUp(u, p,
onComplete = { removeProgressBar(supportFragmentManager) },
onSuccess = { shortToast(this, resources.getString(R.string.toast_successful_sign_up)); login() },
onSuccess = {
shortToast(this, resources.getString(R.string.toast_successful_sign_up))
startActivity(this, MainActivity::class.java)
},
onFailure = { shortToast(this, resources.getString(R.string.toast_sign_up_failed)) },
logInActivity = this)
}
......
......@@ -86,8 +86,9 @@ class MainActivity : AppCompatActivity() {
private fun setUpVocabularyAdapter() {
addProgressBar(supportFragmentManager, R.id.container_main)
FirestoreManager().extractVocabularyId({
FirestoreManager().extractVocabulary { querySnapshot ->
FirestoreManager().extractVocabularyId(
onSuccess = {
FirestoreManager().extractVocabulary { querySnapshot ->
run {
if (querySnapshot != null && !querySnapshot.isEmpty) {
vocabularyAdapter = VocabularyAdapter(VocabSet.createFromSnapshot(querySnapshot), this@MainActivity)
......@@ -97,10 +98,12 @@ class MainActivity : AppCompatActivity() {
}
}
},
{ shortToast(this, getString(R.string.toast_empty_vocabulary)) },
{ removeProgressBar(supportFragmentManager) }, this)
showToastNoWords = { shortToast(this, getString(R.string.toast_empty_vocabulary)) },
removeProgressBar = { removeProgressBar(supportFragmentManager) },
mainActivity = this)
}
private fun setUpAddWordTouchListeners() {
word.setOnTouchListener { _, _ ->
addSubmitFragment(supportFragmentManager, SubmitAddedFragment(), R.id.main_activity_container)
......
......@@ -3,6 +3,7 @@ package com.paktalin.vocabularynotebook.utils
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Patterns
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
......@@ -10,7 +11,6 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestore.isEmail
import com.paktalin.vocabularynotebook.ui.fragments.ProgressFragment
import com.paktalin.vocabularynotebook.ui.fragments.SubmitFragment
import org.apache.commons.lang3.StringUtils
......@@ -49,7 +49,7 @@ fun fieldsNotEmpty(username: String, password: String, context: Context): Boolea
}
fun validUsername(username: String, context: Context): Boolean {
return if (isEmail(username) ||
return if (Patterns.EMAIL_ADDRESS.matcher(username).matches() ||
StringUtils.containsOnly(username.toLowerCase(), "abcdefghijklmnopqrstuvwxyz0123456789-_."))
true
else {
......
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