forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfair_use_page.dart
More file actions
396 lines (370 loc) · 13.5 KB
/
Copy pathfair_use_page.dart
File metadata and controls
396 lines (370 loc) · 13.5 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:omi/backend/http/api/users.dart';
import 'package:omi/services/wals/sync_rate_limit_reconciliation.dart';
import 'package:omi/utils/l10n_extensions.dart';
class FairUsePage extends StatefulWidget {
const FairUsePage({super.key});
@override
State<FairUsePage> createState() => _FairUsePageState();
}
class _FairUsePageState extends State<FairUsePage> {
Map<String, dynamic>? _status;
bool _isLoading = true;
String? _error;
@override
void initState() {
super.initState();
_loadStatus();
}
Future<void> _loadStatus() async {
setState(() {
_isLoading = true;
_error = null;
});
try {
final result = await getFairUseStatus();
// Reconcile the rate-limit cooldown regardless of whether the widget is
// still mounted — this only touches the SyncRateLimiter singleton and
// must not be skipped if the user navigates away before the response
// returns (otherwise a stale cooldown is never cleared).
reconcileSyncRateLimitWithFairUseStatus(result);
if (mounted) {
if (result == null) {
setState(() {
_error = 'Unable to load fair use status';
_isLoading = false;
});
} else {
setState(() {
_status = result;
_isLoading = false;
});
}
}
} catch (e) {
if (mounted) {
setState(() {
_error = e.toString();
_isLoading = false;
});
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.black,
title: Text(context.l10n.fairUsePolicy),
leading: IconButton(icon: const Icon(Icons.arrow_back_ios), onPressed: () => Navigator.of(context).pop()),
),
body: _isLoading
? const Center(child: CircularProgressIndicator(color: Colors.white))
: _error != null
? _buildError()
: _status == null
? _buildError()
: RefreshIndicator(
onRefresh: _loadStatus,
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildStatusBanner(),
_buildUsageSection(),
_buildBudgetSection(),
_buildMessageBanner(),
const SizedBox(height: 24),
_buildAboutFooter(),
],
),
),
),
);
}
Widget _buildError() {
return Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
context.l10n.fairUseLoadError,
style: const TextStyle(color: Color(0xFF8E8E93), fontSize: 15),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
TextButton(
onPressed: _loadStatus,
child: Text(context.l10n.retry, style: const TextStyle(color: Color(0xFF8B5CF6))),
),
],
),
),
);
}
Widget _buildStatusBanner() {
final stage = _status!['stage'] as String? ?? 'none';
if (stage == 'none') return const SizedBox.shrink();
final caseRef = _status!['case_ref'] as String? ?? '';
Color dotColor;
String stageLabel;
switch (stage) {
case 'warning':
dotColor = const Color(0xFFFBBF24);
stageLabel = context.l10n.fairUseStageWarning;
break;
case 'throttle':
dotColor = const Color(0xFFF97316);
stageLabel = context.l10n.fairUseStageThrottle;
break;
case 'restrict':
dotColor = const Color(0xFFEF4444);
stageLabel = context.l10n.fairUseStageRestrict;
break;
default:
return const SizedBox.shrink();
}
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
decoration: BoxDecoration(color: dotColor.withValues(alpha: 0.08), borderRadius: BorderRadius.circular(12)),
child: Row(
children: [
Container(
width: 8,
height: 8,
decoration: BoxDecoration(color: dotColor, shape: BoxShape.circle),
),
const SizedBox(width: 10),
Text(
stageLabel,
style: TextStyle(color: dotColor, fontSize: 14, fontWeight: FontWeight.w500),
),
const Spacer(),
if (caseRef.isNotEmpty)
GestureDetector(
onTap: () {
Clipboard.setData(ClipboardData(text: caseRef));
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(context.l10n.fairUseCaseRefCopied(caseRef)),
duration: const Duration(seconds: 2),
backgroundColor: const Color(0xFF2C2C2E),
),
);
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
caseRef,
style: const TextStyle(color: Color(0xFF8E8E93), fontSize: 12, fontFamily: 'monospace'),
),
const SizedBox(width: 4),
const Icon(Icons.copy, size: 12, color: Color(0xFF8E8E93)),
],
),
),
],
),
),
);
}
Widget _buildUsageSection() {
final usagePct = _status!['usage_pct'] as Map<String, dynamic>? ?? {};
final limits = _status!['limits'] as Map<String, dynamic>? ?? {};
final speechToday = (_status!['speech_hours_today'] as num?)?.toDouble() ?? 0;
final speech3day = (_status!['speech_hours_3day'] as num?)?.toDouble() ?? 0;
final speechWeekly = (_status!['speech_hours_weekly'] as num?)?.toDouble() ?? 0;
return Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(color: const Color(0xFF1C1C1E), borderRadius: BorderRadius.circular(16)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.l10n.fairUseSpeechUsage,
style: const TextStyle(color: Color(0xFF8E8E93), fontSize: 13, fontWeight: FontWeight.w500),
),
const SizedBox(height: 16),
_buildUsageBar(
label: context.l10n.fairUseToday,
hours: speechToday,
limit: (limits['daily_hours'] as num?)?.toDouble() ?? 2.0,
pct: (usagePct['daily'] as num?)?.toDouble() ?? 0,
),
const SizedBox(height: 14),
_buildUsageBar(
label: context.l10n.fairUse3Day,
hours: speech3day,
limit: (limits['three_day_hours'] as num?)?.toDouble() ?? 8.0,
pct: (usagePct['three_day'] as num?)?.toDouble() ?? 0,
),
const SizedBox(height: 14),
_buildUsageBar(
label: context.l10n.fairUseWeekly,
hours: speechWeekly,
limit: (limits['weekly_hours'] as num?)?.toDouble() ?? 10.0,
pct: (usagePct['weekly'] as num?)?.toDouble() ?? 0,
),
],
),
);
}
Widget _buildUsageBar({required String label, required double hours, required double limit, required double pct}) {
final barColor = pct >= 100
? const Color(0xFFEF4444)
: pct >= 80
? const Color(0xFFFBBF24)
: const Color(0xFF8B5CF6);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(label, style: const TextStyle(color: Color(0xFF8E8E93), fontSize: 13)),
Text(
'${hours.toStringAsFixed(1)}h / ${limit.toStringAsFixed(0)}h',
style: const TextStyle(color: Colors.white, fontSize: 13, fontWeight: FontWeight.w500),
),
],
),
const SizedBox(height: 6),
ClipRRect(
borderRadius: BorderRadius.circular(3),
child: LinearProgressIndicator(
value: (pct / 100).clamp(0.0, 1.0),
backgroundColor: const Color(0xFF2C2C2E),
valueColor: AlwaysStoppedAnimation<Color>(barColor),
minHeight: 4,
),
),
],
);
}
Widget _buildBudgetSection() {
final stage = _status!['stage'] as String? ?? 'none';
if (stage != 'restrict') return const SizedBox.shrink();
final dgBudget = _status!['dg_budget'] as Map<String, dynamic>?;
if (dgBudget == null) return const SizedBox.shrink();
final dailyLimitMs = (dgBudget['daily_limit_ms'] as num?)?.toInt() ?? 0;
final usedMs = (dgBudget['used_ms'] as num?)?.toInt() ?? 0;
final exhausted = dgBudget['exhausted'] as bool? ?? false;
final resetsAt = dgBudget['resets_at'] as String? ?? '';
if (dailyLimitMs <= 0) return const SizedBox.shrink();
final usedMin = (usedMs / 60000).round();
final limitMin = (dailyLimitMs / 60000).round();
final pct = (usedMs / dailyLimitMs * 100).clamp(0.0, 100.0);
final barColor = exhausted ? const Color(0xFFEF4444) : const Color(0xFF8B5CF6);
String resetLabel = '';
if (resetsAt.isNotEmpty) {
try {
final resetTime = DateTime.parse(resetsAt);
final now = DateTime.now().toUtc();
final diff = resetTime.difference(now);
if (diff.inHours > 0) {
resetLabel = context.l10n.fairUseBudgetResetsAt('${diff.inHours}h');
} else if (diff.inMinutes > 0) {
resetLabel = context.l10n.fairUseBudgetResetsAt('${diff.inMinutes}m');
}
} catch (_) {}
}
return Padding(
padding: const EdgeInsets.only(top: 12),
child: Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: exhausted ? const Color(0xFFEF4444).withValues(alpha: 0.06) : const Color(0xFF1C1C1E),
borderRadius: BorderRadius.circular(16),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
context.l10n.fairUseDailyTranscription,
style: const TextStyle(color: Color(0xFF8E8E93), fontSize: 13, fontWeight: FontWeight.w500),
),
Text(
context.l10n.fairUseBudgetUsed('$usedMin', '$limitMin'),
style: const TextStyle(color: Colors.white, fontSize: 13, fontWeight: FontWeight.w500),
),
],
),
const SizedBox(height: 8),
ClipRRect(
borderRadius: BorderRadius.circular(3),
child: LinearProgressIndicator(
value: (pct / 100).clamp(0.0, 1.0),
backgroundColor: const Color(0xFF2C2C2E),
valueColor: AlwaysStoppedAnimation<Color>(barColor),
minHeight: 4,
),
),
if (exhausted) ...[
const SizedBox(height: 10),
Text(
context.l10n.fairUseBudgetExhausted,
style: const TextStyle(color: Color(0xFFEF4444), fontSize: 13, fontWeight: FontWeight.w500),
),
],
if (resetLabel.isNotEmpty) ...[
const SizedBox(height: 4),
Text(resetLabel, style: const TextStyle(color: Color(0xFF636366), fontSize: 12)),
],
],
),
),
);
}
Widget _buildMessageBanner() {
final message = _status!['message'] as String? ?? '';
if (message.isEmpty) return const SizedBox.shrink();
return Padding(
padding: const EdgeInsets.only(top: 12),
child: Container(
padding: const EdgeInsets.all(14),
decoration: BoxDecoration(color: const Color(0xFF1C1C1E), borderRadius: BorderRadius.circular(12)),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Icon(Icons.info_outline, color: Color(0xFF8E8E93), size: 16),
const SizedBox(width: 10),
Expanded(
child: Text(message, style: const TextStyle(color: Color(0xFFAAAAAA), fontSize: 13, height: 1.4)),
),
],
),
),
);
}
Widget _buildAboutFooter() {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.l10n.fairUseAboutTitle,
style: const TextStyle(color: Color(0xFF636366), fontSize: 12, fontWeight: FontWeight.w500),
),
const SizedBox(height: 4),
Text(
context.l10n.fairUseAboutBody,
style: const TextStyle(color: Color(0xFF48484A), fontSize: 12, height: 1.4),
),
],
),
);
}
}