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
41f1256d
authored
Nov 11, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added refresh by swipe
parent
36c1dfbf
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
0 deletions
app/src/main/java/com/paktalin/vocabularynotebook/Vocabulary.kt
app/src/main/java/com/paktalin/vocabularynotebook/Vocabulary.kt
0 → 100644
View file @
41f1256d
package
com.paktalin.vocabularynotebook
import
com.google.firebase.Timestamp
import
com.google.firebase.firestore.DocumentSnapshot
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.vocabularyId
class
Vocabulary
()
{
private
var
words
:
MutableList
<
WordItem
>
=
mutableListOf
()
constructor
(
words
:
MutableList
<
WordItem
>)
:
this
()
{
this
.
words
=
words
}
companion
object
{
private
val
TAG
=
"VN/"
+
Vocabulary
::
class
.
java
.
simpleName
private
const
val
SORT_BY_TIME
=
0
private
const
val
SORT_BY_WORD
=
1
private
const
val
SORT_BY_TRANSLATION
=
2
}
fun
sort
(
sortOrder
:
Int
)
{
when
(
sortOrder
)
{
SORT_BY_TIME
->
sortByTime
()
SORT_BY_WORD
->
sortByWord
()
SORT_BY_TRANSLATION
->
sortByTranslation
()
}
}
fun
deleteWord
(
position
:
Int
)
{
words
[
position
].
delete
()
// delete word from the database
words
.
removeAt
(
position
)
// delete word from the list
}
fun
addWord
(
newWord
:
WordItem
)
{
words
.
add
(
0
,
newWord
)
}
fun
addWords
(
newWords
:
MutableList
<
WordItem
>)
{
words
.
addAll
(
newWords
)
}
fun
addWordsAsDocuments
(
documents
:
MutableList
<
DocumentSnapshot
>)
{
for
(
ref
in
documents
)
{
val
word
=
ref
[
"word"
].
toString
()
val
translation
=
ref
[
"translation"
].
toString
()
val
time
=
ref
[
"time"
]
as
Timestamp
words
.
add
(
WordItem
(
word
,
translation
,
time
.
toDate
(),
ref
.
id
,
vocabularyId
!!
))
}
}
fun
addWordsFittingQuery
(
newWords
:
MutableList
<
WordItem
>,
query
:
String
)
{
for
(
newWord
in
newWords
)
{
if
(
newWord
.
contains
(
query
))
this
.
addWord
(
newWord
)
}
}
fun
updateWord
(
updatedWord
:
WordItem
)
{
val
updatedItemIndex
=
words
.
indexOf
(
updatedWord
)
words
[
updatedItemIndex
]
=
updatedWord
}
fun
getAt
(
position
:
Int
):
WordItem
{
return
words
[
position
]
}
fun
get
():
MutableList
<
WordItem
>
{
return
words
}
fun
size
():
Int
{
return
words
.
size
}
fun
clear
()
{
words
.
clear
()
}
private
fun
sortByTime
()
{
words
.
sortWith
(
Comparator
{
item1
,
item2
->
-
item1
.
pojo
.
time
!!
.
compareTo
(
item2
.
pojo
.
time
)
})
}
private
fun
sortByWord
()
{
words
.
sortWith
(
Comparator
{
item1
,
item2
->
item1
.
pojo
.
word
.
compareTo
(
item2
.
pojo
.
word
)
})
}
private
fun
sortByTranslation
()
{
words
.
sortWith
(
Comparator
{
item1
,
item2
->
item1
.
pojo
.
translation
.
compareTo
(
item2
.
pojo
.
translation
)
})
}
}
\ 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