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