From 409ecbe4b548ff70eeaf112d0809b7c81e8920a8 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 18 Apr 2017 19:31:22 +0200 Subject: [PATCH] use the copy/move functionality from the commons library --- app/build.gradle | 2 +- .../gallery/activities/SimpleActivity.kt | 78 +------------------ 2 files changed, 3 insertions(+), 77 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index cf23c9e3e..09212a2ef 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,7 +32,7 @@ android { } dependencies { - compile 'com.simplemobiletools:commons:2.16.9' + compile 'com.simplemobiletools:commons:2.17.0' compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0' compile 'com.theartofdev.edmodo:android-image-cropper:2.4.0' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' 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 abfa4b48e..dc1b3739f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt @@ -1,10 +1,6 @@ package com.simplemobiletools.gallery.activities -import android.support.v4.util.Pair import com.simplemobiletools.commons.activities.BaseSimpleActivity -import com.simplemobiletools.commons.asynctasks.CopyMoveTask -import com.simplemobiletools.commons.extensions.isPathOnSD -import com.simplemobiletools.commons.extensions.scanFiles import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.dialogs.PickAlbumDialog @@ -12,85 +8,15 @@ import java.io.File import java.util.* open class SimpleActivity : BaseSimpleActivity() { - var copyMoveCallback: (() -> Unit)? = null - 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.trimEnd('/') + val source = if (files[0].isFile) files[0].parent else files[0].absolutePath PickAlbumDialog(this, source) { - copyMoveFilesTo(files, source, it, isCopyOperation, callback) - } - } - - private fun copyMoveFilesTo(files: ArrayList, source: String, destination: String, isCopyOperation: Boolean, callback: () -> Unit) { - if (source == destination) { - toast(R.string.source_and_destination_same) - return - } - - val destinationFolder = File(destination) - if (!destinationFolder.exists()) { - toast(R.string.invalid_destination) - return - } - - 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 { - 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) - } - } - } - } - } - } - - private val copyMoveListener = object : CopyMoveTask.CopyMoveListener { - override fun copySucceeded(copyOnly: Boolean, copiedAll: Boolean) { - if (copyOnly) { - toast(if (copiedAll) R.string.copying_success else R.string.copying_success_partial) - } else { - toast(if (copiedAll) R.string.moving_success else R.string.moving_success_partial) - } - copyMoveCallback?.invoke() - copyMoveCallback = null - } - - override fun copyFailed() { - toast(R.string.copy_move_failed) - copyMoveCallback = null + copyMoveFilesTo(files, source.trimEnd('/'), it, isCopyOperation, callback) } } }