Skip to content

Commit

Permalink
Fixes renaming issue while pasting conflicting files multiple times.
Browse files Browse the repository at this point in the history
Disables overwrite button if pasting in same directory.
Sets dialog shown during conflict resolution to not cancellable.
  • Loading branch information
yatiksihag01 committed Sep 27, 2023
1 parent 6783e4d commit 46cfb90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class PreparePasteTask(strongRefMain: MainActivity) {

if (isCloudOrRootMode) {
startService(filesToCopy, targetPath, openMode, isMove, isRootMode)
return
}

val totalBytes = FileUtils.getTotalBytes(filesToCopy, context.get())
Expand Down Expand Up @@ -188,7 +189,7 @@ class PreparePasteTask(strongRefMain: MainActivity) {
}
)
withContext(Dispatchers.Main) {
prepareDialog(conflictingFiles, filesToCopy)
prepareDialog(conflictingFiles, conflictingDirActionMap)
@Suppress("DEPRECATION")
progressDialog?.setMessage(context.get()?.getString(R.string.copying))
}
Expand All @@ -198,7 +199,7 @@ class PreparePasteTask(strongRefMain: MainActivity) {

private suspend fun prepareDialog(
conflictingFiles: MutableList<HybridFileParcelable>,
filesToCopy: ArrayList<HybridFileParcelable>
conflictingDirActionMap: HashMap<HybridFileParcelable, String>
) {
if (conflictingFiles.isEmpty()) return

Expand All @@ -219,9 +220,10 @@ class PreparePasteTask(strongRefMain: MainActivity) {
dialogBuilder.negativeColor(accentColor)
dialogBuilder.neutralColor(accentColor)
dialogBuilder.negativeText(R.string.overwrite)
dialogBuilder.cancelable(false)
showDialog(
conflictingFiles,
filesToCopy,
conflictingDirActionMap,
copyDialogBinding,
dialogBuilder,
checkBox
Expand All @@ -230,7 +232,7 @@ class PreparePasteTask(strongRefMain: MainActivity) {

private suspend fun showDialog(
conflictingFiles: MutableList<HybridFileParcelable>,
filesToCopy: ArrayList<HybridFileParcelable>,
conflictingDirActionMap: HashMap<HybridFileParcelable, String>,
copyDialogBinding: CopyDialogBinding,
dialogBuilder: MaterialDialog.Builder,
checkBox: AppCompatCheckBox
Expand All @@ -240,6 +242,10 @@ class PreparePasteTask(strongRefMain: MainActivity) {
val hybridFileParcelable = iterator.next()
copyDialogBinding.fileNameText.text = hybridFileParcelable.name
val dialog = dialogBuilder.build()
if (hybridFileParcelable.getParent(context.get()) == targetPath) {
dialog.getActionButton(DialogAction.NEGATIVE)
.isEnabled = false
}
val resultDeferred = CompletableDeferred<DialogAction>()
dialogBuilder.onPositive { _, _ ->
resultDeferred.complete(DialogAction.POSITIVE)
Expand All @@ -256,36 +262,25 @@ class PreparePasteTask(strongRefMain: MainActivity) {
if (checkBox.isChecked) {
renameAll = true
return
} else conflictingDirActionMap[hybridFileParcelable] = Action.RENAME
iterator.remove()
}
conflictingDirActionMap[hybridFileParcelable] = Action.RENAME
}
DialogAction.NEGATIVE -> {
if (hybridFileParcelable.getParent(context.get()) == targetPath) {
Toast.makeText(
context.get(),
R.string.same_dir_overwrite_error,
Toast.LENGTH_SHORT
).show()
if (checkBox.isChecked) {
filesToCopy.removeAll(conflictingFiles.toSet())
conflictingFiles.clear()
return
}
filesToCopy.remove(hybridFileParcelable)
} else if (checkBox.isChecked) {
if (checkBox.isChecked) {
overwriteAll = true
return
} else conflictingDirActionMap[hybridFileParcelable] = Action.OVERWRITE
iterator.remove()
}
conflictingDirActionMap[hybridFileParcelable] = Action.OVERWRITE
}
DialogAction.NEUTRAL -> {
if (checkBox.isChecked) {
skipAll = true
return
} else conflictingDirActionMap[hybridFileParcelable] = Action.SKIP
iterator.remove()
}
conflictingDirActionMap[hybridFileParcelable] = Action.SKIP
}
}
iterator.remove()
}
}

Expand Down Expand Up @@ -397,16 +392,20 @@ class PreparePasteTask(strongRefMain: MainActivity) {
while (iterator.hasNext()) {
val hybridFileParcelable = iterator.next()
if (conflictingDirActionMap.contains(hybridFileParcelable)) {
val fileAtTarget = HybridFile(
hybridFileParcelable.mode,
path,
hybridFileParcelable.name,
hybridFileParcelable.isDirectory
)
when (conflictingDirActionMap[hybridFileParcelable]) {
Action.RENAME -> {
if (hybridFileParcelable.isDirectory) {
val newName =
FilenameHelper.increment(
hybridFileParcelable
).getName(context.get())
FilenameHelper.increment(fileAtTarget).getName(context.get())
val newPath = "$path/$newName"
val hybridFile = HybridFile(hybridFileParcelable.mode, newPath)
MakeDirectoryOperation.mkdirs(context.get()!!, hybridFile)
val newDir = HybridFile(hybridFileParcelable.mode, newPath)
MakeDirectoryOperation.mkdirs(context.get()!!, newDir)
@Suppress("DEPRECATION")
nextNodes.add(
CopyNode(
Expand All @@ -418,7 +417,7 @@ class PreparePasteTask(strongRefMain: MainActivity) {
} else {
filesToCopy[filesToCopy.indexOf(hybridFileParcelable)].name =
FilenameHelper.increment(
hybridFileParcelable
fileAtTarget
).getName(context.get())
}
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,5 @@ You only need to do this once, until the next time you select a new location for
<string name="complete_paste_warning">Please complete paste operation first</string>
<string name="same_dir_move_error">Destination and source folder shouldn\'t match to move.</string>
<string name="checking_conflicts">Checking for conflicts</string>
<string name="same_dir_overwrite_error">Destination and source folder shouldn\'t match to overwrite.</string>
</resources>

0 comments on commit 46cfb90

Please sign in to comment.