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
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
96 additions
and
87 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
class
VocabularyPojo
(
var
title
:
String
?,
val
id
:
String
?)
{
import
java.util.*
class
VocabularyPojo
(
var
title
:
String
?,
var
id
:
String
?
=
null
)
{
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 {
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"Couldn't addAll user to the database"
,
it
.
cause
)
deleteUser
(
firebaseUser
)
MyUserManager
.
deleteUser
(
firebaseUser
)
}
}
...
...
@@ -69,7 +69,7 @@ class FirestoreManager {
private
fun
setNewUserWithVocabularyData
(
firebaseUser
:
FirebaseUser
,
firstVocabularyRef
:
DocumentReference
,
logInActivity
:
LogInActivity
)
{
val
user
=
UserPojo
(
firebaseUser
.
email
)
val
user
=
UserPojo
(
firebaseUser
.
email
,
firebaseUser
.
uid
)
user
.
vocabularies
=
Collections
.
singletonList
(
firstVocabularyRef
)
userDocument
(
firebaseUser
.
uid
).
set
(
user
)
.
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,61 +3,68 @@ 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.R
import
com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import
com.paktalin.vocabularynotebook.utils.Log
import
com.paktalin.vocabularynotebook.utils.removeProgressBar
import
com.paktalin.vocabularynotebook.utils.shortToast
private
const
val
TAG
=
"VN/UserManager"
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
)
}
}
fun
logIn
(
onComplete
:
()
->
Unit
,
onSuccess
:
()
->
Unit
,
onFailure
:
()
->
Unit
,
username
:
String
,
password
:
String
)
{
mAuth
?.
signInWithEmailAndPassword
(
constructEmail
(
username
),
password
)
?.
addOnCompleteListener
{
onComplete
()
}
?.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed in"
)
onSuccess
()
}
?.
addOnFailureListener
{
Log
.
w
(
TAG
,
"signInWithEmail:failure"
,
it
)
onFailure
()
}
}
internal
fun
isEmail
(
string
:
String
)
=
Patterns
.
EMAIL_ADDRESS
.
matcher
(
string
).
matches
()
fun
signUp
(
activity
:
LogInActivity
,
username
:
String
,
password
:
String
)
{
mAuth
?.
createUserWithEmailAndPassword
(
constructEmail
(
username
),
password
)
?.
addOnCompleteListener
{
removeProgressBar
(
activity
.
supportFragmentManager
)
}
?.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed up a new user"
)
mAuth
?.
currentUser
?.
let
{
it1
->
FirestoreManager
().
addNewUser
(
it1
,
activity
)
}
shortToast
(
activity
,
activity
.
resources
.
getString
(
R
.
string
.
toast_successful_sign_up
))
activity
.
login
()
}
?.
addOnFailureListener
{
msg
->
Log
.
d
(
TAG
,
"createUserWithEmail:failure"
,
msg
.
fillInStackTrace
())
msg
.
message
?.
let
{
it1
->
shortToast
(
activity
,
it1
)
}
}
}
fun
userLoggedIn
():
Boolean
{
return
mAuth
?.
currentUser
!=
null
}
fun
logOut
()
{
mAuth
?.
signOut
()
Log
.
i
(
TAG
,
"User logged out"
)
}
private
fun
constructEmail
(
username
:
String
):
String
{
return
if
(
isEmail
(
username
))
username
else
username
.
toLowerCase
()
+
"@wordbook.com"
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
)
}
}
fun
logIn
(
username
:
String
,
password
:
String
,
onComplete
:
()
->
Unit
,
onSuccess
:
()
->
Unit
,
onFailure
:
()
->
Unit
)
{
mAuth
?.
signInWithEmailAndPassword
(
constructEmail
(
username
),
password
)
?.
addOnCompleteListener
{
onComplete
()
}
?.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed in"
)
onSuccess
()
}
?.
addOnFailureListener
{
Log
.
w
(
TAG
,
"signInWithEmail:failure"
,
it
)
onFailure
()
}
}
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
)
?.
addOnCompleteListener
{
onComplete
()
}
?.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed up a new user"
)
mAuth
?.
currentUser
?.
let
{
it1
->
FirestoreManager
().
addNewUser
(
it1
,
activity
)
}
onSuccess
()
}
?.
addOnFailureListener
{
e
->
Log
.
d
(
TAG
,
"createUserWithEmail:failure"
,
e
.
fillInStackTrace
())
onFailure
()
}
}
fun
userLoggedIn
():
Boolean
{
return
mAuth
?.
currentUser
!=
null
}
fun
logOut
()
{
mAuth
?.
signOut
()
Log
.
i
(
TAG
,
"User logged out"
)
}
private
fun
constructEmail
(
username
:
String
):
String
{
return
if
(
isEmail
(
username
))
username
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
import
android.os.Bundle
import
androidx.appcompat.app.AppCompatActivity
import
com.paktalin.vocabularynotebook.R
import
com.paktalin.vocabularynotebook.firestore.logIn
import
com.paktalin.vocabularynotebook.firestore.signUp
import
com.paktalin.vocabularynotebook.firestore.userLoggedIn
import
com.paktalin.vocabularynotebook.firestore.MyUserManager
import
com.paktalin.vocabularynotebook.utils.*
import
kotlinx.android.synthetic.main.activity_log_in.*
...
...
@@ -14,36 +12,32 @@ class LogInActivity : AppCompatActivity() {
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_log_in
)
btnLogIn
!!
.
setOnClickListener
{
login
()
}
btnSignUp
!!
.
setOnClickListener
{
signUp
()
}
btnLogIn
?
.
setOnClickListener
{
login
()
}
btnSignUp
?
.
setOnClickListener
{
signUp
()
}
}
override
fun
onStart
()
{
super
.
onStart
()
if
(
userLoggedIn
())
{
startUserActivity
()
}
if
(
MyUserManager
.
userLoggedIn
())
startActivity
(
this
,
MainActivity
::
class
.
java
)
}
fun
login
()
{
val
(
username
,
password
)
=
getUsernameAndPassword
()
if
(
fieldsNotEmpty
(
username
,
password
,
this
))
{
addProgressBar
()
logIn
(
{
removeProgressBar
(
supportFragmentManager
)
},
{
startUserActivity
()
},
{
shortToast
(
this
@LogInActivity
,
getString
(
R
.
string
.
toast_auth_failed
))
},
username
,
password
)
private
fun
login
()
{
getValidUsernameAndPassword
()
?.
let
{
(
u
,
p
)
->
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_login
)
MyUserManager
.
logIn
(
u
,
p
,
onComplete
=
{
removeProgressBar
(
supportFragmentManager
)
},
onSuccess
=
{
startActivity
(
this
,
MainActivity
::
class
.
java
)
},
onFailure
=
{
shortToast
(
this
,
getString
(
R
.
string
.
toast_auth_failed
))
})
}
}
private
fun
signUp
()
{
val
(
username
,
password
)
=
getUsernameAndPassword
()
if
(
fieldsNotEmpty
(
username
,
password
,
this
)
&&
validUsername
(
username
,
this
))
{
addProgressBar
()
signUp
(
this
,
username
,
password
)
getValidUsernameAndPassword
()
?.
let
{
(
u
,
p
)
->
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_login
)
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
))
}
)
}
}
...
...
@@ -52,12 +46,14 @@ class LogInActivity : AppCompatActivity() {
startActivity
(
this
@LogInActivity
,
MainActivity
::
class
.
java
)
}
private
fun
addProgressBar
()
{
com
.
paktalin
.
vocabularynotebook
.
utils
.
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_login
)
private
fun
getValidUsernameAndPassword
():
Pair
<
String
,
String
>?
{
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
{
private
val
TAG
=
"VN/"
+
LogInActivity
::
class
.
simpleName
}
...
...
app/src/main/res/values/strings.xml
View file @
5934185c
...
...
@@ -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_select_words_to_tag"
>
Select words to 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>
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