From feba98917a570e5f84ad225376f59e378732c4c4 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 20 Oct 2018 20:12:02 +0200 Subject: [PATCH] optimize the way some actionmenu button visibility is determined --- .../gallery/adapters/DirectoryAdapter.kt | 28 +++-------------- .../gallery/adapters/MediaAdapter.kt | 30 ++++--------------- 2 files changed, 9 insertions(+), 49 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 c1daa3a09..784e2e65e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -119,34 +119,14 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList) { - var hiddenCnt = 0 - var unhiddenCnt = 0 - selectedPaths.forEach { - if (File(it).doesThisOrParentHaveNoMedia()) { - hiddenCnt++ - } else { - unhiddenCnt++ - } - } - - menu.findItem(R.id.cab_hide).isVisible = unhiddenCnt > 0 - menu.findItem(R.id.cab_unhide).isVisible = hiddenCnt > 0 + menu.findItem(R.id.cab_hide).isVisible = selectedPaths.any { !File(it).doesThisOrParentHaveNoMedia() } + menu.findItem(R.id.cab_unhide).isVisible = selectedPaths.any { File(it).doesThisOrParentHaveNoMedia() } } private fun checkPinBtnVisibility(menu: Menu, selectedPaths: ArrayList) { val pinnedFolders = config.pinnedFolders - var pinnedCnt = 0 - var unpinnedCnt = 0 - selectedPaths.forEach { - if (pinnedFolders.contains(it)) { - pinnedCnt++ - } else { - unpinnedCnt++ - } - } - - menu.findItem(R.id.cab_pin).isVisible = unpinnedCnt > 0 - menu.findItem(R.id.cab_unpin).isVisible = pinnedCnt > 0 + menu.findItem(R.id.cab_pin).isVisible = selectedPaths.any { !pinnedFolders.contains(it) } + menu.findItem(R.id.cab_unpin).isVisible = selectedPaths.any { pinnedFolders.contains(it) } } private fun showProperties() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index 03511c29f..ed43c8dcb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -113,7 +113,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList 0 + findItem(R.id.cab_confirm_selection).isVisible = isAGetIntent && allowMultiplePicks && selectedKeys.isNotEmpty() findItem(R.id.cab_restore_recycle_bin_files).isVisible = selectedPaths.all { it.startsWith(activity.filesDir.absolutePath) } checkHideBtnVisibility(this, selectedItems) @@ -170,34 +170,14 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList) { - var hiddenCnt = 0 - var unhiddenCnt = 0 - selectedItems.forEach { - if (it.isHidden()) { - hiddenCnt++ - } else { - unhiddenCnt++ - } - } - val isInRecycleBin = selectedItems.firstOrNull()?.getIsInRecycleBin() == true - menu.findItem(R.id.cab_hide).isVisible = unhiddenCnt > 0 && !isInRecycleBin - menu.findItem(R.id.cab_unhide).isVisible = hiddenCnt > 0 && !isInRecycleBin + menu.findItem(R.id.cab_hide).isVisible = !isInRecycleBin && selectedItems.any { !it.isHidden() } + menu.findItem(R.id.cab_unhide).isVisible = !isInRecycleBin && selectedItems.any { it.isHidden() } } private fun checkFavoriteBtnVisibility(menu: Menu, selectedItems: ArrayList) { - var favoriteCnt = 0 - var nonFavoriteCnt = 0 - selectedItems.forEach { - if (it.isFavorite) { - favoriteCnt++ - } else { - nonFavoriteCnt++ - } - } - - menu.findItem(R.id.cab_add_to_favorites).isVisible = nonFavoriteCnt > 0 - menu.findItem(R.id.cab_remove_from_favorites).isVisible = favoriteCnt > 0 + menu.findItem(R.id.cab_add_to_favorites).isVisible = selectedItems.any { !it.isFavorite } + menu.findItem(R.id.cab_remove_from_favorites).isVisible = selectedItems.any { it.isFavorite } } private fun confirmSelection() {