allow selecting multiple directories

This commit is contained in:
tibbi 2016-11-17 10:35:51 +01:00
parent 06f345240a
commit 258c49eeb8
4 changed files with 81 additions and 16 deletions

View file

@ -1,27 +1,62 @@
package com.simplemobiletools.gallery.adapters
import android.content.Context
import android.support.v7.view.ActionMode
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.*
import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback
import com.bignerdranch.android.multiselector.MultiSelector
import com.bignerdranch.android.multiselector.SwappingHolder
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.signature.StringSignature
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.models.Directory
import kotlinx.android.synthetic.main.directory_item.view.*
import kotlinx.android.synthetic.main.directory_tmb.view.*
import java.util.*
class DirectoryAdapter(val mContext: Context, val mDirs: MutableList<Directory>, val itemClick: (Directory) -> Unit) :
class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Directory>, val itemClick: (Directory) -> Unit) :
RecyclerView.Adapter<DirectoryAdapter.ViewHolder>() {
override fun getItemCount() = mDirs.size
val multiSelector = MultiSelector()
val views = ArrayList<View>()
companion object {
var actMode: ActionMode? = null
}
val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem): Boolean {
return when (item.itemId) {
R.id.cab_edit -> {
true
}
else -> false
}
}
override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean {
super.onCreateActionMode(actionMode, menu)
actMode = actionMode
activity.menuInflater.inflate(R.menu.cab_directories, menu)
return true
}
override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu?): Boolean {
val menuItem = menu?.findItem(R.id.cab_edit)
menuItem?.isVisible = multiSelector.selectedPositions.size == 1
return true
}
override fun onDestroyActionMode(actionMode: ActionMode?) {
super.onDestroyActionMode(actionMode)
views.forEach { it.dir_thumbnail.isSelected = false }
}
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindView(mContext, mDirs[position])
views.add(holder.bindView(activity, multiSelectorMode, multiSelector, dirs[position]))
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
@ -29,22 +64,52 @@ class DirectoryAdapter(val mContext: Context, val mDirs: MutableList<Directory>,
return ViewHolder(view, itemClick)
}
override fun getItemCount() = dirs.size
class ViewHolder(view: View, val itemClick: (Directory) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
fun bindView(context: Context, directory: Directory) {
fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, directory: Directory): View {
itemView.dir_name.text = directory.name
itemView.photo_cnt.text = directory.mediaCnt.toString()
val tmb = directory.thumbnail
val timestampSignature = StringSignature(directory.timestamp.toString())
if (tmb.endsWith(".gif")) {
Glide.with(context).load(tmb).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature)
Glide.with(activity.applicationContext).load(tmb).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature)
.placeholder(R.color.tmb_background).centerCrop().crossFade().into(itemView.dir_thumbnail)
} else {
Glide.with(context).load(tmb).diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature)
Glide.with(activity.applicationContext).load(tmb).diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature)
.placeholder(R.color.tmb_background).centerCrop().crossFade().into(itemView.dir_thumbnail)
}
itemView.setOnClickListener { itemClick(directory) }
itemView.setOnClickListener { viewClicked(multiSelector, directory) }
itemView.setOnLongClickListener {
if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback)
multiSelector.setSelected(this, true)
actMode?.title = multiSelector.selectedPositions.size.toString()
itemView.dir_thumbnail.isSelected = true
}
true
}
return itemView
}
fun viewClicked(multiSelector: MultiSelector, directory: Directory) {
if (multiSelector.isSelectable) {
val isSelected = multiSelector.selectedPositions.contains(layoutPosition)
multiSelector.setSelected(this, !isSelected)
itemView.dir_thumbnail.isSelected = !isSelected
val selectedCnt = multiSelector.selectedPositions.size
if (selectedCnt == 0) {
actMode?.finish()
} else {
actMode?.title = selectedCnt.toString()
}
actMode?.invalidate()
} else {
itemClick(directory)
}
}
}
}

View file

@ -1,15 +1,15 @@
package com.simplemobiletools.gallery.dialogs
import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.adapters.DirectoryAdapter
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.models.Directory
import java.util.*
class PickAlbumDialog(val activity: Activity, val listener: OnPickAlbumListener) : GetDirectoriesAsynctask.GetDirectoriesListener {
class PickAlbumDialog(val activity: SimpleActivity, val listener: OnPickAlbumListener) : GetDirectoriesAsynctask.GetDirectoriesListener {
val context = activity.applicationContext
var dialog: AlertDialog
@ -29,7 +29,7 @@ class PickAlbumDialog(val activity: Activity, val listener: OnPickAlbumListener)
}
override fun gotDirectories(dirs: ArrayList<Directory>) {
DirectoryAdapter(context, dirs) {
DirectoryAdapter(activity, dirs) {
listener.onSuccess(it.path)
dialog.dismiss()
}

View file

@ -4,7 +4,7 @@
<selector>
<item
android:drawable="@color/activated_item_foreground"
android:state_activated="true"/>
android:state_selected="true"/>
</selector>
</item>
<item>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/selected_pressed_mask" android:state_pressed="true"/>
<item android:drawable="@drawable/selected_mask" android:state_activated="true"/>
<item android:drawable="@drawable/selected_mask" android:state_selected="true"/>
</selector>