You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
meshcore-client/lib/widgets/link_router_host.dart

154 lines
4.5 KiB

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../connector/meshcore_connector.dart';
import '../connector/meshcore_protocol.dart';
import '../helpers/channel_share_add.dart';
import '../helpers/snack_bar_builder.dart';
import '../l10n/l10n.dart';
import '../services/link_router.dart';
import '../utils/app_logger.dart';
import 'add_contact_by_key_dialog.dart';
import 'contact_add_feedback.dart';
/// Shows the confirmation for a share link held by [LinkRouter] as soon as
/// one can be shown, and says why when it cannot (#1289).
///
/// It wraps the home route, so it sits below the Navigator and its dialogs
/// and messages open over whatever screen is on top.
class LinkRouterHost extends StatefulWidget {
const LinkRouterHost({super.key, required this.child, this.osLinks});
final Widget child;
/// Links the OS opened the app with: the one that launched it, then any
/// that arrive while it runs (#1291). Null in tests that do not need it.
final Stream<Uri>? osLinks;
@override
State<LinkRouterHost> createState() => _LinkRouterHostState();
}
class _LinkRouterHostState extends State<LinkRouterHost> {
late final LinkRouter _router;
late final MeshCoreConnector _connector;
StreamSubscription<Uri>? _osLinks;
bool _scheduled = false;
bool _showing = false;
@override
void initState() {
super.initState();
_router = context.read<LinkRouter>();
_connector = context.read<MeshCoreConnector>();
_router.addListener(_schedule);
_connector.addListener(_schedule);
_osLinks = widget.osLinks?.listen(
_router.handleFromOs,
onError: (Object e) =>
appLogger.warn('OS link stream error: $e', tag: 'LinkRouter'),
);
_schedule();
}
@override
void dispose() {
_osLinks?.cancel();
_router.removeListener(_schedule);
_connector.removeListener(_schedule);
super.dispose();
}
// Listeners can fire mid-build; dialogs open after the frame.
void _schedule() {
if (_scheduled) return;
_scheduled = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
_scheduled = false;
if (mounted) _check();
});
}
Future<void> _check() async {
if (_showing) return;
final l10n = context.l10n;
if (_router.takeRejected() != null) {
showDismissibleSnackBar(
context,
content: Text(l10n.link_invalid),
persist: true,
);
}
final link = _router.takeIfReady(
connected: _connector.isConnected,
channelTableBusy: _connector.isChannelTableBusy,
);
if (link == null) {
final waiting = _router.pending;
if (waiting != null && !_router.pendingAnnounced) {
_router.markPendingAnnounced();
final name = waiting.displayName;
showDismissibleSnackBar(
context,
content: Text(
name == null
? l10n.link_waitingForRadioContact
: l10n.link_waitingForRadio(name),
),
persist: true,
);
}
return;
}
_showing = true;
try {
switch (link) {
case ChannelShareLink(:final channel):
await confirmAndAddSharedChannel(context, channel);
case ContactShareLink(:final uri):
await showAddContactByKeyDialog(context, initialKeyText: uri);
case AdvertShareLink(:final advert):
await _importAdvert(advert);
}
} finally {
_showing = false;
}
// A link that arrived while this one was open.
if (mounted) _schedule();
}
/// The same import "Add Contact from Clipboard" does, after a confirmation.
Future<void> _importAdvert(Uint8List advert) async {
final confirmed = await showDialog<bool>(
context: context,
builder: (dialogContext) => AlertDialog(
title: Text(dialogContext.l10n.link_addContactTitle),
content: Text(dialogContext.l10n.link_importAdvertConfirm),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext, false),
child: Text(dialogContext.l10n.common_cancel),
),
FilledButton(
onPressed: () => Navigator.pop(dialogContext, true),
child: Text(dialogContext.l10n.common_add),
),
],
),
);
if (confirmed != true || !mounted) return;
await addContactWithFeedback(
context,
_connector,
'',
() => _connector.importContact(buildImportContactFrame(advert)),
);
}
@override
Widget build(BuildContext context) => widget.child;
}

Powered by TurnKey Linux.