user_preferences.dart
4 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
import '../user/user_models.dart';
import '../vip/vip_info.dart';
/// Local session snapshot.
class UserPreferences {
const UserPreferences({
this.meUserInfo,
this.partnerUserInfo,
this.accessToken = '',
this.rongcloudToken = '',
this.vipInfo,
this.showRealtimeStress,
});
final UserInfoResponse? meUserInfo;
final UserInfoResponse? partnerUserInfo;
final String accessToken;
final String rongcloudToken;
final UserPreferencesVipInfo? vipInfo;
/// `null` preserves the product default: enabled for members and disabled
/// for non-members.
final bool? showRealtimeStress;
static const empty = UserPreferences();
factory UserPreferences.fromJson(Map<String, dynamic> json) {
return UserPreferences(
meUserInfo: json['me_user_info'] == null
? null
: UserInfoResponse.fromJson(
json['me_user_info'] as Map<String, dynamic>),
partnerUserInfo: json['partner_user_info'] == null
? null
: UserInfoResponse.fromJson(
json['partner_user_info'] as Map<String, dynamic>),
accessToken: (json['access_token'] as String?) ?? '',
rongcloudToken: (json['rongcloud_token'] as String?) ?? '',
vipInfo: json['vip_info'] == null
? null
: UserPreferencesVipInfo.fromJson(
json['vip_info'] as Map<String, dynamic>),
showRealtimeStress: json['show_realtime_stress'] as bool?,
);
}
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
if (meUserInfo != null) val['me_user_info'] = meUserInfo!.toJson();
if (partnerUserInfo != null) {
val['partner_user_info'] = partnerUserInfo!.toJson();
}
val['access_token'] = accessToken;
val['rongcloud_token'] = rongcloudToken;
if (vipInfo != null) val['vip_info'] = vipInfo!.toJson();
if (showRealtimeStress != null) {
val['show_realtime_stress'] = showRealtimeStress;
}
return val;
}
UserPreferences copyWith({
UserInfoResponse? meUserInfo,
UserInfoResponse? partnerUserInfo,
String? accessToken,
String? rongcloudToken,
UserPreferencesVipInfo? vipInfo,
bool? showRealtimeStress,
bool clearPartner = false,
}) {
return UserPreferences(
meUserInfo: meUserInfo ?? this.meUserInfo,
partnerUserInfo:
clearPartner ? null : (partnerUserInfo ?? this.partnerUserInfo),
accessToken: accessToken ?? this.accessToken,
rongcloudToken: rongcloudToken ?? this.rongcloudToken,
vipInfo: vipInfo ?? this.vipInfo,
showRealtimeStress: showRealtimeStress ?? this.showRealtimeStress,
);
}
}
class UserPreferencesVipInfo {
const UserPreferencesVipInfo({
this.isVip = false,
this.isForeverVip = false,
this.isShare = false,
this.vipStartDate = 0,
this.vipEndDate = 0,
this.vipType = 0,
});
final bool isVip;
final bool isForeverVip;
final bool isShare;
final int vipStartDate;
final int vipEndDate;
final int vipType;
factory UserPreferencesVipInfo.fromJson(Map<String, dynamic> json) {
return UserPreferencesVipInfo(
isVip: (json['is_vip'] as bool?) ?? false,
isForeverVip: (json['is_forever_vip'] as bool?) ?? false,
isShare: (json['is_share'] as bool?) ?? false,
vipStartDate: (json['vip_start_date'] as num?)?.toInt() ?? 0,
vipEndDate: (json['vip_end_date'] as num?)?.toInt() ?? 0,
vipType: (json['vip_type'] as num?)?.toInt() ?? 0,
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'is_vip': isVip,
'is_forever_vip': isForeverVip,
'is_share': isShare,
'vip_start_date': vipStartDate,
'vip_end_date': vipEndDate,
'vip_type': vipType,
};
}
factory UserPreferencesVipInfo.fromVipInfo(VipInfo info) {
return UserPreferencesVipInfo(
isVip: info.isVip ?? false,
isForeverVip: info.isForeverVip ?? false,
isShare: info.isShare ?? false,
vipStartDate: info.vipStartDate ?? 0,
vipEndDate: info.vipEndDate ?? 0,
vipType: info.vipType ?? 0,
);
}
}