health_v2_models.dart
11.9 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import 'dart:ui';
import 'package:doublefeel_flutter/core/theme/app_theme.dart';
import 'package:doublefeel_flutter/l10n/l10n_extensions.dart';
import 'package:flutter/src/widgets/framework.dart';
int? _parseInt(dynamic value) {
if (value == null) return null;
if (value is int) return value;
if (value is double) return value.toInt();
if (value is String) return int.tryParse(value);
return null;
}
double? _parseDouble(dynamic value) {
if (value == null) return null;
if (value is num) return value.toDouble();
if (value is String) {
if (value == 'NaN') return double.nan;
if (value == 'Infinity') return double.infinity;
if (value == '-Infinity') return double.negativeInfinity;
return double.tryParse(value);
}
return null;
}
/// v2/lastest_hrv/ response
class V2LatestHrvData {
const V2LatestHrvData(
{this.latestHrv, this.latestDataTime, this.state, this.tip});
final double? latestHrv;
final int? latestDataTime;
final int? state;
final String? tip;
String stateString() {
switch (state) {
case 1:
return l10n.pressureOverload;
case 2:
return l10n.beMindfulOfStress;
case 3:
return l10n.statusNormal;
case 4:
return l10n.inExcellentCondition;
}
return l10n.waitingForData;
}
stateColor(BuildContext context) {
switch (state) {
case 1:
return context.colors.chartPink;
case 2:
return context.colors.chartOrange;
case 3:
return context.colors.chartBlue;
case 4:
return context.colors.chartGreen;
}
return context.colors.chartGreen;
}
factory V2LatestHrvData.fromJson(Map<String, dynamic> json) {
return V2LatestHrvData(
latestHrv: _parseDouble(json['latest_hrv']),
latestDataTime: _parseInt(json['latest_data_time']),
state: _parseInt(json['state']),
tip: json['tip_text'],
);
}
}
/// 单段睡眠时间区间
class V2SleepTimeRange {
const V2SleepTimeRange({required this.fromTime, required this.toTime});
final int fromTime; // Unix 时间戳(秒)
final int toTime; // Unix 时间戳(秒)
factory V2SleepTimeRange.fromJson(Map<String, dynamic> json) {
return V2SleepTimeRange(
fromTime: json['from_time'] as int,
toTime: json['to_time'] as int,
);
}
Map<String, dynamic> toJson() => {
'from_time': fromTime,
'to_time': toTime,
};
}
/// v2/health_data/ response
class V2HealthData {
const V2HealthData({
this.hrvAvg,
this.lastRestingHrValue,
this.hrAvg,
this.move,
this.exercise,
this.stand,
this.steps,
this.sleepDuration,
this.sleepScore,
this.sleepState,
this.sleepTimeList,
});
final int? hrvAvg;
final int? lastRestingHrValue;
final int? hrAvg;
final int? move;
final int? exercise;
final int? stand;
final int? steps;
final int? sleepDuration;
final double? sleepScore;
final int? sleepState;
final List<V2SleepTimeRange>? sleepTimeList;
factory V2HealthData.fromJson(Map<String, dynamic> json) {
return V2HealthData(
hrvAvg: _parseInt(json['hrv_avg']),
lastRestingHrValue: json['last_resting_hr_value'] == 0
? null
: _parseInt(json['last_resting_hr_value']),
hrAvg: _parseInt(json['hr_avg']),
move: _parseInt(json['move']),
exercise: _parseInt(json['exercise']),
stand: _parseInt(json['stand']),
steps: _parseInt(json['steps']),
sleepDuration: _parseInt(json['sleep_duration']),
sleepScore: _parseDouble(json['sleep_score']),
sleepState: _parseInt(json['sleep_state']),
sleepTimeList: (json['sleep_time_list'] as List<dynamic>?)
?.map((e) => V2SleepTimeRange.fromJson(e as Map<String, dynamic>))
.toList(),
);
}
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
if (hrvAvg != null) val['hrv_avg'] = hrvAvg;
if (lastRestingHrValue != null) {
val['last_resting_hr_value'] = lastRestingHrValue;
}
if (hrAvg != null) val['hr_avg'] = hrAvg;
if (move != null) val['move'] = move;
if (exercise != null) val['exercise'] = exercise;
if (stand != null) val['stand'] = stand;
if (steps != null) val['steps'] = steps;
if (sleepDuration != null) val['sleep_duration'] = sleepDuration;
if (sleepScore != null) val['sleep_score'] = sleepScore;
if (sleepState != null) val['sleep_state'] = sleepState;
if (sleepTimeList != null) {
val['sleep_time_list'] = sleepTimeList!.map((e) => e.toJson()).toList();
}
return val;
}
/// 根据睡眠时长(秒)计算得分。
int sleepDurationScore() {
final duration = sleepDuration;
if (duration == null || duration <= 0) return 0;
final hours = duration / 3600.0;
if (hours >= 7 && hours <= 9) return 100;
if (hours > 9 && hours <= 10) return 80;
if (hours >= 6 && hours < 7) return 80;
if (hours >= 5 && hours < 6) return 60;
if (hours > 10) return 50;
return 30; // < 5h
}
sleepStateColor(BuildContext context) {
switch (sleepState) {
case 1:
return context.colors.chartGreen;
case 2:
return context.colors.chartBlue;
case 3:
return context.colors.chartOrange;
}
return null;
}
sleepStateStr() {
switch (sleepState) {
case 1:
return l10n.sleepQualityExcellent;
case 2:
return l10n.sleepQualityNormal;
case 3:
return l10n.sleepQualityAttention;
}
return '--';
}
}
/// v2/stress_score/ response
class V2StressScore {
const V2StressScore({
this.state,
this.hrvScore,
this.hrScore,
this.latestHr,
this.comprehensiveScore,
});
final int? state;
final int? hrvScore;
final int? hrScore;
final int? latestHr;
final int? comprehensiveScore;
factory V2StressScore.fromJson(Map<String, dynamic> json) {
return V2StressScore(
state: _parseInt(json['state']),
hrvScore: _parseInt(json['hrv_score']),
hrScore: _parseInt(json['hr_score']),
latestHr: _parseInt(json['latest_hr']),
comprehensiveScore: _parseInt(json['comprehensive_score']),
);
}
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
if (state != null) val['state'] = state;
if (hrvScore != null) val['hrv_score'] = hrvScore;
if (hrScore != null) val['hr_score'] = hrScore;
if (latestHr != null) val['latest_hr'] = latestHr;
if (comprehensiveScore != null) {
val['comprehensive_score'] = comprehensiveScore;
}
return val;
}
String stateString() {
switch (state) {
case 1:
return l10n.pressureOverload;
case 2:
return l10n.beMindfulOfStress;
case 3:
return l10n.statusNormal;
case 4:
return l10n.inExcellentCondition;
}
return l10n.waitingForData;
}
String stateBg() {
switch (state) {
case 1:
return 'assets/images/today/bg_stress_state_1.png';
case 2:
return 'assets/images/today/bg_stress_state_2.png';
case 3:
return 'assets/images/today/bg_stress_state_3.png';
case 4:
return 'assets/images/today/bg_stress_state_4.png';
}
return 'assets/images/today/bg_stress_state_0.png';
}
Color getBackgroundColor() {
switch (state) {
case 1:
return const Color(0xFFFFE3EE);
case 2:
return const Color(0xFFFFE8DE);
case 3:
return const Color(0xFFD4E2FF);
case 4:
return const Color(0xFFC8F6E5);
}
return const Color(0xFFE3E1EC);
}
}
/// Single item in v2/hrv_trend/ list
class V2HrvTrendItem {
const V2HrvTrendItem({this.time, this.rawHrv, this.trendHrv, this.state});
final int? time;
final double? rawHrv;
final int? trendHrv;
final int? state;
factory V2HrvTrendItem.fromJson(Map<String, dynamic> json) {
return V2HrvTrendItem(
time: _parseInt(json['time']),
rawHrv: _parseDouble(json['raw_hrv']),
trendHrv: _parseInt(json['trend_hrv']),
state: _parseInt(json['state']),
);
}
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
if (time != null) val['time'] = time;
if (rawHrv != null) val['raw_hrv'] = rawHrv;
if (trendHrv != null) val['trend_hrv'] = trendHrv;
if (state != null) val['state'] = state;
return val;
}
}
/// v2/hrv_trend/ response
class V2HrvTrendData {
const V2HrvTrendData({this.list});
final List<V2HrvTrendItem>? list;
factory V2HrvTrendData.fromJson(Map<String, dynamic> json) {
return V2HrvTrendData(
list: (json['list'] as List<dynamic>?)
?.map((e) => V2HrvTrendItem.fromJson(e as Map<String, dynamic>))
.toList(),
);
}
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
if (list != null) {
val['list'] = list!.map((e) => e.toJson()).toList();
}
return val;
}
}
/// Single item in v2/realtime_stress/ list
class V2RealtimeStressItem {
const V2RealtimeStressItem({this.time, this.value, this.state});
final int? time;
final int? value;
final int? state;
factory V2RealtimeStressItem.fromJson(Map<String, dynamic> json) {
return V2RealtimeStressItem(
time: _parseInt(json['time']),
value: _parseInt(json['value']),
state: _parseInt(json['state']),
);
}
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
if (time != null) val['time'] = time;
if (value != null) val['value'] = value;
if (state != null) val['state'] = state;
return val;
}
}
/// v2/realtime_stress/ response
class V2RealtimeStressData {
const V2RealtimeStressData({this.list});
final List<V2RealtimeStressItem>? list;
factory V2RealtimeStressData.fromJson(Map<String, dynamic> json) {
return V2RealtimeStressData(
list: (json['list'] as List<dynamic>?)
?.map((e) => V2RealtimeStressItem.fromJson(e as Map<String, dynamic>))
.toList(),
);
}
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
if (list != null) {
val['list'] = list!.map((e) => e.toJson()).toList();
}
return val;
}
}
/// v2/activity_target/ response
/// {"id":0,"create_time":0,"update_time":0,"user_id":131,
/// "move":300,"step":8000,"stand":12,"exercise":3600}
class V2ActivityTarget {
const V2ActivityTarget({
this.id,
this.userId,
this.move,
this.step,
this.stand,
this.exercise,
this.createTime,
this.updateTime,
});
final int? id;
final int? userId;
/// 活动消耗目标(千卡)
final int? move;
/// 步数目标
final int? step;
/// 站立小时目标
final int? stand;
/// 锻炼时长目标(秒)
final int? exercise;
final int? createTime;
final int? updateTime;
factory V2ActivityTarget.fromJson(Map<String, dynamic> json) {
return V2ActivityTarget(
id: _parseInt(json['id']),
userId: _parseInt(json['user_id']),
move: _parseInt(json['move']),
step: _parseInt(json['step']),
stand: _parseInt(json['stand']),
exercise: _parseInt(json['exercise']),
createTime: _parseInt(json['create_time']),
updateTime: _parseInt(json['update_time']),
);
}
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
if (id != null) val['id'] = id;
if (userId != null) val['user_id'] = userId;
if (move != null) val['move'] = move;
if (step != null) val['step'] = step;
if (stand != null) val['stand'] = stand;
if (exercise != null) val['exercise'] = exercise;
if (createTime != null) val['create_time'] = createTime;
if (updateTime != null) val['update_time'] = updateTime;
return val;
}
}
class V2HealthEverUploaded {
const V2HealthEverUploaded({
this.flag,
this.startTime,
});
final bool? flag;
final int? startTime;
factory V2HealthEverUploaded.fromJson(Map<String, dynamic> json) {
return V2HealthEverUploaded(
flag: json['flag'] as bool?,
startTime: _parseInt(json['start_time']),
);
}
Map<String, dynamic> toJson() {
return {
'flag': flag,
'start_time': startTime,
};
}
}