From 192d963986c9fe637a88fc6a0ca18affe80fd8c9 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 20 Nov 2016 22:02:17 +0100 Subject: [PATCH] make sure the proper items are marked at long click --- .../gallery/adapters/DirectoryAdapter.kt | 31 +++++++++++++------ .../gallery/adapters/MediaAdapter.kt | 25 ++++++++++----- 2 files changed, 39 insertions(+), 17 deletions(-) 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 d90654c4a..46e433cb2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -36,12 +36,21 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList() - fun toggleItemSelection(itemView: View, select: Boolean) { + fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) itemView.dir_frame.isSelected = select else itemView.dir_thumbnail.isSelected = select + + if (pos == -1) + return + + if (select) + markedItems.add(pos) + else + markedItems.remove(pos) } } @@ -108,6 +117,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList (Unit)) : SwappingHolder(view, MultiSelector()) { - fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, directory: Directory): View { + fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, directory: Directory, pos: Int): View { itemView.dir_name.text = directory.name itemView.photo_cnt.text = directory.mediaCnt.toString() + toggleItemSelection(itemView, markedItems.contains(pos), pos) val tmb = directory.thumbnail val timestampSignature = StringSignature(directory.timestamp.toString()) @@ -224,13 +235,13 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList, companion object { var actMode: ActionMode? = null var displayFilenames = false + val markedItems = HashSet() - fun toggleItemSelection(itemView: View, select: Boolean) { + fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) itemView.medium_thumbnail_holder.isSelected = select else itemView.medium_thumbnail.isSelected = select + + if (pos == -1) + return + + if (select) + markedItems.add(pos) + else + markedItems.remove(pos) } } @@ -80,6 +89,7 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, override fun onDestroyActionMode(actionMode: ActionMode?) { super.onDestroyActionMode(actionMode) views.forEach { toggleItemSelection(it, false) } + markedItems.clear() } } @@ -154,7 +164,7 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, } override fun onBindViewHolder(holder: ViewHolder, position: Int) { - views.add(holder.bindView(activity, multiSelectorMode, multiSelector, media[position])) + views.add(holder.bindView(activity, multiSelectorMode, multiSelector, media[position], position)) } override fun getItemCount() = media.size @@ -165,10 +175,11 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, } class ViewHolder(view: View, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) { - fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium): View { + fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium, pos: Int): View { itemView.play_outline.visibility = if (medium.isVideo) View.VISIBLE else View.GONE itemView.file_name.beVisibleIf(displayFilenames) itemView.file_name.text = medium.name + toggleItemSelection(itemView, markedItems.contains(pos), pos) val path = medium.path val timestampSignature = StringSignature(medium.timestamp.toString()) @@ -179,13 +190,13 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, .placeholder(R.color.tmb_background).centerCrop().crossFade().into(itemView.medium_thumbnail) } - itemView.setOnClickListener { viewClicked(multiSelector, medium) } + itemView.setOnClickListener { viewClicked(multiSelector, medium, pos) } itemView.setOnLongClickListener { if (!multiSelector.isSelectable) { activity.startSupportActionMode(multiSelectorCallback) multiSelector.setSelected(this, true) actMode?.title = multiSelector.selectedPositions.size.toString() - toggleItemSelection(itemView, true) + toggleItemSelection(itemView, true, pos) actMode?.invalidate() } true @@ -194,11 +205,11 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, return itemView } - fun viewClicked(multiSelector: MultiSelector, medium: Medium) { + fun viewClicked(multiSelector: MultiSelector, medium: Medium, pos: Int) { if (multiSelector.isSelectable) { val isSelected = multiSelector.selectedPositions.contains(layoutPosition) multiSelector.setSelected(this, !isSelected) - toggleItemSelection(itemView, !isSelected) + toggleItemSelection(itemView, !isSelected, pos) val selectedCnt = multiSelector.selectedPositions.size if (selectedCnt == 0) {