Commit 12b51fb5 by Paktalin

Wrapped selectionTracker

parent ede577cb
...@@ -9,26 +9,20 @@ import android.view.WindowManager ...@@ -9,26 +9,20 @@ 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.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.MyItemKeyProvider
import com.paktalin.vocabularynotebook.ui.recycler_view.VocabularyAdapter import com.paktalin.vocabularynotebook.ui.recycler_view.VocabularyAdapter
import com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker.MySelectionTracker
import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import com.paktalin.vocabularynotebook.utils.* import com.paktalin.vocabularynotebook.utils.*
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.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.* import kotlinx.android.synthetic.main.content_main.*
import kotlinx.android.synthetic.main.tag.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
var searchView: SearchView? = null var searchView: SearchView? = null
lateinit var vocabularyAdapter: VocabularyAdapter lateinit var vocabularyAdapter: VocabularyAdapter
private var tracker: SelectionTracker<Long>? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -123,29 +117,10 @@ class MainActivity : AppCompatActivity() { ...@@ -123,29 +117,10 @@ class MainActivity : AppCompatActivity() {
} }
private fun setUpSelectionTracker() { private fun setUpSelectionTracker() {
tracker = SelectionTracker.Builder<Long>( vocabularyAdapter.selectionTracker = MySelectionTracker().apply {
"mySelection", init(recyclerView)
recyclerView, addObserver(this@MainActivity)
MyItemKeyProvider(recyclerView), }
MyItemDetailsLookup(recyclerView),
StorageStrategy.createLongStorage()
).withSelectionPredicate(
SelectionPredicates.createSelectAnything()
).build()
tracker?.addObserver(
object : SelectionTracker.SelectionObserver<Long>() {
override fun onSelectionChanged() {
super.onSelectionChanged()
if (tracker?.hasSelection()!!) {
Log.d(TAG, "selection ${tracker?.selection?.toList().toString()}")
visible(tagLayout)
} else {
gone(tagLayout)
}
}
})
(recyclerView.adapter as VocabularyAdapter).tracker = tracker
} }
override fun onPause() { override fun onPause() {
......
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
...@@ -2,7 +2,7 @@ package com.paktalin.vocabularynotebook.ui.recycler_view ...@@ -2,7 +2,7 @@ package com.paktalin.vocabularynotebook.ui.recycler_view
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
class OnQueryTextListener(var adapter: VocabularyAdapter) : SearchView.OnQueryTextListener { class SearchQueryTextListener(var adapter: VocabularyAdapter) : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean { override fun onQueryTextSubmit(query: String): Boolean {
adapter.filter(query) adapter.filter(query)
......
...@@ -7,12 +7,12 @@ import android.view.LayoutInflater ...@@ -7,12 +7,12 @@ 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.SelectionTracker
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.MainActivity 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.ui.recycler_view.selection_tracker.MySelectionTracker
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.vocabulary.ModifiedVocabulary import com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary
...@@ -23,7 +23,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -23,7 +23,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
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 lateinit var selectionTracker: MySelectionTracker
init { init {
setHasStableIds(true) setHasStableIds(true)
...@@ -40,7 +40,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -40,7 +40,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView) super.onAttachedToRecyclerView(recyclerView)
this.recyclerView = recyclerView this.recyclerView = recyclerView
mainActivity.searchView?.setOnQueryTextListener(OnQueryTextListener(this@VocabularyAdapter)) mainActivity.searchView?.setOnQueryTextListener(SearchQueryTextListener(this@VocabularyAdapter))
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
...@@ -54,8 +54,8 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi ...@@ -54,8 +54,8 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val wordItem = vocabulary.displayedAt(position) val wordItem = vocabulary.displayedAt(position)
tracker?.let { selectionTracker.let {
viewHolder.bind(wordItem, position, it.isSelected(position.toLong())) { view, p -> showPopupMenu(view, p) } viewHolder.bind(wordItem, position, it.selected(position.toLong())) { view, p -> showPopupMenu(view, p) }
} }
this.viewHolder = viewHolder this.viewHolder = viewHolder
//todo set click listener to menu //todo set click listener to menu
......
package com.paktalin.vocabularynotebook.ui.recycler_view package com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker
import android.view.MotionEvent import android.view.MotionEvent
import androidx.recyclerview.selection.ItemDetailsLookup import androidx.recyclerview.selection.ItemDetailsLookup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.paktalin.vocabularynotebook.ui.recycler_view.ViewHolder
class MyItemDetailsLookup(private val recyclerView: RecyclerView) : ItemDetailsLookup<Long>() { class MyItemDetailsLookup(private val recyclerView: RecyclerView) : ItemDetailsLookup<Long>() {
override fun getItemDetails(event: MotionEvent): ItemDetails<Long>? { override fun getItemDetails(event: MotionEvent): ItemDetails<Long>? {
......
package com.paktalin.vocabularynotebook.ui.recycler_view package com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker
import androidx.recyclerview.selection.ItemKeyProvider import androidx.recyclerview.selection.ItemKeyProvider
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
......
package com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker
import androidx.recyclerview.selection.SelectionPredicates
import androidx.recyclerview.selection.SelectionTracker
import androidx.recyclerview.selection.StorageStrategy
import androidx.recyclerview.widget.RecyclerView
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.Log
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.visible
import kotlinx.android.synthetic.main.tag.*
class MySelectionTracker {
private var tracker: SelectionTracker<Long>? = null
fun init(recyclerView: RecyclerView) {
tracker = SelectionTracker.Builder<Long>(
"mySelection",
recyclerView,
MyItemKeyProvider(recyclerView),
MyItemDetailsLookup(recyclerView),
StorageStrategy.createLongStorage()
).withSelectionPredicate(
SelectionPredicates.createSelectAnything()
).build()
}
fun addObserver(mainActivity: MainActivity) {
tracker?.addObserver(object : SelectionTracker.SelectionObserver<Long>() {
override fun onSelectionChanged() {
super.onSelectionChanged()
if (tracker?.hasSelection()!!) {
// TODO disable popup menu
Log.d(TAG, "selection ${tracker?.selection?.toList().toString()}")
visible(mainActivity.tagLayout)
} else {
gone(mainActivity.tagLayout)
}
}
})
}
fun selected(position: Long): Boolean {
tracker?.let {
return it.isSelected(position)
}
return false
}
companion object {
private val TAG = "VN/" + MySelectionTracker::class.simpleName
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment