Commit 82cb4ebe by Paktalin

Naming refactoring

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