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
import
android.view.inputmethod.InputMethodManager
import
androidx.appcompat.app.AppCompatActivity
import
androidx.appcompat.widget.SearchView
import
androidx.recyclerview.selection.SelectionPredicates
import
androidx.recyclerview.selection.SelectionTracker
import
androidx.recyclerview.selection.StableIdKeyProvider
import
androidx.recyclerview.selection.StorageStrategy
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.views.LockableLayoutManager
import
com.paktalin.vocabularynotebook.utils.*
...
...
@@ -54,8 +50,6 @@ class MainActivity : AppCompatActivity() {
vocabularyAdapter
.
sort
=
Sort
.
BY_WORD
if
(
item
.
itemId
==
R
.
id
.
sortByTranslation
)
vocabularyAdapter
.
sort
=
Sort
.
BY_TRANSLATION
if
(
item
.
itemId
==
R
.
id
.
addTag
)
vocabularyAdapter
.
addTag
()
return
super
.
onOptionsItemSelected
(
item
)
}
...
...
@@ -89,28 +83,18 @@ class MainActivity : AppCompatActivity() {
private
fun
setUpVocabularyAdapter
()
{
addProgressBar
(
supportFragmentManager
,
R
.
id
.
container_main
)
FirestoreManager
().
extractVocabularyId
({
recyclerView
.
layoutManager
=
LockableLayoutManager
(
this
@MainActivity
)
FirestoreManager
().
extractVocabulary
{
querySnapshot
->
run
{
if
(
querySnapshot
!=
null
&&
!
querySnapshot
.
isEmpty
)
{
vocabularyAdapter
=
VocabularyAdapter
(
VocabSet
.
createFromSnapshot
(
querySnapshot
),
this
@MainActivity
)
recyclerView
.
adapter
=
vocabularyAdapter
tracker
=
SelectionTracker
.
Builder
<
Long
>(
"mySelection"
,
recyclerView
,
StableIdKeyProvider
(
recyclerView
),
MyItemDetailsLookup
(
recyclerView
),
StorageStrategy
.
createLongStorage
()
).
withSelectionPredicate
(
SelectionPredicates
.
createSelectAnything
()
).
build
()
(
recyclerView
.
adapter
as
VocabularyAdapter
).
tracker
=
tracker
}
else
showToastNoWords
()
}
}
},
recyclerView
.
layoutManager
=
LockableLayoutManager
(
this
@MainActivity
)
recyclerView
.
adapter
=
VocabularyAdapter
(
VocabSet
(
mutableListOf
()),
this
@MainActivity
)
FirestoreManager
().
extractVocabulary
{
querySnapshot
->
run
{
if
(
querySnapshot
!=
null
&&
!
querySnapshot
.
isEmpty
)
{
vocabularyAdapter
=
VocabularyAdapter
(
VocabSet
.
createFromSnapshot
(
querySnapshot
),
this
@MainActivity
)
recyclerView
.
adapter
=
vocabularyAdapter
}
else
showToastNoWords
()
}
}
},
{
shortToast
(
this
,
getString
(
R
.
string
.
toast_empty_vocabulary
))
},
{
removeProgressBar
(
supportFragmentManager
)
},
this
)
}
...
...
@@ -137,7 +121,8 @@ class MainActivity : AppCompatActivity() {
hideKeyboard
()
try
{
FirestoreManager
().
saveWords
(
vocabularyAdapter
.
getModifiedWords
())
}
catch
(
ignored
:
Exception
)
{}
}
catch
(
ignored
:
Exception
)
{
}
}
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
import
androidx.recyclerview.selection.ItemDetailsLookup
import
androidx.recyclerview.widget.RecyclerView
class
MyItemDetailsLookup
(
private
val
recyclerView
:
RecyclerView
)
:
ItemDetailsLookup
<
Long
>()
{
class
MyItemDetailsLookup
(
private
val
recyclerView
:
RecyclerView
)
:
ItemDetailsLookup
<
Long
>()
{
override
fun
getItemDetails
(
event
:
MotionEvent
):
ItemDetails
<
Long
>?
{
val
view
=
recyclerView
.
findChildViewUnder
(
event
.
x
,
event
.
y
)
if
(
view
!=
null
)
{
return
(
recyclerView
.
getChildViewHolder
(
view
)
as
ViewHolder
)
.
getItemDetails
()
}
if
(
view
!=
null
)
return
(
recyclerView
.
getChildViewHolder
(
view
)
as
ViewHolder
).
getItemDetails
()
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
import
android.graphics.Color
import
android.view.View
import
android.widget.LinearLayout
import
android.widget.TextView
import
androidx.recyclerview.selection.ItemDetailsLookup
import
androidx.recyclerview.widget.RecyclerView
import
com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import
com.paktalin.vocabularynotebook.utils.Log
import
kotlinx.android.synthetic.main.word_item.view.*
class
ViewHolder
(
itemView
:
View
)
:
RecyclerView
.
ViewHolder
(
itemView
),
View
.
OnClickListener
{
override
fun
onClick
(
p0
:
View
?)
{
Log
.
d
(
TAG
,
"click"
)
p0
?.
setBackgroundColor
(
Color
.
GREEN
)
}
class
ViewHolder
(
itemView
:
View
)
:
RecyclerView
.
ViewHolder
(
itemView
)
{
private
val
tvWord
:
TextView
=
itemView
.
word
private
val
tvTranslation
:
TextView
=
itemView
.
translation
val
layout
:
LinearLayout
=
itemView
.
layout
fun
bind
(
wordPojo
:
WordPojo
,
position
:
Int
,
isActivated
:
Boolean
,
showPopupMenu
:
(
View
,
Int
)
->
Unit
)
{
tvWord
.
text
=
wordPojo
.
word
tvTranslation
.
text
=
wordPojo
.
translation
//
itemView.setOnClickListener { showPopupMenu(itemView, position) }
itemView
.
setOnClickListener
{
showPopupMenu
(
itemView
,
position
)
}
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
import
android.view.View
import
android.view.ViewGroup
import
androidx.appcompat.widget.PopupMenu
import
androidx.recyclerview.selection.Selection
import
androidx.recyclerview.selection.SelectionTracker
import
androidx.recyclerview.widget.RecyclerView
import
com.paktalin.vocabularynotebook.R
...
...
@@ -15,16 +16,19 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import
com.paktalin.vocabularynotebook.ui.fragments.EditWordFragment
import
com.paktalin.vocabularynotebook.utils.Log
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.Sort
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
>()
{
private
lateinit
var
recyclerView
:
RecyclerView
private
lateinit
var
viewHolder
:
ViewHolder
var
tracker
:
SelectionTracker
<
Long
>?
=
null
private
var
tracker
:
SelectionTracker
<
Long
>?
=
null
private
var
menu
=
true
init
{
setHasStableIds
(
true
)
...
...
@@ -33,14 +37,15 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
override
fun
getItemId
(
position
:
Int
):
Long
=
position
.
toLong
()
var
sort
:
Sort
=
Sort
.
BY_TIME
set
(
value
)
{
field
=
value
sort
()
}
set
(
value
)
{
field
=
value
sort
()
}
override
fun
onAttachedToRecyclerView
(
recyclerView
:
RecyclerView
)
{
super
.
onAttachedToRecyclerView
(
recyclerView
)
this
.
recyclerView
=
recyclerView
this
.
tracker
=
initializeTracker
(
recyclerView
)
{
s
->
wordsSelected
(
s
)
}
mainActivity
.
searchView
.
setOnQueryTextListener
(
OnQueryTextListener
(
this
@VocabularyAdapter
))
}
...
...
@@ -62,18 +67,25 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
//todo set click listener to menu
}
private
fun
showPopupMenu
(
v
:
View
,
position
:
Int
)
{
val
popup
=
PopupMenu
(
mainActivity
,
v
)
popup
.
menuInflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
popup
.
setOnMenuItemClickListener
{
if
(
it
.
itemId
==
R
.
id
.
option_delete
)
{
deleteWord
(
vocabulary
.
displayedAt
(
position
),
position
)
}
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
startEditFragment
(
v
,
vocabulary
.
displayedAt
(
position
))
}
true
if
(
menu
)
{
val
popup
=
PopupMenu
(
mainActivity
,
v
)
popup
.
menuInflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
popup
.
setOnMenuItemClickListener
{
if
(
it
.
itemId
==
R
.
id
.
option_delete
)
{
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
()
{
...
...
@@ -133,11 +145,15 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
return
vocabulary
.
getModified
()
}
fun
addTag
()
{
shortToast
(
mainActivity
,
mainActivity
.
getString
(
R
.
string
.
toast_select_words_to_tag
))
// TODO make words selectable
// TODO add complete button
// TODO collect selected words in a list
private
fun
wordsSelected
(
selection
:
Selection
<
Long
>)
{
if
(
selection
.
isEmpty
)
gone
(
mainActivity
.
btnSubmitLayout
)
else
{
visible
(
mainActivity
.
btnSubmitLayout
)
mainActivity
.
btnSubmit
.
setOnClickListener
{
// TODO add tag to the words
}
}
}
companion
object
{
...
...
app/src/main/res/menu/options_menu.xml
View file @
c882a289
...
...
@@ -23,9 +23,4 @@
android:id=
"@+id/sortByTranslation"
android:title=
"@string/sort_by_translation"
app:showAsAction=
"never"
/>
<item
android:id=
"@+id/addTag"
android:title=
"@string/add_tag"
app:showAsAction=
"never"
/>
</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