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
81792b36
authored
Nov 11, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in FirestoreManager
parent
f3574940
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
57 additions
and
41 deletions
.idea/dictionaries/Paktalin.xml
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/Vocabulary.kt
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/dictionaries/Paktalin.xml
0 → 100644
View file @
81792b36
<component
name=
"ProjectDictionaryState"
>
<dictionary
name=
"Paktalin"
>
<words>
<w>
firebase
</w>
<w>
firestore
</w>
</words>
</dictionary>
</component>
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/Vocabulary.kt
View file @
81792b36
...
@@ -7,9 +7,6 @@ class Vocabulary(words: MutableList<WordItem>) {
...
@@ -7,9 +7,6 @@ class Vocabulary(words: MutableList<WordItem>) {
private
const
val
SORT_BY_TIME
=
0
private
const
val
SORT_BY_TIME
=
0
private
const
val
SORT_BY_WORD
=
1
private
const
val
SORT_BY_WORD
=
1
private
const
val
SORT_BY_TRANSLATION
=
2
private
const
val
SORT_BY_TRANSLATION
=
2
const
val
WORDS
=
"words"
const
val
VOCABULARIES
=
"vocabularies"
}
}
var
pojo
:
Pojo
var
pojo
:
Pojo
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/LogInActivity.kt
View file @
81792b36
...
@@ -67,7 +67,7 @@ class LogInActivity : AppCompatActivity() {
...
@@ -67,7 +67,7 @@ class LogInActivity : AppCompatActivity() {
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
(
this
@LogInActivity
,
email
!!
,
password
!!
)
}
processUser
{
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 @
81792b36
...
@@ -2,10 +2,8 @@ package com.paktalin.vocabularynotebook.ui.activities
...
@@ -2,10 +2,8 @@ package com.paktalin.vocabularynotebook.ui.activities
import
android.os.Bundle
import
android.os.Bundle
import
android.support.v7.app.AppCompatActivity
import
android.support.v7.app.AppCompatActivity
import
android.util.Log
import
kotlinx.android.synthetic.main.activity_main.*
import
kotlinx.android.synthetic.main.activity_main.*
import
android.view.WindowManager
import
android.app.Activity
import
android.app.Activity
import
android.view.Menu
import
android.view.Menu
import
android.view.MenuItem
import
android.view.MenuItem
...
@@ -13,12 +11,10 @@ import android.view.View
...
@@ -13,12 +11,10 @@ import android.view.View
import
android.view.inputmethod.InputMethodManager
import
android.view.inputmethod.InputMethodManager
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
android.view.WindowManager
import
com.paktalin.vocabularynotebook.*
import
com.paktalin.vocabularynotebook.*
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.*
import
com.paktalin.vocabularynotebook.utils.extractVocabularyData
import
com.paktalin.vocabularynotebook.utils.shortToast
import
com.paktalin.vocabularynotebook.utils.startActivity
class
MainActivity
:
AppCompatActivity
()
{
class
MainActivity
:
AppCompatActivity
()
{
...
@@ -64,7 +60,7 @@ class MainActivity : AppCompatActivity() {
...
@@ -64,7 +60,7 @@ class MainActivity : AppCompatActivity() {
private
fun
extractVocabularyData
()
{
private
fun
extractVocabularyData
()
{
addProgressBar
()
addProgressBar
()
extractVocabularyData
(
FirestoreManager
().
extractVocabularyData
(
{
id
->
addVocabularyFragment
(
id
)
},
{
id
->
addVocabularyFragment
(
id
)
},
{
showToastNoWords
()
},
{
showToastNoWords
()
},
{
removeProgressBar
()
})
{
removeProgressBar
()
})
...
...
app/src/main/java/com/paktalin/vocabularynotebook/utils/FirestoreManager.kt
View file @
81792b36
...
@@ -12,53 +12,66 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
...
@@ -12,53 +12,66 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import
com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import
com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import
java.util.*
import
java.util.*
private
const
val
USERS
=
"users"
class
FirestoreManager
{
private
const
val
TAG
=
"VN/FirestoreManager"
fun
extractVocabularyData
(
onSuccess
:
(
vocabularyId
:
String
)
->
Unit
,
private
val
db
:
FirebaseFirestore
=
ConfiguredFirestore
.
instance
fun
extractVocabularyData
(
onSuccess
:
(
vocabularyId
:
String
)
->
Unit
,
showToastNoWords
:
()
->
Unit
,
removeProgressBar
:
()
->
Unit
)
{
showToastNoWords
:
()
->
Unit
,
removeProgressBar
:
()
->
Unit
)
{
val
userId
=
FirebaseAuth
.
getInstance
()
!!
.
currentUser
!!
.
uid
val
userId
=
FirebaseAuth
.
getInstance
()
!!
.
currentUser
!!
.
uid
val
db
=
ConfiguredFirestore
.
instance
userDocument
(
userId
).
get
()
val
userDocument
=
db
.
collection
(
USERS
).
document
(
userId
)
userDocument
.
get
()
.
addOnSuccessListener
{
task
->
.
addOnSuccessListener
{
task
->
removeProgressBar
()
removeProgressBar
()
if
(
task
.
get
(
Vocabulary
.
VOCABULARIES
)
!=
null
)
{
if
(
task
.
get
(
VOCABULARIES
)
!=
null
)
{
val
vocabularyId
=
retrieveVocabularyID
(
task
,
db
)
val
vocabularyId
=
retrieveVocabularyID
(
task
,
db
)
onSuccess
(
vocabularyId
)
onSuccess
(
vocabularyId
)
}
else
{
}
else
{
Log
.
w
(
TAG
,
"There's no collection \"vocabularies\""
)
Log
.
w
(
TAG
,
"There's no collection \"vocabularies\""
)
showToastNoWords
()
}
}
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
(
new
User
:
FirebaseUser
,
logInActivity
:
LogInActivity
)
{
fun
addNewUserToDb
(
firebase
User
:
FirebaseUser
,
logInActivity
:
LogInActivity
)
{
//todo add condition to writing to the db in Firebase Console (request.auth.uid)
//todo add condition to writing to the db in Firebase Console (request.auth.uid)
val
db
=
ConfiguredFirestore
.
instance
db
.
collection
(
VOCABULARIES
).
add
(
Vocabulary
.
Pojo
(
null
))
val
user
=
UserPojo
(
newUser
.
email
)
db
.
collection
(
Vocabulary
.
VOCABULARIES
).
add
(
Vocabulary
.
Pojo
(
null
))
.
addOnSuccessListener
{
firstVocabularyRef
->
.
addOnSuccessListener
{
firstVocabularyRef
->
Log
.
d
(
TAG
,
"VocabularyPojo successfully created: "
+
firstVocabularyRef
.
path
)
Log
.
d
(
TAG
,
"VocabularyPojo successfully created: "
+
firstVocabularyRef
.
path
)
user
.
vocabularies
=
Collections
.
singletonList
(
firstVocabularyRef
)
setNewUserWithVocabularyData
(
firebaseUser
,
firstVocabularyRef
,
logInActivity
)
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"Couldn't add user to the database"
,
it
.
cause
)
deleteUser
(
firebaseUser
)
}
}
private
fun
userDocument
(
userId
:
String
):
DocumentReference
{
return
db
.
collection
(
USERS
).
document
(
userId
)
}
db
.
collection
(
"users"
).
document
(
newUser
.
uid
).
set
(
user
)
private
fun
setNewUserWithVocabularyData
(
firebaseUser
:
FirebaseUser
,
.
addOnCompleteListener
{
task
->
firstVocabularyRef
:
DocumentReference
,
if
(
task
.
isSuccessful
)
{
logInActivity
:
LogInActivity
)
{
val
user
=
UserPojo
(
firebaseUser
.
email
)
user
.
vocabularies
=
Collections
.
singletonList
(
firstVocabularyRef
)
userDocument
(
firebaseUser
.
uid
).
set
(
user
)
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully added user to the collection"
)
Log
.
i
(
TAG
,
"Successfully added user to the collection"
)
logInActivity
.
startUserActivity
()
logInActivity
.
startUserActivity
()
}
else
Log
.
w
(
TAG
,
"addUser:failure"
,
task
.
exception
)
}
}
.
addOnFailureListener
{
exception
->
Log
.
w
(
TAG
,
"addUser:failure"
,
exception
)
}
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"Couldn't add user to the database"
,
it
.
cause
)
deleteUser
(
newUser
)
}
}
}
private
fun
retrieveVocabularyID
(
task
:
DocumentSnapshot
,
db
:
FirebaseFirestore
):
String
{
val
vocabularies
:
List
<
DocumentReference
>
=
task
.
get
(
VOCABULARIES
)
as
List
<
DocumentReference
>
val
vocabulary
=
db
.
collection
(
VOCABULARIES
).
document
(
vocabularies
[
0
].
id
)
return
vocabulary
.
id
}
companion
object
{
private
const
val
USERS
=
"users"
private
const
val
WORDS
=
"words"
private
const
val
VOCABULARIES
=
"vocabularies"
private
const
val
TAG
=
"VN/FirestoreManager"
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/utils/UserManager.kt
View file @
81792b36
...
@@ -33,7 +33,7 @@ fun signUp(activity: LogInActivity, email: String, password: String) {
...
@@ -33,7 +33,7 @@ fun signUp(activity: LogInActivity, email: String, password: String) {
.
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
)
FirestoreManager
().
addNewUserToDb
(
mAuth
!!
.
currentUser
!!
,
activity
)
activity
.
login
()
activity
.
login
()
}
}
.
addOnFailureListener
{
.
addOnFailureListener
{
...
...
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