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
eb0e29a6
authored
Apr 29, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More refactoring. Null safety
parent
5934185c
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
33 deletions
app/src/main/java/com/paktalin/vocabularynotebook/firestore/FirestoreManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/firestore/MyUserManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/LogInActivity.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
app/src/main/java/com/paktalin/vocabularynotebook/firestore/FirestoreManager.kt
View file @
eb0e29a6
...
...
@@ -23,8 +23,8 @@ class FirestoreManager {
fun
extractVocabularyId
(
onSuccess
:
()
->
Unit
,
showToastNoWords
:
()
->
Unit
,
removeProgressBar
:
()
->
Unit
,
mainActivity
:
Context
)
{
val
userId
=
FirebaseAuth
.
getInstance
()
!!
.
currentUser
!!
.
uid
userDocument
(
userId
).
get
()
.
addOnSuccessListener
{
snapshot
->
userDocument
(
userId
)
?
.
get
()
?
.
addOnSuccessListener
{
snapshot
->
removeProgressBar
()
if
(
snapshot
.
get
(
VOCABULARIES
)
!=
null
)
{
setVocabularyID
(
snapshot
,
mainActivity
)
...
...
@@ -36,7 +36,7 @@ class FirestoreManager {
}
}
fun
addNewUser
(
firebaseUser
:
FirebaseUser
,
logInActivity
:
LogInActivity
)
{
fun
addNewUser
(
firebaseUser
:
FirebaseUser
?
,
logInActivity
:
LogInActivity
)
{
//todo addAll condition to writing to the db in Firebase Console (request.auth.uid)
vocabularyCollection
.
add
(
VocabularyPojo
(
null
))
.
addOnSuccessListener
{
firstVocabularyRef
->
...
...
@@ -66,17 +66,17 @@ class FirestoreManager {
.
addOnCompleteListener
{
t
->
onComplete
(
t
.
result
)
}
}
private
fun
setNewUserWithVocabularyData
(
firebaseUser
:
FirebaseUser
,
private
fun
setNewUserWithVocabularyData
(
firebaseUser
:
FirebaseUser
?
,
firstVocabularyRef
:
DocumentReference
,
logInActivity
:
LogInActivity
)
{
val
user
=
UserPojo
(
firebaseUser
.
email
,
firebaseUser
.
uid
)
val
user
=
UserPojo
(
firebaseUser
?.
email
,
firebaseUser
?
.
uid
)
user
.
vocabularies
=
Collections
.
singletonList
(
firstVocabularyRef
)
userDocument
(
firebaseUser
.
uid
)
.
set
(
user
)
.
addOnSuccessListener
{
userDocument
(
firebaseUser
?.
uid
)
?
.
set
(
user
)
?
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully added user to the collection"
)
logInActivity
.
startUserActivity
()
}
.
addOnFailureListener
{
exception
->
?
.
addOnFailureListener
{
exception
->
Log
.
w
(
TAG
,
"addUser:failure"
,
exception
)
}
}
...
...
@@ -88,8 +88,8 @@ class FirestoreManager {
vocabularyId
=
vocabulary
.
id
}
private
fun
userDocument
(
userId
:
String
):
DocumentReference
{
return
db
.
collection
(
USERS
).
document
(
userId
)
private
fun
userDocument
(
userId
:
String
?):
DocumentReference
?
{
return
userId
?.
let
{
db
.
collection
(
USERS
).
document
(
it
)
}
}
private
fun
vocabularyDocument
():
DocumentReference
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/firestore/MyUserManager.kt
View file @
eb0e29a6
...
...
@@ -3,6 +3,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.utils.Log
class
MyUserManager
{
...
...
@@ -10,19 +11,14 @@ class MyUserManager {
companion
object
{
private
const
val
TAG
=
"VN/MyUserManager"
private
var
mAuth
:
FirebaseAuth
?
=
FirebaseAuth
.
getInstance
()
fun
deleteUser
(
user
:
FirebaseUser
)
{
user
.
delete
()
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"UserPojo was successfully deleted"
)
}
.
addOnFailureListener
{
Log
.
i
(
TAG
,
"deleteUser:failure"
,
it
.
cause
)
}
}
private
var
auth
:
FirebaseAuth
?
=
FirebaseAuth
.
getInstance
()
private
var
user
:
FirebaseUser
?
=
auth
?.
currentUser
fun
logIn
(
username
:
String
,
password
:
String
,
onComplete
:
()
->
Unit
,
onSuccess
:
()
->
Unit
,
onFailure
:
()
->
Unit
)
{
mA
uth
?.
signInWithEmailAndPassword
(
constructEmail
(
username
),
password
)
a
uth
?.
signInWithEmailAndPassword
(
constructEmail
(
username
),
password
)
?.
addOnCompleteListener
{
onComplete
()
}
?.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed in"
)
...
...
@@ -34,17 +30,16 @@ class MyUserManager {
}
}
private
fun
isEmail
(
string
:
String
)
=
Patterns
.
EMAIL_ADDRESS
.
matcher
(
string
).
matches
()
fun
signUp
(
username
:
String
,
password
:
String
,
onComplete
:
()
->
Unit
,
onSuccess
:
()
->
Unit
,
onFailure
:
()
->
Unit
)
{
mAuth
?.
createUserWithEmailAndPassword
(
constructEmail
(
username
),
password
)
onFailure
:
()
->
Unit
,
logInActivity
:
LogInActivity
)
{
auth
?.
createUserWithEmailAndPassword
(
constructEmail
(
username
),
password
)
?.
addOnCompleteListener
{
onComplete
()
}
?.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed up a new user"
)
mAuth
?.
currentUser
?.
let
{
it1
->
FirestoreManager
().
addNewUser
(
it1
,
activity
)
}
FirestoreManager
().
addNewUser
(
user
,
logInActivity
)
onSuccess
()
}
?.
addOnFailureListener
{
e
->
...
...
@@ -53,15 +48,23 @@ 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
userLoggedIn
():
Boolean
{
return
mAuth
?.
currentU
ser
!=
null
return
u
ser
!=
null
}
fun
logOut
()
{
mA
uth
?.
signOut
()
a
uth
?.
signOut
()
Log
.
i
(
TAG
,
"User logged out"
)
}
private
fun
isEmail
(
string
:
String
)
=
Patterns
.
EMAIL_ADDRESS
.
matcher
(
string
).
matches
()
private
fun
constructEmail
(
username
:
String
):
String
{
return
if
(
isEmail
(
username
))
username
else
username
.
toLowerCase
()
+
"@wordbook.com"
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/LogInActivity.kt
View file @
eb0e29a6
...
...
@@ -37,7 +37,8 @@ class LogInActivity : AppCompatActivity() {
MyUserManager
.
signUp
(
u
,
p
,
onComplete
=
{
removeProgressBar
(
supportFragmentManager
)
},
onSuccess
=
{
shortToast
(
this
,
resources
.
getString
(
R
.
string
.
toast_successful_sign_up
));
login
()
},
onFailure
=
{
shortToast
(
this
,
resources
.
getString
(
R
.
string
.
toast_sign_up_failed
))
})
onFailure
=
{
shortToast
(
this
,
resources
.
getString
(
R
.
string
.
toast_sign_up_failed
))
},
logInActivity
=
this
)
}
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
View file @
eb0e29a6
...
...
@@ -12,6 +12,7 @@ import androidx.appcompat.widget.SearchView
import
androidx.recyclerview.selection.Selection
import
com.paktalin.vocabularynotebook.R
import
com.paktalin.vocabularynotebook.firestore.FirestoreManager
import
com.paktalin.vocabularynotebook.firestore.MyUserManager
import
com.paktalin.vocabularynotebook.ui.fragments.SubmitAddedFragment
import
com.paktalin.vocabularynotebook.ui.fragments.TagFragment
import
com.paktalin.vocabularynotebook.ui.recycler_view.State
...
...
@@ -61,11 +62,6 @@ class MainActivity : AppCompatActivity() {
return
super
.
onOptionsItemSelected
(
item
)
}
private
fun
logOut
()
{
com
.
paktalin
.
vocabularynotebook
.
firestore
.
logOut
()
startActivity
(
this
@MainActivity
,
LogInActivity
::
class
.
java
)
}
private
fun
refreshVocabulary
()
{
vocabularyAdapter
?.
refresh
()
swipeRefresh
.
isRefreshing
=
false
...
...
@@ -74,8 +70,10 @@ class MainActivity : AppCompatActivity() {
private
fun
setUpNavigationView
()
{
navigationView
.
setNavigationItemSelectedListener
{
menuItem
->
menuItem
.
isChecked
=
true
if
(
menuItem
.
itemId
==
R
.
id
.
logOut
)
logOut
()
if
(
menuItem
.
itemId
==
R
.
id
.
logOut
)
{
MyUserManager
.
logOut
()
startActivity
(
this
@MainActivity
,
LogInActivity
::
class
.
java
)
}
drawerLayout
!!
.
closeDrawers
()
true
}
...
...
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