Browse Source

refactor(neon): externalize NeonStorage

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/674/head
Nikolas Rimikis 1 year ago
parent
commit
28cc5f0c5c
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 2
      packages/neon/neon/lib/neon.dart
  2. 39
      packages/neon/neon/lib/src/settings/models/storage.dart
  3. 2
      packages/neon/neon/lib/src/utils/push_utils.dart
  4. 6
      packages/neon/neon/test/storage_test.dart

2
packages/neon/neon/lib/neon.dart

@ -31,7 +31,7 @@ Future runNeon({
await NeonPlatform.setup();
await RequestManager.instance.initCache();
await AppStorage.init();
await NeonStorage.init();
final packageInfo = await PackageInfo.fromPlatform();
buildUserAgent(packageInfo);

39
packages/neon/neon/lib/src/settings/models/storage.dart

@ -15,22 +15,17 @@ abstract interface class SettingsStorage {
Future<bool> remove(final String key);
}
@immutable
@internal
class AppStorage implements SettingsStorage {
const AppStorage(this._id);
final String _id;
final class NeonStorage {
/// Shared preferences instance.
///
/// Use [reqireDatabase] to access it.
/// Use [database] to access it.
/// Make sure it has been initialized wiht [init] before.
static SharedPreferences? _sharedPreferences;
/// Sets up the [SharedPreferences] instance.
///
/// Required to be called before accessing [reqireDatabase].
/// Required to be called before accessing [database].
static Future init() async {
if (_sharedPreferences != null) {
return;
@ -40,37 +35,45 @@ class AppStorage implements SettingsStorage {
}
@visibleForTesting
static SharedPreferences get reqireDatabase {
static SharedPreferences get database {
if (_sharedPreferences == null) {
throw StateError(
'AppStorage has not been initialized yet. Please make sure AppStorage.init() has been called before and completed.',
'NeonStorage has not been initialized yet. Please make sure NeonStorage.init() has been called before and completed.',
);
}
return _sharedPreferences!;
}
}
@immutable
@internal
class AppStorage implements SettingsStorage {
const AppStorage(this._id);
final String _id;
String _formatKey(final String key) => '$_id-$key';
bool containsKey(final String key) => reqireDatabase.containsKey(_formatKey(key));
bool containsKey(final String key) => NeonStorage.database.containsKey(_formatKey(key));
@override
Future<bool> remove(final String key) => reqireDatabase.remove(_formatKey(key));
Future<bool> remove(final String key) => NeonStorage.database.remove(_formatKey(key));
@override
String? getString(final String key) => reqireDatabase.getString(_formatKey(key));
String? getString(final String key) => NeonStorage.database.getString(_formatKey(key));
@override
Future setString(final String key, final String value) => reqireDatabase.setString(_formatKey(key), value);
Future setString(final String key, final String value) => NeonStorage.database.setString(_formatKey(key), value);
@override
bool? getBool(final String key) => reqireDatabase.getBool(_formatKey(key));
bool? getBool(final String key) => NeonStorage.database.getBool(_formatKey(key));
@override
Future setBool(final String key, final bool value) => reqireDatabase.setBool(_formatKey(key), value);
Future setBool(final String key, final bool value) => NeonStorage.database.setBool(_formatKey(key), value);
List<String>? getStringList(final String key) => reqireDatabase.getStringList(_formatKey(key));
List<String>? getStringList(final String key) => NeonStorage.database.getStringList(_formatKey(key));
Future setStringList(final String key, final List<String> value) =>
reqireDatabase.setStringList(_formatKey(key), value);
NeonStorage.database.setStringList(_formatKey(key), value);
}

2
packages/neon/neon/lib/src/utils/push_utils.dart

@ -70,7 +70,7 @@ class PushUtils {
}
},
);
await AppStorage.init();
await NeonStorage.init();
final keypair = await loadRSAKeypair(const AppStorage(AppIDs.notifications));

6
packages/neon/neon/test/storage_test.dart

@ -4,11 +4,11 @@ import 'package:test/test.dart';
void main() {
test('AppStorage', () async {
expect(() => AppStorage.reqireDatabase, throwsA(isA<StateError>()));
expect(() => NeonStorage.database, throwsA(isA<StateError>()));
SharedPreferences.setMockInitialValues({});
await AppStorage.init();
await NeonStorage.init();
expect(AppStorage.reqireDatabase, isA<SharedPreferences>());
expect(NeonStorage.database, isA<SharedPreferences>());
});
}

Loading…
Cancel
Save