Skip to content

Commit

Permalink
feat: ✨ Integrated dapp opens external websites in default browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralu committed Nov 28, 2024
1 parent 18650c6 commit 6fed420
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions lib/infrastructure/rpc/awc_webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,17 @@ class _AWCWebviewState extends State<AWCWebview> with WidgetsBindingObserver {
final matchingEvmWallet = EVMWallet.fromScheme(
uri.scheme,
);

if (matchingEvmWallet == null) {
return NavigationActionPolicy.ALLOW;
if (matchingEvmWallet != null) {
await _openEvmWallet(uri, matchingEvmWallet);
return NavigationActionPolicy.CANCEL;
}

if (!await canLaunchUrl(uri.uriValue)) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Wallet ${matchingEvmWallet.name} not installed', // TODO(Chralu): internationalize this
),
),
);
if (uri.scheme != widget.uri.scheme ||
uri.host != widget.uri.host) {
await _openThirdPartyWebsite(uri);
return NavigationActionPolicy.CANCEL;
}

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Opening wallet ${matchingEvmWallet.name}',
), // TODO(Chralu): internationalize this
duration: const Duration(days: 1),
),
);

unawaited(
launchUrl(uri, mode: LaunchMode.externalApplication),
);

return NavigationActionPolicy.CANCEL;
return NavigationActionPolicy.ALLOW;
},
onReceivedHttpError: (controller, request, errorResponse) {
AWCWebview._logger.warning(
Expand All @@ -217,6 +198,36 @@ class _AWCWebviewState extends State<AWCWebview> with WidgetsBindingObserver {
],
);

Future<bool> _openThirdPartyWebsite(WebUri uri) async {
return launchUrl(uri.uriValue, mode: LaunchMode.externalApplication);
}

Future<void> _openEvmWallet(WebUri uri, EVMWallet evmWallet) async {
if (!await canLaunchUrl(uri.uriValue)) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Wallet ${evmWallet.name} not installed', // TODO(Chralu): internationalize this
),
),
);
return;
}

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Opening wallet ${evmWallet.name}',
), // TODO(Chralu): internationalize this
duration: const Duration(days: 1),
),
);

unawaited(
launchUrl(uri, mode: LaunchMode.externalApplication),
);
}

Future<void> _restoreMessageChannelRPC(
InAppWebViewController controller,
) async {
Expand Down

0 comments on commit 6fed420

Please sign in to comment.