This repository was archived by the owner on Jun 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp_state.dart
More file actions
221 lines (184 loc) · 5.56 KB
/
Copy pathapp_state.dart
File metadata and controls
221 lines (184 loc) · 5.56 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import 'package:flutter/foundation.dart';
import 'package:timeline/bridge_generated.dart';
import 'package:timeline/common/models/dispatcher.dart';
class AppState {
final Timeline timeline;
final AppEventDispatcher dispatcher;
final Native native;
final Notifications notifications;
AppState._(this.timeline, this.dispatcher, this.native, this.notifications);
static AppState build(AppEventDispatcher dispatcher, Native native) {
final timeline = Timeline(dispatcher, native);
final notifications = Notifications(dispatcher, native);
return AppState._(timeline, dispatcher, native, notifications);
}
Future<CardView> createCard() async {
return native.createCard();
}
Future<CardView> findCard(String id) async {
return native.getCard(cardId: id);
}
syncBackend() {
native.sync();
}
/// Clear all local data
Future<void> dangerousLogout() async {
await native.logout();
dispatcher.dispatch(const OutputEvent.logOut());
}
String getCurrentDeviceId() {
return native.getCurrentDeviceId();
}
}
class Timeline extends ChangeNotifier {
final AppEventDispatcher _dispatcher;
final Native native;
Timeline(this._dispatcher, this.native) {
_dispatcher.addListener<OutputEvent_TimelineUpdated>((_) {
refresh();
});
_dispatcher.addListener<OutputEvent_DocUpdated>((_) {
refresh();
});
}
void refresh() {
notifyListeners();
}
Future<List<String>> days({required List<String> labelIds}) async {
// Use to test scrolling
// final today = DateTime.now();
// return List.generate(
// 50,
// (index) => DateFormat('yyyy-MM-dd')
// .format(today.subtract(Duration(days: index))),
// );
return native.timelineDays(labelIds: labelIds);
}
Future<TimelineDay> byDay(String day,
{required List<String> labelIds}) async {
// Use to test scrolling
// await Future.delayed(const Duration(seconds: 3));
// final date = DateTime.parse(day).add(const Duration(hours: 6));
// final count = date.day;
// return List.generate(count, (index) {
// return TimelineItem(
// id: '$date-$index',
// createdAtSec: date.millisecondsSinceEpoch ~/ 1000,
// editedAtSec: date.millisecondsSinceEpoch ~/ 1000,
// content: TimelineCardPreview(CardDocPreview(
// text: '$date-$index',
// hasAttachment: false,
// )),
// );
// });
return native.timelineByDay(day: day, labelIds: labelIds);
}
}
class Notifications extends ChangeNotifier {
final AppEventDispatcher _dispatcher;
final Native _native;
final List<String> ids = [];
Notifications(this._dispatcher, this._native) {
_dispatcher.addListener<OutputEvent_Notification>((event) {
ids.add(event.id);
notifyListeners();
});
_dispatcher.addListener<OutputEvent_NotificationsUpdated>((event) {
_load();
});
_load();
}
void _load() async {
final fetched = await _native.listNotificationIds();
ids.clear();
ids.addAll(fetched);
notifyListeners();
}
Future<void> accept(String id) async {
await _native.acceptNotification(id: id);
ids.remove(id);
notifyListeners();
}
Future<void> ignore(String id) async {
await _native.ignoreNotification(id: id);
ids.remove(id);
notifyListeners();
}
}
class AccEdit extends ChangeNotifier {
AccView view;
final AppEventDispatcher _dispatcher;
final Native native;
AccEdit(this.view, this._dispatcher, this.native) {
setView(view);
_dispatcher.addListener<OutputEvent_AccUpdated>((event) {
if (event.field0.id == view.id) {
setView(event.field0);
notifyListeners();
}
});
}
AccEdit.empty(AppEventDispatcher dispatcher, Native native)
: this(
AccView(
id: 'empty',
name: '',
contacts: [],
devices: [],
labels: [],
createdAtSec: 0,
),
dispatcher,
native);
Future<void> editName(String name) async {
final acc = await native.editName(name: name);
setView(acc);
notifyListeners();
}
Future<void> addContact(
{required String accountId, required String name}) async {
final acc = await native.addContact(
contact: AccContact(accountId: accountId, name: name));
setView(acc);
notifyListeners();
}
Future<void> editContactName(
{required String accountId, required String name}) async {
final acc = await native.editContactName(accountId: accountId, name: name);
setView(acc);
notifyListeners();
}
Future<AccLabel> createLabel({required String name}) async {
final res = await native.createAccLabel(name: name);
setView(res.view);
notifyListeners();
return res.label;
}
Future<void> deleteLabel({required String labelId}) async {
final acc = await native.deleteAccLabel(labelId: labelId);
setView(acc);
notifyListeners();
}
Future<String> linkDevice(String share) async {
final newDeviceName = await native.linkDevice(share: share);
refresh();
return newDeviceName;
}
Future<void> removeDevice(String deviceId) async {
final acc = await native.removeDevice(removeId: deviceId);
setView(acc);
notifyListeners();
}
Future<void> refresh() async {
final acc = await native.getAccount();
if (acc != null) {
setView(acc);
notifyListeners();
}
}
setView(AccView acc) {
view = acc;
view.labels.sort((a, b) => a.name.compareTo(b.name));
view.contacts.sort((a, b) => a.name.compareTo(b.name));
}
}