feat(dialog): Implemented android save dialog. (#1657)

* Implemented android save dialog.

* small cleanup

* lint

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
mikoto2000
2024-08-13 20:41:30 +09:00
committed by GitHub
parent 5081f30daf
commit bc7eecf420
9 changed files with 112 additions and 12 deletions
@@ -41,6 +41,11 @@ class MessageOptions {
var cancelButtonLabel: String? = null
}
@InvokeArg
class SaveFileDialogOptions {
var fileName: String? = null
}
@TauriPlugin
class DialogPlugin(private val activity: Activity): Plugin(activity) {
var filePickerOptions: FilePickerOptions? = null
@@ -204,4 +209,46 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
dialog.show()
}
}
@Command
fun saveFileDialog(invoke: Invoke) {
try {
val args = invoke.parseArgs(SaveFileDialogOptions::class.java)
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("text/plain")
intent.putExtra(Intent.EXTRA_TITLE, args.fileName ?: "")
startActivityForResult(invoke, intent, "saveFileDialogResult")
} catch (ex: Exception) {
val message = ex.message ?: "Failed to pick save file"
Logger.error(message)
invoke.reject(message)
}
}
@ActivityCallback
fun saveFileDialogResult(invoke: Invoke, result: ActivityResult) {
try {
when (result.resultCode) {
Activity.RESULT_OK -> {
val callResult = JSObject()
val intent: Intent? = result.data
if (intent != null) {
val uri = intent.data
if (uri != null) {
callResult.put("file", uri.toString())
}
}
invoke.resolve(callResult)
}
Activity.RESULT_CANCELED -> invoke.reject("File picker cancelled")
else -> invoke.reject("Failed to pick files")
}
} catch (ex: java.lang.Exception) {
val message = ex.message ?: "Failed to read file pick result"
Logger.error(message)
invoke.reject(message)
}
}
}