From 8af61d89bbbdad84675fea50bb4989cc4e740aea Mon Sep 17 00:00:00 2001 From: darthpaul Date: Thu, 24 Feb 2022 16:27:33 +0000 Subject: [PATCH] Revert changes and handle recycler view animation with SDK-30 delete --- .../gallery/pro/activities/MediaActivity.kt | 14 ++--- .../gallery/pro/activities/SearchActivity.kt | 15 +++-- .../gallery/pro/adapters/DirectoryAdapter.kt | 56 ++++++++++--------- .../gallery/pro/adapters/MediaAdapter.kt | 30 ++++++---- .../pro/interfaces/MediaOperationsListener.kt | 2 +- 5 files changed, 64 insertions(+), 53 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt index a4f9ba824..c8d077376 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt @@ -855,7 +855,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { } } - override fun tryDeleteFiles(fileDirItems: ArrayList, onDelete: () -> Unit) { + override fun tryDeleteFiles(fileDirItems: ArrayList) { val filtered = fileDirItems.filter { !getIsPathDirectory(it.path) && it.path.isMediaFile() } as ArrayList if (filtered.isEmpty()) { return @@ -867,7 +867,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { movePathsInRecycleBin(filtered.map { it.path } as ArrayList) { if (it) { - deleteFilteredFiles(filtered, onDelete) + deleteFilteredFiles(filtered) } else { toast(R.string.unknown_error_occurred) } @@ -875,17 +875,15 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { } else { val deletingItems = resources.getQuantityString(R.plurals.deleting_items, filtered.size, filtered.size) toast(deletingItems) - deleteFilteredFiles(filtered, onDelete) + deleteFilteredFiles(filtered) } } private fun shouldSkipAuthentication() = intent.getBooleanExtra(SKIP_AUTHENTICATION, false) - private fun deleteFilteredFiles(filtered: ArrayList, onDelete: () -> Unit) { - deleteFiles(filtered) { deleteSuccess -> - if (deleteSuccess) { - onDelete.invoke() - } else { + private fun deleteFilteredFiles(filtered: ArrayList) { + deleteFiles(filtered) { + if (!it) { toast(R.string.unknown_error_occurred) return@deleteFiles } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt index fa4ec65ef..73af513a2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt @@ -25,6 +25,7 @@ import com.simplemobiletools.gallery.pro.helpers.* import com.simplemobiletools.gallery.pro.interfaces.MediaOperationsListener import com.simplemobiletools.gallery.pro.models.Medium import com.simplemobiletools.gallery.pro.models.ThumbnailItem +import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_search.* import java.io.File @@ -251,7 +252,7 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener { startAsyncTask(true) } - override fun tryDeleteFiles(fileDirItems: ArrayList, onDelete: () -> Unit) { + override fun tryDeleteFiles(fileDirItems: ArrayList) { val filtered = fileDirItems.filter { File(it.path).isFile && it.path.isMediaFile() } as ArrayList if (filtered.isEmpty()) { return @@ -263,7 +264,7 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener { movePathsInRecycleBin(filtered.map { it.path } as ArrayList) { if (it) { - deleteFilteredFiles(filtered, onDelete) + deleteFilteredFiles(filtered) } else { toast(R.string.unknown_error_occurred) } @@ -271,15 +272,13 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener { } else { val deletingItems = resources.getQuantityString(R.plurals.deleting_items, filtered.size, filtered.size) toast(deletingItems) - deleteFilteredFiles(filtered, onDelete) + deleteFilteredFiles(filtered) } } - private fun deleteFilteredFiles(filtered: ArrayList, onDelete: () -> Unit) { - deleteFiles(filtered) { deleteSuccess -> - if (deleteSuccess) { - onDelete.invoke() - } else { + private fun deleteFilteredFiles(filtered: ArrayList) { + deleteFiles(filtered) { + if (!it) { toast(R.string.unknown_error_occurred) return@deleteFiles } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt index 84e92b726..85556a175 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt @@ -626,37 +626,43 @@ class DirectoryAdapter( return@handleSAFDialog } - var foldersToDelete = ArrayList(selectedKeys.size) - selectedDirs.forEach { - if (it.areFavorites() || it.isRecycleBin()) { - if (it.isRecycleBin()) { - tryEmptyRecycleBin(false) + activity.handleSAFDeleteSdk30Dialog(SAFPath){ + if (!it) { + return@handleSAFDeleteSdk30Dialog + } + + var foldersToDelete = ArrayList(selectedKeys.size) + selectedDirs.forEach { + if (it.areFavorites() || it.isRecycleBin()) { + if (it.isRecycleBin()) { + tryEmptyRecycleBin(false) + } else { + ensureBackgroundThread { + activity.mediaDB.clearFavorites() + activity.favoritesDB.clearFavorites() + listener?.refreshItems() + } + } + + if (selectedKeys.size == 1) { + finishActMode() + } } else { - ensureBackgroundThread { - activity.mediaDB.clearFavorites() - activity.favoritesDB.clearFavorites() - listener?.refreshItems() + foldersToDelete.add(File(it.path)) + } + } + + if (foldersToDelete.size == 1) { + activity.handleLockedFolderOpening(foldersToDelete.first().absolutePath) { success -> + if (success) { + listener?.deleteFolders(foldersToDelete) } } - - if (selectedKeys.size == 1) { - finishActMode() - } } else { - foldersToDelete.add(File(it.path)) + foldersToDelete = foldersToDelete.filter { !config.isFolderProtected(it.absolutePath) }.toMutableList() as ArrayList + listener?.deleteFolders(foldersToDelete) } } - - if (foldersToDelete.size == 1) { - activity.handleLockedFolderOpening(foldersToDelete.first().absolutePath) { success -> - if (success) { - listener?.deleteFolders(foldersToDelete) - } - } - } else { - foldersToDelete = foldersToDelete.filter { !config.isFolderProtected(it.absolutePath) }.toMutableList() as ArrayList - listener?.deleteFolders(foldersToDelete) - } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt index 036cf82ec..78d68bca3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt @@ -459,27 +459,35 @@ class MediaAdapter( return } - val SAFPath = getSelectedPaths().firstOrNull { activity.needsStupidWritePermissions(it) } ?: getFirstSelectedItemPath() ?: return + val selectedItems = getSelectedItems() + val selectedPaths = selectedItems.map { it.path } as ArrayList + val SAFPath = selectedPaths.firstOrNull { activity.needsStupidWritePermissions(it) } ?: getFirstSelectedItemPath() ?: return activity.handleSAFDialog(SAFPath) { if (!it) { return@handleSAFDialog } - val fileDirItems = ArrayList(selectedKeys.size) - val removeMedia = ArrayList(selectedKeys.size) - val positions = getSelectedItemPositions() + val sdk30SafPath = selectedPaths.firstOrNull { activity.isAccessibleWithSAFSdk30(it) } ?: getFirstSelectedItemPath() ?: return@handleSAFDialog + activity.handleSAFDeleteSdk30Dialog(sdk30SafPath){ + if (!it) { + return@handleSAFDeleteSdk30Dialog + } - getSelectedItems().forEach { - fileDirItems.add(FileDirItem(it.path, it.name)) - removeMedia.add(it) - } + val fileDirItems = ArrayList(selectedKeys.size) + val removeMedia = ArrayList(selectedKeys.size) + val positions = getSelectedItemPositions() - media.removeAll(removeMedia) - listener?.tryDeleteFiles(fileDirItems){ + selectedItems.forEach { + fileDirItems.add(FileDirItem(it.path, it.name)) + removeMedia.add(it) + } + + media.removeAll(removeMedia) + listener?.tryDeleteFiles(fileDirItems) + listener?.updateMediaGridDecoration(media) removeSelectedItems(positions) currentMediaHash = media.hashCode() } - listener?.updateMediaGridDecoration(media) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/MediaOperationsListener.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/MediaOperationsListener.kt index b25c92e01..997adc44f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/MediaOperationsListener.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/MediaOperationsListener.kt @@ -6,7 +6,7 @@ import com.simplemobiletools.gallery.pro.models.ThumbnailItem interface MediaOperationsListener { fun refreshItems() - fun tryDeleteFiles(fileDirItems: ArrayList, onDelete: () -> Unit) + fun tryDeleteFiles(fileDirItems: ArrayList) fun selectedPaths(paths: ArrayList)