Skip to content

Instantly share code, notes, and snippets.

@omkar-tenkale
Created June 3, 2023 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omkar-tenkale/1980b112552f4898c8fa2f3de2cdf039 to your computer and use it in GitHub Desktop.
Save omkar-tenkale/1980b112552f4898c8fa2f3de2cdf039 to your computer and use it in GitHub Desktop.
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