local_storage.dart
4.1 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
import 'package:doublefeel_flutter/core/config/app_environment_config.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../core/config/app_environment.dart';
import '../../core/constants/storage_const.dart';
/// Opens local persistence accessors used during bootstrap.
class LocalStorage {
LocalStorage._(this.sharedPreferences);
final SharedPreferences sharedPreferences;
static Future<LocalStorage> open() async {
final prefs = await SharedPreferences.getInstance();
return LocalStorage._(prefs);
}
// ─── Environment Configuration Storage ──────────────────────────────────────
Future<AppEnvironment> readEnvironment() async {
final ordinal = sharedPreferences.getInt(StorageConst.appEnvironmentKey);
if (ordinal == null) {
return AppEnvironment.release;
}
return AppEnvironment.fromOrdinal(ordinal);
}
Future<void> writeEnvironment(AppEnvironment environment) async {
await sharedPreferences.setInt(
StorageConst.appEnvironmentKey,
environment.index,
);
}
Future<AppRegion?> readRegion() async {
final ordinal = sharedPreferences.getInt(StorageConst.appRegionKey);
if (ordinal == null) {
return null;
}
return AppRegion.values[ordinal];
}
Future<void> writeRegion(AppRegion region) async {
await sharedPreferences.setInt(StorageConst.appRegionKey, region.index);
}
// ─── App Settings Storage ──────────────────────────────────────────────────
bool get termsAgreed =>
sharedPreferences.getBool(StorageConst.termsAgreedKey) ?? false;
Future<void> setTermsAgreed(bool value) async {
await sharedPreferences.setBool(StorageConst.termsAgreedKey, value);
}
String get lastLoginMethod =>
sharedPreferences.getString(StorageConst.lastLoginMethodKey) ?? '';
Future<void> setLastLoginMethod(String value) async {
await sharedPreferences.setString(StorageConst.lastLoginMethodKey, value);
}
// ─── App Review Prompt Storage ──────────────────────────────────────────────
DateTime? appReviewPromptNextShowAt(int userId) {
final value = sharedPreferences.getInt(
_appReviewPromptNextShowAtKey(userId),
);
if (value == null || value <= 0) return null;
return DateTime.fromMillisecondsSinceEpoch(value);
}
Future<void> setAppReviewPromptNextShowAt(int userId, DateTime value) async {
await sharedPreferences.setInt(
_appReviewPromptNextShowAtKey(userId),
value.millisecondsSinceEpoch,
);
}
bool hasAppReviewPromptPending(int userId) {
return sharedPreferences.getBool(_appReviewPromptPendingKey(userId)) ??
false;
}
Future<void> setAppReviewPromptPending(int userId) async {
await sharedPreferences.setBool(_appReviewPromptPendingKey(userId), true);
}
Future<void> clearAppReviewPromptPending(int userId) async {
await sharedPreferences.remove(_appReviewPromptPendingKey(userId));
}
String _appReviewPromptNextShowAtKey(int userId) =>
'${StorageConst.appReviewPromptNextShowAtKey}_$userId';
String _appReviewPromptPendingKey(int userId) =>
'${StorageConst.appReviewPromptPendingKey}_$userId';
// ─── Home Membership Offer Prompt Storage ─────────────────────────────────
int? get homeMembershipOfferPromptDate =>
sharedPreferences.getInt(StorageConst.homeMembershipOfferPromptDateKey);
int? get homeMembershipOfferPromptUserId =>
sharedPreferences.getInt(StorageConst.homeMembershipOfferPromptUserIdKey);
Future<void> setHomeMembershipOfferPromptState({
required int date,
required int userId,
}) async {
await sharedPreferences.setInt(
StorageConst.homeMembershipOfferPromptDateKey,
date,
);
await sharedPreferences.setInt(
StorageConst.homeMembershipOfferPromptUserIdKey,
userId,
);
}
}