Commit 953f2eac by Paktalin

Vocabulary is not created when creating a user account

parent e5deb43a
package com.paktalin.vocabularynotebook.entities
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.auth.FirebaseUser
class UserPojo(var email: String?, id: String?) {
class UserPojo(var email: String?, var id: String?) {
var name: String? = null
var vocabularies: List<DocumentReference>? = null
var vocabularies: List<String>? = null
constructor(firebaseUser: FirebaseUser?) : this(firebaseUser?.email, firebaseUser?.uid)
}
package com.paktalin.vocabularynotebook.firestore
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.QuerySnapshot
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.entities.UserPojo
import com.paktalin.vocabularynotebook.entities.VocabularyPojo
import com.paktalin.vocabularynotebook.entities.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.Log
import com.paktalin.vocabularynotebook.utils.startActivity
import com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary.Label
import java.util.*
class FirestoreManager {
private val db: FirebaseFirestore = ConfiguredFirestore.instance
private val wordsCollection = db.collection(VOCABULARIES).document(vocabularyId!!).collection(WORDS)
fun extractVocabularyId(onSuccess: (String?) -> Unit) {
Log.d(TAG, "userId: $userId")
userDocument(userId)?.get()?.addOnSuccessListener { user ->
onSuccess((user.get(VOCABULARIES) as List<DocumentReference>?)?.get(0)?.id)
MyUserManager.getUserId()?.let { id ->
db.collection(USERS).document(id).get()
.addOnSuccessListener { user ->
onSuccess((user.get(VOCABULARIES) as List<DocumentReference>?)?.get(0)?.id)
}
}
}
......@@ -38,46 +33,35 @@ class FirestoreManager {
}
}
fun addNewUser(firebaseUser: FirebaseUser?, logInActivity: LogInActivity) {
fun addNewUser(userPojo: UserPojo, onSuccess: () -> Unit) {
//todo addAll condition to writing to the db in Firebase Console (request.auth.uid)
db.collection(VOCABULARIES).add(VocabularyPojo(null))
.addOnSuccessListener { firstVocabularyRef ->
Log.d(TAG, "VocabularyPojo successfully created: " + firstVocabularyRef.path)
setNewUserWithVocabularyData(firebaseUser, firstVocabularyRef, logInActivity)
}
.addOnFailureListener {
Log.w(TAG, "Couldn't addAll user to the database", it.cause)
MyUserManager.deleteUser(firebaseUser)
}
userPojo.id?.let { id ->
db.collection(USERS).document(id)
.set(userPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully added user to the collection")
onSuccess()
}
.addOnFailureListener { e ->
Log.w(TAG, "addUser:failure", e)
MyUserManager.deleteCurrentUser()
}
}
}
fun saveWords(wordMap: MutableMap<Label, MutableList<WordPojo>>) {
// TODO add logic for creating vocabulary if empty
if (wordMap.isEmpty()) return
val batch = db.batch()
wordMap[Label.DELETED]?.forEach { w -> batch.delete(wordsCollection.document(w.id!!)) }
wordMap[Label.EDITED]?.forEach { w -> batch.set(wordsCollection.document(w.id!!), w) }
wordMap[Label.DELETED]?.clear()
wordMap[Label.EDITED]?.clear()
batch.commit()
}
private fun setNewUserWithVocabularyData(firebaseUser: FirebaseUser?,
firstVocabularyRef: DocumentReference,
logInActivity: LogInActivity) {
val user = UserPojo(firebaseUser?.email, firebaseUser?.uid)
user.vocabularies = Collections.singletonList(firstVocabularyRef)
userDocument(firebaseUser?.uid)?.set(user)
?.addOnSuccessListener {
Log.i(TAG, "Successfully added user to the collection")
startActivity(logInActivity, MainActivity::class.java)
}
?.addOnFailureListener { exception ->
Log.w(TAG, "addUser:failure", exception)
}
}
vocabularyId?.let { vocabId ->
val wordsCollection = db.collection(VOCABULARIES).document(vocabId).collection(WORDS)
wordMap[Label.DELETED]?.forEach { w -> batch.delete(wordsCollection.document(w.id!!)) }
wordMap[Label.EDITED]?.forEach { w -> batch.set(wordsCollection.document(w.id!!), w) }
wordMap[Label.DELETED]?.clear()
wordMap[Label.EDITED]?.clear()
batch.commit()
private fun userDocument(userId: String?): DocumentReference? {
return userId?.let { db.collection(USERS).document(it) }
}
}
companion object {
......@@ -87,6 +71,5 @@ class FirestoreManager {
private const val TAG = "VN/FirestoreManager"
var vocabularyId: String? = null
var userId: String? = null
}
}
\ No newline at end of file
......@@ -2,8 +2,7 @@ package com.paktalin.vocabularynotebook.firestore
import android.util.Patterns
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import com.paktalin.vocabularynotebook.entities.UserPojo
import com.paktalin.vocabularynotebook.utils.Log
class MyUserManager {
......@@ -21,7 +20,6 @@ class MyUserManager {
?.addOnCompleteListener { onComplete() }
?.addOnSuccessListener {
Log.d(TAG, "Successfully signed in")
FirestoreManager.userId = auth?.currentUser?.uid
onSuccess()
}
?.addOnFailureListener { e ->
......@@ -33,15 +31,12 @@ class MyUserManager {
fun signUp(username: String, password: String,
onComplete: () -> Unit,
onSuccess: () -> Unit,
onFailure: () -> Unit,
logInActivity: LogInActivity) {
onFailure: () -> Unit) {
auth?.createUserWithEmailAndPassword(constructEmail(username), password)
?.addOnCompleteListener { onComplete() }
?.addOnSuccessListener {
Log.d(TAG, "Successfully signed up a new user")
FirestoreManager().addNewUser(auth?.currentUser, logInActivity)
FirestoreManager.userId = auth?.currentUser?.uid
onSuccess()
FirestoreManager().addNewUser(UserPojo(auth?.currentUser), onSuccess)
}
?.addOnFailureListener { e ->
Log.d(TAG, "createUserWithEmail:failure", e.fillInStackTrace())
......@@ -49,10 +44,10 @@ class MyUserManager {
}
}
fun deleteUser(user: FirebaseUser?) {
user?.delete()
?.addOnSuccessListener { Log.i(TAG, "UserPojo was successfully deleted") }
?.addOnFailureListener { Log.i(TAG, "deleteUser:failure", it.cause) }
fun deleteCurrentUser() {
auth?.currentUser?.delete()
?.addOnSuccessListener { Log.i(TAG, "deleteCurrentUser: success") }
?.addOnFailureListener { Log.i(TAG, "deleteCurrentUser: failure", it.cause) }
}
fun userLoggedIn(): Boolean {
......@@ -64,6 +59,10 @@ class MyUserManager {
Log.i(TAG, "User logged out")
}
fun getUserId(): String? {
return auth?.currentUser?.uid
}
private fun isEmail(string: String) = Patterns.EMAIL_ADDRESS.matcher(string).matches()
private fun constructEmail(username: String): String {
......
......@@ -3,7 +3,6 @@ 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.FirestoreManager
import com.paktalin.vocabularynotebook.firestore.MyUserManager
import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.activity_log_in.*
......@@ -21,7 +20,6 @@ class LogInActivity : AppCompatActivity() {
super.onStart()
if (MyUserManager.userLoggedIn()) {
startActivity(this@LogInActivity, MainActivity::class.java)
FirestoreManager.userId = MyUserManager.auth?.currentUser?.uid
}
}
......@@ -45,8 +43,7 @@ class LogInActivity : AppCompatActivity() {
shortToast(this@LogInActivity, resources.getString(R.string.toast_successful_sign_up))
startActivity(this@LogInActivity, MainActivity::class.java)
},
onFailure = { shortToast(this@LogInActivity, resources.getString(R.string.toast_sign_up_failed)) },
logInActivity = this@LogInActivity)
onFailure = { shortToast(this@LogInActivity, resources.getString(R.string.toast_sign_up_failed)) })
}
}
......
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