Files
umbrix/lib/features/deep_link/notifier/deep_link_notifier.dart

47 lines
1.2 KiB
Dart
Raw Normal View History

2023-07-23 13:40:59 +03:30
import 'dart:io';
import 'package:umbrix/utils/utils.dart';
2023-07-06 17:18:41 +03:30
import 'package:protocol_handler/protocol_handler.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
2024-01-05 15:21:10 +03:30
part 'deep_link_notifier.g.dart';
2023-07-06 17:18:41 +03:30
typedef NewProfileLink = ({String? url, String? name});
@Riverpod(keepAlive: true)
2024-01-05 15:21:10 +03:30
class DeepLinkNotifier extends _$DeepLinkNotifier
2023-07-06 17:18:41 +03:30
with ProtocolListener, InfraLogger {
@override
Future<NewProfileLink?> build() async {
2024-03-11 15:40:29 +01:00
if (Platform.isLinux) return null;
2023-07-06 17:18:41 +03:30
2024-03-11 15:40:29 +01:00
for (final protocol in LinkParser.protocols) {
await protocolHandler.register(protocol);
}
protocolHandler.addListener(this);
ref.onDispose(() {
protocolHandler.removeListener(this);
});
final initialPayload = await protocolHandler.getInitialUrl();
if (initialPayload != null) {
loggy.debug('initial payload: [$initialPayload]');
final link = LinkParser.deep(initialPayload);
return link;
2023-07-06 17:18:41 +03:30
}
return null;
}
@override
void onProtocolUrlReceived(String url) {
super.onProtocolUrlReceived(url);
loggy.debug("url received: [$url]");
final link = LinkParser.deep(url);
if (link == null) {
loggy.debug("link was not valid");
return;
}
update((_) => link);
}
}