Commit c370f256 by Paktalin

Refactoring

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