add drag selection to media thumbnails screen

This commit is contained in:
tibbi 2017-05-14 10:40:35 +02:00
parent 5ae11b8928
commit 7aa7b6e2da
2 changed files with 67 additions and 20 deletions

View file

@ -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<File>) {
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<File>) {
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)
}
}

View file

@ -24,6 +24,7 @@ import java.util.*
class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>, val listener: MediaOperationsListener?, val itemClick: (Medium) -> Unit) :
RecyclerView.Adapter<MediaAdapter.ViewHolder>() {
val multiSelector = MultiSelector()
val views = ArrayList<View>()
val config = activity.config
@ -41,9 +42,6 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
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<Medium>,
}
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<Medium>,
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<Medium>,
activity.startSupportActionMode(multiSelectorCallback)
toggleItemSelection(true, pos)
}
listener!!.itemLongClicked(pos)
true
}
@ -312,5 +353,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
fun refreshItems()
fun deleteFiles(files: ArrayList<File>)
fun itemLongClicked(position: Int)
}
}