diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index 9c2236863..794bd15f0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -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, val itemClick: (Directory) -> Unit) : +class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList, val itemClick: (Directory) -> Unit) : RecyclerView.Adapter() { - override fun getItemCount() = mDirs.size + val multiSelector = MultiSelector() + val views = ArrayList() + + 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, 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) + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt index 132112229..cfdfb5eb9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt @@ -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) { - DirectoryAdapter(context, dirs) { + DirectoryAdapter(activity, dirs) { listener.onSuccess(it.path) dialog.dismiss() } diff --git a/app/src/main/res/drawable-v21/selector.xml b/app/src/main/res/drawable-v21/selector.xml index 4d61c7b4d..a24cfe2b1 100644 --- a/app/src/main/res/drawable-v21/selector.xml +++ b/app/src/main/res/drawable-v21/selector.xml @@ -4,7 +4,7 @@ + android:state_selected="true"/> diff --git a/app/src/main/res/drawable/selector.xml b/app/src/main/res/drawable/selector.xml index 480db1e64..c0b842372 100644 --- a/app/src/main/res/drawable/selector.xml +++ b/app/src/main/res/drawable/selector.xml @@ -1,5 +1,5 @@ - +