forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannouncement.dart
More file actions
333 lines (279 loc) · 9.68 KB
/
Copy pathannouncement.dart
File metadata and controls
333 lines (279 loc) · 9.68 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import 'package:omi/backend/schema/gen/announcements_wire.g.dart' as wire;
enum AnnouncementType { changelog, feature, announcement }
enum TriggerType { immediate, versionUpgrade, firmwareUpgrade }
String _triggerTypeToWire(TriggerType trigger) {
switch (trigger) {
case TriggerType.immediate:
return 'immediate';
case TriggerType.versionUpgrade:
return 'version_upgrade';
case TriggerType.firmwareUpgrade:
return 'firmware_upgrade';
}
}
TriggerType _triggerTypeFromWire(String? trigger) {
switch (trigger) {
case 'immediate':
return TriggerType.immediate;
case 'firmware_upgrade':
return TriggerType.firmwareUpgrade;
case 'version_upgrade':
return TriggerType.versionUpgrade;
default:
throw FormatException('Unknown announcement trigger: $trigger');
}
}
String _announcementTypeToWire(AnnouncementType type) => type.name;
AnnouncementType _announcementTypeFromWire(String type) {
final announcementType = AnnouncementType.values.asNameMap()[type];
if (announcementType == null) {
throw FormatException('Unknown announcement type: $type');
}
return announcementType;
}
class Targeting {
final String? appVersionMin;
final String? appVersionMax;
final String? firmwareVersionMin;
final String? firmwareVersionMax;
final List<String>? deviceModels;
final List<String>? platforms;
final TriggerType trigger;
final List<String>? testUids;
Targeting({
this.appVersionMin,
this.appVersionMax,
this.firmwareVersionMin,
this.firmwareVersionMax,
this.deviceModels,
this.platforms,
this.trigger = TriggerType.versionUpgrade,
this.testUids,
});
factory Targeting.fromJson(Map<String, dynamic> json) {
return Targeting.fromGenerated(wire.GeneratedTargeting.fromJson(json));
}
factory Targeting.fromGenerated(wire.GeneratedTargeting generated) {
return Targeting(
appVersionMin: generated.appVersionMin,
appVersionMax: generated.appVersionMax,
firmwareVersionMin: generated.firmwareVersionMin,
firmwareVersionMax: generated.firmwareVersionMax,
deviceModels: generated.deviceModels ?? const [],
platforms: generated.platforms ?? const [],
trigger: _triggerTypeFromWire(generated.trigger),
testUids: generated.testUids,
);
}
wire.GeneratedTargeting toGenerated() {
return wire.GeneratedTargeting(
appVersionMin: appVersionMin,
appVersionMax: appVersionMax,
firmwareVersionMin: firmwareVersionMin,
firmwareVersionMax: firmwareVersionMax,
deviceModels: deviceModels,
platforms: platforms,
trigger: _triggerTypeToWire(trigger),
testUids: testUids,
);
}
Map<String, dynamic> toJson() => toGenerated().toJson();
}
// Display model for controlling announcement presentation
class Display {
final int priority;
final DateTime? startAt;
final DateTime? expiresAt;
final bool dismissible;
final bool showOnce;
Display({this.priority = 0, this.startAt, this.expiresAt, this.dismissible = true, this.showOnce = true});
factory Display.fromJson(Map<String, dynamic> json) {
return Display.fromGenerated(wire.GeneratedDisplay.fromJson(json));
}
factory Display.fromGenerated(wire.GeneratedDisplay generated) {
return Display(
priority: generated.priority,
startAt: generated.startAt,
expiresAt: generated.expiresAt,
dismissible: generated.dismissible,
showOnce: generated.showOnce,
);
}
wire.GeneratedDisplay toGenerated() {
return wire.GeneratedDisplay(
priority: priority,
startAt: startAt,
expiresAt: expiresAt,
dismissible: dismissible,
showOnce: showOnce,
);
}
Map<String, dynamic> toJson() => toGenerated().toJson();
}
// Changelog content models
class ChangelogItem {
final String title;
final String description;
final String? icon;
ChangelogItem({required this.title, required this.description, this.icon});
factory ChangelogItem.fromJson(Map<String, dynamic> json) {
return ChangelogItem(
title: json['title'] as String,
description: json['description'] as String,
icon: json['icon'] as String?,
);
}
Map<String, dynamic> toJson() => {'title': title, 'description': description, 'icon': icon};
}
class ChangelogContent {
final String title;
final List<ChangelogItem> changes;
ChangelogContent({required this.title, required this.changes});
factory ChangelogContent.fromJson(Map<String, dynamic> json) {
return ChangelogContent(
title: json['title'] as String,
changes: (json['changes'] as List<dynamic>)
.map((item) => ChangelogItem.fromJson(item as Map<String, dynamic>))
.toList(),
);
}
Map<String, dynamic> toJson() => {'title': title, 'changes': changes.map((item) => item.toJson()).toList()};
}
// Feature content models
class FeatureStep {
final String title;
final String description;
final String? imageUrl;
final String? videoUrl;
final String? highlightText;
FeatureStep({required this.title, required this.description, this.imageUrl, this.videoUrl, this.highlightText});
factory FeatureStep.fromJson(Map<String, dynamic> json) {
return FeatureStep(
title: json['title'] as String,
description: json['description'] as String,
imageUrl: json['image_url'] as String?,
videoUrl: json['video_url'] as String?,
highlightText: json['highlight_text'] as String?,
);
}
Map<String, dynamic> toJson() {
return {
'title': title,
'description': description,
'image_url': imageUrl,
'video_url': videoUrl,
'highlight_text': highlightText,
};
}
}
class FeatureContent {
final String title;
final List<FeatureStep> steps;
FeatureContent({required this.title, required this.steps});
factory FeatureContent.fromJson(Map<String, dynamic> json) {
return FeatureContent(
title: json['title'] as String,
steps:
(json['steps'] as List<dynamic>).map((item) => FeatureStep.fromJson(item as Map<String, dynamic>)).toList(),
);
}
Map<String, dynamic> toJson() => {'title': title, 'steps': steps.map((step) => step.toJson()).toList()};
}
// Announcement content models
class AnnouncementCTA {
final String text;
final String action;
AnnouncementCTA({required this.text, required this.action});
factory AnnouncementCTA.fromJson(Map<String, dynamic> json) {
return AnnouncementCTA(text: json['text'] as String, action: json['action'] as String);
}
Map<String, dynamic> toJson() => {'text': text, 'action': action};
}
class AnnouncementContent {
final String title;
final String body;
final String? imageUrl;
final AnnouncementCTA? cta;
AnnouncementContent({required this.title, required this.body, this.imageUrl, this.cta});
factory AnnouncementContent.fromJson(Map<String, dynamic> json) {
return AnnouncementContent(
title: json['title'] as String,
body: json['body'] as String,
imageUrl: json['image_url'] as String?,
cta: json['cta'] == null ? null : AnnouncementCTA.fromJson(json['cta'] as Map<String, dynamic>),
);
}
Map<String, dynamic> toJson() => {'title': title, 'body': body, 'image_url': imageUrl, 'cta': cta?.toJson()};
}
// Main announcement model
class Announcement {
final String id;
final AnnouncementType type;
final DateTime createdAt;
final bool active;
// Version fields (used by /changelogs endpoint)
final String? appVersion;
final String? firmwareVersion;
final List<String>? deviceModels;
final DateTime? expiresAt;
// Flexible targeting and display options
final Targeting? targeting;
final Display? display;
// Raw content - parsed based on type
final Map<String, dynamic> content;
Announcement({
required this.id,
required this.type,
required this.createdAt,
this.active = true,
this.appVersion,
this.firmwareVersion,
this.deviceModels,
this.expiresAt,
this.targeting,
this.display,
required this.content,
});
factory Announcement.fromJson(Map<String, dynamic> json) {
return Announcement.fromGenerated(wire.GeneratedAnnouncement.fromJson(json));
}
factory Announcement.fromGenerated(wire.GeneratedAnnouncement generated) {
return Announcement(
id: generated.id,
type: _announcementTypeFromWire(generated.type),
createdAt: generated.createdAt,
active: generated.active,
appVersion: generated.appVersion,
firmwareVersion: generated.firmwareVersion,
deviceModels: generated.deviceModels,
expiresAt: generated.expiresAt,
targeting: generated.targeting == null ? null : Targeting.fromGenerated(generated.targeting!),
display: generated.display == null ? null : Display.fromGenerated(generated.display!),
content: generated.content,
);
}
wire.GeneratedAnnouncement toGenerated() {
return wire.GeneratedAnnouncement(
id: id,
type: _announcementTypeToWire(type),
createdAt: createdAt,
active: active,
appVersion: appVersion,
firmwareVersion: firmwareVersion,
deviceModels: deviceModels,
expiresAt: expiresAt,
targeting: targeting?.toGenerated(),
display: display?.toGenerated(),
content: content,
);
}
Map<String, dynamic> toJson() => toGenerated().toJson();
// Type-specific content getters
ChangelogContent get changelogContent => ChangelogContent.fromJson(content);
FeatureContent get featureContent => FeatureContent.fromJson(content);
AnnouncementContent get announcementContent => AnnouncementContent.fromJson(content);
// Get effective display priority (defaults to 0)
int get effectivePriority => display?.priority ?? 0;
// Get effective trigger type
TriggerType get effectiveTrigger => targeting?.trigger ?? TriggerType.versionUpgrade;
}