2023-07-23 13:40:59 +03:30
|
|
|
import 'dart:io';
|
|
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/utils/utils.dart';
|
|
|
|
|
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 12:40:19 +01:00
|
|
|
// if (Platform.isLinux) return null;
|
|
|
|
|
try {
|
|
|
|
|
for (final protocol in LinkParser.protocols) {
|
|
|
|
|
await protocolHandler.register(protocol);
|
|
|
|
|
}
|
|
|
|
|
protocolHandler.addListener(this);
|
|
|
|
|
ref.onDispose(() {
|
|
|
|
|
protocolHandler.removeListener(this);
|
|
|
|
|
});
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2024-03-11 12:40:19 +01:00
|
|
|
final initialPayload = await protocolHandler.getInitialUrl();
|
|
|
|
|
if (initialPayload != null) {
|
|
|
|
|
loggy.debug('initial payload: [$initialPayload]');
|
|
|
|
|
final link = LinkParser.deep(initialPayload);
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
loggy.error("failed to init deeplink", e);
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|