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
28dea025
authored
Sep 14, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in Vocabulary class
parent
bde85cd1
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
54 additions
and
50 deletions
app/src/main/java/com/paktalin/vocabularynotebook/OnQueryTextListener.kt
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/firestoreitems/WordItem.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/MainActivity.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/VocabularyFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/OnQueryTextListener.kt
View file @
28dea025
package
com.paktalin.vocabularynotebook
import
android.support.v7.widget.RecyclerView
import
android.support.v7.widget.SearchView
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
class
OnQueryTextListener
(
var
recyclerView
:
RecyclerView
)
:
SearchView
.
OnQueryTextListener
{
class
OnQueryTextListener
(
var
adapter
:
VocabularyAdapter
)
:
SearchView
.
OnQueryTextListener
{
override
fun
onQueryTextSubmit
(
query
:
String
):
Boolean
{
(
recyclerView
.
adapter
as
VocabularyAdapter
)
.
filter
(
query
)
adapter
.
filter
(
query
)
return
true
}
override
fun
onQueryTextChange
(
query
:
String
):
Boolean
{
(
recyclerView
.
adapter
as
VocabularyAdapter
)
.
filter
(
query
)
adapter
.
filter
(
query
)
return
true
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
View file @
28dea025
package
com.paktalin.vocabularynotebook
import
android.annotation.SuppressLint
import
android.app.Activity
import
android.os.Build
import
android.os.Bundle
import
android.support.v7.widget.PopupMenu
...
...
@@ -15,22 +14,25 @@ import com.paktalin.vocabularynotebook.ui.EditWordFragment
import
com.paktalin.vocabularynotebook.ui.MainActivity
import
kotlinx.android.synthetic.main.word_item.view.*
class
VocabularyAdapter
(
internal
val
vocabulary
:
Vocabulary
,
private
val
activity
:
Activity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
class
VocabularyAdapter
(
private
val
vocabulary
:
Vocabulary
,
private
val
mainActivity
:
Main
Activity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
private
lateinit
var
recyclerView
:
RecyclerView
private
var
sortOrder
:
Int
=
0
set
(
value
)
{
field
=
value
;
sort
()
}
private
var
sortOrder
:
Int
=
0
set
(
value
)
{
field
=
value
;
sort
()
}
private
var
wordsCopy
:
MutableList
<
WordItem
>
=
mutableListOf
()
private
var
wordsCopy
:
MutableList
<
WordItem
>
=
mutableListOf
()
// stores all the words loaded from the db
init
{
wordsCopy
.
addAll
(
vocabulary
.
words
)
wordsCopy
.
addAll
(
vocabulary
.
get
()
)
}
override
fun
onAttachedToRecyclerView
(
recyclerView
:
RecyclerView
)
{
super
.
onAttachedToRecyclerView
(
recyclerView
)
this
.
recyclerView
=
recyclerView
mainActivity
.
searchView
.
setOnQueryTextListener
(
OnQueryTextListener
(
this
@VocabularyAdapter
))
}
override
fun
onCreateViewHolder
(
parent
:
ViewGroup
,
viewType
:
Int
):
ViewHolder
{
...
...
@@ -47,10 +49,12 @@ class VocabularyAdapter(internal val vocabulary: Vocabulary, private val activit
//todo set click listener to menu
}
override
fun
getItemCount
():
Int
{
return
vocabulary
.
size
()
}
override
fun
getItemCount
():
Int
{
return
vocabulary
.
size
()
}
private
fun
showPopupMenu
(
v
:
View
,
position
:
Int
)
{
val
popup
=
PopupMenu
(
a
ctivity
,
v
)
val
popup
=
PopupMenu
(
mainA
ctivity
,
v
)
val
inflater
=
popup
.
menuInflater
inflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
popup
.
setOnMenuItemClickListener
{
...
...
@@ -90,7 +94,7 @@ class VocabularyAdapter(internal val vocabulary: Vocabulary, private val activit
}
@SuppressLint
(
"ResourceType"
)
private
fun
editWord
(
container
:
View
,
wordItem
:
WordItem
)
{
private
fun
editWord
(
container
:
View
,
wordItem
:
WordItem
)
{
//set container id
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
)
{
container
.
id
=
View
.
generateViewId
()
...
...
@@ -101,19 +105,7 @@ class VocabularyAdapter(internal val vocabulary: Vocabulary, private val activit
val
arguments
=
Bundle
()
arguments
.
putSerializable
(
"wordItem"
,
wordItem
)
editWordFragment
.
arguments
=
arguments
(
activity
as
MainActivity
).
supportFragmentManager
.
beginTransaction
().
add
(
container
.
id
,
editWordFragment
).
commit
()
}
fun
replaceAll
(
words
:
List
<
WordItem
>)
{
//vocabulary.words.beginBatchedUpdates()
for
(
i
in
vocabulary
.
words
.
size
-
1
downTo
0
)
{
val
model
=
vocabulary
.
words
[
i
]
if
(!
words
.
contains
(
model
))
{
vocabulary
.
words
.
remove
(
model
)
}
}
vocabulary
.
words
.
addAll
(
words
)
//vocabulary.words.endBatchedUpdates()
mainActivity
.
supportFragmentManager
.
beginTransaction
().
add
(
container
.
id
,
editWordFragment
).
commit
()
}
inner
class
ViewHolder
(
itemView
:
View
)
:
RecyclerView
.
ViewHolder
(
itemView
)
{
...
...
@@ -123,21 +115,15 @@ class VocabularyAdapter(internal val vocabulary: Vocabulary, private val activit
}
fun
filter
(
query
:
String
)
{
var
query
=
query
vocabulary
.
words
.
clear
()
if
(
query
.
isEmpty
())
{
vocabulary
.
words
.
addAll
(
wordsCopy
)
}
else
{
query
=
query
.
toLowerCase
()
for
(
wordCopy
in
wordsCopy
)
{
if
(
wordCopy
.
pojo
.
word
.
toLowerCase
().
contains
(
query
)
||
wordCopy
.
pojo
.
translation
.
toLowerCase
().
contains
(
query
))
{
vocabulary
.
words
.
add
(
wordCopy
)
}
}
}
vocabulary
.
clear
()
if
(
query
.
isEmpty
())
vocabulary
.
addWords
(
wordsCopy
)
else
vocabulary
.
addWordsFittingQuery
(
wordsCopy
,
query
.
toLowerCase
())
notifyDataSetChanged
()
}
companion
object
{
private
val
TAG
=
"VN/"
+
VocabularyAdapter
::
class
.
java
.
simpleName
}
companion
object
{
private
val
TAG
=
"VN/"
+
VocabularyAdapter
::
class
.
java
.
simpleName
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/Vocabulary.kt
View file @
28dea025
...
...
@@ -10,7 +10,7 @@ class Vocabulary(words: MutableList<WordItem>) {
}
var
pojo
:
Pojo
var
words
:
MutableList
<
WordItem
>
private
var
words
:
MutableList
<
WordItem
>
class
Pojo
(
var
title
:
String
?)
{
init
{
...
...
@@ -40,6 +40,17 @@ class Vocabulary(words: MutableList<WordItem>) {
words
.
add
(
0
,
newWord
)
}
fun
addWords
(
newWords
:
MutableList
<
WordItem
>)
{
words
.
addAll
(
newWords
)
}
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
...
...
@@ -49,8 +60,12 @@ class Vocabulary(words: MutableList<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
)
})
...
...
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/WordItem.kt
View file @
28dea025
...
...
@@ -25,5 +25,10 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
.
addOnFailureListener
{
e
->
Log
.
w
(
TAG
,
"deleteWordWithId $id:failure"
,
e
.
fillInStackTrace
())
}
}
fun
contains
(
string
:
String
):
Boolean
{
return
pojo
.
word
.
toLowerCase
().
contains
(
string
)
||
pojo
.
translation
.
toLowerCase
().
contains
(
string
)
}
companion
object
{
private
val
TAG
=
"VN/"
+
WordItem
::
class
.
java
.
simpleName
}
}
app/src/main/java/com/paktalin/vocabularynotebook/ui/MainActivity.kt
View file @
28dea025
...
...
@@ -11,7 +11,6 @@ import com.paktalin.vocabularynotebook.R
import
kotlinx.android.synthetic.main.activity_main.*
import
android.view.WindowManager
import
android.app.Activity
import
android.app.SearchManager
import
android.support.v4.app.Fragment
import
android.view.Menu
import
android.view.MenuItem
...
...
@@ -21,9 +20,7 @@ import android.widget.Toast
import
com.paktalin.vocabularynotebook.VocabularyAdapter
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
kotlinx.android.synthetic.main.fragment_vocabulary.*
import
android.content.Context
import
android.support.v7.widget.SearchView
import
com.paktalin.vocabularynotebook.OnQueryTextListener
class
MainActivity
:
AppCompatActivity
()
{
...
...
@@ -90,7 +87,6 @@ class MainActivity : AppCompatActivity() {
vocabularyFragment
.
arguments
=
arguments
supportFragmentManager
.
beginTransaction
().
add
(
R
.
id
.
fragment_container
,
vocabularyFragment
)
.
commitNowAllowingStateLoss
()
searchView
.
setOnQueryTextListener
(
OnQueryTextListener
(
recyclerView
))
}
else
{
Log
.
w
(
TAG
,
"There's no collection \"vocabularies\""
)
showToastNoWords
()
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/VocabularyFragment.kt
View file @
28dea025
...
...
@@ -25,6 +25,7 @@ class VocabularyFragment : Fragment() {
}
private
val
db
=
ConfiguredFirestore
.
instance
private
lateinit
var
mainActivity
:
MainActivity
override
fun
onCreateView
(
inflater
:
LayoutInflater
,
container
:
ViewGroup
?,
savedInstanceState
:
Bundle
?):
View
?
{
return
inflater
.
inflate
(
R
.
layout
.
fragment_vocabulary
,
container
,
false
)
...
...
@@ -32,14 +33,15 @@ class VocabularyFragment : Fragment() {
override
fun
onActivityCreated
(
savedInstanceState
:
Bundle
?)
{
super
.
onActivityCreated
(
savedInstanceState
)
mainActivity
=
activity
as
MainActivity
setEmptyAdapter
()
retrieveWordsData
(
arguments
!!
[
"vocabularyId"
]
as
String
)
}
private
fun
setEmptyAdapter
()
{
val
emptyList
:
MutableList
<
WordItem
>
=
mutableListOf
()
recyclerView
.
adapter
=
VocabularyAdapter
(
Vocabulary
(
emptyList
),
activity
!!
)
val
mLayoutManager
=
LinearLayoutManager
(
a
ctivity
)
recyclerView
.
adapter
=
VocabularyAdapter
(
Vocabulary
(
emptyList
),
mainActivity
)
val
mLayoutManager
=
LinearLayoutManager
(
mainA
ctivity
)
recyclerView
.
layoutManager
=
mLayoutManager
}
...
...
@@ -52,7 +54,7 @@ class VocabularyFragment : Fragment() {
setVocabularyAdapter
(
it
.
documents
,
vocabularyId
)
else
{
Log
.
i
(
TAG
,
"There are no documents in collection \"words\""
)
(
activity
as
MainActivity
)
.
showToastNoWords
()
mainActivity
.
showToastNoWords
()
}}
}
...
...
@@ -67,7 +69,7 @@ class VocabularyFragment : Fragment() {
}
val
vocabulary
=
Vocabulary
(
wordItems
)
val
adapter
=
VocabularyAdapter
(
vocabulary
,
activity
!!
)
val
adapter
=
VocabularyAdapter
(
vocabulary
,
mainActivity
)
recyclerView
.
adapter
=
adapter
}
...
...
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