friend_models.dart
7.2 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
// ─── Responses ───────────────────────────────────────────────────────────────
class FriendListResponse {
const FriendListResponse({
this.list = const [],
this.limit,
this.healthData,
});
final List<FriendItem> list;
final int? limit;
final FriendHealthData? healthData;
factory FriendListResponse.fromJson(Map<String, dynamic> json) {
return FriendListResponse(
list: (json['list'] as List<dynamic>?)
?.map((e) => FriendItem.fromJson(e as Map<String, dynamic>))
.toList() ??
const [],
limit: json['limit'] as int?,
healthData: json['health_data'] == null
? null
: FriendHealthData.fromJson(
json['health_data'] as Map<String, dynamic>,
),
);
}
Map<String, dynamic> toJson() => {
'list': list.map((e) => e.toJson()).toList(),
'limit': limit,
if (healthData != null) 'health_data': healthData!.toJson(),
};
}
class FriendItem {
const FriendItem({
this.id,
this.userId,
this.friendUserId,
this.remarkName,
this.showInDial,
this.state,
this.createTime,
this.updateTime,
this.avatar,
this.friendNickname,
this.healthData,
});
final int? id;
final int? userId;
final int? friendUserId;
final String? remarkName;
/// 0 = hidden, 1 = shown in dial
final int? showInDial;
/// 1 = normal
final int? state;
final int? createTime;
final int? updateTime;
final String? avatar;
final String? friendNickname;
final FriendHealthData? healthData;
bool get isShowInDial => showInDial == 1;
factory FriendItem.fromJson(Map<String, dynamic> json) {
return FriendItem(
id: json['id'] as int?,
userId: json['user_id'] as int?,
friendUserId: json['friend_user_id'] as int?,
remarkName: json['remark_name'] as String?,
showInDial: json['show_in_dial'] as int?,
state: json['state'] as int?,
createTime: json['create_time'] as int?,
updateTime: json['update_time'] as int?,
avatar: json['avatar'] as String?,
friendNickname: json['friend_nickname'] as String?,
healthData: json['health_data'] == null
? null
: FriendHealthData.fromJson(
json['health_data'] as Map<String, dynamic>),
);
}
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
if (id != null) val['id'] = id;
if (userId != null) val['user_id'] = userId;
if (friendUserId != null) val['friend_user_id'] = friendUserId;
if (remarkName != null) val['remark_name'] = remarkName;
if (showInDial != null) val['show_in_dial'] = showInDial;
if (state != null) val['state'] = state;
if (createTime != null) val['create_time'] = createTime;
if (updateTime != null) val['update_time'] = updateTime;
if (avatar != null) val['avatar'] = avatar;
if (friendNickname != null) val['friend_nickname'] = friendNickname;
if (healthData != null) val['health_data'] = healthData!.toJson();
return val;
}
}
class FriendHealthData {
const FriendHealthData({
this.hrvState,
this.comprehensiveStressScore,
this.comprehensiveStressState,
this.latestHrv,
this.realtimeStress,
this.sleepEvaluate,
this.totalSteps,
this.totalMove,
this.lastDataTime,
});
/// HRV state indicator
final int? hrvState;
/// Comprehensive stress score.
final int? comprehensiveStressScore;
/// Comprehensive stress state indicator.
final int? comprehensiveStressState;
/// Latest HRV value used by the friend health card.
final double? latestHrv;
/// Real-time stress samples returned by the API (may be an empty list `[]`).
final List<FriendRealtimeStressItem>? realtimeStress;
/// Sleep quality score
final int? sleepEvaluate;
/// Total step count for the day (nullable)
final int? totalSteps;
/// Active calories burned for the day (nullable).
final int? totalMove;
/// Last health data timestamp, in seconds or milliseconds.
final num? lastDataTime;
factory FriendHealthData.fromJson(Map<String, dynamic> json) {
return FriendHealthData(
hrvState: _parseInt(json['hrv_state']),
comprehensiveStressScore: _parseInt(json['comprehensive_stress_score']),
comprehensiveStressState: _parseInt(json['comprehensive_stress_state']),
latestHrv: _parseDouble(json['latest_hrv']),
realtimeStress: (json['realtime_stress'] as List<dynamic>?)
?.map(
(item) => FriendRealtimeStressItem.fromJson(
item as Map<String, dynamic>,
),
)
.toList(),
sleepEvaluate: _parseInt(json['sleep_evaluate']),
totalSteps: _parseInt(json['total_steps']),
totalMove: _parseInt(json['total_move']),
lastDataTime: _parseNum(json['last_data_time']),
);
}
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
if (hrvState != null) val['hrv_state'] = hrvState;
if (comprehensiveStressScore != null) {
val['comprehensive_stress_score'] = comprehensiveStressScore;
}
if (comprehensiveStressState != null) {
val['comprehensive_stress_state'] = comprehensiveStressState;
}
if (latestHrv != null) val['latest_hrv'] = latestHrv;
if (realtimeStress != null) {
val['realtime_stress'] =
realtimeStress!.map((item) => item.toJson()).toList();
}
if (sleepEvaluate != null) val['sleep_evaluate'] = sleepEvaluate;
if (totalSteps != null) val['total_steps'] = totalSteps;
if (totalMove != null) val['total_move'] = totalMove;
if (lastDataTime != null) val['last_data_time'] = lastDataTime;
return val;
}
}
class FriendRealtimeStressItem {
const FriendRealtimeStressItem({
this.time,
this.value,
this.state,
this.isAsleep,
});
/// Unix timestamp in seconds.
final num? time;
/// Real-time stress score.
final double? value;
/// Stress state indicator.
final int? state;
/// Whether the sample was recorded while asleep (0 = no, 1 = yes).
final int? isAsleep;
factory FriendRealtimeStressItem.fromJson(Map<String, dynamic> json) {
return FriendRealtimeStressItem(
time: _parseNum(json['time']),
value: _parseDouble(json['value']),
state: _parseInt(json['state']),
isAsleep: _parseInt(json['is_asleep']),
);
}
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;
if (isAsleep != null) val['is_asleep'] = isAsleep;
return val;
}
}
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) return double.tryParse(value);
return null;
}
num? _parseNum(dynamic value) {
if (value == null) return null;
if (value is num) return value;
if (value is String) return num.tryParse(value);
return null;
}