use the copy/move functionality from the commons library

This commit is contained in:
tibbi 2017-04-18 19:31:22 +02:00
parent 3629f0ff55
commit 409ecbe4b5
2 changed files with 3 additions and 77 deletions

View file

@ -32,7 +32,7 @@ android {
} }
dependencies { 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.davemorrissey.labs:subsampling-scale-image-view:3.6.0'
compile 'com.theartofdev.edmodo:android-image-cropper:2.4.0' compile 'com.theartofdev.edmodo:android-image-cropper:2.4.0'
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'

View file

@ -1,10 +1,6 @@
package com.simplemobiletools.gallery.activities package com.simplemobiletools.gallery.activities
import android.support.v4.util.Pair
import com.simplemobiletools.commons.activities.BaseSimpleActivity 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.commons.extensions.toast
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.dialogs.PickAlbumDialog import com.simplemobiletools.gallery.dialogs.PickAlbumDialog
@ -12,85 +8,15 @@ import java.io.File
import java.util.* import java.util.*
open class SimpleActivity : BaseSimpleActivity() { open class SimpleActivity : BaseSimpleActivity() {
var copyMoveCallback: (() -> Unit)? = null
fun tryCopyMoveFilesTo(files: ArrayList<File>, isCopyOperation: Boolean, callback: () -> Unit) { fun tryCopyMoveFilesTo(files: ArrayList<File>, isCopyOperation: Boolean, callback: () -> Unit) {
if (files.isEmpty()) { if (files.isEmpty()) {
toast(R.string.unknown_error_occurred) toast(R.string.unknown_error_occurred)
return 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) { PickAlbumDialog(this, source) {
copyMoveFilesTo(files, source, it, isCopyOperation, callback) copyMoveFilesTo(files, source.trimEnd('/'), it, isCopyOperation, callback)
}
}
private fun copyMoveFilesTo(files: ArrayList<File>, 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<ArrayList<File>, 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<ArrayList<File>, File>(files, destinationFolder)
CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair)
}
} else {
val updatedFiles = ArrayList<File>(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
} }
} }
} }