From 3629f0ff55242fe84e0660a6c24c7f00e55cff33 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 18 Apr 2017 19:16:59 +0200 Subject: [PATCH] refactor the copy/mvoe functionality a bit --- .../gallery/activities/SimpleActivity.kt | 89 ++++++++++--------- .../gallery/activities/ViewPagerActivity.kt | 2 +- .../gallery/adapters/DirectoryAdapter.kt | 2 +- .../gallery/adapters/MediaAdapter.kt | 2 +- 4 files changed, 49 insertions(+), 46 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt index 165b0acd7..abfa4b48e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt @@ -14,59 +14,62 @@ import java.util.* open class SimpleActivity : BaseSimpleActivity() { var copyMoveCallback: (() -> Unit)? = null - fun copyMoveFilesTo(files: ArrayList, isCopyOperation: Boolean, callback: () -> Unit) { + fun tryCopyMoveFilesTo(files: ArrayList, isCopyOperation: Boolean, callback: () -> Unit) { if (files.isEmpty()) { toast(R.string.unknown_error_occurred) return } - val source = if (files[0].isFile) files[0].parent.trimEnd('/') else files[0].absolutePath - val currPath = source.trimEnd('/') - PickAlbumDialog(this, currPath) { - val destinationFolder = File(it) - if (currPath == it.trimEnd('/')) { - toast(R.string.source_and_destination_same) - return@PickAlbumDialog - } + val source = if (files[0].isFile) files[0].parent.trimEnd('/') else files[0].absolutePath.trimEnd('/') + PickAlbumDialog(this, source) { + copyMoveFilesTo(files, source, it, isCopyOperation, callback) + } + } - if (!destinationFolder.exists()) { - toast(R.string.invalid_destination) - return@PickAlbumDialog - } + private fun copyMoveFilesTo(files: ArrayList, source: String, destination: String, isCopyOperation: Boolean, callback: () -> Unit) { + if (source == destination) { + toast(R.string.source_and_destination_same) + return + } - if (files.size == 1) { - if (File(destinationFolder.absolutePath, files[0].name).exists()) { - toast(R.string.name_taken) - return@PickAlbumDialog - } - } + val destinationFolder = File(destination) + if (!destinationFolder.exists()) { + toast(R.string.invalid_destination) + return + } - handleSAFDialog(destinationFolder) { - copyMoveCallback = callback - if (isCopyOperation) { - toast(R.string.copying) - val pair = Pair, File>(files, destinationFolder) - CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) + if (files.size == 1) { + if (File(destinationFolder.absolutePath, files[0].name).exists()) { + toast(R.string.name_taken) + return + } + } + + handleSAFDialog(destinationFolder) { + copyMoveCallback = callback + if (isCopyOperation) { + toast(R.string.copying) + val pair = Pair, File>(files, destinationFolder) + CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) + } else { + if (isPathOnSD(source) || isPathOnSD(destinationFolder.absolutePath)) { + handleSAFDialog(files[0]) { + toast(R.string.moving) + val pair = Pair, File>(files, destinationFolder) + CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) + } } else { - if (isPathOnSD(currPath) || isPathOnSD(destinationFolder.absolutePath)) { - handleSAFDialog(files[0]) { - toast(R.string.moving) - val pair = Pair, File>(files, destinationFolder) - CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) - } - } else { - val updatedFiles = ArrayList(files.size * 2) - updatedFiles.addAll(files) - for (file in files) { - val destination = File(destinationFolder, file.name) - if (!destination.exists() && file.renameTo(destination)) - updatedFiles.add(destination) - } + val updatedFiles = ArrayList(files.size * 2) + updatedFiles.addAll(files) + for (file in files) { + val newFile = File(destinationFolder, file.name) + if (!newFile.exists() && file.renameTo(newFile)) + updatedFiles.add(newFile) + } - scanFiles(updatedFiles) { - runOnUiThread { - copyMoveListener.copySucceeded(true, files.size * 2 == updatedFiles.size) - } + scanFiles(updatedFiles) { + runOnUiThread { + copyMoveListener.copySucceeded(true, files.size * 2 == updatedFiles.size) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index 5d2037031..308303df2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -222,7 +222,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun copyMoveTo(isCopyOperation: Boolean) { val files = ArrayList(1).apply { add(getCurrentFile()) } - copyMoveFilesTo(files, isCopyOperation) { + tryCopyMoveFilesTo(files, isCopyOperation) { if (!isCopyOperation) { reloadViewPager() } 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 24fa047bc..07819dfa7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -236,7 +236,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList, val positions = multiSelector.selectedPositions positions.forEach { files.add(File(media[it].path)) } - activity.copyMoveFilesTo(files, isCopyOperation) { + activity.tryCopyMoveFilesTo(files, isCopyOperation) { if (!isCopyOperation) { listener?.refreshItems() }