Commit 6a158ecd authored by Nicolas Pomepuy's avatar Nicolas Pomepuy
Browse files

Batch select implementation for Videos / History / Browser

parent f1bb3f03
......@@ -26,27 +26,20 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.tools.MultiSelectAdapter
import org.videolan.tools.MultiSelectHelper
import org.videolan.vlc.databinding.HistoryItemBinding
import org.videolan.vlc.gui.helpers.SelectorViewHolder
import org.videolan.vlc.gui.helpers.getMediaIconDrawable
import org.videolan.vlc.interfaces.IEventsHandler
import org.videolan.vlc.util.UPDATE_SELECTION
import org.videolan.vlc.util.Util
import java.util.*
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
class HistoryAdapter(private val mEventsHandler: IEventsHandler) : DiffUtilAdapter<MediaWrapper, HistoryAdapter.ViewHolder>() {
class HistoryAdapter(private val mEventsHandler: IEventsHandler) : DiffUtilAdapter<MediaWrapper, HistoryAdapter.ViewHolder>(), MultiSelectAdapter<MediaWrapper> {
private var mLayoutInflater: LayoutInflater? = null
val selection: List<MediaWrapper>
get() {
val selection = LinkedList<MediaWrapper>()
for (media in dataset) {
if (media.hasStateFlags(MediaLibraryItem.FLAG_SELECTED))
selection.add(media)
}
return selection
}
var multiSelectHelper: MultiSelectHelper<MediaWrapper> = MultiSelectHelper(this, UPDATE_SELECTION)
inner class ViewHolder(binding: HistoryItemBinding) : SelectorViewHolder<HistoryItemBinding>(binding) {
......@@ -83,17 +76,16 @@ class HistoryAdapter(private val mEventsHandler: IEventsHandler) : DiffUtilAdapt
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val media = getItem(position)
val isSelected = media.hasStateFlags(MediaLibraryItem.FLAG_SELECTED)
holder.binding.media = media
holder.binding.cover = getMediaIconDrawable(holder.itemView.context, media.type)
holder.selectView(isSelected)
holder.selectView(multiSelectHelper.isSelected(position))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: List<Any>) {
if (Util.isListEmpty(payloads))
super.onBindViewHolder(holder, position, payloads)
else
holder.selectView((payloads[0] as MediaLibraryItem).hasStateFlags(MediaLibraryItem.FLAG_SELECTED))
holder.selectView(multiSelectHelper.isSelected(position))
}
override fun getItemId(arg0: Int): Long {
......
......@@ -35,6 +35,7 @@ import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.tools.KeyHelper
import org.videolan.tools.MultiSelectHelper
import org.videolan.vlc.R
import org.videolan.vlc.gui.browser.MediaBrowserFragment
import org.videolan.vlc.gui.helpers.UiTools
......@@ -51,6 +52,7 @@ private const val TAG = "VLC/HistoryFragment"
@ExperimentalCoroutinesApi
class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHistory, SwipeRefreshLayout.OnRefreshListener, IEventsHandler {
private lateinit var multiSelectHelper: MultiSelectHelper<MediaWrapper>
private val historyAdapter: HistoryAdapter = HistoryAdapter(this)
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
......@@ -61,10 +63,12 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProviders.of(requireActivity(), HistoryModel.Factory(requireContext())).get(HistoryModel::class.java)
viewModel.dataset.observe(this, Observer<List<MediaWrapper>> { list -> list?.let {
historyAdapter.update(it)
updateEmptyView()
} })
viewModel.dataset.observe(this, Observer<List<MediaWrapper>> { list ->
list?.let {
historyAdapter.update(it)
updateEmptyView()
}
})
}
override fun onStart() {
......@@ -80,6 +84,8 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
list.nextFocusLeftId = android.R.id.list
list.nextFocusRightId = android.R.id.list
list.nextFocusForwardId = android.R.id.list
multiSelectHelper = historyAdapter.multiSelectHelper
list.requestFocus()
registerForContextMenu(list)
swipeRefreshLayout!!.setOnRefreshListener(this)
......@@ -146,7 +152,7 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
}
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
val selectionCount = historyAdapter.selection.size
val selectionCount = multiSelectHelper.getSelectionCount()
if (selectionCount == 0) {
stopActionMode()
return false
......@@ -157,7 +163,7 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
}
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
val selection = historyAdapter.selection
val selection = multiSelectHelper.getSelection()
if (selection.isNotEmpty()) {
when (item.itemId) {
R.id.action_history_play -> MediaUtils.openList(activity, selection, 0)
......@@ -175,14 +181,7 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
override fun onDestroyActionMode(mode: ActionMode) {
actionMode = null
var index = -1
for (media in viewModel.dataset.value) {
++index
if (media.hasStateFlags(MediaLibraryItem.FLAG_SELECTED)) {
media.removeStateFlags(MediaLibraryItem.FLAG_SELECTED)
historyAdapter.notifyItemChanged(index, media)
}
}
multiSelectHelper.clearSelection()
}
override fun onClick(v: View, position: Int, item: MediaLibraryItem) {
......@@ -191,7 +190,7 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
return
}
if (actionMode != null) {
item.toggleStateFlag(MediaLibraryItem.FLAG_SELECTED)
multiSelectHelper.toggleSelection(position)
historyAdapter.notifyItemChanged(position, item)
invalidateActionMode()
return
......@@ -201,14 +200,13 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
}
override fun onLongClick(v: View, position: Int, item: MediaLibraryItem): Boolean {
if (actionMode != null) return false
item.toggleStateFlag(MediaLibraryItem.FLAG_SELECTED)
multiSelectHelper.toggleSelection(position, true)
historyAdapter.notifyItemChanged(position, item)
startActionMode()
if (actionMode == null) startActionMode()
invalidateActionMode()
return true
}
override fun onImageClick(v: View, position: Int, item: MediaLibraryItem) {
if (actionMode != null) {
onClick(v, position, item)
......
......@@ -405,14 +405,13 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
}
override fun onLongClick(v: View, position: Int, item: MediaLibraryItem): Boolean {
if (actionMode != null || item.itemType != MediaLibraryItem.TYPE_MEDIA) return false
if (item.itemType != MediaLibraryItem.TYPE_MEDIA) return false
val mediaWrapper = item as MediaWrapper
if (mediaWrapper.type == MediaWrapper.TYPE_AUDIO ||
mediaWrapper.type == MediaWrapper.TYPE_VIDEO ||
mediaWrapper.type == MediaWrapper.TYPE_DIR) {
if (actionMode != null) return false
adapter.multiSelectHelper.toggleSelection(position, true)
startActionMode()
if (actionMode == null) startActionMode()
} else onCtxClick(v, position, item)
return true
}
......
......@@ -335,9 +335,8 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
}
override fun onLongClick(v: View, position: Int, item: MediaLibraryItem): Boolean {
if (actionMode != null) return false
multiSelectHelper.toggleSelection(position, true)
startActionMode()
if (actionMode == null) startActionMode()
return true
}
......
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 to comment