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
757b3b59
authored
Nov 11, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved vocabulary data extraction to util class
parent
6737ef4b
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
104 additions
and
70 deletions
.idea/misc.xml
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/utils/FirestoreManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/utils/UserManager.kt
.idea/misc.xml
View file @
757b3b59
...
@@ -25,7 +25,7 @@
...
@@ -25,7 +25,7 @@
</value>
</value>
</option>
</option>
</component>
</component>
<component
name=
"ProjectRootManager"
version=
"2"
languageLevel=
"JDK_1_
8
"
project-jdk-name=
"1.8"
project-jdk-type=
"JavaSDK"
>
<component
name=
"ProjectRootManager"
version=
"2"
languageLevel=
"JDK_1_
7
"
project-jdk-name=
"1.8"
project-jdk-type=
"JavaSDK"
>
<output
url=
"file://$PROJECT_DIR$/build/classes"
/>
<output
url=
"file://$PROJECT_DIR$/build/classes"
/>
</component>
</component>
<component
name=
"ProjectType"
>
<component
name=
"ProjectType"
>
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/LogInActivity.kt
View file @
757b3b59
...
@@ -6,22 +6,20 @@ import android.support.v7.app.AppCompatActivity
...
@@ -6,22 +6,20 @@ import android.support.v7.app.AppCompatActivity
import
android.os.Bundle
import
android.os.Bundle
import
android.util.Log
import
android.util.Log
import
com.google.firebase.auth.FirebaseAuth
import
com.paktalin.vocabularynotebook.*
import
com.paktalin.vocabularynotebook.*
import
com.paktalin.vocabularynotebook.utils.fieldsNotEmpty
import
com.paktalin.vocabularynotebook.utils.fieldsNotEmpty
import
com.paktalin.vocabularynotebook.utils.shortToast
import
com.paktalin.vocabularynotebook.utils.shortToast
import
com.paktalin.vocabularynotebook.utils.signUp
import
com.paktalin.vocabularynotebook.utils.userLoggedIn
import
kotlinx.android.synthetic.main.activity_log_in.*
import
kotlinx.android.synthetic.main.activity_log_in.*
class
LogInActivity
:
AppCompatActivity
()
{
class
LogInActivity
:
AppCompatActivity
()
{
private
var
mAuth
:
FirebaseAuth
?
=
null
private
var
email
:
String
?
=
null
private
var
email
:
String
?
=
null
private
var
password
:
String
?
=
null
private
var
password
:
String
?
=
null
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
)
mAuth
=
FirebaseAuth
.
getInstance
()
btnLogIn
!!
.
setOnClickListener
{
login
()
}
btnLogIn
!!
.
setOnClickListener
{
login
()
}
btnSignUp
!!
.
setOnClickListener
{
signUp
()
}
btnSignUp
!!
.
setOnClickListener
{
signUp
()
}
btnRandomUser
!!
.
setOnClickListener
{
createRandomUser
()
}
btnRandomUser
!!
.
setOnClickListener
{
createRandomUser
()
}
...
@@ -29,7 +27,7 @@ class LogInActivity : AppCompatActivity() {
...
@@ -29,7 +27,7 @@ class LogInActivity : AppCompatActivity() {
override
fun
onStart
()
{
override
fun
onStart
()
{
super
.
onStart
()
super
.
onStart
()
if
(
mAuth
!!
.
currentUser
!=
null
)
{
startUserActivity
()
}
if
(
userLoggedIn
()
)
{
startUserActivity
()
}
}
}
fun
login
()
{
fun
login
()
{
...
@@ -38,10 +36,10 @@ class LogInActivity : AppCompatActivity() {
...
@@ -38,10 +36,10 @@ class LogInActivity : AppCompatActivity() {
{
removeProgressBar
()
},
{
removeProgressBar
()
},
{
startUserActivity
()
},
{
startUserActivity
()
},
{
showToastFailure
()
},
{
showToastFailure
()
},
mAuth
,
email
!!
,
password
!!
)
email
!!
,
password
!!
)
}
}
}
}
private
fun
signUp
()
{
processUser
{
com
.
paktalin
.
vocabularynotebook
.
utils
.
signUp
(
mAuth
,
this
,
email
!!
,
password
!!
)
}
}
private
fun
signUp
()
{
processUser
{
signUp
(
this
,
email
!!
,
password
!!
)
}
}
private
fun
processUser
(
authAction
:
()
->
Unit
)
{
private
fun
processUser
(
authAction
:
()
->
Unit
)
{
email
=
etEmail
!!
.
text
.
toString
()
email
=
etEmail
!!
.
text
.
toString
()
...
@@ -67,15 +65,13 @@ class LogInActivity : AppCompatActivity() {
...
@@ -67,15 +65,13 @@ class LogInActivity : AppCompatActivity() {
com
.
paktalin
.
vocabularynotebook
.
utils
.
removeProgressBar
(
supportFragmentManager
)
com
.
paktalin
.
vocabularynotebook
.
utils
.
removeProgressBar
(
supportFragmentManager
)
}
}
private
fun
showToastFailure
()
{
private
fun
showToastFailure
()
{
shortToast
(
this
@LogInActivity
,
getString
(
R
.
string
.
toast_auth_failed
))
}
shortToast
(
this
@LogInActivity
,
getString
(
R
.
string
.
toast_auth_failed
))
}
@SuppressLint
(
"SetTextI18n"
)
@SuppressLint
(
"SetTextI18n"
)
private
fun
createRandomUser
()
{
private
fun
createRandomUser
()
{
etEmail
.
setText
(
"random@gmail.com"
)
etEmail
.
setText
(
"random@gmail.com"
)
etPassword
.
setText
(
"123456"
)
etPassword
.
setText
(
"123456"
)
processUser
{
com
.
paktalin
.
vocabularynotebook
.
utils
.
signUp
(
mAuth
,
this
@LogInActivity
,
email
!!
,
password
!!
)
}
processUser
{
com
.
paktalin
.
vocabularynotebook
.
utils
.
signUp
(
this
@LogInActivity
,
email
!!
,
password
!!
)
}
}
}
companion
object
{
private
val
TAG
=
"VN/"
+
LogInActivity
::
class
.
simpleName
}
companion
object
{
private
val
TAG
=
"VN/"
+
LogInActivity
::
class
.
simpleName
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
View file @
757b3b59
...
@@ -5,7 +5,6 @@ import android.os.Bundle
...
@@ -5,7 +5,6 @@ import android.os.Bundle
import
android.support.v7.app.AppCompatActivity
import
android.support.v7.app.AppCompatActivity
import
android.util.Log
import
android.util.Log
import
com.google.firebase.auth.FirebaseAuth
import
com.google.firebase.auth.FirebaseAuth
import
com.google.firebase.firestore.DocumentReference
import
kotlinx.android.synthetic.main.activity_main.*
import
kotlinx.android.synthetic.main.activity_main.*
import
android.view.WindowManager
import
android.view.WindowManager
...
@@ -14,13 +13,12 @@ import android.view.Menu
...
@@ -14,13 +13,12 @@ import android.view.Menu
import
android.view.MenuItem
import
android.view.MenuItem
import
android.view.View
import
android.view.View
import
android.view.inputmethod.InputMethodManager
import
android.view.inputmethod.InputMethodManager
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
kotlinx.android.synthetic.main.fragment_vocabulary.*
import
kotlinx.android.synthetic.main.fragment_vocabulary.*
import
android.support.v7.widget.SearchView
import
android.support.v7.widget.SearchView
import
com.paktalin.vocabularynotebook.*
import
com.paktalin.vocabularynotebook.*
import
com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import
com.paktalin.vocabularynotebook.ui.fragments.VocabularyFragment
import
com.paktalin.vocabularynotebook.ui.fragments.VocabularyFragment
import
com.paktalin.vocabularynotebook.utils.addFragment
import
com.paktalin.vocabularynotebook.utils.addFragment
import
com.paktalin.vocabularynotebook.utils.extractVocabularyData
import
com.paktalin.vocabularynotebook.utils.shortToast
import
com.paktalin.vocabularynotebook.utils.shortToast
class
MainActivity
:
AppCompatActivity
()
{
class
MainActivity
:
AppCompatActivity
()
{
...
@@ -59,36 +57,28 @@ class MainActivity : AppCompatActivity() {
...
@@ -59,36 +57,28 @@ 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
)
{
logOut
()
}
if
(
menuItem
.
itemId
==
R
.
id
.
logOut
)
{
logOut
()
}
drawerLayout
!!
.
closeDrawers
()
drawerLayout
!!
.
closeDrawers
()
true
true
}
}
}
}
private
fun
extractVocabularyData
()
{
private
fun
extractVocabularyData
()
{
val
userId
=
FirebaseAuth
.
getInstance
()
!!
.
currentUser
!!
.
uid
val
db
=
ConfiguredFirestore
.
instance
val
userDocument
=
db
.
collection
(
"users"
).
document
(
userId
)
addProgressBar
()
addProgressBar
()
userDocument
.
get
()
extractVocabularyData
(
.
addOnSuccessListener
{
task
->
{
id
->
addVocabularyFragment
(
id
)
},
removeProgressBar
()
{
showToastNoWords
()
},
//todo move Firestore logic and collections names to a separate class
{
removeProgressBar
()
})
if
(
task
.
get
(
VOCABULARIES
)
!=
null
)
{
}
val
vocabularies
:
List
<
DocumentReference
>
=
task
.
get
(
VOCABULARIES
)
as
List
<
DocumentReference
>
val
vocabulary
=
db
.
collection
(
VOCABULARIES
).
document
(
vocabularies
[
0
].
id
)
private
fun
addVocabularyFragment
(
vocabularyId
:
String
)
{
vocabularyId
=
vocabulary
.
id
this
.
vocabularyId
=
vocabularyId
vocabularyFragment
=
VocabularyFragment
()
// start VocabularyFragment
val
arguments
=
Bundle
()
vocabularyFragment
=
VocabularyFragment
()
arguments
.
putString
(
"vocabularyId"
,
vocabularyId
)
val
arguments
=
Bundle
()
addFragment
(
supportFragmentManager
,
vocabularyFragment
,
R
.
id
.
container_vocabulary
,
arguments
)
arguments
.
putString
(
"vocabularyId"
,
vocabularyId
)
addFragment
(
supportFragmentManager
,
vocabularyFragment
,
R
.
id
.
container_vocabulary
,
arguments
)
}
else
{
Log
.
w
(
TAG
,
"There's no collection \"vocabularies\""
)
showToastNoWords
()
}
}
}
}
private
fun
hideKeyboard
()
{
private
fun
hideKeyboard
()
{
...
@@ -98,7 +88,9 @@ class MainActivity : AppCompatActivity() {
...
@@ -98,7 +88,9 @@ class MainActivity : AppCompatActivity() {
fun
hideKeyboardNotFromActivity
(
activity
:
Activity
)
{
fun
hideKeyboardNotFromActivity
(
activity
:
Activity
)
{
val
imm
=
activity
.
getSystemService
(
Activity
.
INPUT_METHOD_SERVICE
)
as
InputMethodManager
val
imm
=
activity
.
getSystemService
(
Activity
.
INPUT_METHOD_SERVICE
)
as
InputMethodManager
var
view
=
activity
.
currentFocus
var
view
=
activity
.
currentFocus
if
(
view
==
null
)
{
view
=
View
(
activity
)
}
if
(
view
==
null
)
{
view
=
View
(
activity
)
}
imm
.
hideSoftInputFromWindow
(
view
.
windowToken
,
0
)
imm
.
hideSoftInputFromWindow
(
view
.
windowToken
,
0
)
}
}
...
@@ -119,5 +111,7 @@ class MainActivity : AppCompatActivity() {
...
@@ -119,5 +111,7 @@ class MainActivity : AppCompatActivity() {
hideKeyboard
()
hideKeyboard
()
}
}
companion
object
{
private
val
TAG
=
"VN/"
+
MainActivity
::
class
.
simpleName
}
companion
object
{
private
val
TAG
=
"VN/"
+
MainActivity
::
class
.
simpleName
}
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/utils/FirestoreManager.kt
View file @
757b3b59
package
com.paktalin.vocabularynotebook.utils
package
com.paktalin.vocabularynotebook.utils
import
android.util.Log
import
com.google.firebase.auth.FirebaseAuth
import
com.google.firebase.auth.FirebaseUser
import
com.google.firebase.firestore.DocumentReference
import
com.google.firebase.firestore.DocumentSnapshot
import
com.google.firebase.firestore.FirebaseFirestore
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import
com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import
com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import
java.util.*
private
const
val
USERS
=
"users"
private
const
val
TAG
=
"VN/FirestoreManager"
fun
extractVocabularyData
(
onSuccess
:
(
vocabularyId
:
String
)
->
Unit
,
showToastNoWords
:
()
->
Unit
,
removeProgressBar
:
()
->
Unit
)
{
val
userId
=
FirebaseAuth
.
getInstance
()
!!
.
currentUser
!!
.
uid
val
db
=
ConfiguredFirestore
.
instance
val
userDocument
=
db
.
collection
(
USERS
).
document
(
userId
)
userDocument
.
get
()
.
addOnSuccessListener
{
task
->
removeProgressBar
()
if
(
task
.
get
(
Vocabulary
.
VOCABULARIES
)
!=
null
)
{
val
vocabularyId
=
retrieveVocabularyID
(
task
,
db
)
onSuccess
(
vocabularyId
)
}
else
{
Log
.
w
(
TAG
,
"There's no collection \"vocabularies\""
)
showToastNoWords
()
}
}
}
private
fun
retrieveVocabularyID
(
task
:
DocumentSnapshot
,
db
:
FirebaseFirestore
):
String
{
val
vocabularies
:
List
<
DocumentReference
>
=
task
.
get
(
Vocabulary
.
VOCABULARIES
)
as
List
<
DocumentReference
>
val
vocabulary
=
db
.
collection
(
Vocabulary
.
VOCABULARIES
).
document
(
vocabularies
[
0
].
id
)
return
vocabulary
.
id
}
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
(
Vocabulary
.
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
)
}
}
app/src/main/java/com/paktalin/vocabularynotebook/utils/UserManager.kt
View file @
757b3b59
...
@@ -11,39 +11,16 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCAB
...
@@ -11,39 +11,16 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCAB
import
java.util.*
import
java.util.*
private
const
val
TAG
=
"VN/UserManager"
private
const
val
TAG
=
"VN/UserManager"
private
var
mAuth
:
FirebaseAuth
?
=
FirebaseAuth
.
getInstance
()
private
fun
deleteUser
(
user
:
FirebaseUser
)
{
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
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
,
fun
login
(
onComplete
:
()
->
Unit
,
onSuccess
:
()
->
Unit
,
onFailure
:
()
->
Unit
,
mAuth
:
FirebaseAuth
?,
email
:
String
,
password
:
String
)
{
email
:
String
,
password
:
String
)
{
mAuth
!!
.
signInWithEmailAndPassword
(
email
,
password
)
mAuth
!!
.
signInWithEmailAndPassword
(
email
,
password
)
.
addOnCompleteListener
{
onComplete
()
}
.
addOnCompleteListener
{
onComplete
()
}
.
addOnSuccessListener
{
.
addOnSuccessListener
{
...
@@ -56,16 +33,19 @@ fun login(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
...
@@ -56,16 +33,19 @@ fun login(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
}
}
}
}
fun
signUp
(
mAuth
:
FirebaseAuth
?,
activity
:
LogInActivity
,
email
:
String
,
password
:
String
)
{
fun
signUp
(
activity
:
LogInActivity
,
email
:
String
,
password
:
String
)
{
mAuth
!!
.
createUserWithEmailAndPassword
(
email
,
password
)
mAuth
!!
.
createUserWithEmailAndPassword
(
email
,
password
)
.
addOnCompleteListener
{
removeProgressBar
(
activity
.
supportFragmentManager
)
}
.
addOnCompleteListener
{
removeProgressBar
(
activity
.
supportFragmentManager
)
}
.
addOnSuccessListener
{
.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed up a new user"
)
Log
.
d
(
TAG
,
"Successfully signed up a new user"
)
addNewUserToDb
(
mAuth
.
currentUser
!!
,
activity
)
addNewUserToDb
(
mAuth
!!
.
currentUser
!!
,
activity
)
activity
.
login
()
activity
.
login
()
}
}
.
addOnFailureListener
{
.
addOnFailureListener
{
Log
.
d
(
TAG
,
"createUserWithEmail:failure"
,
it
.
fillInStackTrace
())
Log
.
d
(
TAG
,
"createUserWithEmail:failure"
,
it
.
fillInStackTrace
())
shortToast
(
activity
,
it
.
message
!!
)
shortToast
(
activity
,
it
.
message
!!
)
}
}
}
}
fun
userLoggedIn
():
Boolean
{
return
mAuth
!!
.
currentUser
!=
null
}
\ No newline at end of file
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