user_preferences_storage.dart
5.17 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
import 'dart:convert';
import 'package:doublefeel_flutter/core/config/app_environment_config.dart';
import 'package:doublefeel_flutter/core/logging/app_diagnostic_log_store.dart';
import 'package:doublefeel_flutter/core/logging/app_logger.dart';
import 'package:doublefeel_flutter/core/services/raw_data_service/health_raw_data_core_service.dart';
import 'package:doublefeel_flutter/pigeon/platform_api.g.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/local/user_preferences.dart';
import '../models/user/user_models.dart';
import '../models/vip/vip_info.dart';
/// Persists [UserPreferences] as JSON.
class UserPreferencesStorage {
UserPreferencesStorage(this._prefs);
final SharedPreferences _prefs;
static const _storageKey = 'double_feel_user_preferences_json';
final Rx<UserPreferences> preferences = UserPreferences.empty.obs;
Future<UserPreferencesStorage> init() async {
await _load();
return this;
}
// bool get isBound => (preferences.value.partnerUserInfo?.id ?? 0) > 0;
String get accessToken => preferences.value.accessToken;
bool get isLoggedIn => accessToken.isNotEmpty;
String get rongcloudToken => preferences.value.rongcloudToken;
Future<void> _load() async {
final raw = _prefs.getString(_storageKey);
if (raw == null || raw.isEmpty) {
preferences.value = UserPreferences.empty;
return;
}
try {
final map = jsonDecode(raw) as Map<String, dynamic>;
preferences.value = UserPreferences.fromJson(map);
} catch (error, stackTrace) {
AppLogger.e('UserPreferences JSON parse failed', error, stackTrace);
AppDiagnosticLogStore.instance.log(
category: 'UserPreferences',
action: 'load_deserialization_failed',
details: {
'error': error.toString(),
'stackTrace': stackTrace.toString(),
'rawPreview': raw.length > 200 ? '${raw.substring(0, 200)}...' : raw,
},
);
preferences.value = UserPreferences.empty;
}
}
/// Call after the startup dependency graph has registered the environment and
/// health services. This deliberately runs on every cold start so iOS has a
/// valid session before any HealthKit observer callback is processed.
Future<void> syncLoginInfoToNative() async {
await _syncLoginInfoToNative(userInfo: preferences.value);
}
Future<void> _syncLoginInfoToNative(
{required UserPreferences userInfo}) async {
if (userInfo.accessToken.isEmpty) {
try {
if (Get.isRegistered<AppEnvironmentConfig>()) {
await PlatformHostApi().logout();
}
} on Exception {
return;
}
return;
}
try {
if (Get.isRegistered<AppEnvironmentConfig>()) {
var env = Get.find<AppEnvironmentConfig>();
await PlatformHostApi()
.updateLoginInfo(jsonEncode(userInfo), env.serverBaseUrl);
await _syncHealthRawDatabaseToNative();
}
AppLogger.d('Native login info synced for cold start');
} catch (error, stackTrace) {
AppLogger.e('Native login info sync failed', error, stackTrace);
}
}
Future<void> _syncHealthRawDatabaseToNative() async {
if (!Get.isRegistered<HealthRawDataCoreService>()) return;
await Get.find<HealthRawDataCoreService>().syncDatabaseToNativeIfNeeded();
}
Future<void> _persist(UserPreferences value) async {
preferences.value = value;
await _prefs.setString(_storageKey, jsonEncode(value.toJson()));
}
Future<void> update(UserPreferences value) => _persist(value);
Future<void> updateAccessToken(String token) async {
await _persist(preferences.value.copyWith(accessToken: token));
}
Future<void> updateRongcloudToken(String token) async {
await _persist(preferences.value.copyWith(rongcloudToken: token));
}
Future<void> updateMeUserInfo(UserInfoResponse info) async {
await _persist(preferences.value.copyWith(meUserInfo: info));
}
Future<void> updatePartnerUserInfo(UserInfoResponse info) async {
await _persist(preferences.value.copyWith(partnerUserInfo: info));
}
Future<void> updateBothUserInfo({
UserInfoResponse? me,
UserInfoResponse? partner,
}) async {
await _persist(
preferences.value.copyWith(
meUserInfo: me ?? preferences.value.meUserInfo,
partnerUserInfo: partner ?? preferences.value.partnerUserInfo,
),
);
}
Future<void> updateVipInfo(UserPreferencesVipInfo vipInfo) async {
await _persist(preferences.value.copyWith(vipInfo: vipInfo));
}
Future<void> updateFromLogin({
required String accessToken,
UserInfoResponse? me,
UserInfoResponse? partner,
VipInfo? vip,
}) async {
final userInfo = UserPreferences(
accessToken: accessToken,
meUserInfo: me,
partnerUserInfo: partner,
vipInfo: vip != null ? UserPreferencesVipInfo.fromVipInfo(vip) : null,
);
await _persist(userInfo);
await _syncLoginInfoToNative(userInfo: userInfo);
}
Future<void> clearPartnerUserInfo() async {
await _persist(preferences.value.copyWith(clearPartner: true));
}
Future<void> clear() async {
await _prefs.remove(_storageKey);
preferences.value = UserPreferences.empty;
}
}