Skip to content

Commit

Permalink
feat(dialog): Make title optional (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored May 2, 2022
1 parent 302d813 commit 497f627
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ 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())
.post(
() -> {
AlertDialog.Builder builder = new AlertDialog.Builder(context);

if (title != null) {
builder.setTitle(title);
}
builder
.setMessage(message)
.setTitle(alertTitle)
.setPositiveButton(
alertOkButtonTitle,
(dialog, buttonIndex) -> {
Expand Down Expand Up @@ -84,18 +85,18 @@ 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;

new Handler(Looper.getMainLooper())
.post(
() -> {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);

if (title != null) {
builder.setTitle(title);
}
builder
.setMessage(message)
.setTitle(confirmTitle)
.setPositiveButton(
confirmOkButtonTitle,
(dialog, buttonIndex) -> {
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 9 additions & 9 deletions dialog/ios/Plugin/DialogPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

Expand All @@ -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 ?? ""
Expand Down
6 changes: 3 additions & 3 deletions dialog/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface AlertOptions {
*
* @since 1.0.0
*/
title: string;
title?: string;

/**
* Message to show on the dialog.
Expand All @@ -28,7 +28,7 @@ export interface PromptOptions {
*
* @since 1.0.0
*/
title: string;
title?: string;

/**
* Message to show on the dialog.
Expand Down Expand Up @@ -74,7 +74,7 @@ export interface ConfirmOptions {
*
* @since 1.0.0
*/
title: string;
title?: string;

/**
* Message to show on the dialog.
Expand Down

0 comments on commit 497f627

Please sign in to comment.