Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add closeAllIfPossible() method #67

Merged
merged 7 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public void onDetachedFromActivity() {
public void onMethodCall(@NonNull MethodCall call, @NonNull final MethodChannel.Result result) {
if ("launch".equals(call.method)) {
launch(((Map<String, Object>) call.arguments), result);
} else {
} if ("closeAllIfPossible".equals(call.method)) {
closeAllIfPossible(result);
}else {
result.notImplemented();
}
}
Expand All @@ -98,4 +100,9 @@ private void launch(@NonNull Map<String, Object> args, @NonNull MethodChannel.Re
result.error(CODE_LAUNCH_ERROR, e.getMessage(), null);
}
}

@SuppressWarnings({"unchecked", "ConstantConditions"})
private void closeAllIfPossible(@NonNull MethodChannel.Result result) {
result.success(null);
}
}
18 changes: 18 additions & 0 deletions flutter_custom_tabs/ios/Classes/CustomTabsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ private let keyURL = "url"
private let keyOption = "safariVCOption"

public class CustomTabsPlugin: NSObject, FlutterPlugin {
private var dismissStack = [() -> Void]()

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(
name: "plugins.flutter.droibit.github.io/custom_tabs",
Expand All @@ -22,6 +24,8 @@ public class CustomTabsPlugin: NSObject, FlutterPlugin {
let url = arguments[keyURL] as! String
let option = arguments[keyOption] as! [String: Any]
present(withURL: url, option: option, result: result)
case "closeAllIfPossible":
dismissAllIfPossible(result: result)
default:
result(FlutterMethodNotImplemented)
}
Expand All @@ -31,6 +35,9 @@ public class CustomTabsPlugin: NSObject, FlutterPlugin {
if #available(iOS 9.0, *) {
if let topViewController = UIWindow.keyWindow?.topViewController() {
let safariViewController = SFSafariViewController.make(url: URL(string: url)!, option: option)
dismissStack.append({ [weak safariViewController] in
safariViewController?.dismiss(animated: true)
})
topViewController.present(safariViewController, animated: true) {
result(nil)
}
Expand All @@ -39,6 +46,17 @@ public class CustomTabsPlugin: NSObject, FlutterPlugin {
result(FlutterMethodNotImplemented)
}
}

private func dismissAllIfPossible(result: @escaping FlutterResult) {
if #available(iOS 9.0, *) {
while let task = dismissStack.popLast() {
task()
}
result(nil)
} else {
result(FlutterMethodNotImplemented)
}
}
}

private extension UIWindow {
Expand Down
4 changes: 4 additions & 0 deletions flutter_custom_tabs/lib/src/launcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ void _applyStatusBarBrightnessTemporally(
previousAutomaticSystemUiAdjustment;
}
}

Future<void> closeAllIfPossible() async {
await CustomTabsPlatform.instance.closeAllIfPossible();
}
19 changes: 19 additions & 0 deletions flutter_custom_tabs/test/launcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ void main() {
await launchResult;
expect(binding.renderView.automaticSystemUiAdjustment, isTrue);
});

test('closeAllIfPossible', () async {
when(mock.closeAllIfPossible()).thenAnswer((_) async => null);

await closeAllIfPossible();

verify(mock.closeAllIfPossible());
});
}

class _MockCustomTabsPlatform extends Mock
Expand All @@ -180,4 +188,15 @@ class _MockCustomTabsPlatform extends Mock
returnValue: Future.value(null),
);
}

@override
Future<void> closeAllIfPossible() async {
return super.noSuchMethod(
Invocation.method(
#closeAllIfPossible,
{},
),
returnValue: Future.value(null),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ abstract class CustomTabsPlatform extends PlatformInterface {
}) {
throw UnimplementedError('launch() has not been implemented.');
}

/// Close all the launched implementation by platform if possible.
Future<void> closeAllIfPossible() {
throw UnimplementedError('closeAllIfPossible() has not been implemented.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ class MethodChannelCustomTabs extends CustomTabsPlatform {
};
return _channel.invokeMethod('launch', args);
}

@override
Future<void> closeAllIfPossible() {
return _channel.invokeMethod('closeAllIfPossible');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,14 @@ void main() {
],
);
});

test('closeAllIfPossible invoke method "closeAllIfPossible"', () async {
await customTabs.closeAllIfPossible();
expect(
log,
<Matcher>[
isMethodCall('closeAllIfPossible', arguments: null),
],
);
});
}
3 changes: 3 additions & 0 deletions flutter_custom_tabs_web/lib/flutter_custom_tabs_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ class CustomTabsPlugin extends CustomTabsPlatform {
final plugin = UrlLauncherPlatform.instance as UrlLauncherPlugin;
return plugin.launch(urlString).then((_) => null);
}

@override
Future<void> closeAllIfPossible() {}
}
2 changes: 1 addition & 1 deletion flutter_custom_tabs_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
sdk: flutter
flutter_web_plugins:
sdk: flutter
flutter_custom_tabs_platform_interface: ^1.0.0
flutter_custom_tabs_platform_interface: ^1.0.1
meta: ^1.9.1
url_launcher_web: ^2.0.1
url_launcher_platform_interface: ^2.0.3
Expand Down
Loading