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() {
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()) {
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<File>, 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<ArrayList<File>, 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<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 {
if (isPathOnSD(currPath) || 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 destination = File(destinationFolder, file.name)
if (!destination.exists() && file.renameTo(destination))
updatedFiles.add(destination)
}
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)
}
scanFiles(updatedFiles) {
runOnUiThread {
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) {
val files = ArrayList<File>(1).apply { add(getCurrentFile()) }
copyMoveFilesTo(files, isCopyOperation) {
tryCopyMoveFilesTo(files, isCopyOperation) {
if (!isCopyOperation) {
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() })
}
activity.copyMoveFilesTo(files, isCopyOperation) {
activity.tryCopyMoveFilesTo(files, isCopyOperation) {
if (!isCopyOperation) {
listener?.refreshItems()
}

View file

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