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
6737ef4b
authored
Nov 11, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring
parent
350ce549
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
122 additions
and
0 deletions
app/src/main/java/com/paktalin/vocabularynotebook/utils/ActivityUtil.kt
app/src/main/java/com/paktalin/vocabularynotebook/utils/UserManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/utils/ActivityUtil.kt
0 → 100644
View file @
6737ef4b
package
com.paktalin.vocabularynotebook.utils
import
android.content.Context
import
android.os.Bundle
import
android.support.v4.app.Fragment
import
android.support.v4.app.FragmentManager
import
android.text.TextUtils
import
android.widget.Toast
import
com.paktalin.vocabularynotebook.ui.activities.MainActivity
import
com.paktalin.vocabularynotebook.ui.fragments.ProgressFragment
import
kotlinx.android.synthetic.main.content_main.*
val
progressFragment
:
Fragment
=
ProgressFragment
()
fun
addFragment
(
fragmentManager
:
FragmentManager
,
fragment
:
Fragment
,
containerId
:
Int
,
arguments
:
Bundle
?)
{
fragment
.
arguments
=
arguments
fragmentManager
.
beginTransaction
().
add
(
containerId
,
fragment
).
commitAllowingStateLoss
()
}
fun
removeFragment
(
fragmentManager
:
FragmentManager
,
fragment
:
Fragment
)
{
fragmentManager
.
beginTransaction
().
remove
(
fragment
).
commitAllowingStateLoss
()
}
fun
addProgressBar
(
fragmentManager
:
FragmentManager
,
containerId
:
Int
)
{
addFragment
(
fragmentManager
,
progressFragment
,
containerId
,
null
)
}
fun
removeProgressBar
(
fragmentManager
:
FragmentManager
)
{
removeFragment
(
fragmentManager
,
progressFragment
)
}
fun
fieldsNotEmpty
(
text1
:
String
,
text2
:
String
,
toastMessage
:
String
,
context
:
Context
):
Boolean
{
if
(
TextUtils
.
isEmpty
(
text1
)
||
TextUtils
.
isEmpty
(
text2
))
{
shortToast
(
context
,
toastMessage
)
return
false
}
return
true
}
fun
disableScrolling
(
mainActivity
:
MainActivity
)
{
mainActivity
.
scrollView
!!
.
setScrollingEnabled
(
false
)
}
fun
enableScrolling
(
mainActivity
:
MainActivity
)
{
mainActivity
.
scrollView
!!
.
setScrollingEnabled
(
true
)
}
fun
shortToast
(
context
:
Context
,
text
:
String
)
{
Toast
.
makeText
(
context
,
text
,
Toast
.
LENGTH_SHORT
).
show
()
}
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/utils/UserManager.kt
0 → 100644
View file @
6737ef4b
package
com.paktalin.vocabularynotebook.utils
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
import
com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import
com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import
com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import
java.util.*
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
!!
)
}
}
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