Line data Source code
1 : // Copyright 2026 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'dart:async';
5 :
6 : import 'package:app_finance/_classes/controller/deep_link/url_scheme.dart';
7 : import 'package:app_links/app_links.dart';
8 : import 'package:flutter/foundation.dart';
9 :
10 : // Links opening the application
11 : // > fingrom://integration/enablebanking?... sent back by the bank's authorization
12 : // > fingrom://integration/teller?token=... sent back by the Teller Connect page
13 : // > fingrom://integration/monzo?code=...&state=... sent back by the Monzo sign-in
14 : class DeepLinkHandler {
15 : static const scheme = 'fingrom';
16 : static const enableBankingPath = '/enablebanking';
17 : static const tellerPath = '/teller';
18 : static const monzoPath = '/monzo';
19 :
20 6 : static final enableBankingCallback = ValueNotifier<Uri?>(null);
21 3 : static final tellerCallback = ValueNotifier<Uri?>(null);
22 3 : static final monzoCallback = ValueNotifier<Uri?>(null);
23 : static StreamSubscription<Uri>? _subscription;
24 :
25 0 : static Future<void> init({Stream<Uri>? links}) async {
26 0 : unawaited(registerUrlScheme(scheme).catchError((_) {}));
27 0 : await _subscription?.cancel();
28 : try {
29 0 : _subscription = (links ?? AppLinks().uriLinkStream).listen(handle, onError: (_) {});
30 : } catch (_) {
31 : // Platform without link support
32 : }
33 : }
34 :
35 2 : static bool isEnableBankingCallback(Uri uri) =>
36 12 : uri.scheme == scheme && uri.host == 'integration' && uri.path == enableBankingPath;
37 :
38 7 : static bool isTellerCallback(Uri uri) => uri.scheme == scheme && uri.host == 'integration' && uri.path == tellerPath;
39 :
40 7 : static bool isMonzoCallback(Uri uri) => uri.scheme == scheme && uri.host == 'integration' && uri.path == monzoPath;
41 :
42 2 : static bool handle(Uri uri) {
43 2 : if (isEnableBankingCallback(uri)) {
44 2 : enableBankingCallback.value = uri;
45 : return true;
46 : }
47 1 : if (isTellerCallback(uri)) {
48 2 : tellerCallback.value = uri;
49 : return true;
50 : }
51 1 : if (isMonzoCallback(uri)) {
52 2 : monzoCallback.value = uri;
53 : return true;
54 : }
55 : return false;
56 : }
57 :
58 1 : static Uri? takeEnableBankingCallback() {
59 2 : final value = enableBankingCallback.value;
60 2 : enableBankingCallback.value = null;
61 : return value;
62 : }
63 :
64 1 : static Uri? takeTellerCallback() {
65 2 : final value = tellerCallback.value;
66 2 : tellerCallback.value = null;
67 : return value;
68 : }
69 :
70 1 : static Uri? takeMonzoCallback() {
71 2 : final value = monzoCallback.value;
72 2 : monzoCallback.value = null;
73 : return value;
74 : }
75 : }
|