Line data Source code
1 : // Copyright 2023 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 'package:app_finance/_classes/storage/app_preferences.dart';
5 : import 'package:flutter/foundation.dart';
6 : import 'package:flutter/widgets.dart';
7 : import 'package:peerdart/peerdart.dart';
8 : import 'package:uuid/uuid.dart';
9 :
10 : class SyncStatus {
11 : final String id;
12 : bool? status;
13 0 : SyncStatus(this.id, [this.status]);
14 : }
15 :
16 : class SyncPeer extends SyncStatus {
17 : DataConnection connection;
18 0 : SyncPeer(super.id, this.connection);
19 : }
20 :
21 : class AppSync extends ChangeNotifier {
22 : late Peer peer;
23 : final Map<String, SyncPeer> _status = {};
24 : final Map<Type, Function> _cb = {};
25 : final Map<Type, Function> _cbBin = {};
26 :
27 : // Offline instance (for the background import) is not connected to peers
28 0 : AppSync({bool isOnline = true}) : super() {
29 0 : final id = getUuid();
30 0 : if (isOnline && id != null && id.isNotEmpty) {
31 0 : enable(id);
32 0 : connect();
33 : }
34 : }
35 :
36 0 : List<String> _get() {
37 0 : return AppPreferences.get(AppPreferences.prefP2P)?.split(',').where((e) => e.isNotEmpty).toList() ?? [];
38 : }
39 :
40 0 : void add(String uuid) {
41 0 : List<String> peers = _get();
42 0 : peers.add(uuid);
43 0 : AppPreferences.set(AppPreferences.prefP2P, (<String>{}..addAll(peers)).toList().join(',')).then((_) => connect());
44 : }
45 :
46 0 : void del(String uuid) {
47 0 : _status[uuid]?.connection.dispose();
48 0 : _status.remove(uuid);
49 0 : AppPreferences.set(AppPreferences.prefP2P, (_get()..remove(uuid)).join(',')).then((_) => notifyListeners());
50 : }
51 :
52 0 : void connect() {
53 0 : List<String> peers = _get();
54 0 : for (int i = 0; i < peers.length; i++) {
55 0 : trace(peers[i]);
56 : }
57 : }
58 :
59 0 : void trace(String id) {
60 0 : WidgetsBinding.instance.addPostFrameCallback((_) {
61 0 : if (peer == null) {
62 : return;
63 : }
64 0 : if (!peer.open) {
65 0 : peer.reconnect();
66 : }
67 0 : if (_status[id] == null) {
68 0 : _status[id] = SyncPeer(id, peer.connect(id));
69 0 : } else if (!_status[id]!.connection.open) {
70 0 : peer.removeConnection(_status[id]!.connection);
71 0 : _status[id]!.connection = peer.connect(id);
72 : }
73 : // TBD: when connected, verify postponed messages
74 0 : _status[id]!.connection.once('open').then((v) {
75 0 : WidgetsBinding.instance.addPostFrameCallback((_) {
76 0 : _status[id]!.status = true;
77 0 : notifyListeners();
78 : });
79 : });
80 0 : _status[id]!.connection.once('close').then((v) {
81 0 : WidgetsBinding.instance.addPostFrameCallback((_) {
82 0 : _status[id]!.status = false;
83 0 : notifyListeners();
84 : });
85 : });
86 : });
87 : }
88 :
89 0 : List<SyncStatus> getPeers() {
90 0 : return _status.values.map((e) => SyncStatus(e.id, e.status)).toList();
91 : }
92 :
93 0 : void follow(Type type, Function callback) => _cb[type] = callback;
94 :
95 0 : void followBinary(Type type, Function callback) => _cbBin[type] = callback;
96 :
97 0 : void unfollow(Type type) {
98 0 : _cb.remove(type);
99 0 : _cbBin.remove(type);
100 : }
101 :
102 0 : bool isActive() {
103 0 : return AppPreferences.get(AppPreferences.prefPeer) != null;
104 : }
105 :
106 0 : void disable() {
107 0 : WidgetsBinding.instance.addPostFrameCallback((_) {
108 0 : AppPreferences.clear(AppPreferences.prefPeer);
109 0 : AppPreferences.clear(AppPreferences.prefP2P);
110 0 : peer.dispose();
111 : });
112 : }
113 :
114 0 : void enable([String? id]) {
115 0 : WidgetsBinding.instance.addPostFrameCallback((_) {
116 0 : id ??= getUuid(true);
117 0 : peer = Peer(id: id);
118 0 : peer.on<DataConnection>('connection').listen((conn) {
119 0 : WidgetsBinding.instance.addPostFrameCallback((_) => _listen(conn));
120 : });
121 : });
122 : }
123 :
124 0 : _listen(DataConnection conn) {
125 0 : conn.on('data').listen((data) {
126 0 : WidgetsBinding.instance.addPostFrameCallback((_) {
127 0 : _cb.forEach((_, callback) => callback(data));
128 : });
129 : });
130 0 : conn.on('binary').listen((data) {
131 0 : WidgetsBinding.instance.addPostFrameCallback((_) {
132 0 : _cbBin.forEach((_, callback) => callback(data));
133 : });
134 : });
135 : }
136 :
137 0 : send(String data, [String? uuid]) {
138 0 : WidgetsBinding.instance.addPostFrameCallback((_) {
139 : if (uuid != null) {
140 0 : if (_status[uuid]?.status == true) {
141 0 : _status[uuid]!.connection.send(data);
142 : }
143 : } else {
144 0 : final keyList = _status.keys.toList();
145 0 : for (int i = 0; i < keyList.length; i++) {
146 0 : if (_status[keyList[i]]?.status == true && _status[keyList[i]]!.connection.open) {
147 0 : _status[keyList[i]]!.connection.send(data);
148 : } else {
149 : // .. postpone message
150 : }
151 : }
152 : }
153 : });
154 : }
155 :
156 0 : ping(String uuid) {
157 0 : WidgetsBinding.instance.addPostFrameCallback((_) {
158 0 : if (_status[uuid]?.status == true) {
159 0 : _status[uuid]!.connection.sendBinary(Uint8List.fromList(getUuid()?.codeUnits ?? [0]));
160 : }
161 : });
162 : }
163 :
164 0 : String getNewUuid() {
165 0 : return const Uuid().v4();
166 : }
167 :
168 0 : String? getUuid([bool force = false]) {
169 0 : String? id = AppPreferences.get(AppPreferences.prefPeer);
170 : if (force) {
171 0 : AppPreferences.set(AppPreferences.prefPeer, id = getNewUuid());
172 : }
173 : return id;
174 : }
175 : }
|