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
dacdb0e0
authored
Sep 12, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Word addition and deletion is made inside Vocabulary class
parent
af63849b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
17 deletions
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/Vocabulary.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/VocabularyFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
View file @
dacdb0e0
...
@@ -13,7 +13,6 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
...
@@ -13,7 +13,6 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
com.paktalin.vocabularynotebook.ui.EditWordFragment
import
com.paktalin.vocabularynotebook.ui.EditWordFragment
import
com.paktalin.vocabularynotebook.ui.MainActivity
import
com.paktalin.vocabularynotebook.ui.MainActivity
import
java.util.*
class
VocabularyAdapter
(
private
val
vocabulary
:
Vocabulary
,
private
val
activity
:
Activity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
class
VocabularyAdapter
(
private
val
vocabulary
:
Vocabulary
,
private
val
activity
:
Activity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
...
@@ -39,34 +38,34 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
...
@@ -39,34 +38,34 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
//todo set click listener to menu
//todo set click listener to menu
}
}
override
fun
getItemCount
():
Int
{
return
vocabulary
.
words
.
size
}
override
fun
getItemCount
():
Int
{
return
vocabulary
.
size
()
}
private
fun
showPopupMenu
(
v
:
View
,
position
:
Int
)
{
private
fun
showPopupMenu
(
v
:
View
,
position
:
Int
)
{
val
popup
=
PopupMenu
(
activity
,
v
)
val
popup
=
PopupMenu
(
activity
,
v
)
val
inflater
=
popup
.
menuInflater
val
inflater
=
popup
.
menuInflater
inflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
inflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
popup
.
setOnMenuItemClickListener
{
popup
.
setOnMenuItemClickListener
{
if
(
it
.
itemId
==
R
.
id
.
option_delete
)
{
deleteWord
Item
(
position
)
}
if
(
it
.
itemId
==
R
.
id
.
option_delete
)
{
deleteWord
(
position
)
}
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
editWord
Item
(
v
,
vocabulary
.
words
[
position
])
}
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
editWord
(
v
,
vocabulary
.
words
[
position
])
}
true
true
}
}
popup
.
show
()
popup
.
show
()
}
}
private
fun
deleteWord
Item
(
position
:
Int
)
{
private
fun
deleteWord
(
position
:
Int
)
{
vocabulary
.
words
[
position
].
delete
(
)
vocabulary
.
deleteWord
(
position
)
vocabulary
.
words
.
removeAt
(
position
)
// update recyclerView
recyclerView
.
removeViewAt
(
position
)
recyclerView
.
removeViewAt
(
position
)
this
.
notifyItemRemoved
(
position
)
this
.
notifyItemRemoved
(
position
)
this
.
notifyItemRangeChanged
(
position
,
vocabulary
.
words
.
size
)
this
.
notifyItemRangeChanged
(
position
,
vocabulary
.
size
()
)
}
}
fun
addWord
Item
(
newWordItem
:
WordItem
)
{
fun
addWord
(
newWord
:
WordItem
)
{
vocabulary
.
words
.
add
(
0
,
newWordItem
)
vocabulary
.
addWord
(
newWord
)
this
.
sort
()
this
.
sort
()
}
}
fun
updateWord
Item
(
updatedWordItem
:
WordItem
)
{
fun
updateWord
(
updatedWordItem
:
WordItem
)
{
val
updatedItemId
=
vocabulary
.
words
.
indexOf
(
updatedWordItem
)
val
updatedItemId
=
vocabulary
.
words
.
indexOf
(
updatedWordItem
)
vocabulary
.
words
[
updatedItemId
]
=
updatedWordItem
vocabulary
.
words
[
updatedItemId
]
=
updatedWordItem
this
.
sort
()
this
.
sort
()
...
@@ -82,7 +81,7 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
...
@@ -82,7 +81,7 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
}
}
@SuppressLint
(
"ResourceType"
)
@SuppressLint
(
"ResourceType"
)
private
fun
editWord
Item
(
container
:
View
,
wordItem
:
WordItem
)
{
private
fun
editWord
(
container
:
View
,
wordItem
:
WordItem
)
{
//set container id
//set container id
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
)
{
container
.
id
=
View
.
generateViewId
()
container
.
id
=
View
.
generateViewId
()
...
...
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/Vocabulary.kt
View file @
dacdb0e0
package
com.paktalin.vocabularynotebook.firestoreitems
package
com.paktalin.vocabularynotebook.firestoreitems
class
Vocabulary
(
var
words
:
MutableList
<
WordItem
>)
{
class
Vocabulary
(
words
:
MutableList
<
WordItem
>)
{
companion
object
{
companion
object
{
private
val
TAG
=
"VN/"
+
Vocabulary
::
class
.
java
.
simpleName
private
val
TAG
=
"VN/"
+
Vocabulary
::
class
.
java
.
simpleName
...
@@ -11,6 +11,9 @@ class Vocabulary(var words: MutableList<WordItem>) {
...
@@ -11,6 +11,9 @@ class Vocabulary(var words: MutableList<WordItem>) {
var
pojo
:
Pojo
var
pojo
:
Pojo
//todo set private
var
words
:
MutableList
<
WordItem
>
class
Pojo
(
var
title
:
String
?)
{
class
Pojo
(
var
title
:
String
?)
{
init
{
init
{
if
(
title
==
null
)
title
=
"Untitled vocabulary"
if
(
title
==
null
)
title
=
"Untitled vocabulary"
...
@@ -18,7 +21,8 @@ class Vocabulary(var words: MutableList<WordItem>) {
...
@@ -18,7 +21,8 @@ class Vocabulary(var words: MutableList<WordItem>) {
}
}
init
{
init
{
pojo
=
Pojo
(
null
)
this
.
pojo
=
Pojo
(
null
)
this
.
words
=
words
}
}
fun
sort
(
sortOrder
:
Int
)
{
fun
sort
(
sortOrder
:
Int
)
{
...
@@ -29,6 +33,18 @@ class Vocabulary(var words: MutableList<WordItem>) {
...
@@ -29,6 +33,18 @@ class Vocabulary(var words: MutableList<WordItem>) {
}
}
}
}
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
size
():
Int
{
return
words
.
size
}
//region Private methods
private
fun
sortByTime
()
{
private
fun
sortByTime
()
{
words
.
sortWith
(
Comparator
{
item1
,
item2
->
words
.
sortWith
(
Comparator
{
item1
,
item2
->
-
item1
.
pojo
.
time
!!
.
compareTo
(
item2
.
pojo
.
time
)
})
-
item1
.
pojo
.
time
!!
.
compareTo
(
item2
.
pojo
.
time
)
})
...
@@ -41,5 +57,5 @@ class Vocabulary(var words: MutableList<WordItem>) {
...
@@ -41,5 +57,5 @@ class Vocabulary(var words: MutableList<WordItem>) {
words
.
sortWith
(
Comparator
{
item1
,
item2
->
words
.
sortWith
(
Comparator
{
item1
,
item2
->
item1
.
pojo
.
translation
.
compareTo
(
item2
.
pojo
.
translation
)
})
item1
.
pojo
.
translation
.
compareTo
(
item2
.
pojo
.
translation
)
})
}
}
//endregion
}
}
app/src/main/java/com/paktalin/vocabularynotebook/ui/VocabularyFragment.kt
View file @
dacdb0e0
...
@@ -78,10 +78,10 @@ class VocabularyFragment : Fragment() {
...
@@ -78,10 +78,10 @@ class VocabularyFragment : Fragment() {
}
}
fun
addWordItem
(
newWordItem
:
WordItem
)
{
fun
addWordItem
(
newWordItem
:
WordItem
)
{
(
recyclerView
.
adapter
as
VocabularyAdapter
).
addWord
Item
(
newWordItem
)
(
recyclerView
.
adapter
as
VocabularyAdapter
).
addWord
(
newWordItem
)
}
}
fun
updateWordItem
(
updatedWordItem
:
WordItem
)
{
fun
updateWordItem
(
updatedWordItem
:
WordItem
)
{
(
recyclerView
.
adapter
as
VocabularyAdapter
).
updateWord
Item
(
updatedWordItem
)
(
recyclerView
.
adapter
as
VocabularyAdapter
).
updateWord
(
updatedWordItem
)
}
}
}
}
\ 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