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
5934185c
authored
Apr 29, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in LogInActivity
parent
82cb4ebe
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
65 additions
and
56 deletions
app/src/main/java/com/paktalin/vocabularynotebook/entities/VocabularyPojo.kt
app/src/main/java/com/paktalin/vocabularynotebook/firestore/FirestoreManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/firestore/UserManager.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/res/values/strings.xml
app/src/main/java/com/paktalin/vocabularynotebook/entities/VocabularyPojo.kt
View file @
5934185c
package
com.paktalin.vocabularynotebook.entities
package
com.paktalin.vocabularynotebook.entities
class
VocabularyPojo
(
var
title
:
String
?,
val
id
:
String
?)
{
import
java.util.*
class
VocabularyPojo
(
var
title
:
String
?,
var
id
:
String
?
=
null
)
{
init
{
init
{
if
(
title
==
null
)
title
=
"Untitled vocabulary"
title
=
title
?:
"Untitled vocabulary"
id
=
id
?:
UUID
.
randomUUID
().
toString
()
}
}
}
}
app/src/main/java/com/paktalin/vocabularynotebook/firestore/FirestoreManager.kt
View file @
5934185c
...
@@ -45,7 +45,7 @@ class FirestoreManager {
...
@@ -45,7 +45,7 @@ class FirestoreManager {
}
}
.
addOnFailureListener
{
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"Couldn't addAll user to the database"
,
it
.
cause
)
Log
.
w
(
TAG
,
"Couldn't addAll user to the database"
,
it
.
cause
)
deleteUser
(
firebaseUser
)
MyUserManager
.
deleteUser
(
firebaseUser
)
}
}
}
}
...
@@ -69,7 +69,7 @@ class FirestoreManager {
...
@@ -69,7 +69,7 @@ class FirestoreManager {
private
fun
setNewUserWithVocabularyData
(
firebaseUser
:
FirebaseUser
,
private
fun
setNewUserWithVocabularyData
(
firebaseUser
:
FirebaseUser
,
firstVocabularyRef
:
DocumentReference
,
firstVocabularyRef
:
DocumentReference
,
logInActivity
:
LogInActivity
)
{
logInActivity
:
LogInActivity
)
{
val
user
=
UserPojo
(
firebaseUser
.
email
)
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
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/firestore/UserManager.kt
→
app/src/main/java/com/paktalin/vocabularynotebook/firestore/
My
UserManager.kt
View file @
5934185c
...
@@ -3,23 +3,25 @@ package com.paktalin.vocabularynotebook.firestore
...
@@ -3,23 +3,25 @@ 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.R
import
com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import
com.paktalin.vocabularynotebook.utils.Log
import
com.paktalin.vocabularynotebook.utils.Log
import
com.paktalin.vocabularynotebook.utils.removeProgressBar
import
com.paktalin.vocabularynotebook.utils.shortToast
private
const
val
TAG
=
"VN/UserManager"
class
MyUserManager
{
private
var
mAuth
:
FirebaseAuth
?
=
FirebaseAuth
.
getInstance
()
fun
deleteUser
(
user
:
FirebaseUser
)
{
companion
object
{
private
const
val
TAG
=
"VN/MyUserManager"
private
var
mAuth
:
FirebaseAuth
?
=
FirebaseAuth
.
getInstance
()
fun
deleteUser
(
user
:
FirebaseUser
)
{
user
.
delete
()
user
.
delete
()
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"UserPojo was successfully deleted"
)
}
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"UserPojo was successfully deleted"
)
}
.
addOnFailureListener
{
Log
.
i
(
TAG
,
"deleteUser:failure"
,
it
.
cause
)
}
.
addOnFailureListener
{
Log
.
i
(
TAG
,
"deleteUser:failure"
,
it
.
cause
)
}
}
}
fun
logIn
(
onComplete
:
()
->
Unit
,
onSuccess
:
()
->
Unit
,
onFailure
:
()
->
Unit
,
fun
logIn
(
username
:
String
,
password
:
String
,
username
:
String
,
password
:
String
)
{
onComplete
:
()
->
Unit
,
onSuccess
:
()
->
Unit
,
onFailure
:
()
->
Unit
)
{
mAuth
?.
signInWithEmailAndPassword
(
constructEmail
(
username
),
password
)
mAuth
?.
signInWithEmailAndPassword
(
constructEmail
(
username
),
password
)
?.
addOnCompleteListener
{
onComplete
()
}
?.
addOnCompleteListener
{
onComplete
()
}
?.
addOnSuccessListener
{
?.
addOnSuccessListener
{
...
@@ -30,34 +32,39 @@ fun logIn(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
...
@@ -30,34 +32,39 @@ fun logIn(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
Log
.
w
(
TAG
,
"signInWithEmail:failure"
,
it
)
Log
.
w
(
TAG
,
"signInWithEmail:failure"
,
it
)
onFailure
()
onFailure
()
}
}
}
}
internal
fun
isEmail
(
string
:
String
)
=
Patterns
.
EMAIL_ADDRESS
.
matcher
(
string
).
matches
()
private
fun
isEmail
(
string
:
String
)
=
Patterns
.
EMAIL_ADDRESS
.
matcher
(
string
).
matches
()
fun
signUp
(
activity
:
LogInActivity
,
username
:
String
,
password
:
String
)
{
fun
signUp
(
username
:
String
,
password
:
String
,
onComplete
:
()
->
Unit
,
onSuccess
:
()
->
Unit
,
onFailure
:
()
->
Unit
)
{
mAuth
?.
createUserWithEmailAndPassword
(
constructEmail
(
username
),
password
)
mAuth
?.
createUserWithEmailAndPassword
(
constructEmail
(
username
),
password
)
?.
addOnCompleteListener
{
removeProgressBar
(
activity
.
supportFragmentManager
)
}
?.
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
)
}
mAuth
?.
currentUser
?.
let
{
it1
->
FirestoreManager
().
addNewUser
(
it1
,
activity
)
}
shortToast
(
activity
,
activity
.
resources
.
getString
(
R
.
string
.
toast_successful_sign_up
))
onSuccess
()
activity
.
login
()
}
?.
addOnFailureListener
{
e
->
Log
.
d
(
TAG
,
"createUserWithEmail:failure"
,
e
.
fillInStackTrace
())
onFailure
()
}
}
}
?.
addOnFailureListener
{
msg
->
Log
.
d
(
TAG
,
"createUserWithEmail:failure"
,
msg
.
fillInStackTrace
())
msg
.
message
?.
let
{
it1
->
shortToast
(
activity
,
it1
)
}
}
}
fun
userLoggedIn
():
Boolean
{
fun
userLoggedIn
():
Boolean
{
return
mAuth
?.
currentUser
!=
null
return
mAuth
?.
currentUser
!=
null
}
}
fun
logOut
()
{
fun
logOut
()
{
mAuth
?.
signOut
()
mAuth
?.
signOut
()
Log
.
i
(
TAG
,
"User logged out"
)
Log
.
i
(
TAG
,
"User logged out"
)
}
}
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"
}
}
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/LogInActivity.kt
View file @
5934185c
...
@@ -3,9 +3,7 @@ package com.paktalin.vocabularynotebook.ui.activities
...
@@ -3,9 +3,7 @@ package com.paktalin.vocabularynotebook.ui.activities
import
android.os.Bundle
import
android.os.Bundle
import
androidx.appcompat.app.AppCompatActivity
import
androidx.appcompat.app.AppCompatActivity
import
com.paktalin.vocabularynotebook.R
import
com.paktalin.vocabularynotebook.R
import
com.paktalin.vocabularynotebook.firestore.logIn
import
com.paktalin.vocabularynotebook.firestore.MyUserManager
import
com.paktalin.vocabularynotebook.firestore.signUp
import
com.paktalin.vocabularynotebook.firestore.userLoggedIn
import
com.paktalin.vocabularynotebook.utils.*
import
com.paktalin.vocabularynotebook.utils.*
import
kotlinx.android.synthetic.main.activity_log_in.*
import
kotlinx.android.synthetic.main.activity_log_in.*
...
@@ -14,36 +12,32 @@ class LogInActivity : AppCompatActivity() {
...
@@ -14,36 +12,32 @@ class LogInActivity : AppCompatActivity() {
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_log_in
)
setContentView
(
R
.
layout
.
activity_log_in
)
btnLogIn
!!
.
setOnClickListener
{
login
()
}
btnLogIn
?
.
setOnClickListener
{
login
()
}
btnSignUp
!!
.
setOnClickListener
{
signUp
()
}
btnSignUp
?
.
setOnClickListener
{
signUp
()
}
}
}
override
fun
onStart
()
{
override
fun
onStart
()
{
super
.
onStart
()
super
.
onStart
()
if
(
userLoggedIn
())
{
if
(
MyUserManager
.
userLoggedIn
())
startActivity
(
this
,
MainActivity
::
class
.
java
)
startUserActivity
()
}
}
}
fun
login
()
{
private
fun
login
()
{
val
(
username
,
password
)
=
getUsernameAndPassword
()
getValidUsernameAndPassword
()
?.
let
{
(
u
,
p
)
->
if
(
fieldsNotEmpty
(
username
,
password
,
this
))
{
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_login
)
addProgressBar
()
MyUserManager
.
logIn
(
u
,
p
,
logIn
(
onComplete
=
{
removeProgressBar
(
supportFragmentManager
)
},
{
removeProgressBar
(
supportFragmentManager
)
},
onSuccess
=
{
startActivity
(
this
,
MainActivity
::
class
.
java
)
},
{
startUserActivity
()
},
onFailure
=
{
shortToast
(
this
,
getString
(
R
.
string
.
toast_auth_failed
))
})
{
shortToast
(
this
@LogInActivity
,
getString
(
R
.
string
.
toast_auth_failed
))
},
username
,
password
)
}
}
}
}
private
fun
signUp
()
{
private
fun
signUp
()
{
val
(
username
,
password
)
=
getUsernameAndPassword
()
getValidUsernameAndPassword
()
?.
let
{
(
u
,
p
)
->
if
(
fieldsNotEmpty
(
username
,
password
,
this
)
&&
validUsername
(
username
,
this
))
{
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_login
)
addProgressBar
()
MyUserManager
.
signUp
(
u
,
p
,
onComplete
=
{
removeProgressBar
(
supportFragmentManager
)
},
onSuccess
=
{
shortToast
(
this
,
resources
.
getString
(
R
.
string
.
toast_successful_sign_up
));
login
()
},
signUp
(
this
,
username
,
password
)
onFailure
=
{
shortToast
(
this
,
resources
.
getString
(
R
.
string
.
toast_sign_up_failed
))
}
)
}
}
}
}
...
@@ -52,11 +46,13 @@ class LogInActivity : AppCompatActivity() {
...
@@ -52,11 +46,13 @@ class LogInActivity : AppCompatActivity() {
startActivity
(
this
@LogInActivity
,
MainActivity
::
class
.
java
)
startActivity
(
this
@LogInActivity
,
MainActivity
::
class
.
java
)
}
}
private
fun
addProgressBar
()
{
private
fun
getValidUsernameAndPassword
():
Pair
<
String
,
String
>?
{
com
.
paktalin
.
vocabularynotebook
.
utils
.
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_login
)
val
username
=
etUsername
!!
.
text
.
toString
()
val
password
=
etPassword
!!
.
text
.
toString
()
return
Pair
(
username
,
password
).
takeIf
{
(
fieldsNotEmpty
(
username
,
password
,
this
)
&&
validUsername
(
username
,
this
))
}
}
}
private
fun
getUsernameAndPassword
()
=
Pair
(
etUsername
!!
.
text
.
toString
(),
etPassword
!!
.
text
.
toString
())
companion
object
{
companion
object
{
private
val
TAG
=
"VN/"
+
LogInActivity
::
class
.
simpleName
private
val
TAG
=
"VN/"
+
LogInActivity
::
class
.
simpleName
...
...
app/src/main/res/values/strings.xml
View file @
5934185c
...
@@ -24,4 +24,5 @@
...
@@ -24,4 +24,5 @@
<string
name=
"toast_invalid_username"
>
Please, create a username using letters a–z, numbers 0–9 and characters -_.
</string>
<string
name=
"toast_invalid_username"
>
Please, create a username using letters a–z, numbers 0–9 and characters -_.
</string>
<string
name=
"toast_select_words_to_tag"
>
Select words to tag
</string>
<string
name=
"toast_select_words_to_tag"
>
Select words to tag
</string>
<string
name=
"toast_tag_cannot_be_empty"
>
Please, enter the tag
</string>
<string
name=
"toast_tag_cannot_be_empty"
>
Please, enter the tag
</string>
<string
name=
"toast_sign_up_failed"
>
Sign up failed
</string>
</resources>
</resources>
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