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() }