Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
likorn
/
vocabulary_notebook
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5bbf1a85
authored
Sep 07, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved user logic to the UserManager
parent
7eae68a2
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
69 additions
and
43 deletions
app/src/main/java/com/paktalin/vocabularynotebook/UserManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/User.java → app/src/main/java/com/paktalin/vocabularynotebook/UserPojo.java
app/src/main/java/com/paktalin/vocabularynotebook/Vocabulary.java → app/src/main/java/com/paktalin/vocabularynotebook/VocabularyPojo.java
app/src/main/java/com/paktalin/vocabularynotebook/activities/LogInActivity.kt
app/src/main/java/com/paktalin/vocabularynotebook/activities/UserActivity.kt
app/src/main/res/layout/activity_main.xml
app/src/main/res/layout/content_main.xml → app/src/main/res/layout/activity_vocabulary.xml
app/src/main/java/com/paktalin/vocabularynotebook/UserManager.kt
0 → 100644
View file @
5bbf1a85
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
)
}
}
}
}
app/src/main/java/com/paktalin/vocabularynotebook/User.java
→
app/src/main/java/com/paktalin/vocabularynotebook/User
Pojo
.java
View file @
5bbf1a85
...
...
@@ -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
User
Pojo
{
private
final
static
String
TAG
=
"VN/"
+
User
Pojo
.
class
.
getSimpleName
();
private
String
email
,
name
;
private
List
<
DocumentReference
>
vocabularies
;
public
User
(
String
email
)
{
public
User
Pojo
(
String
email
)
{
this
.
email
=
email
;
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/Vocabulary.java
→
app/src/main/java/com/paktalin/vocabularynotebook/Vocabulary
Pojo
.java
View file @
5bbf1a85
package
com
.
paktalin
.
vocabularynotebook
;
public
class
Vocabulary
{
public
class
Vocabulary
Pojo
{
String
title
;
public
Vocabulary
()
{
public
Vocabulary
Pojo
()
{
title
=
"First vocabulary"
;
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/activities/LogInActivity.kt
View file @
5bbf1a85
...
...
@@ -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
({
sign
In
()
})
btnLogIn
!!
.
setOnClickListener
({
log
In
()
})
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
sign
In
()
{
private
fun
log
In
()
{
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
app/src/main/java/com/paktalin/vocabularynotebook/activities/UserActivity.kt
View file @
5bbf1a85
...
...
@@ -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
}
...
...
app/src/main/res/layout/activity_main.xml
View file @
5bbf1a85
...
...
@@ -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"
/>
...
...
app/src/main/res/layout/
content_main
.xml
→
app/src/main/res/layout/
activity_vocabulary
.xml
View file @
5bbf1a85
...
...
@@ -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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment