Commit e92751b8 by Paktalin

Linked vocabularies collection to users

parent c34cf757
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();
private String email, name;
private List<DocumentReference> vocabularies;
public User(String email) {
this.email = email;
......@@ -22,4 +29,12 @@ public class User {
public void setName(String name) {
this.name = name;
}
public List<DocumentReference> getVocabularies() {
return vocabularies;
}
public void setVocabularies(List<DocumentReference> vocabularies) {
this.vocabularies = vocabularies;
}
}
package com.paktalin.vocabularynotebook;
public class Vocabulary {
String title;
public Vocabulary() {
title = "First vocabulary";
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
package com.paktalin.vocabularynotebook.activities
import android.annotation.SuppressLint
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
......@@ -14,6 +15,8 @@ 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.*
class LogInActivity : AppCompatActivity() {
......@@ -26,6 +29,7 @@ class LogInActivity : AppCompatActivity() {
mAuth = FirebaseAuth.getInstance()
btnLogIn!!.setOnClickListener({ signIn() })
btnSignUp!!.setOnClickListener({ signUp() })
btnRandomUser!!.setOnClickListener({ createRandomUser() })
}
override fun onStart() {
......@@ -93,12 +97,28 @@ class LogInActivity : AppCompatActivity() {
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()
db.collection("users").document(newUser.uid).set(User(newUser.email))
.addOnCompleteListener({ task ->
if (task.isSuccessful) Log.i(TAG, "Successfully added user to the collection")
else Log.w(TAG, "addUser:failure", task.exception)
})
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")
etPassword.setText("123456")
signUp()
}
companion object {
......
......@@ -74,4 +74,18 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/btnLogIn"
app:layout_constraintTop_toTopOf="@+id/btnLogIn" />
<Button
android:id="@+id/btnRandomUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Create random user"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
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