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
781ae10e
authored
Apr 19, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed inEditMode property + refactoring
parent
307bfc47
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
33 additions
and
52 deletions
app/src/main/java/com/paktalin/vocabularynotebook/Vocabulary.kt
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/fragments/EditWordFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/utils/ActivityUtil.kt
app/src/main/java/com/paktalin/vocabularynotebook/Vocabulary.kt
View file @
781ae10e
...
...
@@ -8,16 +8,17 @@ import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.vocabula
class
Vocabulary
()
{
private
var
words
:
MutableList
<
WordItem
>
=
mutableListOf
()
/* constructor(words: MutableList<WordItem>
) : this() {
this.words = words
}
*/
constructor
(
vocabulary
:
Vocabulary
)
:
this
()
{
copyWordsFrom
(
vocabulary
)
}
constructor
(
documents
:
MutableList
<
DocumentSnapshot
>)
:
this
()
{
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
!!
))
documents
.
forEach
{
ref
->
words
.
add
(
WordItem
(
ref
[
"word"
].
toString
(),
ref
[
"translation"
].
toString
(),
(
ref
[
"time"
]
as
Timestamp
).
toDate
(),
ref
.
id
,
vocabularyId
!!
))
}
}
...
...
@@ -46,24 +47,12 @@ class Vocabulary() {
words
.
add
(
0
,
newWord
)
}
fun
addWords
(
newWords
:
MutableList
<
WordItem
>
)
{
words
.
addAll
(
newWords
)
fun
copyWordsFrom
(
vocabulary
:
Vocabulary
)
{
words
.
addAll
(
vocabulary
.
get
()
)
}
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
addWordsFittingQuery
(
vocabulary
:
Vocabulary
,
query
:
String
)
{
vocabulary
.
get
().
filter
{
wordItem
->
wordItem
.
contains
(
query
)
}.
toCollection
(
words
)
}
fun
updateWord
(
updatedWord
:
WordItem
)
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
View file @
781ae10e
...
...
@@ -14,9 +14,9 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import
com.paktalin.vocabularynotebook.utils.addFragment
import
kotlinx.android.synthetic.main.word_item.view.*
class
VocabularyAdapter
(
private
val
displayed
Vocabulary
:
Vocabulary
,
private
val
mainActivity
:
MainActivity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
class
VocabularyAdapter
(
private
val
full
Vocabulary
:
Vocabulary
,
private
val
mainActivity
:
MainActivity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
private
var
fullVocabulary
:
MutableList
<
WordItem
>
=
mutableListOf
()
// stores all the words loaded from the db
private
var
displayedVocabulary
=
Vocabulary
(
fullVocabulary
)
private
lateinit
var
recyclerView
:
RecyclerView
var
sortOrder
:
Int
=
0
...
...
@@ -24,10 +24,6 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
field
=
value
;
sort
()
}
init
{
fullVocabulary
.
addAll
(
displayedVocabulary
.
get
())
}
override
fun
onAttachedToRecyclerView
(
recyclerView
:
RecyclerView
)
{
super
.
onAttachedToRecyclerView
(
recyclerView
)
this
.
recyclerView
=
recyclerView
...
...
@@ -58,10 +54,11 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
inflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
popup
.
setOnMenuItemClickListener
{
if
(
it
.
itemId
==
R
.
id
.
option_delete
)
{
deleteWord
(
position
)
}
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
editWord
(
v
,
displayedVocabulary
.
getAt
(
position
))
}
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
startEditFragment
(
v
,
displayedVocabulary
.
getAt
(
position
))
}
true
}
if
(!
mainActivity
.
inEditMode
)
popup
.
show
()
if
(
mainActivity
.
supportFragmentManager
.
findFragmentByTag
(
"edit_fragment"
)
==
null
)
popup
.
show
()
}
private
fun
deleteWord
(
position
:
Int
)
{
...
...
@@ -96,18 +93,16 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
}
@SuppressLint
(
"ResourceType"
)
private
fun
editWord
(
container
:
View
,
wordItem
:
WordItem
)
{
if
(!
mainActivity
.
inEditMode
)
{
//set container id
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
)
{
container
.
id
=
View
.
generateViewId
()
}
else
container
.
id
=
18071999
// start EditWordFragment
val
arguments
=
Bundle
()
arguments
.
putSerializable
(
"wordItem"
,
wordItem
)
addFragment
(
mainActivity
.
supportFragmentManager
,
EditWordFragment
(),
container
.
id
,
arguments
)
}
private
fun
startEditFragment
(
container
:
View
,
wordItem
:
WordItem
)
{
//set container id
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
)
{
container
.
id
=
View
.
generateViewId
()
}
else
container
.
id
=
18071999
// start EditWordFragment
val
arguments
=
Bundle
()
arguments
.
putSerializable
(
"wordItem"
,
wordItem
)
addFragment
(
mainActivity
.
supportFragmentManager
,
EditWordFragment
(),
container
.
id
,
arguments
,
"edit_fragment"
)
}
inner
class
ViewHolder
(
itemView
:
View
)
:
RecyclerView
.
ViewHolder
(
itemView
)
{
...
...
@@ -119,7 +114,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
fun
filter
(
query
:
String
)
{
displayedVocabulary
.
clear
()
if
(
query
.
isEmpty
())
displayedVocabulary
.
addWords
(
fullVocabulary
)
displayedVocabulary
.
copyWordsFrom
(
fullVocabulary
)
else
displayedVocabulary
.
addWordsFittingQuery
(
fullVocabulary
,
query
.
toLowerCase
())
notifyDataSetChanged
()
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
View file @
781ae10e
...
...
@@ -24,7 +24,6 @@ class MainActivity : AppCompatActivity() {
lateinit
var
vocabularyFragment
:
VocabularyFragment
lateinit
var
searchView
:
SearchView
var
inEditMode
=
false
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/fragments/EditWordFragment.kt
View file @
781ae10e
...
...
@@ -28,7 +28,6 @@ class EditWordFragment : WordFragment() {
override
fun
onCreateView
(
inflater
:
LayoutInflater
,
container
:
ViewGroup
?,
savedInstanceState
:
Bundle
?):
View
?
{
mainActivity
=
activity
as
MainActivity
mainActivity
.
inEditMode
=
true
this
.
container
=
container
hidePreviousViews
()
wordItem
=
arguments
!!
[
"wordItem"
]
as
WordItem
...
...
@@ -87,8 +86,8 @@ class EditWordFragment : WordFragment() {
}
private
fun
onFailure
()
{
shortToast
(
mainActivity
,
getString
(
R
.
string
.
toast_update_word_failed
))
stop
()
/*
shortToast(mainActivity, getString(R.string.toast_update_word_failed))
stop()
*/
}
private
fun
stop
()
{
...
...
@@ -97,7 +96,6 @@ class EditWordFragment : WordFragment() {
removeFragment
(
mainActivity
.
supportFragmentManager
,
this
)
mainActivity
.
hideKeyboardNotFromActivity
(
mainActivity
)
gone
(
mainActivity
.
btnSubmitLayout
)
mainActivity
.
inEditMode
=
false
}
companion
object
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/utils/ActivityUtil.kt
View file @
781ae10e
...
...
@@ -13,12 +13,12 @@ import org.apache.commons.lang3.StringUtils
val
progressFragment
:
Fragment
=
ProgressFragment
()
fun
addFragment
(
fragmentManager
:
FragmentManager
,
fragment
:
Fragment
,
containerId
:
Int
,
arguments
:
Bundle
?)
{
fun
addFragment
(
fragmentManager
:
FragmentManager
,
fragment
:
Fragment
,
containerId
:
Int
,
arguments
:
Bundle
?
,
tag
:
String
?
=
null
)
{
fragment
.
arguments
=
arguments
// remove progressFragment if it exists
if
(
fragmentManager
.
findFragmentById
(
fragment
.
id
)
!=
null
)
removeFragment
(
fragmentManager
,
fragment
)
fragmentManager
.
beginTransaction
().
add
(
containerId
,
fragment
).
commitAllowingStateLoss
()
fragmentManager
.
beginTransaction
().
add
(
containerId
,
fragment
,
tag
).
commitAllowingStateLoss
()
}
fun
removeFragment
(
fragmentManager
:
FragmentManager
,
fragment
:
Fragment
)
{
...
...
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