From 7aa7b6e2daef42b1af239a3f8ff91ea58f884169 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 14 May 2017 10:40:35 +0200 Subject: [PATCH] add drag selection to media thumbnails screen --- .../gallery/activities/MediaActivity.kt | 34 ++++++------ .../gallery/adapters/MediaAdapter.kt | 53 +++++++++++++++++-- 2 files changed, 67 insertions(+), 20 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index 75904f413..ac2b800f8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -280,11 +280,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } } override fun selectItem(position: Int) { - + (media_grid.adapter as MediaAdapter).selectItem(position) } override fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) { - + (media_grid.adapter as MediaAdapter).selectRange(initialSelection, lastDraggedIndex, minReached, maxReached) } } } @@ -299,19 +299,6 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { invalidateOptionsMenu() } - override fun deleteFiles(files: ArrayList) { - val filtered = files.filter { it.exists() && it.isImageVideoGif() } as ArrayList - deleteFiles(filtered) { - if (!it) { - runOnUiThread { - toast(R.string.unknown_error_occurred) - } - } else if (mMedia.isEmpty()) { - finish() - } - } - } - private fun isSetWallpaperIntent() = intent.getBooleanExtra(SET_WALLPAPER_INTENT, false) override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { @@ -387,7 +374,24 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { config.saveFolderMedia(mPath, json) } + override fun deleteFiles(files: ArrayList) { + val filtered = files.filter { it.exists() && it.isImageVideoGif() } as ArrayList + deleteFiles(filtered) { + if (!it) { + runOnUiThread { + toast(R.string.unknown_error_occurred) + } + } else if (mMedia.isEmpty()) { + finish() + } + } + } + override fun refreshItems() { getMedia() } + + override fun itemLongClicked(position: Int) { + media_grid.setDragSelectActive(position) + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index a3299599f..636022163 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -24,6 +24,7 @@ import java.util.* class MediaAdapter(val activity: SimpleActivity, var media: MutableList, val listener: MediaOperationsListener?, val itemClick: (Medium) -> Unit) : RecyclerView.Adapter() { + val multiSelector = MultiSelector() val views = ArrayList() val config = activity.config @@ -41,9 +42,6 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, if (itemViews[pos] != null) getProperView(itemViews[pos]!!).isSelected = select - if (pos == -1) - return - if (select) selectedPositions.add(pos) else @@ -247,7 +245,8 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, } override fun onBindViewHolder(holder: ViewHolder, position: Int) { - views.add(holder.bindView(activity, multiSelectorMode, multiSelector, media[position], position)) + views.add(holder.bindView(activity, multiSelectorMode, multiSelector, media[position], position, listener)) + holder.itemView.tag = holder } override fun onViewRecycled(holder: ViewHolder?) { @@ -267,8 +266,48 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, notifyDataSetChanged() } + fun selectItem(pos: Int) { + toggleItemSelection(true, pos) + } + + fun selectRange(from: Int, to: Int, min: Int, max: Int) { + if (from == to) { + (min..max).filter { it != from } + .forEach { toggleItemSelection(false, it) } + return + } + + if (to < from) { + for (i in to..from) + toggleItemSelection(true, i) + + if (min > -1 && min < to) { + (min..to - 1).filter { it != from } + .forEach { toggleItemSelection(false, it) } + } + if (max > -1) { + for (i in from + 1..max) + toggleItemSelection(false, i) + } + } else { + for (i in from..to) + toggleItemSelection(true, i) + + if (max > -1 && max > to) { + (to + 1..max).filter { it != from } + .forEach { toggleItemSelection(false, it) } + } + + if (min > -1) { + for (i in min..from - 1) + toggleItemSelection(false, i) + } + } + } + class ViewHolder(val view: View, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) { - fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium, pos: Int): View { + fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium, + pos: Int, listener: MediaOperationsListener?): View { itemViews.put(pos, itemView) itemView.apply { play_outline.visibility = if (medium.video) View.VISIBLE else View.GONE @@ -283,6 +322,8 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, activity.startSupportActionMode(multiSelectorCallback) toggleItemSelection(true, pos) } + + listener!!.itemLongClicked(pos) true } @@ -312,5 +353,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, fun refreshItems() fun deleteFiles(files: ArrayList) + + fun itemLongClicked(position: Int) } }