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,30 +14,34 @@ 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
} }
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()) { if (!destinationFolder.exists()) {
toast(R.string.invalid_destination) toast(R.string.invalid_destination)
return@PickAlbumDialog return
} }
if (files.size == 1) { if (files.size == 1) {
if (File(destinationFolder.absolutePath, files[0].name).exists()) { if (File(destinationFolder.absolutePath, files[0].name).exists()) {
toast(R.string.name_taken) toast(R.string.name_taken)
return@PickAlbumDialog return
} }
} }
@ -48,7 +52,7 @@ open class SimpleActivity : BaseSimpleActivity() {
val pair = Pair<ArrayList<File>, File>(files, destinationFolder) val pair = Pair<ArrayList<File>, File>(files, destinationFolder)
CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair)
} else { } else {
if (isPathOnSD(currPath) || isPathOnSD(destinationFolder.absolutePath)) { if (isPathOnSD(source) || isPathOnSD(destinationFolder.absolutePath)) {
handleSAFDialog(files[0]) { handleSAFDialog(files[0]) {
toast(R.string.moving) toast(R.string.moving)
val pair = Pair<ArrayList<File>, File>(files, destinationFolder) val pair = Pair<ArrayList<File>, File>(files, destinationFolder)
@ -58,9 +62,9 @@ open class SimpleActivity : BaseSimpleActivity() {
val updatedFiles = ArrayList<File>(files.size * 2) val updatedFiles = ArrayList<File>(files.size * 2)
updatedFiles.addAll(files) updatedFiles.addAll(files)
for (file in files) { for (file in files) {
val destination = File(destinationFolder, file.name) val newFile = File(destinationFolder, file.name)
if (!destination.exists() && file.renameTo(destination)) if (!newFile.exists() && file.renameTo(newFile))
updatedFiles.add(destination) updatedFiles.add(newFile)
} }
scanFiles(updatedFiles) { scanFiles(updatedFiles) {
@ -72,7 +76,6 @@ open class SimpleActivity : BaseSimpleActivity() {
} }
} }
} }
}
private val copyMoveListener = object : CopyMoveTask.CopyMoveListener { private val copyMoveListener = object : CopyMoveTask.CopyMoveListener {
override fun copySucceeded(copyOnly: Boolean, copiedAll: Boolean) { override fun copySucceeded(copyOnly: Boolean, copiedAll: Boolean) {

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