51 lines
1.1 KiB
Dart
51 lines
1.1 KiB
Dart
import 'dart:async';
|
|
import 'dart:developer';
|
|
|
|
import 'package:app_links/app_links.dart';
|
|
import 'package:mnemo_cards/features/analytics/analytics.dart';
|
|
|
|
import 'deeplinks_manager.dart';
|
|
|
|
class DeeplinkManager implements DeeplinkHandler {
|
|
final AppLinks _appLinks;
|
|
final List<DeeplinkHandler> _handlers;
|
|
|
|
StreamSubscription? _subscription;
|
|
|
|
DeeplinkManager(
|
|
this._appLinks,
|
|
this._handlers,
|
|
);
|
|
|
|
Future<void> init() async {
|
|
_subscription ??= _appLinks.uriLinkStream.listen(_handleDeeplink);
|
|
}
|
|
|
|
Future<void> dispose() async {
|
|
await _subscription?.cancel();
|
|
_subscription = null;
|
|
}
|
|
|
|
@override
|
|
bool canHandle(Uri uri) {
|
|
for (final handler in _handlers) {
|
|
if (handler.canHandle(uri)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@override
|
|
void handle(Uri uri) => _handleDeeplink(uri);
|
|
|
|
void _handleDeeplink(Uri uri) {
|
|
for (final handler in _handlers) {
|
|
if (handler.canHandle(uri)) {
|
|
return handler.handle(uri);
|
|
}
|
|
}
|
|
log('Deeplink not handled! ${uri}');
|
|
Analytics.deeplinkError(uri);
|
|
}
|
|
}
|