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
c882a289
authored
Apr 20, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleaned up selection implementation
parent
0e1ce4af
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
86 additions
and
72 deletions
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/recycler_view/MyItemDetailsLookup.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/recycler_view/MyTracker.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/recycler_view/ViewHolder.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/recycler_view/VocabularyAdapter.kt
app/src/main/res/menu/options_menu.xml
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
View file @
c882a289
...
@@ -9,12 +9,8 @@ import android.view.WindowManager
...
@@ -9,12 +9,8 @@ import android.view.WindowManager
import
android.view.inputmethod.InputMethodManager
import
android.view.inputmethod.InputMethodManager
import
androidx.appcompat.app.AppCompatActivity
import
androidx.appcompat.app.AppCompatActivity
import
androidx.appcompat.widget.SearchView
import
androidx.appcompat.widget.SearchView
import
androidx.recyclerview.selection.SelectionPredicates
import
androidx.recyclerview.selection.SelectionTracker
import
androidx.recyclerview.selection.SelectionTracker
import
androidx.recyclerview.selection.StableIdKeyProvider
import
androidx.recyclerview.selection.StorageStrategy
import
com.paktalin.vocabularynotebook.R
import
com.paktalin.vocabularynotebook.R
import
com.paktalin.vocabularynotebook.ui.recycler_view.MyItemDetailsLookup
import
com.paktalin.vocabularynotebook.ui.recycler_view.VocabularyAdapter
import
com.paktalin.vocabularynotebook.ui.recycler_view.VocabularyAdapter
import
com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import
com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import
com.paktalin.vocabularynotebook.utils.*
import
com.paktalin.vocabularynotebook.utils.*
...
@@ -54,8 +50,6 @@ class MainActivity : AppCompatActivity() {
...
@@ -54,8 +50,6 @@ class MainActivity : AppCompatActivity() {
vocabularyAdapter
.
sort
=
Sort
.
BY_WORD
vocabularyAdapter
.
sort
=
Sort
.
BY_WORD
if
(
item
.
itemId
==
R
.
id
.
sortByTranslation
)
if
(
item
.
itemId
==
R
.
id
.
sortByTranslation
)
vocabularyAdapter
.
sort
=
Sort
.
BY_TRANSLATION
vocabularyAdapter
.
sort
=
Sort
.
BY_TRANSLATION
if
(
item
.
itemId
==
R
.
id
.
addTag
)
vocabularyAdapter
.
addTag
()
return
super
.
onOptionsItemSelected
(
item
)
return
super
.
onOptionsItemSelected
(
item
)
}
}
...
@@ -89,28 +83,18 @@ class MainActivity : AppCompatActivity() {
...
@@ -89,28 +83,18 @@ class MainActivity : AppCompatActivity() {
private
fun
setUpVocabularyAdapter
()
{
private
fun
setUpVocabularyAdapter
()
{
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_main
)
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_main
)
FirestoreManager
().
extractVocabularyId
({
FirestoreManager
().
extractVocabularyId
({
recyclerView
.
layoutManager
=
LockableLayoutManager
(
this
@MainActivity
)
recyclerView
.
layoutManager
=
LockableLayoutManager
(
this
@MainActivity
)
FirestoreManager
().
extractVocabulary
{
querySnapshot
->
recyclerView
.
adapter
=
VocabularyAdapter
(
VocabSet
(
mutableListOf
()),
this
@MainActivity
)
run
{
FirestoreManager
().
extractVocabulary
{
querySnapshot
->
if
(
querySnapshot
!=
null
&&
!
querySnapshot
.
isEmpty
)
{
run
{
vocabularyAdapter
=
VocabularyAdapter
(
VocabSet
.
createFromSnapshot
(
querySnapshot
),
this
@MainActivity
)
if
(
querySnapshot
!=
null
&&
!
querySnapshot
.
isEmpty
)
{
recyclerView
.
adapter
=
vocabularyAdapter
vocabularyAdapter
=
VocabularyAdapter
(
VocabSet
.
createFromSnapshot
(
querySnapshot
),
this
@MainActivity
)
tracker
=
SelectionTracker
.
Builder
<
Long
>(
recyclerView
.
adapter
=
vocabularyAdapter
"mySelection"
,
recyclerView
,
}
else
showToastNoWords
()
StableIdKeyProvider
(
recyclerView
),
}
MyItemDetailsLookup
(
recyclerView
),
}
StorageStrategy
.
createLongStorage
()
},
).
withSelectionPredicate
(
SelectionPredicates
.
createSelectAnything
()
).
build
()
(
recyclerView
.
adapter
as
VocabularyAdapter
).
tracker
=
tracker
}
else
showToastNoWords
()
}
}
},
{
shortToast
(
this
,
getString
(
R
.
string
.
toast_empty_vocabulary
))
},
{
shortToast
(
this
,
getString
(
R
.
string
.
toast_empty_vocabulary
))
},
{
removeProgressBar
(
supportFragmentManager
)
},
this
)
{
removeProgressBar
(
supportFragmentManager
)
},
this
)
}
}
...
@@ -137,7 +121,8 @@ class MainActivity : AppCompatActivity() {
...
@@ -137,7 +121,8 @@ class MainActivity : AppCompatActivity() {
hideKeyboard
()
hideKeyboard
()
try
{
try
{
FirestoreManager
().
saveWords
(
vocabularyAdapter
.
getModifiedWords
())
FirestoreManager
().
saveWords
(
vocabularyAdapter
.
getModifiedWords
())
}
catch
(
ignored
:
Exception
)
{}
}
catch
(
ignored
:
Exception
)
{
}
}
}
companion
object
{
companion
object
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/recycler_view/MyItemDetailsLookup.kt
View file @
c882a289
...
@@ -4,14 +4,11 @@ import android.view.MotionEvent
...
@@ -4,14 +4,11 @@ import android.view.MotionEvent
import
androidx.recyclerview.selection.ItemDetailsLookup
import
androidx.recyclerview.selection.ItemDetailsLookup
import
androidx.recyclerview.widget.RecyclerView
import
androidx.recyclerview.widget.RecyclerView
class
MyItemDetailsLookup
(
private
val
recyclerView
:
RecyclerView
)
:
class
MyItemDetailsLookup
(
private
val
recyclerView
:
RecyclerView
)
:
ItemDetailsLookup
<
Long
>()
{
ItemDetailsLookup
<
Long
>()
{
override
fun
getItemDetails
(
event
:
MotionEvent
):
ItemDetails
<
Long
>?
{
override
fun
getItemDetails
(
event
:
MotionEvent
):
ItemDetails
<
Long
>?
{
val
view
=
recyclerView
.
findChildViewUnder
(
event
.
x
,
event
.
y
)
val
view
=
recyclerView
.
findChildViewUnder
(
event
.
x
,
event
.
y
)
if
(
view
!=
null
)
{
if
(
view
!=
null
)
return
(
recyclerView
.
getChildViewHolder
(
view
)
as
ViewHolder
)
return
(
recyclerView
.
getChildViewHolder
(
view
)
as
ViewHolder
).
getItemDetails
()
.
getItemDetails
()
}
return
null
return
null
}
}
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/ui/recycler_view/MyTracker.kt
0 → 100644
View file @
c882a289
package
com.paktalin.vocabularynotebook.ui.recycler_view
import
androidx.recyclerview.selection.*
import
androidx.recyclerview.widget.RecyclerView
fun
initializeTracker
(
recyclerView
:
RecyclerView
,
onSelection
:
(
Selection
<
Long
>)
->
Unit
):
SelectionTracker
<
Long
>?
{
val
tracker
=
SelectionTracker
.
Builder
<
Long
>(
"mySelection"
,
recyclerView
,
StableIdKeyProvider
(
recyclerView
),
MyItemDetailsLookup
(
recyclerView
),
StorageStrategy
.
createLongStorage
()
).
withSelectionPredicate
(
SelectionPredicates
.
createSelectAnything
()
).
build
()
tracker
.
addObserver
(
object
:
SelectionTracker
.
SelectionObserver
<
Long
>()
{
override
fun
onSelectionChanged
()
{
super
.
onSelectionChanged
()
onSelection
(
tracker
?.
selection
!!
)
}
})
return
tracker
}
private
val
TAG
=
"VN/MyTracker"
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/ui/recycler_view/ViewHolder.kt
View file @
c882a289
package
com.paktalin.vocabularynotebook.ui.recycler_view
package
com.paktalin.vocabularynotebook.ui.recycler_view
import
android.graphics.Color
import
android.view.View
import
android.view.View
import
android.widget.LinearLayout
import
android.widget.TextView
import
android.widget.TextView
import
androidx.recyclerview.selection.ItemDetailsLookup
import
androidx.recyclerview.selection.ItemDetailsLookup
import
androidx.recyclerview.widget.RecyclerView
import
androidx.recyclerview.widget.RecyclerView
import
com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import
com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import
com.paktalin.vocabularynotebook.utils.Log
import
kotlinx.android.synthetic.main.word_item.view.*
import
kotlinx.android.synthetic.main.word_item.view.*
class
ViewHolder
(
itemView
:
View
)
:
RecyclerView
.
ViewHolder
(
itemView
),
View
.
OnClickListener
{
class
ViewHolder
(
itemView
:
View
)
:
RecyclerView
.
ViewHolder
(
itemView
)
{
override
fun
onClick
(
p0
:
View
?)
{
Log
.
d
(
TAG
,
"click"
)
p0
?.
setBackgroundColor
(
Color
.
GREEN
)
}
private
val
tvWord
:
TextView
=
itemView
.
word
private
val
tvWord
:
TextView
=
itemView
.
word
private
val
tvTranslation
:
TextView
=
itemView
.
translation
private
val
tvTranslation
:
TextView
=
itemView
.
translation
val
layout
:
LinearLayout
=
itemView
.
layout
fun
bind
(
wordPojo
:
WordPojo
,
position
:
Int
,
isActivated
:
Boolean
,
showPopupMenu
:
(
View
,
Int
)
->
Unit
)
{
fun
bind
(
wordPojo
:
WordPojo
,
position
:
Int
,
isActivated
:
Boolean
,
showPopupMenu
:
(
View
,
Int
)
->
Unit
)
{
tvWord
.
text
=
wordPojo
.
word
tvWord
.
text
=
wordPojo
.
word
tvTranslation
.
text
=
wordPojo
.
translation
tvTranslation
.
text
=
wordPojo
.
translation
//
itemView.setOnClickListener { showPopupMenu(itemView, position) }
itemView
.
setOnClickListener
{
showPopupMenu
(
itemView
,
position
)
}
itemView
.
isActivated
=
isActivated
itemView
.
isActivated
=
isActivated
}
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/recycler_view/VocabularyAdapter.kt
View file @
c882a289
...
@@ -7,6 +7,7 @@ import android.view.LayoutInflater
...
@@ -7,6 +7,7 @@ import android.view.LayoutInflater
import
android.view.View
import
android.view.View
import
android.view.ViewGroup
import
android.view.ViewGroup
import
androidx.appcompat.widget.PopupMenu
import
androidx.appcompat.widget.PopupMenu
import
androidx.recyclerview.selection.Selection
import
androidx.recyclerview.selection.SelectionTracker
import
androidx.recyclerview.selection.SelectionTracker
import
androidx.recyclerview.widget.RecyclerView
import
androidx.recyclerview.widget.RecyclerView
import
com.paktalin.vocabularynotebook.R
import
com.paktalin.vocabularynotebook.R
...
@@ -15,16 +16,19 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity
...
@@ -15,16 +16,19 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import
com.paktalin.vocabularynotebook.ui.fragments.EditWordFragment
import
com.paktalin.vocabularynotebook.ui.fragments.EditWordFragment
import
com.paktalin.vocabularynotebook.utils.Log
import
com.paktalin.vocabularynotebook.utils.Log
import
com.paktalin.vocabularynotebook.utils.addFragment
import
com.paktalin.vocabularynotebook.utils.addFragment
import
com.paktalin.vocabularynotebook.utils.shortToast
import
com.paktalin.vocabularynotebook.utils.gone
import
com.paktalin.vocabularynotebook.utils.visible
import
com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary
import
com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary
import
com.paktalin.vocabularynotebook.vocabulary.Sort
import
com.paktalin.vocabularynotebook.vocabulary.Sort
import
com.paktalin.vocabularynotebook.vocabulary.VocabSet
import
com.paktalin.vocabularynotebook.vocabulary.VocabSet
import
kotlinx.android.synthetic.main.content_main.*
class
VocabularyAdapter
(
private
val
vocabulary
:
VocabSet
,
private
val
mainActivity
:
MainActivity
)
:
RecyclerView
.
Adapter
<
ViewHolder
>()
{
class
VocabularyAdapter
(
private
val
vocabulary
:
VocabSet
,
private
val
mainActivity
:
MainActivity
)
:
RecyclerView
.
Adapter
<
ViewHolder
>()
{
private
lateinit
var
recyclerView
:
RecyclerView
private
lateinit
var
recyclerView
:
RecyclerView
private
lateinit
var
viewHolder
:
ViewHolder
private
lateinit
var
viewHolder
:
ViewHolder
var
tracker
:
SelectionTracker
<
Long
>?
=
null
private
var
tracker
:
SelectionTracker
<
Long
>?
=
null
private
var
menu
=
true
init
{
init
{
setHasStableIds
(
true
)
setHasStableIds
(
true
)
...
@@ -33,14 +37,15 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
...
@@ -33,14 +37,15 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
override
fun
getItemId
(
position
:
Int
):
Long
=
position
.
toLong
()
override
fun
getItemId
(
position
:
Int
):
Long
=
position
.
toLong
()
var
sort
:
Sort
=
Sort
.
BY_TIME
var
sort
:
Sort
=
Sort
.
BY_TIME
set
(
value
)
{
set
(
value
)
{
field
=
value
field
=
value
sort
()
sort
()
}
}
override
fun
onAttachedToRecyclerView
(
recyclerView
:
RecyclerView
)
{
override
fun
onAttachedToRecyclerView
(
recyclerView
:
RecyclerView
)
{
super
.
onAttachedToRecyclerView
(
recyclerView
)
super
.
onAttachedToRecyclerView
(
recyclerView
)
this
.
recyclerView
=
recyclerView
this
.
recyclerView
=
recyclerView
this
.
tracker
=
initializeTracker
(
recyclerView
)
{
s
->
wordsSelected
(
s
)
}
mainActivity
.
searchView
.
setOnQueryTextListener
(
OnQueryTextListener
(
this
@VocabularyAdapter
))
mainActivity
.
searchView
.
setOnQueryTextListener
(
OnQueryTextListener
(
this
@VocabularyAdapter
))
}
}
...
@@ -62,18 +67,25 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
...
@@ -62,18 +67,25 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
//todo set click listener to menu
//todo set click listener to menu
}
}
private
fun
showPopupMenu
(
v
:
View
,
position
:
Int
)
{
private
fun
showPopupMenu
(
v
:
View
,
position
:
Int
)
{
val
popup
=
PopupMenu
(
mainActivity
,
v
)
if
(
menu
)
{
popup
.
menuInflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
val
popup
=
PopupMenu
(
mainActivity
,
v
)
popup
.
setOnMenuItemClickListener
{
popup
.
menuInflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
if
(
it
.
itemId
==
R
.
id
.
option_delete
)
{
deleteWord
(
vocabulary
.
displayedAt
(
position
),
position
)
}
popup
.
setOnMenuItemClickListener
{
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
startEditFragment
(
v
,
vocabulary
.
displayedAt
(
position
))
}
if
(
it
.
itemId
==
R
.
id
.
option_delete
)
{
true
deleteWord
(
vocabulary
.
displayedAt
(
position
),
position
)
}
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
startEditFragment
(
v
,
vocabulary
.
displayedAt
(
position
))
}
true
}
// if not edit mode, then start EditFragment
if
(
mainActivity
.
supportFragmentManager
.
findFragmentByTag
(
"edit_fragment"
)
==
null
)
popup
.
show
()
}
else
{
Log
.
d
(
TAG
,
"not menu!"
)
}
}
// if not edit mode, then start EditFragment
if
(
mainActivity
.
supportFragmentManager
.
findFragmentByTag
(
"edit_fragment"
)
==
null
)
popup
.
show
()
}
}
fun
refresh
()
{
fun
refresh
()
{
...
@@ -133,11 +145,15 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
...
@@ -133,11 +145,15 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
return
vocabulary
.
getModified
()
return
vocabulary
.
getModified
()
}
}
fun
addTag
()
{
private
fun
wordsSelected
(
selection
:
Selection
<
Long
>)
{
shortToast
(
mainActivity
,
mainActivity
.
getString
(
R
.
string
.
toast_select_words_to_tag
))
if
(
selection
.
isEmpty
)
// TODO make words selectable
gone
(
mainActivity
.
btnSubmitLayout
)
// TODO add complete button
else
{
// TODO collect selected words in a list
visible
(
mainActivity
.
btnSubmitLayout
)
mainActivity
.
btnSubmit
.
setOnClickListener
{
// TODO add tag to the words
}
}
}
}
companion
object
{
companion
object
{
...
...
app/src/main/res/menu/options_menu.xml
View file @
c882a289
...
@@ -23,9 +23,4 @@
...
@@ -23,9 +23,4 @@
android:id=
"@+id/sortByTranslation"
android:id=
"@+id/sortByTranslation"
android:title=
"@string/sort_by_translation"
android:title=
"@string/sort_by_translation"
app:showAsAction=
"never"
/>
app:showAsAction=
"never"
/>
<item
android:id=
"@+id/addTag"
android:title=
"@string/add_tag"
app:showAsAction=
"never"
/>
</menu>
</menu>
\ 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