From 44a71c032bfb865b9a7551b9cb4afb6f1c7f474d Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 13 May 2017 17:39:07 +0200 Subject: [PATCH] rewrite the Directory adapter, prepare it for drag-selection --- .../gallery/activities/MainActivity.kt | 1 + .../gallery/adapters/DirectoryAdapter.kt | 154 +++++++++++------- 2 files changed, 96 insertions(+), 59 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 0f15aeaf4..b5fdd60bf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -124,6 +124,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { override fun onStop() { super.onStop() config.temporarilyShowHidden = false + DirectoryAdapter.cleanup() } private fun tryloadGallery() { 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 c75e607c8..35ce4a8d1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -29,29 +29,36 @@ import java.util.* class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList, val listener: DirOperationsListener?, val itemClick: (Directory) -> Unit) : RecyclerView.Adapter() { - val multiSelector = MultiSelector() val views = ArrayList() val config = activity.config var pinnedFolders = config.pinnedFolders companion object { + val multiSelector = MultiSelector() var actMode: ActionMode? = null - val markedItems = HashSet() var foregroundColor = 0 var backgroundColor = 0 var animateGifs = true var itemCnt = 0 + var itemViews: HashMap = HashMap() + val selectedPositions: HashSet = HashSet() - fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) { - getProperView(itemView).isSelected = select - - if (pos == -1) - return + fun toggleItemSelection(select: Boolean, pos: Int) { + if (itemViews[pos] != null) + getProperView(itemViews[pos]!!).isSelected = select if (select) - markedItems.add(pos) + selectedPositions.add(pos) else - markedItems.remove(pos) + selectedPositions.remove(pos) + + if (selectedPositions.isEmpty()) { + actMode?.finish() + return + } + + updateTitle(selectedPositions.size) + actMode?.invalidate() } fun getProperView(itemView: View): View { @@ -64,6 +71,11 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList) { + fun checkHideBtnVisibility(menu: Menu) { var hiddenCnt = 0 var unhiddenCnt = 0 - positions.map { dirs[it].path }.forEach { + selectedPositions.map { dirs[it].path }.forEach { if (File(it).containsNoMedia()) hiddenCnt++ else @@ -130,11 +143,11 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList 0 } - fun checkPinBtnVisibility(menu: Menu, positions: List) { + fun checkPinBtnVisibility(menu: Menu) { val pinnedFolders = config.pinnedFolders var pinnedCnt = 0 var unpinnedCnt = 0 - positions.map { dirs[it].path }.forEach { + selectedPositions.map { dirs[it].path }.forEach { if (pinnedFolders.contains(it)) pinnedCnt++ else @@ -147,18 +160,17 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList() - selections.forEach { paths.add(dirs[it].path) } + selectedPositions.forEach { paths.add(dirs[it].path) } PropertiesDialog(activity, paths, config.shouldShowHidden) } } private fun renameDir() { - val path = dirs[multiSelector.selectedPositions[0]].path + val path = dirs[selectedPositions.first()].path val dir = File(path) if (activity.isAStorageRootFolder(dir.absolutePath)) { activity.toast(R.string.rename_folder_root) @@ -167,8 +179,8 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList() - val positions = multiSelector.selectedPositions - if (positions.isEmpty()) + if (selectedPositions.isEmpty()) return - positions.forEach { + selectedPositions.forEach { val dir = File(dirs[it].path) files.addAll(dir.listFiles().filter { it.isFile && it.isImageVideoGif() }) } @@ -246,8 +257,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList(selections.size) - val removeFolders = ArrayList(selections.size) + val folders = ArrayList(selectedPositions.size) + val removeFolders = ArrayList(selectedPositions.size) var needPermissionForPath = "" - selections.forEach { + selectedPositions.forEach { val path = dirs[it].path if (activity.needsStupidWritePermissions(path) && activity.config.treeUri.isEmpty()) { needPermissionForPath = path @@ -275,8 +284,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList { - val positions = multiSelector.selectedPositions - val paths = HashSet(positions.size) - positions.forEach { paths.add(dirs[it].path) } + val paths = HashSet(selectedPositions.size) + selectedPositions.forEach { paths.add(dirs[it].path) } return paths } @@ -304,7 +310,8 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList -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: (Directory) -> (Unit)) : SwappingHolder(view, MultiSelector()) { - fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, directory: Directory, pos: Int, isPinned: Boolean) - : View { + fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, directory: Directory, pos: Int, + isPinned: Boolean, listener: DirOperationsListener?): View { + itemViews.put(pos, itemView) itemView.apply { dir_name.text = directory.name photo_cnt.text = directory.mediaCnt.toString() dir_pin.visibility = if (isPinned) View.VISIBLE else View.GONE - toggleItemSelection(this, markedItems.contains(pos), pos) + toggleItemSelection(selectedPositions.contains(pos), pos) activity.loadImage(directory.tmb, dir_thumbnail) setOnClickListener { viewClicked(multiSelector, directory, pos) } setOnLongClickListener { if (!multiSelector.isSelectable) { activity.startSupportActionMode(multiSelectorCallback) - multiSelector.setSelected(this@ViewHolder, true) - updateTitle(multiSelector.selectedPositions.size) - toggleItemSelection(this, true, pos) + toggleItemSelection(true, pos) actMode?.invalidate() } + + listener!!.itemLongClicked(pos) true } @@ -346,17 +389,8 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList) + + fun itemLongClicked(position: Int) } }