Skip to content

Commit

Permalink
Reorder SavedFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
teccheck committed May 15, 2023
1 parent 67c038f commit 95a04c7
Showing 1 changed file with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,58 +32,35 @@ class SavedFragment : Fragment() {
// onDestroyView.
private val binding get() = _binding!!


private lateinit var viewModel: SavedViewModel
private lateinit var adapter: RecyclerAdapter
private lateinit var selectionTracker: SelectionTracker<Long>

private var actionMode: ActionMode? = null

// These two only forward to methods within SavedFragment
private val selectionObserver = object : SelectionTracker.SelectionObserver<Long>() {
override fun onSelectionChanged() {
super.onSelectionChanged()

if (selectionTracker.hasSelection()) {
if (actionMode == null) {
actionMode =
(activity as AppCompatActivity).startSupportActionMode(actionModeCallback)
}

actionMode?.title =
getString(R.string.items_selected, selectionTracker.selection.size())
} else {
actionMode?.finish()
}
this@SavedFragment.onSelectionChanged()
}
}

private val actionModeCallback = object : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
activity?.menuInflater?.inflate(R.menu.fragment_saved_contextual_appbar_menu, menu)
return true
return this@SavedFragment.onCreateActionMode(menu)
}

override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}

override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
return when (item?.itemId) {
R.id.delete -> {
deleteItems(selectionTracker.selection.toList())
viewModel.fetchSongs()
actionMode?.finish()
selectionTracker.clearSelection()
true
}

else -> false
}
return this@SavedFragment.onActionItemClicked(mode, item)
}

override fun onDestroyActionMode(mode: ActionMode?) {
actionMode = null
selectionTracker.clearSelection()
this@SavedFragment.onDestroyActionMode()
}
}

Expand All @@ -99,7 +76,7 @@ class SavedFragment : Fragment() {
binding.recyclerView.layoutManager = LinearLayoutManager(context)

selectionTracker = SelectionTracker.Builder(
"my-selection-id",
SELECTION_ID,
binding.recyclerView,
StableIdKeyProvider(binding.recyclerView),
DetailsLookup(binding.recyclerView),
Expand All @@ -110,7 +87,6 @@ class SavedFragment : Fragment() {
adapter.setSelectionTracker(selectionTracker)

viewModel.songs.observe(viewLifecycleOwner) { result ->
Log.d(TAG, "test")
if (result is Success) {
adapter.setSongs(result.value)
}
Expand Down Expand Up @@ -143,6 +119,43 @@ class SavedFragment : Fragment() {
return false
}

private fun onSelectionChanged() {
if (!selectionTracker.hasSelection()) {
actionMode?.finish()
return
}

if (actionMode == null) {
actionMode = (activity as AppCompatActivity).startSupportActionMode(actionModeCallback)
}

actionMode?.title = getString(R.string.items_selected, selectionTracker.selection.size())
}

private fun onCreateActionMode(menu: Menu?): Boolean {
activity?.menuInflater?.inflate(R.menu.fragment_saved_contextual_appbar_menu, menu)
return true
}

private fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
return when (item?.itemId) {
R.id.delete -> {
deleteItems(selectionTracker.selection.toList())
viewModel.fetchSongs()
selectionTracker.clearSelection()
mode?.finish()
true
}

else -> false
}
}

private fun onDestroyActionMode() {
actionMode = null
selectionTracker.clearSelection()
}

private fun viewSong(id: Long) {
Log.d(TAG, "Show song $id")
val bundle = Bundle()
Expand All @@ -159,5 +172,6 @@ class SavedFragment : Fragment() {

companion object {
private const val TAG = "SavedFragment"
private const val SELECTION_ID = "song-selection"
}
}

0 comments on commit 95a04c7

Please sign in to comment.