refactor the copy/mvoe functionality a bit

This commit is contained in:
tibbi 2017-04-18 19:16:59 +02:00
parent 99666b0173
commit 3629f0ff55
4 changed files with 49 additions and 46 deletions

View File

@ -14,59 +14,62 @@ import java.util.*
open class SimpleActivity : BaseSimpleActivity() { open class SimpleActivity : BaseSimpleActivity() {
var copyMoveCallback: (() -> Unit)? = null var copyMoveCallback: (() -> Unit)? = null
fun copyMoveFilesTo(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 val source = if (files[0].isFile) files[0].parent.trimEnd('/') else files[0].absolutePath.trimEnd('/')
val currPath = source.trimEnd('/') PickAlbumDialog(this, source) {
PickAlbumDialog(this, currPath) { copyMoveFilesTo(files, source, it, isCopyOperation, callback)
val destinationFolder = File(it) }
if (currPath == it.trimEnd('/')) { }
toast(R.string.source_and_destination_same)
return@PickAlbumDialog
}
if (!destinationFolder.exists()) { private fun copyMoveFilesTo(files: ArrayList<File>, source: String, destination: String, isCopyOperation: Boolean, callback: () -> Unit) {
toast(R.string.invalid_destination) if (source == destination) {
return@PickAlbumDialog toast(R.string.source_and_destination_same)
} return
}
if (files.size == 1) { val destinationFolder = File(destination)
if (File(destinationFolder.absolutePath, files[0].name).exists()) { if (!destinationFolder.exists()) {
toast(R.string.name_taken) toast(R.string.invalid_destination)
return@PickAlbumDialog return
} }
}
handleSAFDialog(destinationFolder) { if (files.size == 1) {
copyMoveCallback = callback if (File(destinationFolder.absolutePath, files[0].name).exists()) {
if (isCopyOperation) { toast(R.string.name_taken)
toast(R.string.copying) return
val pair = Pair<ArrayList<File>, File>(files, destinationFolder) }
CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) }
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 { } else {
if (isPathOnSD(currPath) || isPathOnSD(destinationFolder.absolutePath)) { val updatedFiles = ArrayList<File>(files.size * 2)
handleSAFDialog(files[0]) { updatedFiles.addAll(files)
toast(R.string.moving) for (file in files) {
val pair = Pair<ArrayList<File>, File>(files, destinationFolder) val newFile = File(destinationFolder, file.name)
CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) if (!newFile.exists() && file.renameTo(newFile))
} updatedFiles.add(newFile)
} else { }
val updatedFiles = ArrayList<File>(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)
}
scanFiles(updatedFiles) { scanFiles(updatedFiles) {
runOnUiThread { runOnUiThread {
copyMoveListener.copySucceeded(true, files.size * 2 == updatedFiles.size) copyMoveListener.copySucceeded(true, files.size * 2 == updatedFiles.size)
}
} }
} }
} }

View File

@ -222,7 +222,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun copyMoveTo(isCopyOperation: Boolean) { private fun copyMoveTo(isCopyOperation: Boolean) {
val files = ArrayList<File>(1).apply { add(getCurrentFile()) } val files = ArrayList<File>(1).apply { add(getCurrentFile()) }
copyMoveFilesTo(files, isCopyOperation) { tryCopyMoveFilesTo(files, isCopyOperation) {
if (!isCopyOperation) { if (!isCopyOperation) {
reloadViewPager() reloadViewPager()
} }

View File

@ -236,7 +236,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
files.addAll(dir.listFiles().filter { it.isFile && it.isImageVideoGif() }) files.addAll(dir.listFiles().filter { it.isFile && it.isImageVideoGif() })
} }
activity.copyMoveFilesTo(files, isCopyOperation) { activity.tryCopyMoveFilesTo(files, isCopyOperation) {
if (!isCopyOperation) { if (!isCopyOperation) {
listener?.refreshItems() listener?.refreshItems()
} }

View File

@ -143,7 +143,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
val positions = multiSelector.selectedPositions val positions = multiSelector.selectedPositions
positions.forEach { files.add(File(media[it].path)) } positions.forEach { files.add(File(media[it].path)) }
activity.copyMoveFilesTo(files, isCopyOperation) { activity.tryCopyMoveFilesTo(files, isCopyOperation) {
if (!isCopyOperation) { if (!isCopyOperation) {
listener?.refreshItems() listener?.refreshItems()
} }