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