Commit 5bbf1a85 by Paktalin

Moved user logic to the UserManager

parent 7eae68a2
package com.paktalin.vocabularynotebook
import android.util.Log
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.activities.LogInActivity
import java.util.*
class UserManager {
companion object {
private val TAG = "VN/" + UserManager::class.simpleName
private fun deleteUser(user: FirebaseUser) {
user.delete()
.addOnSuccessListener { Log.i(TAG, "UserPojo was successfully deleted") }
.addOnFailureListener { Log.i(TAG, "deleteUser:failure", it.cause)}
}
fun addNewUserToDb(newUser: FirebaseUser, logInActivity: LogInActivity) {
//todo add condition to writing to the db in Firebase Console (request.auth.uid)
val db = FirebaseFirestore.getInstance()
val user = UserPojo(newUser.email)
db.collection("vocabularies").add(VocabularyPojo())
.addOnSuccessListener { firstVocabularyRef ->
Log.d(TAG, "VocabularyPojo successfully created: " + firstVocabularyRef.path)
user.vocabularies = Collections.singletonList(firstVocabularyRef)
db.collection("users").document(newUser.uid).set(user)
.addOnCompleteListener({ task ->
if (task.isSuccessful) {
Log.i(TAG, "Successfully added user to the collection")
logInActivity.startUserActivity()
}
else Log.w(TAG, "addUser:failure", task.exception)
})
}
.addOnFailureListener {
Log.w(TAG, "Couldn't add user to the database", it.cause)
UserManager.deleteUser(newUser)
}
}
}
}
...@@ -4,13 +4,13 @@ package com.paktalin.vocabularynotebook; ...@@ -4,13 +4,13 @@ package com.paktalin.vocabularynotebook;
import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentReference;
import java.util.List; import java.util.List;
public class User { public class UserPojo {
private final static String TAG = "VN/" + User.class.getSimpleName(); private final static String TAG = "VN/" + UserPojo.class.getSimpleName();
private String email, name; private String email, name;
private List<DocumentReference> vocabularies; private List<DocumentReference> vocabularies;
public User(String email) { public UserPojo(String email) {
this.email = email; this.email = email;
} }
......
package com.paktalin.vocabularynotebook; package com.paktalin.vocabularynotebook;
public class Vocabulary { public class VocabularyPojo {
String title; String title;
public Vocabulary() { public VocabularyPojo() {
title = "First vocabulary"; title = "First vocabulary";
} }
......
...@@ -7,16 +7,11 @@ import android.os.Bundle ...@@ -7,16 +7,11 @@ import android.os.Bundle
import android.text.TextUtils import android.text.TextUtils
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import com.google.android.gms.signin.SignIn
import com.google.firebase.auth.FirebaseAuth import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import kotlinx.android.synthetic.main.activity_log_in.* import kotlinx.android.synthetic.main.activity_log_in.*
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.User import com.paktalin.vocabularynotebook.UserManager
import com.paktalin.vocabularynotebook.Vocabulary
import java.util.*
class LogInActivity : AppCompatActivity() { class LogInActivity : AppCompatActivity() {
...@@ -27,18 +22,20 @@ class LogInActivity : AppCompatActivity() { ...@@ -27,18 +22,20 @@ class LogInActivity : AppCompatActivity() {
setContentView(R.layout.activity_log_in) setContentView(R.layout.activity_log_in)
mAuth = FirebaseAuth.getInstance() mAuth = FirebaseAuth.getInstance()
btnLogIn!!.setOnClickListener({ signIn() }) btnLogIn!!.setOnClickListener({ logIn() })
btnSignUp!!.setOnClickListener({ signUp() }) btnSignUp!!.setOnClickListener({ signUp() })
btnRandomUser!!.setOnClickListener({ createRandomUser() }) btnRandomUser!!.setOnClickListener({ createRandomUser() })
} }
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
val currentUser = mAuth!!.currentUser if (mAuth!!.currentUser != null) {
if (currentUser != null) startUserActivity() Log.d(TAG, "there is a logged in user")
startUserActivity()
}
} }
private fun signIn() { private fun logIn() {
val email = etEmail!!.text.toString() val email = etEmail!!.text.toString()
val password = etPassword!!.text.toString() val password = etPassword!!.text.toString()
...@@ -69,8 +66,7 @@ class LogInActivity : AppCompatActivity() { ...@@ -69,8 +66,7 @@ class LogInActivity : AppCompatActivity() {
.addOnCompleteListener(this) { task -> .addOnCompleteListener(this) { task ->
if (task.isSuccessful) { if (task.isSuccessful) {
Log.d(TAG, "Successfully signed up a new user") Log.d(TAG, "Successfully signed up a new user")
addNewUserToDb(mAuth!!.currentUser!!) UserManager.addNewUserToDb(mAuth!!.currentUser!!, this)
startUserActivity()
} }
else { else {
Log.w(TAG, "createUserWithEmail:failure", task.exception) Log.w(TAG, "createUserWithEmail:failure", task.exception)
...@@ -81,7 +77,7 @@ class LogInActivity : AppCompatActivity() { ...@@ -81,7 +77,7 @@ class LogInActivity : AppCompatActivity() {
} }
} }
private fun startUserActivity() { fun startUserActivity() {
Log.d(TAG, "Signed in successfully") Log.d(TAG, "Signed in successfully")
val userActivityIntent = Intent(this@LogInActivity, UserActivity::class.java) val userActivityIntent = Intent(this@LogInActivity, UserActivity::class.java)
startActivity(userActivityIntent) startActivity(userActivityIntent)
...@@ -95,25 +91,6 @@ class LogInActivity : AppCompatActivity() { ...@@ -95,25 +91,6 @@ class LogInActivity : AppCompatActivity() {
return true return true
} }
private fun addNewUserToDb(newUser: FirebaseUser) {
//todo add condition to writing to the db in Firebase Console (request.auth.uid)
//todo delete account if couldn't add user to the db
val db = FirebaseFirestore.getInstance()
val user = User(newUser.email)
db.collection("vocabularies").add(Vocabulary())
.addOnSuccessListener { firstVocabularyRef ->
Log.d(TAG, "Vocabulary successfully created: " + firstVocabularyRef.path)
user.vocabularies = Collections.singletonList(firstVocabularyRef)
db.collection("users").document(newUser.uid).set(user)
.addOnCompleteListener({ task ->
if (task.isSuccessful) Log.i(TAG, "Successfully added user to the collection")
else Log.w(TAG, "addUser:failure", task.exception)
})
}
}
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun createRandomUser() { private fun createRandomUser() {
etEmail.setText("random@gmail.com") etEmail.setText("random@gmail.com")
...@@ -122,6 +99,6 @@ class LogInActivity : AppCompatActivity() { ...@@ -122,6 +99,6 @@ class LogInActivity : AppCompatActivity() {
} }
companion object { companion object {
private val TAG = "VN/" + SignIn::class.simpleName private val TAG = "VN/" + LogInActivity::class.simpleName
} }
} }
\ No newline at end of file
...@@ -12,7 +12,7 @@ import com.google.firebase.firestore.DocumentSnapshot ...@@ -12,7 +12,7 @@ import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.* import kotlinx.android.synthetic.main.activity_vocabulary.*
class UserActivity : AppCompatActivity() { class UserActivity : AppCompatActivity() {
...@@ -23,11 +23,9 @@ class UserActivity : AppCompatActivity() { ...@@ -23,11 +23,9 @@ class UserActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
val navigationView = findViewById<NavigationView>(R.id.navigationView)
navigationView.setNavigationItemSelectedListener { menuItem -> navigationView.setNavigationItemSelectedListener { menuItem ->
menuItem.isChecked = true menuItem.isChecked = true
drawerLayout!!.closeDrawers() drawerLayout!!.closeDrawers()
true true
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
tools:openDrawer="start"> tools:openDrawer="start">
<include <include
layout="@layout/content_main" layout="@layout/activity_vocabulary"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
......
...@@ -30,9 +30,15 @@ ...@@ -30,9 +30,15 @@
android:layout_marginRight="8dp" android:layout_marginRight="8dp"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvCongrats" /> app:layout_constraintTop_toBottomOf="@+id/tvCongrats" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/tvUserData" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
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