From 132117a58687024ec4d596413637dc2306a776a8 Mon Sep 17 00:00:00 2001 From: OS-pedrogustavobilro Date: Tue, 17 Dec 2024 11:31:45 +0000 Subject: [PATCH 1/8] feat: Add cancelable parameter on Android References: https://outsystemsrd.atlassian.net/browse/RMET-3576 --- .../capacitorjs/plugins/actionsheet/ActionSheet.java | 4 +++- .../plugins/actionsheet/ActionSheetPlugin.java | 8 +++++++- action-sheet/src/definitions.ts | 10 ++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheet.java b/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheet.java index 13a78f11a..75315532f 100644 --- a/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheet.java +++ b/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheet.java @@ -26,7 +26,9 @@ public interface OnCancelListener { @Override public void onCancel(DialogInterface dialog) { super.onCancel(dialog); - this.cancelListener.onCancel(); + if (this.cancelListener != null) { + this.cancelListener.onCancel(); + } } private String title; diff --git a/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java b/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java index 7ca88b8e4..189c527a0 100644 --- a/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java +++ b/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java @@ -19,6 +19,7 @@ public class ActionSheetPlugin extends Plugin { @PluginMethod public void showActions(final PluginCall call) { String title = call.getString("title"); + boolean cancelable = Boolean.TRUE.equals(call.getBoolean("cancelable", false)); JSArray options = call.getArray("options"); if (options == null) { call.reject("Must supply options"); @@ -39,7 +40,12 @@ public void showActions(final PluginCall call) { } implementation.setTitle(title); implementation.setOptions(actionOptions); - implementation.setCancelable(false); + implementation.setCancelable(cancelable); + if (cancelable) { + implementation.setOnCancelListener( + () -> call.reject("User canceled action sheet") + ); + } implementation.setOnSelectedListener( index -> { JSObject ret = new JSObject(); diff --git a/action-sheet/src/definitions.ts b/action-sheet/src/definitions.ts index 84dbfbaae..ade83a9af 100644 --- a/action-sheet/src/definitions.ts +++ b/action-sheet/src/definitions.ts @@ -15,6 +15,16 @@ export interface ShowActionsOptions { */ message?: string; + /** + * If true, sheet is canceled when clicked outside; If false, it is not. By default, false. + * + * On iOS, sheet is also cancelable if a button with ActionSheetButtonStyle.Cancel is provided and cancelable is false. + * + * @since TODO provide release number + */ + cancelable?: boolean; + + /** * Options the user can choose from. * From aa33a0716c0407940418c31cfe602182b1553046 Mon Sep 17 00:00:00 2001 From: OS-pedrogustavobilro Date: Tue, 17 Dec 2024 16:46:01 +0000 Subject: [PATCH 2/8] feat: Add cancelable parameter on iOS Only is applied if no cancel option is provided. References: https://outsystemsrd.atlassian.net/browse/RMET-3576 --- .../ActionSheetPlugin/ActionSheetPlugin.swift | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift b/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift index b36c43e2f..74cfdad81 100644 --- a/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift +++ b/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift @@ -17,9 +17,11 @@ public class ActionSheetPlugin: CAPPlugin, CAPBridgedPlugin { @objc func showActions(_ call: CAPPluginCall) { let title = call.options["title"] as? String let message = call.options["message"] as? String + let cancelable = call.options["cancelable"] as? Bool ?? false let options = call.getArray("options", JSObject.self) ?? [] var alertActions = [UIAlertAction]() + var forceCancelableOnClickOutside = cancelable for (index, option) in options.enumerated() { let style = option["style"] as? String ?? "DEFAULT" let title = option["title"] as? String ?? "" @@ -27,6 +29,8 @@ public class ActionSheetPlugin: CAPPlugin, CAPBridgedPlugin { if style == "DESTRUCTIVE" { buttonStyle = .destructive } else if style == "CANCEL" { + // if there's a cancel action, then it will already be cancelable when clicked outside + forceCancelableOnClickOutside = false buttonStyle = .cancel } let action = UIAlertAction(title: title, style: buttonStyle, handler: { (_) -> Void in @@ -40,9 +44,31 @@ public class ActionSheetPlugin: CAPPlugin, CAPBridgedPlugin { DispatchQueue.main.async { [weak self] in if let alertController = self?.implementation.buildActionSheet(title: title, message: message, actions: alertActions) { self?.setCenteredPopover(alertController) - self?.bridge?.viewController?.present(alertController, animated: true, completion: nil) + self?.bridge?.viewController?.present(alertController, animated: true) { + if (forceCancelableOnClickOutside) { + let gestureRecognizer = TapGestureRecognizerWithClosure { + alertController.dismiss(animated: true, completion: nil) + call.reject("User canceled action sheet") + } + let backroundView = alertController.view.superview?.subviews[0] + backroundView?.addGestureRecognizer(gestureRecognizer) + } + } } } } + + private final class TapGestureRecognizerWithClosure: UITapGestureRecognizer { + private let onTap: () -> Void + init(onTap: @escaping () -> Void) { + self.onTap = onTap + super.init(target: nil, action: nil) + self.addTarget(self, action: #selector(action)) + } + + @objc private func action() { + onTap() + } + } } From dd4bb5a26fbd6081eb5681f34584fc9c5d2b2d7e Mon Sep 17 00:00:00 2001 From: OS-pedrogustavobilro Date: Tue, 17 Dec 2024 17:58:20 +0000 Subject: [PATCH 3/8] refactor: Return canceled boolean instead of error References: https://outsystemsrd.atlassian.net/browse/RMET-3576 --- .../plugins/actionsheet/ActionSheetPlugin.java | 17 ++++++++++------- .../ActionSheetPlugin/ActionSheetPlugin.swift | 8 ++++++-- action-sheet/src/definitions.ts | 12 ++++++++++-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java b/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java index 189c527a0..613369b0a 100644 --- a/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java +++ b/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java @@ -43,16 +43,11 @@ public void showActions(final PluginCall call) { implementation.setCancelable(cancelable); if (cancelable) { implementation.setOnCancelListener( - () -> call.reject("User canceled action sheet") + () -> resolve(call, -1) ); } implementation.setOnSelectedListener( - index -> { - JSObject ret = new JSObject(); - ret.put("index", index); - call.resolve(ret); - implementation.dismiss(); - } + index -> resolve(call, index) ); implementation.show(getActivity().getSupportFragmentManager(), "capacitorModalsActionSheet"); } catch (JSONException ex) { @@ -60,4 +55,12 @@ public void showActions(final PluginCall call) { call.reject("JSON error processing an option for showActions", ex); } } + + private void resolve(final PluginCall call, int selectedIndex) { + JSObject ret = new JSObject(); + ret.put("index", selectedIndex); + ret.put("canceled", selectedIndex < 0); + call.resolve(ret); + implementation.dismiss(); + } } diff --git a/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift b/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift index 74cfdad81..1b995071a 100644 --- a/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift +++ b/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift @@ -35,7 +35,8 @@ public class ActionSheetPlugin: CAPPlugin, CAPBridgedPlugin { } let action = UIAlertAction(title: title, style: buttonStyle, handler: { (_) -> Void in call.resolve([ - "index": index + "index": index, + "canceled": false ]) }) alertActions.append(action) @@ -48,7 +49,10 @@ public class ActionSheetPlugin: CAPPlugin, CAPBridgedPlugin { if (forceCancelableOnClickOutside) { let gestureRecognizer = TapGestureRecognizerWithClosure { alertController.dismiss(animated: true, completion: nil) - call.reject("User canceled action sheet") + call.resolve([ + "index": -1, + "canceled": true + ]) } let backroundView = alertController.view.superview?.subviews[0] backroundView?.addGestureRecognizer(gestureRecognizer) diff --git a/action-sheet/src/definitions.ts b/action-sheet/src/definitions.ts index ade83a9af..376087064 100644 --- a/action-sheet/src/definitions.ts +++ b/action-sheet/src/definitions.ts @@ -20,7 +20,7 @@ export interface ShowActionsOptions { * * On iOS, sheet is also cancelable if a button with ActionSheetButtonStyle.Cancel is provided and cancelable is false. * - * @since TODO provide release number + * @since 6.1.0 */ cancelable?: boolean; @@ -86,11 +86,19 @@ export interface ActionSheetButton { export interface ShowActionsResult { /** - * The index of the clicked option (Zero-based) + * The index of the clicked option (Zero-based), or -1 if the sheet was canceled. + * + * On iOS, if there is a button with ActionSheetButtonStyle.Cancel, and user clicks outside the sheet, the index of the cancel option is returned * * @since 1.0.0 */ index: number; + /** + * True if sheet was canceled by user; False otherwise + * + * @since 6.1.0 + */ + canceled: boolean; } export interface ActionSheetPlugin { From 1c8180661f601147c55055c558d025dc7544b952 Mon Sep 17 00:00:00 2001 From: OS-pedrogustavobilro Date: Tue, 17 Dec 2024 17:59:01 +0000 Subject: [PATCH 4/8] refactor: Add cancelable parameter on Web References: https://outsystemsrd.atlassian.net/browse/RMET-3576 --- action-sheet/src/web.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/action-sheet/src/web.ts b/action-sheet/src/web.ts index c63e0f576..178610c7b 100644 --- a/action-sheet/src/web.ts +++ b/action-sheet/src/web.ts @@ -15,14 +15,23 @@ export class ActionSheetWeb extends WebPlugin implements ActionSheetPlugin { document.body.appendChild(actionSheet); } actionSheet.header = options.title; - actionSheet.cancelable = false; + actionSheet.cancelable = options.cancelable; actionSheet.options = options.options; actionSheet.addEventListener('onSelection', async (e: any) => { const selection = e.detail; resolve({ index: selection, + canceled: false }); }); + if (options.cancelable) { + actionSheet.addEventListener('onCanceled', async () => { + resolve({ + index: -1, + canceled: true + }); + }) + } }); } } From 433e38d51527fb92f7eb09844bea7fa2b1149a25 Mon Sep 17 00:00:00 2001 From: OS-pedrogustavobilro Date: Tue, 17 Dec 2024 18:12:26 +0000 Subject: [PATCH 5/8] docs: Update README for action-sheet --- action-sheet/README.md | 18 ++++++++++-------- action-sheet/src/definitions.ts | 17 ++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/action-sheet/README.md b/action-sheet/README.md index b72498423..edf72ea0d 100644 --- a/action-sheet/README.md +++ b/action-sheet/README.md @@ -84,18 +84,20 @@ to select. #### ShowActionsResult -| Prop | Type | Description | Since | -| ----------- | ------------------- | -------------------------------------------- | ----- | -| **`index`** | number | The index of the clicked option (Zero-based) | 1.0.0 | +| Prop | Type | Description | Since | +| -------------- | -------------------- | -------------------------------------------- | ----- | +| **`index`** | number | The index of the clicked option (Zero-based), or -1 if the sheet was canceled. On iOS, if there is a button with ActionSheetButtonStyle.Cancel, and user clicks outside the sheet, the index of the cancel option is returned | 1.0.0 | +| **`canceled`** | boolean | True if sheet was canceled by user; False otherwise | 6.1.0 | #### ShowActionsOptions -| Prop | Type | Description | Since | -| ------------- | -------------------------------- | ------------------------------------------------------------------------ | ----- | -| **`title`** | string | The title of the Action Sheet. | 1.0.0 | -| **`message`** | string | A message to show under the title. This option is only supported on iOS. | 1.0.0 | -| **`options`** | ActionSheetButton[] | Options the user can choose from. | 1.0.0 | +| Prop | Type | Description | Since | +| ---------------- | -------------------------------- | ------------------------------------------------------------------------ | ----- | +| **`title`** | string | The title of the Action Sheet. | 1.0.0 | +| **`message`** | string | A message to show under the title. This option is only supported on iOS. | 1.0.0 | +| **`options`** | ActionSheetButton[] | Options the user can choose from. | 1.0.0 | +| **`cancelable`** | boolean | If true, sheet is canceled when clicked outside; If false, it is not. By default, false. On iOS, the sheet is also cancelable if a button with ActionSheetButtonStyle.Cancel is provided and cancelable is false. | 6.1.0 | #### ActionSheetButton diff --git a/action-sheet/src/definitions.ts b/action-sheet/src/definitions.ts index 376087064..145a788af 100644 --- a/action-sheet/src/definitions.ts +++ b/action-sheet/src/definitions.ts @@ -15,22 +15,21 @@ export interface ShowActionsOptions { */ message?: string; + /** + * Options the user can choose from. + * + * @since 1.0.0 + */ + options: ActionSheetButton[]; + /** * If true, sheet is canceled when clicked outside; If false, it is not. By default, false. * - * On iOS, sheet is also cancelable if a button with ActionSheetButtonStyle.Cancel is provided and cancelable is false. + * On iOS, the sheet is also cancelable if a button with ActionSheetButtonStyle.Cancel is provided and cancelable is false. * * @since 6.1.0 */ cancelable?: boolean; - - - /** - * Options the user can choose from. - * - * @since 1.0.0 - */ - options: ActionSheetButton[]; } export enum ActionSheetButtonStyle { From e247aa7390d4a71b970410670367670f2493321b Mon Sep 17 00:00:00 2001 From: OS-pedrogustavobilro Date: Wed, 18 Dec 2024 09:25:59 +0000 Subject: [PATCH 6/8] chore: fix lint issues --- .../plugins/actionsheet/ActionSheetPlugin.java | 10 +++------- action-sheet/src/definitions.ts | 8 ++++---- action-sheet/src/web.ts | 6 +++--- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java b/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java index 613369b0a..ddddbac3d 100644 --- a/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java +++ b/action-sheet/android/src/main/java/com/capacitorjs/plugins/actionsheet/ActionSheetPlugin.java @@ -42,20 +42,16 @@ public void showActions(final PluginCall call) { implementation.setOptions(actionOptions); implementation.setCancelable(cancelable); if (cancelable) { - implementation.setOnCancelListener( - () -> resolve(call, -1) - ); + implementation.setOnCancelListener(() -> resolve(call, -1)); } - implementation.setOnSelectedListener( - index -> resolve(call, index) - ); + implementation.setOnSelectedListener(index -> resolve(call, index)); implementation.show(getActivity().getSupportFragmentManager(), "capacitorModalsActionSheet"); } catch (JSONException ex) { Logger.error("JSON error processing an option for showActions", ex); call.reject("JSON error processing an option for showActions", ex); } } - + private void resolve(final PluginCall call, int selectedIndex) { JSObject ret = new JSObject(); ret.put("index", selectedIndex); diff --git a/action-sheet/src/definitions.ts b/action-sheet/src/definitions.ts index 145a788af..c236dbd50 100644 --- a/action-sheet/src/definitions.ts +++ b/action-sheet/src/definitions.ts @@ -24,9 +24,9 @@ export interface ShowActionsOptions { /** * If true, sheet is canceled when clicked outside; If false, it is not. By default, false. - * + * * On iOS, the sheet is also cancelable if a button with ActionSheetButtonStyle.Cancel is provided and cancelable is false. - * + * * @since 6.1.0 */ cancelable?: boolean; @@ -86,7 +86,7 @@ export interface ActionSheetButton { export interface ShowActionsResult { /** * The index of the clicked option (Zero-based), or -1 if the sheet was canceled. - * + * * On iOS, if there is a button with ActionSheetButtonStyle.Cancel, and user clicks outside the sheet, the index of the cancel option is returned * * @since 1.0.0 @@ -94,7 +94,7 @@ export interface ShowActionsResult { index: number; /** * True if sheet was canceled by user; False otherwise - * + * * @since 6.1.0 */ canceled: boolean; diff --git a/action-sheet/src/web.ts b/action-sheet/src/web.ts index 178610c7b..3a517c5fc 100644 --- a/action-sheet/src/web.ts +++ b/action-sheet/src/web.ts @@ -21,16 +21,16 @@ export class ActionSheetWeb extends WebPlugin implements ActionSheetPlugin { const selection = e.detail; resolve({ index: selection, - canceled: false + canceled: false, }); }); if (options.cancelable) { actionSheet.addEventListener('onCanceled', async () => { resolve({ index: -1, - canceled: true + canceled: true, }); - }) + }); } }); } From 99443aeefa17c7281542b810aac5837e512edb82 Mon Sep 17 00:00:00 2001 From: OS-pedrogustavobilro Date: Mon, 13 Jan 2025 11:05:37 +0000 Subject: [PATCH 7/8] refactor(action-sheet): Explicitly get string and bool arguments --- .../ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift b/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift index 1b995071a..4c2586d54 100644 --- a/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift +++ b/action-sheet/ios/Sources/ActionSheetPlugin/ActionSheetPlugin.swift @@ -15,9 +15,9 @@ public class ActionSheetPlugin: CAPPlugin, CAPBridgedPlugin { private let implementation = ActionSheet() @objc func showActions(_ call: CAPPluginCall) { - let title = call.options["title"] as? String - let message = call.options["message"] as? String - let cancelable = call.options["cancelable"] as? Bool ?? false + let title = call.getString("title") + let message = call.getString("message") + let cancelable = call.getBool("cancelable", false) let options = call.getArray("options", JSObject.self) ?? [] var alertActions = [UIAlertAction]() From 89f87b25cc315b0144c67bcd4b69f5ae3cddf96d Mon Sep 17 00:00:00 2001 From: OS-pedrogustavobilro Date: Mon, 13 Jan 2025 11:05:54 +0000 Subject: [PATCH 8/8] chore(action-sheet) update README.md --- action-sheet/README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/action-sheet/README.md b/action-sheet/README.md index edf72ea0d..8641a0aa3 100644 --- a/action-sheet/README.md +++ b/action-sheet/README.md @@ -84,20 +84,20 @@ to select. #### ShowActionsResult -| Prop | Type | Description | Since | -| -------------- | -------------------- | -------------------------------------------- | ----- | -| **`index`** | number | The index of the clicked option (Zero-based), or -1 if the sheet was canceled. On iOS, if there is a button with ActionSheetButtonStyle.Cancel, and user clicks outside the sheet, the index of the cancel option is returned | 1.0.0 | -| **`canceled`** | boolean | True if sheet was canceled by user; False otherwise | 6.1.0 | +| Prop | Type | Description | Since | +| -------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | +| **`index`** | number | The index of the clicked option (Zero-based), or -1 if the sheet was canceled. On iOS, if there is a button with ActionSheetButtonStyle.Cancel, and user clicks outside the sheet, the index of the cancel option is returned | 1.0.0 | +| **`canceled`** | boolean | True if sheet was canceled by user; False otherwise | 6.1.0 | #### ShowActionsOptions -| Prop | Type | Description | Since | -| ---------------- | -------------------------------- | ------------------------------------------------------------------------ | ----- | -| **`title`** | string | The title of the Action Sheet. | 1.0.0 | -| **`message`** | string | A message to show under the title. This option is only supported on iOS. | 1.0.0 | -| **`options`** | ActionSheetButton[] | Options the user can choose from. | 1.0.0 | -| **`cancelable`** | boolean | If true, sheet is canceled when clicked outside; If false, it is not. By default, false. On iOS, the sheet is also cancelable if a button with ActionSheetButtonStyle.Cancel is provided and cancelable is false. | 6.1.0 | +| Prop | Type | Description | Since | +| ---------------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | +| **`title`** | string | The title of the Action Sheet. | 1.0.0 | +| **`message`** | string | A message to show under the title. This option is only supported on iOS. | 1.0.0 | +| **`options`** | ActionSheetButton[] | Options the user can choose from. | 1.0.0 | +| **`cancelable`** | boolean | If true, sheet is canceled when clicked outside; If false, it is not. By default, false. On iOS, the sheet is also cancelable if a button with ActionSheetButtonStyle.Cancel is provided and cancelable is false. | 6.1.0 | #### ActionSheetButton