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
38842264
authored
Apr 29, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in saveWords method
parent
646fc562
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
20 deletions
app/src/main/java/com/paktalin/vocabularynotebook/utils/FirestoreManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/vocabulary/ModifiedVocabulary.kt
app/src/main/java/com/paktalin/vocabularynotebook/utils/FirestoreManager.kt
View file @
38842264
...
@@ -9,6 +9,7 @@ import com.paktalin.vocabularynotebook.firestoreitems.UserPojo
...
@@ -9,6 +9,7 @@ import com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import
com.paktalin.vocabularynotebook.firestoreitems.VocabularyPojo
import
com.paktalin.vocabularynotebook.firestoreitems.VocabularyPojo
import
com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import
com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import
com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import
com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import
com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary.Label
import
java.util.*
import
java.util.*
class
FirestoreManager
{
class
FirestoreManager
{
...
@@ -46,19 +47,14 @@ class FirestoreManager {
...
@@ -46,19 +47,14 @@ class FirestoreManager {
}
}
}
}
fun
saveWords
(
wordMap
:
MutableMap
<
WordPojo
,
Boolean
>)
{
fun
saveWords
(
wordMap
:
MutableMap
<
Label
,
MutableList
<
WordPojo
>>)
{
if
(
wordMap
.
isEmpty
())
if
(
wordMap
.
isEmpty
())
return
return
val
batch
=
db
.
batch
()
val
batch
=
db
.
batch
()
wordMap
[
Label
.
DELETED
]
?.
forEach
{
w
->
batch
.
delete
(
wordsCollection
.
document
(
w
.
id
!!
))
}
wordMap
.
forEach
{
wordMap
[
Label
.
EDITED
]
?.
forEach
{
w
->
batch
.
set
(
wordsCollection
.
document
(
w
.
id
!!
),
w
)
}
entry
->
run
{
wordMap
[
Label
.
DELETED
]
?.
clear
()
if
(
entry
.
value
)
batch
.
delete
(
wordsCollection
.
document
(
entry
.
key
.
id
!!
))
wordMap
[
Label
.
EDITED
]
?.
clear
()
else
batch
.
set
(
wordsCollection
.
document
(
entry
.
key
.
id
!!
),
entry
.
key
)
batch
.
commit
()
}}
wordMap
.
clear
()
batch
.
commit
().
addOnCompleteListener
{
Log
.
d
(
TAG
,
"words are successfully pushed to Firestore"
)
}
}
}
fun
extractVocabulary
(
onComplete
:
(
querySnapshot
:
QuerySnapshot
?)
->
Unit
)
{
fun
extractVocabulary
(
onComplete
:
(
querySnapshot
:
QuerySnapshot
?)
->
Unit
)
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/vocabulary/ModifiedVocabulary.kt
View file @
38842264
...
@@ -4,30 +4,39 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
...
@@ -4,30 +4,39 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import
com.paktalin.vocabularynotebook.utils.FirestoreManager
import
com.paktalin.vocabularynotebook.utils.FirestoreManager
class
ModifiedVocabulary
:
Vocabulary
{
class
ModifiedVocabulary
:
Vocabulary
{
var
wordMap
=
mutableMapOf
<
WordPojo
,
Boolean
>()
var
wordMap
=
mutableMapOf
(
Pair
(
Label
.
DELETED
,
mutableListOf
<
WordPojo
>()),
Pair
(
Label
.
EDITED
,
mutableListOf
()))
private
val
maxPermitted
=
9
private
val
maxPermitted
=
9
private
var
currentSize
=
0
override
fun
addWord
(
newWord
:
WordPojo
)
{
override
fun
addWord
(
newWord
:
WordPojo
)
{
add
(
newWord
,
false
)
add
(
newWord
,
Label
.
EDITED
)
}
}
override
fun
updateWord
(
updatedWord
:
WordPojo
)
{
override
fun
updateWord
(
updatedWord
:
WordPojo
)
{
add
(
updatedWord
,
false
)
add
(
updatedWord
,
Label
.
EDITED
)
}
}
override
fun
deleteWord
(
wordPojo
:
WordPojo
)
{
override
fun
deleteWord
(
wordPojo
:
WordPojo
)
{
add
(
wordPojo
,
true
)
add
(
wordPojo
,
Label
.
DELETED
)
}
}
override
fun
addAll
(
words
:
MutableList
<
WordPojo
>)
{
}
override
fun
addAll
(
words
:
MutableList
<
WordPojo
>)
{
}
private
fun
add
(
wordPojo
:
WordPojo
,
deleted
:
Boolean
)
{
private
fun
add
(
wordPojo
:
WordPojo
,
label
:
Label
)
{
wordMap
[
wordPojo
]
=
deleted
wordMap
[
label
]
?.
add
(
wordPojo
)
if
(
wordMap
.
size
==
maxPermitted
)
currentSize
++
if
(
currentSize
>
maxPermitted
)
{
currentSize
=
0
FirestoreManager
().
saveWords
(
wordMap
)
FirestoreManager
().
saveWords
(
wordMap
)
}
}
}
fun
get
():
MutableMap
<
WordPojo
,
Boolean
>
{
fun
get
():
MutableMap
<
Label
,
MutableList
<
WordPojo
>
>
{
return
wordMap
return
wordMap
}
}
enum
class
Label
{
DELETED
,
EDITED
}
}
}
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