-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathapp_link_handler.dart
56 lines (51 loc) · 1.68 KB
/
app_link_handler.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import 'dart:async';
import 'package:app_links/app_links.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import './util/goto.dart';
/// Wrapper widget that handles app links / universal links / deep links.
/// It currently supports:
/// - Communities
/// - Users
/// - Posts
///
/// The link format is the same as the web app, with liftoff:// as the scheme.
/// For example:
/// - liftoff://iusearchlinux.fyi/post/33195
/// - liftoff://programming.dev/c/programmer_humor
/// - liftoff://lemmy.world/u/zachatrocity
class AppLinkHandler extends HookWidget {
AppLinkHandler(this.child, {super.key});
final Widget child;
final _appLinks = AppLinks();
@override
Widget build(BuildContext context) {
// The subscription listener only needs to be initialized once.
// useEffect() caused weird crashes, so I'm using state
final initialized = useState(false);
if (!initialized.value) {
// Wait till the app has initialized.
// Otherwise Navigator.of(context) will throw an error.
Future.delayed(Duration.zero, () {
_appLinks.allUriLinkStream.listen((uri) {
final [operation, target] = uri.pathSegments;
switch (operation) {
case 'c':
goToCommunity.byName(context, uri.host, target);
break;
case 'u':
goToUser.byName(context, uri.host, target);
break;
case 'post':
goToPost(context, uri.host, int.parse(target));
break;
default:
// not supported -- ignore it.
}
});
});
initialized.value = true;
}
return child;
}
}