Created
June 3, 2023 18:03
-
-
Save omkar-tenkale/1980b112552f4898c8fa2f3de2cdf039 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileCopyFragment : Fragment() { | |
fun copyFiles(files: List<File>, destination: File) { | |
launch(Dispatcher.Background) { | |
for (file in files) { | |
val needsOverride = destination.listFiles()?.any { it.name == file.name } ?: false | |
if (needsOverride && !confirmFileOverride(file.name)) { | |
continue | |
} | |
file.copyTo(destination) | |
} | |
} | |
} | |
suspend fun confirmFileOverride(fileName: String): Boolean { | |
val overrideAllowed = withContext(Dispatcher.Main) { | |
suspendCoroutine<Boolean> { cont -> | |
AlertDialog.Builder(activity).apply { | |
setMessage("Destination already has file named $fileName") | |
setPositiveButton("Replace the file") { dialog: DialogInterface, _: Int -> | |
dialog.dismiss() | |
cont.resumeWith(Result.success(true)) | |
} | |
setNegativeButton("Skip this file") { dialog: DialogInterface, _: Int -> | |
dialog.dismiss() | |
cont.resumeWith(Result.success(false)) | |
} | |
}.create().show() | |
} | |
} | |
return overrideAllowed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment