Commit 82cb4ebe by Paktalin

Naming refactoring

parent 2027d343
......@@ -3,7 +3,7 @@ package com.paktalin.vocabularynotebook.entities
import com.google.firebase.firestore.DocumentReference
class UserPojo(var email: String?) {
class UserPojo(var email: String?, id: String?) {
var name: String? = null
var vocabularies: List<DocumentReference>? = null
}
package com.paktalin.vocabularynotebook.entities
class VocabularyPojo(var title:String?) {
class VocabularyPojo(var title: String?, val id: String?) {
init {
if (title == null) title = "Untitled vocabulary"
}
......
......@@ -45,7 +45,7 @@ class FirestoreManager {
}
.addOnFailureListener {
Log.w(TAG, "Couldn't addAll user to the database", it.cause)
mDeleteUser(firebaseUser)
deleteUser(firebaseUser)
}
}
......
package com.paktalin.vocabularynotebook.firestore
import com.paktalin.vocabularynotebook.entities.UserPojo
import com.paktalin.vocabularynotebook.entities.VocabularyPojo
import com.paktalin.vocabularynotebook.entities.WordPojo
class Model {
lateinit var user: UserPojo
lateinit var vocabularyPojo: VocabularyPojo
lateinit var words: MutableList<WordPojo>
}
\ No newline at end of file
......@@ -12,58 +12,52 @@ import com.paktalin.vocabularynotebook.utils.shortToast
private const val TAG = "VN/UserManager"
private var mAuth: FirebaseAuth? = FirebaseAuth.getInstance()
fun mDeleteUser(user: FirebaseUser) {
fun deleteUser(user: FirebaseUser) {
user.delete()
.addOnSuccessListener { Log.i(TAG, "UserPojo was successfully deleted") }
.addOnFailureListener { Log.i(TAG, "mDeleteUser:failure", it.cause) }
.addOnFailureListener { Log.i(TAG, "deleteUser:failure", it.cause) }
}
fun mLogIn(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
username: String, password: String) {
mAuth!!.signInWithEmailAndPassword(email(username), password)
.addOnCompleteListener { onComplete() }
.addOnSuccessListener {
fun logIn(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
username: String, password: String) {
mAuth?.signInWithEmailAndPassword(constructEmail(username), password)
?.addOnCompleteListener { onComplete() }
?.addOnSuccessListener {
Log.d(TAG, "Successfully signed in")
onSuccess()
}
.addOnFailureListener {
?.addOnFailureListener {
Log.w(TAG, "signInWithEmail:failure", it)
onFailure()
}
}
internal 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) }
.addOnSuccessListener {
fun signUp(activity: LogInActivity, username: String, password: String) {
mAuth?.createUserWithEmailAndPassword(constructEmail(username), password)
?.addOnCompleteListener { removeProgressBar(activity.supportFragmentManager) }
?.addOnSuccessListener {
Log.d(TAG, "Successfully signed up a new user")
FirestoreManager().addNewUser(mAuth!!.currentUser!!, activity)
mAuth?.currentUser?.let { it1 -> FirestoreManager().addNewUser(it1, activity) }
shortToast(activity, activity.resources.getString(R.string.toast_successful_sign_up))
activity.login()
}
.addOnFailureListener {
Log.d(TAG, "createUserWithEmail:failure", it.fillInStackTrace())
shortToast(activity, it.message!!)
?.addOnFailureListener { msg ->
Log.d(TAG, "createUserWithEmail:failure", msg.fillInStackTrace())
msg.message?.let { it1 -> shortToast(activity, it1) }
} }
fun mUserLoggedIn(): Boolean {
return mAuth!!.currentUser != null
fun userLoggedIn(): Boolean {
return mAuth?.currentUser != null
}
fun mLogOut() {
mAuth!!.signOut()
fun logOut() {
mAuth?.signOut()
Log.i(TAG, "User logged out")
}
private fun email(username: String): String {
return if (isEmail(username))
username
else {
val dummyDomain = "@wordbook.com"
username.toLowerCase() + dummyDomain
}
private fun constructEmail(username: String): String {
return if (isEmail(username)) username
else username.toLowerCase() + "@wordbook.com"
}
\ No newline at end of file
......@@ -3,9 +3,9 @@ package com.paktalin.vocabularynotebook.ui.activities
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestore.mLogIn
import com.paktalin.vocabularynotebook.firestore.mSignUp
import com.paktalin.vocabularynotebook.firestore.mUserLoggedIn
import com.paktalin.vocabularynotebook.firestore.logIn
import com.paktalin.vocabularynotebook.firestore.signUp
import com.paktalin.vocabularynotebook.firestore.userLoggedIn
import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.activity_log_in.*
......@@ -20,7 +20,7 @@ class LogInActivity : AppCompatActivity() {
override fun onStart() {
super.onStart()
if (mUserLoggedIn()) {
if (userLoggedIn()) {
startUserActivity()
}
}
......@@ -29,7 +29,7 @@ class LogInActivity : AppCompatActivity() {
val (username, password) = getUsernameAndPassword()
if (fieldsNotEmpty(username, password, this)) {
addProgressBar()
mLogIn(
logIn(
{ removeProgressBar(supportFragmentManager) },
{ startUserActivity() },
{ shortToast(this@LogInActivity, getString(R.string.toast_auth_failed)) },
......@@ -41,7 +41,9 @@ class LogInActivity : AppCompatActivity() {
val (username, password) = getUsernameAndPassword()
if (fieldsNotEmpty(username, password, this) && validUsername(username, this)) {
addProgressBar()
mSignUp(this, username, password)
signUp(this, username, password)
}
}
......
......@@ -12,7 +12,6 @@ import androidx.appcompat.widget.SearchView
import androidx.recyclerview.selection.Selection
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestore.FirestoreManager
import com.paktalin.vocabularynotebook.firestore.mLogOut
import com.paktalin.vocabularynotebook.ui.fragments.SubmitAddedFragment
import com.paktalin.vocabularynotebook.ui.fragments.TagFragment
import com.paktalin.vocabularynotebook.ui.recycler_view.State
......@@ -63,7 +62,7 @@ class MainActivity : AppCompatActivity() {
}
private fun logOut() {
mLogOut()
com.paktalin.vocabularynotebook.firestore.logOut()
startActivity(this@MainActivity, LogInActivity::class.java)
}
......
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