Skip to content

Commit

Permalink
fixed issue when copy/move multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
anggrayudi committed Jun 18, 2024
1 parent d3ef49b commit 9cd942c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class MainActivity : AppCompatActivity() {
binding.layoutMoveMultipleFilesTargetFolder.btnBrowse.setOnClickListener {
storageHelper.openFolderPicker(REQUEST_CODE_PICK_TARGET_FOLDER_FOR_MULTIPLE_FILE_MOVE)
}
binding.btnStartCopyMultipleFiles.setOnClickListener {
binding.btnStartMoveMultipleFiles.setOnClickListener {
val targetFolder = binding.layoutMoveMultipleFilesTargetFolder.tvFilePath.tag as? DocumentFile
if (targetFolder == null) {
Toast.makeText(this, "Please select target folder", Toast.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ fun List<DocumentFile>.compressToZip(
if (success) {
if (deleteSourceWhenComplete) {
send(ZipCompressionResult.DeletingEntryFiles)
forEach { it.deleteRecursively(context) }
forEach { it.forceDelete(context) }
}
val sizeReduction = (actualFilesSize - zipFile.length()).toFloat() / actualFilesSize * 100
send(ZipCompressionResult.Completed(zipFile, actualFilesSize, totalFiles, sizeReduction))
Expand Down Expand Up @@ -1506,21 +1506,31 @@ private fun List<DocumentFile>.copyTo(

send(MultipleFilesResult.CountingFiles)

class SourceInfo(val children: List<DocumentFile>, val size: Long, val totalFiles: Int, val conflictResolution: FolderConflictCallback.ConflictResolution)
class SourceInfo(val children: List<DocumentFile>?, val size: Long, val totalFiles: Int, val conflictResolution: FolderConflictCallback.ConflictResolution)

val sourceInfos = validSources.associateWith { src ->
val children = if (skipEmptyFiles) src.walkFileTreeAndSkipEmptyFiles() else src.walkFileTree(context)
var totalFilesToCopy = 0
var totalSizeToCopy = 0L
children.forEach {
if (it.isFile) {
totalFilesToCopy++
totalSizeToCopy += it.length()
val resolution = conflictResolutions.find { it.source == src }?.solution ?: FolderConflictCallback.ConflictResolution.CREATE_NEW
if (src.isFile) {
SourceInfo(null, src.length(), 1, resolution)
} else {
val children = if (skipEmptyFiles) src.walkFileTreeAndSkipEmptyFiles() else src.walkFileTree(context)
var totalFilesToCopy = 0
var totalSizeToCopy = 0L
children.forEach {
if (it.isFile) {
totalFilesToCopy++
totalSizeToCopy += it.length()
}
}
SourceInfo(children, totalSizeToCopy, totalFilesToCopy, resolution)
}
val resolution = conflictResolutions.find { it.source == src }?.solution ?: FolderConflictCallback.ConflictResolution.CREATE_NEW
SourceInfo(children, totalSizeToCopy, totalFilesToCopy, resolution)
}.toMutableMap()
// allow empty folders, but empty files need check
}.filterValues { it.children != null || (skipEmptyFiles && it.size > 0 || !skipEmptyFiles) }.toMutableMap()

if (sourceInfos.isEmpty()) {
sendAndClose(MultipleFilesResult.Completed(emptyList(), 0, 0, true))
return@callbackFlow
}

// key=src, value=result
val results = mutableMapOf<DocumentFile, DocumentFile>()
Expand Down Expand Up @@ -1573,7 +1583,7 @@ private fun List<DocumentFile>.copyTo(
return@callbackFlow
}

val totalFilesToCopy = validSources.count { it.isFile } + sourceInfos.values.sumOf { it.totalFiles }
val totalFilesToCopy = sourceInfos.values.sumOf { it.totalFiles }
send(MultipleFilesResult.Starting(sourceInfos.map { it.key }, totalFilesToCopy))

var totalCopiedFiles = 0
Expand Down Expand Up @@ -1668,7 +1678,7 @@ private fun List<DocumentFile>.copyTo(
}

try {
if (targetRootFile.isFile) {
if (targetRootFile.isFile || info.children == null) {
copy(src, targetRootFile)
results[src] = targetRootFile
continue
Expand Down Expand Up @@ -1727,7 +1737,7 @@ private fun List<DocumentFile>.copyTo(
timer?.cancel()
if (!success || conflictedFiles.isEmpty()) {
if (deleteSourceWhenComplete && success) {
sourceInfos.forEach { (src, _) -> src.deleteRecursively(context) }
sourceInfos.forEach { (src, _) -> src.forceDelete(context) }
}
trySend(MultipleFilesResult.Completed(results.map { it.value }, totalFilesToCopy, totalCopiedFiles, success))
true
Expand Down Expand Up @@ -2889,7 +2899,7 @@ private fun List<DocumentFile>.handleParentFolderConflict(
resolution.forEach { conflict ->
when (conflict.solution) {
FolderConflictCallback.ConflictResolution.REPLACE -> {
if (!conflict.target.let { it.deleteRecursively(context, true) || !it.exists() }) {
if (!conflict.target.let { it.forceDelete(context, true) || !it.exists() }) {
scope.trySend(MultipleFilesResult.Error(MultipleFilesErrorCode.CANNOT_CREATE_FILE_IN_TARGET))
return null
}
Expand Down

0 comments on commit 9cd942c

Please sign in to comment.