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
15af5219
authored
Nov 11, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished isolating firebase logic
parent
e6d52044
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
30 deletions
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/WordItem.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/fragments/VocabularyFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/utils/FirestoreManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/WordItem.kt
View file @
15af5219
package
com.paktalin.vocabularynotebook.firestoreitems
import
android.util.Log
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.VOCABULARIES
import
com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.WORDS
import
com.paktalin.vocabularynotebook.utils.FirestoreManager
import
java.io.Serializable
import
java.util.Date
...
...
@@ -20,10 +17,7 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
:
this
(
pojo
.
word
,
pojo
.
translation
,
pojo
.
time
,
id
,
vocabularyId
)
fun
delete
()
{
ConfiguredFirestore
.
instance
.
collection
(
VOCABULARIES
).
document
(
vocabularyId
)
.
collection
(
WORDS
).
document
(
id
).
delete
()
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully deleted word with id $id"
)
}
.
addOnFailureListener
{
e
->
Log
.
w
(
TAG
,
"deleteWordWithId $id:failure"
,
e
.
fillInStackTrace
())
}
FirestoreManager
().
deleteWord
(
id
)
}
fun
contains
(
string
:
String
):
Boolean
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/fragments/VocabularyFragment.kt
View file @
15af5219
...
...
@@ -8,15 +8,12 @@ import android.view.View
import
android.view.ViewGroup
import
com.google.firebase.Timestamp
import
com.google.firebase.firestore.DocumentSnapshot
import
com.google.firebase.firestore.Query
import
com.paktalin.vocabularynotebook.*
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
com.paktalin.vocabularynotebook.ui.activities.MainActivity
import
com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import
com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.VOCABULARIES
import
com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.WORDS
import
com.paktalin.vocabularynotebook.utils.FirestoreManager
import
kotlinx.android.synthetic.main.fragment_vocabulary.*
class
VocabularyFragment
:
Fragment
()
{
...
...
@@ -24,7 +21,6 @@ class VocabularyFragment : Fragment() {
private
val
TAG
=
"VN/"
+
VocabularyFragment
::
class
.
simpleName
}
private
val
db
=
ConfiguredFirestore
.
instance
private
lateinit
var
mainActivity
:
MainActivity
override
fun
onCreateView
(
inflater
:
LayoutInflater
,
container
:
ViewGroup
?,
savedInstanceState
:
Bundle
?):
View
?
{
...
...
@@ -35,7 +31,7 @@ class VocabularyFragment : Fragment() {
super
.
onActivityCreated
(
savedInstanceState
)
mainActivity
=
activity
as
MainActivity
setEmptyAdapter
()
retrieve
WordsData
(
arguments
!!
[
"vocabularyId"
]
as
String
)
extract
WordsData
(
arguments
!!
[
"vocabularyId"
]
as
String
)
}
private
fun
setEmptyAdapter
()
{
...
...
@@ -46,17 +42,18 @@ class VocabularyFragment : Fragment() {
recyclerView
.
layoutManager
=
mLayoutManager
}
private
fun
retrieveWordsData
(
vocabularyId
:
String
)
{
db
.
collection
(
VOCABULARIES
).
document
(
vocabularyId
).
collection
(
WORDS
)
.
orderBy
(
"time"
,
Query
.
Direction
.
DESCENDING
)
.
get
()
.
addOnSuccessListener
{
if
(
it
.
documents
.
size
!=
0
)
setVocabularyAdapter
(
it
.
documents
,
vocabularyId
)
else
{
Log
.
i
(
TAG
,
"There are no documents in collection \"words\""
)
mainActivity
.
showToastNoWords
()
}}
private
fun
extractWordsData
(
vocabularyId
:
String
)
{
FirestoreManager
().
retrieveWordsData
({
documents
->
onSuccessfulWordDataExtraction
(
documents
,
vocabularyId
)},
vocabularyId
)
}
private
fun
onSuccessfulWordDataExtraction
(
documents
:
MutableList
<
DocumentSnapshot
>,
vocabularyId
:
String
)
{
if
(
documents
.
isNotEmpty
())
setVocabularyAdapter
(
documents
,
vocabularyId
)
else
{
Log
.
i
(
TAG
,
"There are no documents in collection \"words\""
)
mainActivity
.
showToastNoWords
()
}
}
private
fun
setVocabularyAdapter
(
documents
:
MutableList
<
DocumentSnapshot
>,
vocabularyId
:
String
)
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/utils/FirestoreManager.kt
View file @
15af5219
...
...
@@ -6,11 +6,13 @@ 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.google.firebase.firestore.Query
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import
com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import
com.paktalin.vocabularynotebook.ui.fragments.VocabularyFragment
import
java.util.*
class
FirestoreManager
{
...
...
@@ -74,6 +76,20 @@ class FirestoreManager {
}
}
fun
deleteWord
(
id
:
String
)
{
vocabularyDocument
(
vocabularyId
)
.
collection
(
WORDS
).
document
(
id
).
delete
()
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully deleted word with id $id"
)
}
.
addOnFailureListener
{
e
->
Log
.
w
(
TAG
,
"deleteWordWithId $id:failure"
,
e
.
fillInStackTrace
())
}
}
fun
retrieveWordsData
(
onSuccess
:
(
documents
:
MutableList
<
DocumentSnapshot
>)
->
Unit
,
vocabularyId
:
String
)
{
vocabularyDocument
(
vocabularyId
).
collection
(
WORDS
)
.
orderBy
(
"time"
,
Query
.
Direction
.
DESCENDING
)
.
get
()
.
addOnSuccessListener
{
onSuccess
(
it
.
documents
)}
}
private
fun
setNewUserWithVocabularyData
(
firebaseUser
:
FirebaseUser
,
firstVocabularyRef
:
DocumentReference
,
logInActivity
:
LogInActivity
)
{
...
...
@@ -99,14 +115,14 @@ class FirestoreManager {
return
db
.
collection
(
USERS
).
document
(
userId
)
}
private
fun
vocabularyDocument
(
i
d
:
String
):
DocumentReference
{
return
vocabularyCollection
.
document
(
i
d
)
private
fun
vocabularyDocument
(
vocabularyI
d
:
String
):
DocumentReference
{
return
vocabularyCollection
.
document
(
vocabularyI
d
)
}
companion
object
{
const
val
USERS
=
"users"
const
val
WORDS
=
"words"
const
val
VOCABULARIES
=
"vocabularies"
private
const
val
USERS
=
"users"
private
const
val
WORDS
=
"words"
private
const
val
VOCABULARIES
=
"vocabularies"
private
const
val
TAG
=
"VN/FirestoreManager"
lateinit
var
vocabularyId
:
String
...
...
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