-
Notifications
You must be signed in to change notification settings - Fork 24
/
preferences.hive.dart
251 lines (196 loc) · 8.81 KB
/
preferences.hive.dart
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
/// SPDX-License-Identifier: AGPL-3.0-or-later
import 'package:aewallet/domain/models/authentication.dart';
import 'package:aewallet/infrastructure/datasources/hive.extension.dart';
import 'package:aewallet/model/authentication_method.dart';
import 'package:aewallet/model/available_language.dart';
import 'package:aewallet/model/available_networks.dart';
import 'package:aewallet/model/device_lock_timeout.dart';
import 'package:aewallet/model/primary_currency.dart';
import 'package:aewallet/model/privacy_mask_option.dart';
import 'package:archethic_dapp_framework_flutter/archethic_dapp_framework_flutter.dart'
as aedappfm;
import 'package:hive/hive.dart';
import 'package:intl/intl.dart';
class PreferencesHiveDatasource {
PreferencesHiveDatasource._(this._box);
static const String _preferencesBox = '_preferencesBox';
final Box<dynamic> _box;
static const String firstLaunch = 'archethic_wallet_first_launch';
static const String authMethod = 'archethic_wallet_auth_method';
static const String curLanguage = 'archethic_wallet_cur_language';
static const String curPrimarySetting =
'archethic_wallet_cur_primary_setting';
static const String curNetwork = 'archethic_wallet_cur_network';
static const String curNetworkDevEndpoint = '_cur_network_dev_endpoint';
static const String curTheme = 'archethic_wallet_cur_theme';
static const String lock = 'archethic_wallet_lock';
static const String privacyMaskEnabled =
'archethic_wallet_privacy_mask_enabled';
static const String lockTimeout = 'archethic_wallet_lock_timeout';
static const String lastInteractionDate =
'archethic_wallet_last_interaction_date';
static const String hasShownRootWarning =
'archethic_wallet_has_shown_root_warning';
static const String pinAttempts = 'archethic_wallet_pin_attempts';
static const String pinLockUntil = 'archethic_wallet_pin_lock_until';
static const String pinPadShuffle = 'archethic_wallet_pinPadShuffle';
static const String showBalances = 'archethic_wallet_showBalances';
static const String showPriceChart = 'archethic_wallet_showPriceChart';
static const String priceChartScale = 'archethic_wallet_priceChartScale';
static const String activeRPCServer = 'archethic_wallet_activeRPCServer';
static const String recoveryPhraseSaved =
'archethic_wallet_recoveryPhraseSaved';
static const String languageSeed = 'archethic_wallet_language_seed';
static const String mainScreenCurrentPage =
'archethic_wallet_main_screen_current_page';
static const String currentVersion = 'archethic_wallet_currentVersion';
// This doesn't have to be a singleton.
// We just want to make sure that the box is open, before we start getting/setting objects on it
static PreferencesHiveDatasource? _instance;
static Future<PreferencesHiveDatasource> getInstance() async {
if (_instance?._box.isOpen == true) return _instance!;
final box = await Hive.openBox<dynamic>(_preferencesBox);
return _instance = PreferencesHiveDatasource._(box);
}
T _getValue<T>(dynamic key, {T? defaultValue}) =>
_box.get(key, defaultValue: defaultValue) as T;
Future<void> _setValue<T>(dynamic key, T value) => _box.put(key, value);
Future<void> _removeValue<T>(dynamic key) => _box.delete(key);
Future<void> setHasShownRootWarning(bool hasShown) =>
_setValue(hasShownRootWarning, hasShown);
bool getHasSeenRootWarning() =>
_getValue(hasShownRootWarning, defaultValue: false);
Future<void> setCurrentDataVersion(int version) =>
_setValue(currentVersion, version);
Future<int?> getCurrentDataVersion() async {
// implicit String to Int conversion to ensure
// compatibility with first MigrationSystem version.
final value = _box.get(currentVersion);
if (value is String) {
return int.tryParse(value);
}
return value;
}
Future<void> setAuthMethod(AuthenticationMethod method) =>
_setValue(authMethod, method.getIndex());
AuthenticationMethod getAuthMethod() => AuthenticationMethod(
AuthMethod.values[_getValue(
authMethod,
defaultValue:
AuthenticationSettings.defaultValue.authenticationMethod.index,
)],
);
Future<void> setLanguage(LanguageSetting language) =>
_setValue(curLanguage, language.getIndex());
LanguageSetting getLanguage() => LanguageSetting(
AvailableLanguage.values[_getValue(
curLanguage,
defaultValue: AvailableLanguage.english.index,
)],
);
Future<void> setPrimaryCurrency(AvailablePrimaryCurrency primaryCurrency) =>
_setValue(curPrimarySetting, primaryCurrency.getIndex());
AvailablePrimaryCurrency getPrimaryCurrency() => AvailablePrimaryCurrency(
AvailablePrimaryCurrencyEnum.values[_getValue(
curPrimarySetting,
defaultValue: AvailablePrimaryCurrencyEnum.native.index,
)],
);
Future<void> setNetwork(NetworksSetting network) async {
await _setValue(curNetwork, network.getIndex());
await _setValue(curNetworkDevEndpoint, network.networkDevEndpoint);
}
NetworksSetting getNetwork() => NetworksSetting(
network: AvailableNetworks.values[_getValue(
curNetwork,
defaultValue: AvailableNetworks.archethicMainNet.index,
)],
networkDevEndpoint: _getValue(
curNetworkDevEndpoint,
defaultValue: '',
),
);
Future<void> setLanguageSeed(String v) => _setValue(languageSeed, v);
String getLanguageSeed() => _getValue(languageSeed, defaultValue: '');
Future<void> setFirstLaunch(bool value) => _setValue(firstLaunch, value);
bool getFirstLaunch() => _getValue(firstLaunch, defaultValue: true);
Future<void> setPinPadShuffle(bool value) => _setValue(pinPadShuffle, value);
bool getPinPadShuffle() => _getValue(
pinPadShuffle,
defaultValue: AuthenticationSettings.defaultValue.pinPadShuffle,
);
Future<void> setShowBalances(bool value) => _setValue(showBalances, value);
bool getShowBalances() => _getValue(showBalances, defaultValue: true);
Future<void> setActiveRPCServer(bool value) =>
_setValue(activeRPCServer, value);
bool getActiveRPCServer() => _getValue(activeRPCServer, defaultValue: true);
Future<void> setRecoveryPhraseSaved(bool value) =>
_setValue(recoveryPhraseSaved, value);
bool getRecoveryPhraseSaved() =>
_getValue(recoveryPhraseSaved, defaultValue: false);
Future<void> setMainScreenCurrentPage(int value) =>
_setValue(mainScreenCurrentPage, value);
int getMainScreenCurrentPage() =>
_getValue(mainScreenCurrentPage, defaultValue: 0);
Future<void> setShowPriceChart(bool value) =>
_setValue(showPriceChart, value);
bool getShowPriceChart() => _getValue(showPriceChart, defaultValue: true);
Future<void> setPriceChartIntervalOption(
aedappfm.MarketPriceHistoryInterval scaleOption,
) =>
_setValue(
priceChartScale,
scaleOption.index,
);
aedappfm.MarketPriceHistoryInterval getPriceChartIntervalOption() =>
aedappfm.MarketPriceHistoryInterval.values[_getValue(
priceChartScale,
defaultValue: aedappfm.MarketPriceHistoryInterval.hour.index,
)];
bool getPrivacyMaskEnabled() => _getValue(
privacyMaskEnabled,
defaultValue: AuthenticationSettings.defaultValue.privacyMask ==
PrivacyMaskOption.enabled,
);
Future<void> setPrivacyMaskEnabled(bool enabled) =>
_setValue(privacyMaskEnabled, enabled);
Future<void> setLockTimeout(LockTimeoutOption lockTimeoutOption) =>
_setValue(lockTimeout, lockTimeoutOption.index);
LockTimeoutOption getLockTimeout() => LockTimeoutOption.values[_getValue(
lockTimeout,
defaultValue: AuthenticationSettings.defaultValue.lockTimeout.index,
)];
int getLockAttempts() => _getValue(pinAttempts, defaultValue: 0);
Future<void> incrementLockAttempts() => _setValue(
pinAttempts,
getLockAttempts() + 1,
);
Future<void> resetLockAttempts() async {
await _removeValue(pinAttempts);
}
Future<void> removeLockDate() async {
await _removeValue(pinLockUntil);
}
Future<void> setLockDate(DateTime lockDate) async {
final lockDateStr = DateFormat.yMd().add_jms().format(lockDate.toUtc());
await _setValue(pinLockUntil, lockDateStr);
}
DateTime? getLockDate() {
final lockDateStr = _getValue(pinLockUntil);
if (lockDateStr != null) {
return DateFormat.yMd().add_jms().parseUtc(lockDateStr);
} else {
return null;
}
}
static Future<void> clear() => Hive.deleteBox<dynamic>(_preferencesBox);
Future<DateTime?> getLastInteractionDate() async {
return _getValue(lastInteractionDate);
}
Future<void> setLastInteractionDate(DateTime date) async {
return _setValue(lastInteractionDate, date);
}
Future<void> clearLastInteractionDate() async {
await _removeValue(lastInteractionDate);
}
}