From 497f6275ee6bc71295403e2cdc5a74b9cc31a310 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Mon, 2 May 2022 17:02:19 +0200 Subject: [PATCH] feat(dialog): Make title optional (#940) --- .../com/capacitorjs/plugins/dialog/Dialog.java | 17 +++++++++-------- .../plugins/dialog/DialogPlugin.java | 12 ++++++------ dialog/ios/Plugin/DialogPlugin.swift | 18 +++++++++--------- dialog/src/definitions.ts | 6 +++--- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/dialog/android/src/main/java/com/capacitorjs/plugins/dialog/Dialog.java b/dialog/android/src/main/java/com/capacitorjs/plugins/dialog/Dialog.java index e89f881aa..7ded29e18 100644 --- a/dialog/android/src/main/java/com/capacitorjs/plugins/dialog/Dialog.java +++ b/dialog/android/src/main/java/com/capacitorjs/plugins/dialog/Dialog.java @@ -40,7 +40,6 @@ public static void alert( final String okButtonTitle, final Dialog.OnResultListener listener ) { - final String alertTitle = title == null ? "Alert" : title; final String alertOkButtonTitle = okButtonTitle == null ? "OK" : okButtonTitle; new Handler(Looper.getMainLooper()) @@ -48,9 +47,11 @@ public static void alert( () -> { AlertDialog.Builder builder = new AlertDialog.Builder(context); + if (title != null) { + builder.setTitle(title); + } builder .setMessage(message) - .setTitle(alertTitle) .setPositiveButton( alertOkButtonTitle, (dialog, buttonIndex) -> { @@ -84,7 +85,6 @@ public static void confirm( final String cancelButtonTitle, final Dialog.OnResultListener listener ) { - final String confirmTitle = title == null ? "Confirm" : title; final String confirmOkButtonTitle = okButtonTitle == null ? "OK" : okButtonTitle; final String confirmCancelButtonTitle = cancelButtonTitle == null ? "Cancel" : cancelButtonTitle; @@ -92,10 +92,11 @@ public static void confirm( .post( () -> { final AlertDialog.Builder builder = new AlertDialog.Builder(context); - + if (title != null) { + builder.setTitle(title); + } builder .setMessage(message) - .setTitle(confirmTitle) .setPositiveButton( confirmOkButtonTitle, (dialog, buttonIndex) -> { @@ -138,7 +139,6 @@ public static void prompt( final String inputText, final Dialog.OnResultListener listener ) { - final String promptTitle = title == null ? "Prompt" : title; final String promptOkButtonTitle = okButtonTitle == null ? "OK" : okButtonTitle; final String promptCancelButtonTitle = cancelButtonTitle == null ? "Cancel" : cancelButtonTitle; final String promptInputPlaceholder = inputPlaceholder == null ? "" : inputPlaceholder; @@ -152,10 +152,11 @@ public static void prompt( input.setHint(promptInputPlaceholder); input.setText(promptInputText); - + if (title != null) { + builder.setTitle(title); + } builder .setMessage(message) - .setTitle(promptTitle) .setView(input) .setPositiveButton( promptOkButtonTitle, diff --git a/dialog/android/src/main/java/com/capacitorjs/plugins/dialog/DialogPlugin.java b/dialog/android/src/main/java/com/capacitorjs/plugins/dialog/DialogPlugin.java index abe147415..369e78539 100644 --- a/dialog/android/src/main/java/com/capacitorjs/plugins/dialog/DialogPlugin.java +++ b/dialog/android/src/main/java/com/capacitorjs/plugins/dialog/DialogPlugin.java @@ -17,8 +17,8 @@ public void alert(final PluginCall call) { final String message = call.getString("message"); final String buttonTitle = call.getString("buttonTitle", "OK"); - if (title == null || message == null) { - call.reject("Please provide a title or message for the alert"); + if (message == null) { + call.reject("Please provide a message for the dialog"); return; } @@ -38,8 +38,8 @@ public void confirm(final PluginCall call) { final String okButtonTitle = call.getString("okButtonTitle", "OK"); final String cancelButtonTitle = call.getString("cancelButtonTitle", "Cancel"); - if (title == null || message == null) { - call.reject("Please provide a title or message for the alert"); + if (message == null) { + call.reject("Please provide a message for the dialog"); return; } @@ -72,8 +72,8 @@ public void prompt(final PluginCall call) { final String inputPlaceholder = call.getString("inputPlaceholder", ""); final String inputText = call.getString("inputText", ""); - if (title == null || message == null) { - call.reject("Please provide a title or message for the alert"); + if (message == null) { + call.reject("Please provide a message for the dialog"); return; } diff --git a/dialog/ios/Plugin/DialogPlugin.swift b/dialog/ios/Plugin/DialogPlugin.swift index b226ef639..74adb2a16 100644 --- a/dialog/ios/Plugin/DialogPlugin.swift +++ b/dialog/ios/Plugin/DialogPlugin.swift @@ -8,11 +8,11 @@ import Capacitor public class DialogPlugin: CAPPlugin { @objc public func alert(_ call: CAPPluginCall) { - guard let title = call.options["title"] as? String else { - call.reject("title must be provided") + let title = call.options["title"] as? String + guard let message = call.options["message"] as? String else { + call.reject("Please provide a message for the dialog") return } - let message = call.options["message"] as? String let buttonTitle = call.options["buttonTitle"] as? String ?? "OK" DispatchQueue.main.async { [weak self] in @@ -25,11 +25,11 @@ public class DialogPlugin: CAPPlugin { } @objc public func confirm(_ call: CAPPluginCall) { - guard let title = call.options["title"] as? String else { - call.reject("title must be provided") + let title = call.options["title"] as? String + guard let message = call.options["message"] as? String else { + call.reject("Please provide a message for the dialog") return } - let message = call.options["message"] as? String ?? "" let okButtonTitle = call.options["okButtonTitle"] as? String ?? "OK" let cancelButtonTitle = call.options["cancelButtonTitle"] as? String ?? "Cancel" @@ -50,11 +50,11 @@ public class DialogPlugin: CAPPlugin { } @objc public func prompt (_ call: CAPPluginCall) { - guard let title = call.options["title"] as? String else { - call.reject("title must be provided") + let title = call.options["title"] as? String + guard let message = call.options["message"] as? String else { + call.reject("Please provide a message for the dialog") return } - let message = call.options["message"] as? String ?? "" let okButtonTitle = call.options["okButtonTitle"] as? String ?? "OK" let cancelButtonTitle = call.options["cancelButtonTitle"] as? String ?? "Cancel" let inputPlaceholder = call.options["inputPlaceholder"] as? String ?? "" diff --git a/dialog/src/definitions.ts b/dialog/src/definitions.ts index 28805c56a..eb6461de0 100644 --- a/dialog/src/definitions.ts +++ b/dialog/src/definitions.ts @@ -4,7 +4,7 @@ export interface AlertOptions { * * @since 1.0.0 */ - title: string; + title?: string; /** * Message to show on the dialog. @@ -28,7 +28,7 @@ export interface PromptOptions { * * @since 1.0.0 */ - title: string; + title?: string; /** * Message to show on the dialog. @@ -74,7 +74,7 @@ export interface ConfirmOptions { * * @since 1.0.0 */ - title: string; + title?: string; /** * Message to show on the dialog.