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.
121 lines
3.8 KiB
121 lines
3.8 KiB
import 'package:flutter/foundation.dart';
|
|
|
|
import '../connector/meshcore_protocol.dart';
|
|
import '../models/channel.dart';
|
|
import '../models/contact.dart';
|
|
|
|
/// A share link Offband can act on.
|
|
sealed class ShareLink {
|
|
const ShareLink();
|
|
|
|
/// What the link adds, for "Connect a radio to add ...", or null when the
|
|
/// link does not say.
|
|
String? get displayName;
|
|
}
|
|
|
|
class ChannelShareLink extends ShareLink {
|
|
const ChannelShareLink(this.channel);
|
|
final Channel channel;
|
|
|
|
@override
|
|
String get displayName => channel.name;
|
|
}
|
|
|
|
class ContactShareLink extends ShareLink {
|
|
const ContactShareLink(this.uri, this.contact);
|
|
|
|
/// The link as received. The add-contact dialog parses it again itself, so
|
|
/// a link and a paste fill the dialog the same way.
|
|
final String uri;
|
|
final Contact contact;
|
|
|
|
@override
|
|
String get displayName => contact.name;
|
|
}
|
|
|
|
/// The older `meshcore://<signed advert hex>` form, which "Copy Advert to
|
|
/// Clipboard" still produces. The radio imports it as is, so it is carried
|
|
/// as bytes; the name is inside the signed advert and only the radio reads
|
|
/// it.
|
|
class AdvertShareLink extends ShareLink {
|
|
const AdvertShareLink(this.advert);
|
|
final Uint8List advert;
|
|
|
|
@override
|
|
String? get displayName => null;
|
|
}
|
|
|
|
// An advert is at least 98 bytes: key, timestamp, signature, flags.
|
|
final _advertLink = RegExp(r'^meshcore://((?:[0-9a-fA-F]{2}){98,})$');
|
|
|
|
/// Parses [raw] into a link Offband can add, or null if it is not one.
|
|
ShareLink? parseShareLink(String raw) {
|
|
final trimmed = raw.trim();
|
|
final channel = Channel.fromShareUri(trimmed);
|
|
if (channel != null) return ChannelShareLink(channel);
|
|
final contact = Contact.fromShareUri(trimmed);
|
|
if (contact != null) return ContactShareLink(trimmed, contact);
|
|
final advert = _advertLink.firstMatch(trimmed);
|
|
if (advert != null) return AdvertShareLink(hex2Uint8List(advert[1]!));
|
|
return null;
|
|
}
|
|
|
|
/// The one place a share link goes, wherever it came from: a tap in a
|
|
/// message, the OS, the web client's address bar (#1289).
|
|
///
|
|
/// A link is never acted on here. It is held until the app can show its
|
|
/// confirmation, and `LinkRouterHost` shows it. Only the latest link is held,
|
|
/// and only in memory: an old link must not pop up days later.
|
|
class LinkRouter extends ChangeNotifier {
|
|
ShareLink? _pending;
|
|
bool _pendingAnnounced = false;
|
|
String? _rejected;
|
|
|
|
ShareLink? get pending => _pending;
|
|
|
|
/// True once the user has been told the pending link is waiting for a
|
|
/// radio, so the message is shown once per link, not on every change.
|
|
bool get pendingAnnounced => _pendingAnnounced;
|
|
|
|
/// Takes a link from any source. Returns false if it is not one Offband
|
|
/// can add; the host then tells the user so.
|
|
bool handle(String raw) {
|
|
final link = parseShareLink(raw);
|
|
if (link == null) {
|
|
_rejected = raw;
|
|
} else {
|
|
_pending = link;
|
|
_pendingAnnounced = false;
|
|
}
|
|
notifyListeners();
|
|
return link != null;
|
|
}
|
|
|
|
/// The pending link if its confirmation can be shown now, removing it.
|
|
///
|
|
/// Adding anything needs a connected radio. A channel also waits for the
|
|
/// channel table to settle: right after connecting, the connector empties
|
|
/// its channel list while it syncs, so a duplicate check would miss a
|
|
/// channel the radio already has, and the slot picker would refuse.
|
|
ShareLink? takeIfReady({
|
|
required bool connected,
|
|
required bool channelTableBusy,
|
|
}) {
|
|
final link = _pending;
|
|
if (link == null || !connected) return null;
|
|
if (link is ChannelShareLink && channelTableBusy) return null;
|
|
_pending = null;
|
|
_pendingAnnounced = false;
|
|
return link;
|
|
}
|
|
|
|
void markPendingAnnounced() => _pendingAnnounced = true;
|
|
|
|
/// The last link that could not be parsed, removing it.
|
|
String? takeRejected() {
|
|
final rejected = _rejected;
|
|
_rejected = null;
|
|
return rejected;
|
|
}
|
|
}
|