Skip to content

Commit 88470bb

Browse files
committed
actions [nfc]: Move open-link logic here, from lib/widgets/content
We'll use when we add a "Learn more"-button param to showErrorDialog, coming up.
1 parent 4e19f3d commit 88470bb

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

lib/widgets/actions.dart

+26
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,30 @@ abstract final class PlatformActions {
281281
SnackBar(behavior: SnackBarBehavior.floating, content: successContent));
282282
}
283283
}
284+
285+
/// Opens a URL with [ZulipBinding.launchUrl], with an error dialog on failure.
286+
// TODO widget tests
287+
static Future<void> launchUrl(BuildContext context, Uri url) async {
288+
final globalSettings = GlobalStoreWidget.settingsOf(context);
289+
290+
bool launched = false;
291+
String? errorMessage;
292+
try {
293+
launched = await ZulipBinding.instance.launchUrl(url,
294+
mode: globalSettings.getUrlLaunchMode(url));
295+
} on PlatformException catch (e) {
296+
errorMessage = e.message;
297+
}
298+
if (!launched) { // TODO(log)
299+
if (!context.mounted) return;
300+
301+
final zulipLocalizations = ZulipLocalizations.of(context);
302+
showErrorDialog(context: context,
303+
title: zulipLocalizations.errorCouldNotOpenLinkTitle,
304+
message: [
305+
zulipLocalizations.errorCouldNotOpenLink(url.toString()),
306+
if (errorMessage != null) errorMessage,
307+
].join("\n\n"));
308+
}
309+
}
284310
}

lib/widgets/content.dart

+2-22
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import 'package:flutter/cupertino.dart';
44
import 'package:flutter/gestures.dart';
55
import 'package:flutter/material.dart';
66
import 'package:flutter/rendering.dart';
7-
import 'package:flutter/services.dart';
87
import 'package:html/dom.dart' as dom;
98
import 'package:intl/intl.dart';
109

1110
import '../api/core.dart';
1211
import '../api/model/model.dart';
1312
import '../generated/l10n/zulip_localizations.dart';
1413
import '../model/avatar_url.dart';
15-
import '../model/binding.dart';
1614
import '../model/content.dart';
1715
import '../model/internal_link.dart';
16+
import 'actions.dart';
1817
import 'code_block.dart';
1918
import 'dialog.dart';
2019
import 'icons.dart';
@@ -1432,26 +1431,7 @@ void _launchUrl(BuildContext context, String urlString) async {
14321431
return;
14331432
}
14341433

1435-
final globalSettings = GlobalStoreWidget.settingsOf(context);
1436-
bool launched = false;
1437-
String? errorMessage;
1438-
try {
1439-
launched = await ZulipBinding.instance.launchUrl(url,
1440-
mode: globalSettings.getUrlLaunchMode(url));
1441-
} on PlatformException catch (e) {
1442-
errorMessage = e.message;
1443-
}
1444-
if (!launched) { // TODO(log)
1445-
if (!context.mounted) return;
1446-
1447-
final zulipLocalizations = ZulipLocalizations.of(context);
1448-
showErrorDialog(context: context,
1449-
title: zulipLocalizations.errorCouldNotOpenLinkTitle,
1450-
message: [
1451-
zulipLocalizations.errorCouldNotOpenLink(url.toString()),
1452-
if (errorMessage != null) errorMessage,
1453-
].join("\n\n"));
1454-
}
1434+
await PlatformActions.launchUrl(context, url);
14551435
}
14561436

14571437
/// Like [Image.network], but includes [authHeader] if [src] is on-realm.

lib/widgets/login.dart

+3
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ class _LoginPageState extends State<LoginPage> {
343343
// Could set [_inProgress]… but we'd need to unset it if the web-auth
344344
// attempt is aborted (by the user closing the browser, for example),
345345
// and I don't think we can reliably know when that happens.
346+
347+
// Not using [PlatformActions.launchUrl] because web auth needs special
348+
// error handling.
346349
await ZulipBinding.instance.launchUrl(url, mode: LaunchMode.inAppBrowserView);
347350
} catch (e) {
348351
assert(debugLog(e.toString()));

0 commit comments

Comments
 (0)