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;
import com.google.firebase.firestore.DocumentReference;
import java.util.List;
public class User {
private final static String TAG = "VN/" + User.class.getSimpleName();
public class UserPojo {
private final static String TAG = "VN/" + UserPojo.class.getSimpleName();
private String email, name;
private List<DocumentReference> vocabularies;
public User(String email) {
public UserPojo(String email) {
this.email = email;
}
......
package com.paktalin.vocabularynotebook;
public class Vocabulary {
public class VocabularyPojo {
String title;
public Vocabulary() {
public VocabularyPojo() {
title = "First vocabulary";
}
......
......@@ -7,16 +7,11 @@ import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.google.android.gms.signin.SignIn
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import kotlinx.android.synthetic.main.activity_log_in.*
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.User
import com.paktalin.vocabularynotebook.Vocabulary
import java.util.*
import com.paktalin.vocabularynotebook.UserManager
class LogInActivity : AppCompatActivity() {
......@@ -27,18 +22,20 @@ class LogInActivity : AppCompatActivity() {
setContentView(R.layout.activity_log_in)
mAuth = FirebaseAuth.getInstance()
btnLogIn!!.setOnClickListener({ signIn() })
btnLogIn!!.setOnClickListener({ logIn() })
btnSignUp!!.setOnClickListener({ signUp() })
btnRandomUser!!.setOnClickListener({ createRandomUser() })
}
override fun onStart() {
super.onStart()
val currentUser = mAuth!!.currentUser
if (currentUser != null) startUserActivity()
if (mAuth!!.currentUser != null) {
Log.d(TAG, "there is a logged in user")
startUserActivity()
}
}
private fun signIn() {
private fun logIn() {
val email = etEmail!!.text.toString()
val password = etPassword!!.text.toString()
......@@ -69,8 +66,7 @@ class LogInActivity : AppCompatActivity() {
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
Log.d(TAG, "Successfully signed up a new user")
addNewUserToDb(mAuth!!.currentUser!!)
startUserActivity()
UserManager.addNewUserToDb(mAuth!!.currentUser!!, this)
}
else {
Log.w(TAG, "createUserWithEmail:failure", task.exception)
......@@ -81,7 +77,7 @@ class LogInActivity : AppCompatActivity() {
}
}
private fun startUserActivity() {
fun startUserActivity() {
Log.d(TAG, "Signed in successfully")
val userActivityIntent = Intent(this@LogInActivity, UserActivity::class.java)
startActivity(userActivityIntent)
......@@ -95,25 +91,6 @@ class LogInActivity : AppCompatActivity() {
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")
private fun createRandomUser() {
etEmail.setText("random@gmail.com")
......@@ -122,6 +99,6 @@ class LogInActivity : AppCompatActivity() {
}
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
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import kotlinx.android.synthetic.main.activity_vocabulary.*
class UserActivity : AppCompatActivity() {
......@@ -23,11 +23,9 @@ class UserActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navigationView = findViewById<NavigationView>(R.id.navigationView)
navigationView.setNavigationItemSelectedListener { menuItem ->
menuItem.isChecked = true
drawerLayout!!.closeDrawers()
true
}
......
......@@ -9,7 +9,7 @@
tools:openDrawer="start">
<include
layout="@layout/content_main"
layout="@layout/activity_vocabulary"
android:layout_width="match_parent"
android:layout_height="match_parent" />
......
......@@ -30,9 +30,15 @@
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
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>
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