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
638c1d44
authored
Nov 11, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved login and signUp actions from LoginActivity to UserManager
parent
472ba8eb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
87 additions
and
78 deletions
.idea/misc.xml
app/src/main/java/com/paktalin/vocabularynotebook/UserManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/LogInActivity.kt
.idea/misc.xml
View file @
638c1d44
...
...
@@ -25,7 +25,7 @@
</value>
</option>
</component>
<component
name=
"ProjectRootManager"
version=
"2"
languageLevel=
"JDK_1_
7
"
project-jdk-name=
"1.8"
project-jdk-type=
"JavaSDK"
>
<component
name=
"ProjectRootManager"
version=
"2"
languageLevel=
"JDK_1_
8
"
project-jdk-name=
"1.8"
project-jdk-type=
"JavaSDK"
>
<output
url=
"file://$PROJECT_DIR$/build/classes"
/>
</component>
<component
name=
"ProjectType"
>
...
...
app/src/main/java/com/paktalin/vocabularynotebook/UserManager.kt
View file @
638c1d44
package
com.paktalin.vocabularynotebook
import
android.util.Log
import
com.google.firebase.auth.FirebaseAuth
import
com.google.firebase.auth.FirebaseUser
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
com.paktalin.vocabularynotebook.ui.activities.LogInActivity
...
...
@@ -9,39 +10,62 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import
com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
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
=
ConfiguredFirestore
.
instance
val
user
=
UserPojo
(
newUser
.
email
)
db
.
collection
(
VOCABULARIES
).
add
(
Vocabulary
.
Pojo
(
null
))
.
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
)
}
}
}
private
const
val
TAG
=
"VN/UserManager"
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
=
ConfiguredFirestore
.
instance
val
user
=
UserPojo
(
newUser
.
email
)
db
.
collection
(
VOCABULARIES
).
add
(
Vocabulary
.
Pojo
(
null
))
.
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
)
deleteUser
(
newUser
)
}
}
fun
login
(
onComplete
:
()
->
Unit
,
onSuccess
:
()
->
Unit
,
onFailure
:
()
->
Unit
,
mAuth
:
FirebaseAuth
?,
email
:
String
,
password
:
String
)
{
mAuth
!!
.
signInWithEmailAndPassword
(
email
,
password
)
.
addOnCompleteListener
{
onComplete
()
}
.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed in"
)
onSuccess
()
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"signInWithEmail:failure"
,
it
)
onFailure
()
}
}
fun
signUp
(
mAuth
:
FirebaseAuth
?,
activity
:
LogInActivity
,
email
:
String
,
password
:
String
)
{
mAuth
!!
.
createUserWithEmailAndPassword
(
email
,
password
)
.
addOnCompleteListener
{
removeProgressBar
(
activity
.
supportFragmentManager
)
}
.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed up a new user"
)
addNewUserToDb
(
mAuth
.
currentUser
!!
,
activity
)
activity
.
login
()
}
.
addOnFailureListener
{
Log
.
d
(
TAG
,
"createUserWithEmail:failure"
,
it
.
fillInStackTrace
())
shortToast
(
activity
,
it
.
message
!!
)
}
}
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/LogInActivity.kt
View file @
638c1d44
...
...
@@ -12,15 +12,17 @@ import kotlinx.android.synthetic.main.activity_log_in.*
class
LogInActivity
:
AppCompatActivity
()
{
private
var
mAuth
:
FirebaseAuth
?
=
null
private
var
email
:
String
?
=
null
private
var
password
:
String
?
=
null
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_log_in
)
mAuth
=
FirebaseAuth
.
getInstance
()
btnLogIn
!!
.
setOnClickListener
({
logIn
()
})
btnSignUp
!!
.
setOnClickListener
({
signUp
()
})
btnRandomUser
!!
.
setOnClickListener
({
createRandomUser
()
})
btnLogIn
!!
.
setOnClickListener
{
login
()
}
btnSignUp
!!
.
setOnClickListener
{
signUp
()
}
btnRandomUser
!!
.
setOnClickListener
{
createRandomUser
()
}
}
override
fun
onStart
()
{
...
...
@@ -28,43 +30,22 @@ class LogInActivity : AppCompatActivity() {
if
(
mAuth
!!
.
currentUser
!=
null
)
{
startUserActivity
()
}
}
private
fun
logIn
()
{
val
email
=
etEmail
!!
.
text
.
toString
()
val
password
=
etPassword
!!
.
text
.
toString
()
if
(
fieldsNotEmpty
(
email
,
password
,
"Please, enter email and password"
,
this
))
{
showProgressBar
()
mAuth
!!
.
signInWithEmailAndPassword
(
email
,
password
)
.
addOnCompleteListener
{
hideProgressBar
()
}
.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed in"
)
startUserActivity
()
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"signInWithEmail:failure"
,
it
)
shortToast
(
this
@LogInActivity
,
getString
(
R
.
string
.
toast_auth_failed
))
}
}
}
fun
login
()
{
processUser
{
login
(
{
removeProgressBar
()
},
{
startUserActivity
()
},
{
showToastFailure
()
},
mAuth
,
email
!!
,
password
!!
)
}
}
private
fun
signUp
()
{
processUser
{
signUp
(
mAuth
,
this
,
email
!!
,
password
!!
)
}
}
private
fun
signUp
()
{
val
email
=
etEmail
!!
.
text
.
toString
()
val
password
=
etPassword
!!
.
text
.
toString
()
if
(
fieldsNotEmpty
(
email
,
password
,
"Please, enter email and password"
,
this
))
{
//todo check if the password is good
// todo verify email
showProgressBar
()
mAuth
!!
.
createUserWithEmailAndPassword
(
email
,
password
)
.
addOnCompleteListener
{
hideProgressBar
()
}
.
addOnSuccessListener
{
_
->
Log
.
d
(
TAG
,
"Successfully signed up a new user"
)
UserManager
.
addNewUserToDb
(
mAuth
!!
.
currentUser
!!
,
this
)
}
.
addOnFailureListener
{
Log
.
d
(
TAG
,
"createUserWithEmail:failure"
,
it
.
fillInStackTrace
())
shortToast
(
this
@LogInActivity
,
it
.
message
!!
)
}
private
fun
processUser
(
authAction
:
()
->
Unit
)
{
email
=
etEmail
!!
.
text
.
toString
()
password
=
etPassword
!!
.
text
.
toString
()
if
(
fieldsNotEmpty
(
email
!!
,
password
!!
,
"Please, enter email and password"
,
this
))
{
addProgressBar
()
authAction
()
}
}
...
...
@@ -74,15 +55,19 @@ class LogInActivity : AppCompatActivity() {
startActivity
(
userActivityIntent
)
}
private
fun
showProgressBar
()
{
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_login
)
}
private
fun
addProgressBar
()
{
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_login
)
}
private
fun
removeProgressBar
()
{
removeProgressBar
(
supportFragmentManager
)
}
private
fun
hideProgressBar
()
{
removeProgressBar
(
supportFragmentManager
)
}
private
fun
showToastFailure
()
{
shortToast
(
this
@LogInActivity
,
getString
(
R
.
string
.
toast_auth_failed
))
}
@SuppressLint
(
"SetTextI18n"
)
private
fun
createRandomUser
()
{
etEmail
.
setText
(
"random@gmail.com"
)
etPassword
.
setText
(
"123456"
)
signUp
()
processUser
{
signUp
(
mAuth
,
this
@LogInActivity
,
email
!!
,
password
!!
)
}
}
companion
object
{
private
val
TAG
=
"VN/"
+
LogInActivity
::
class
.
simpleName
}
...
...
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