From 91a904df0d26c9ee614ae0cfdf04a4c3c96a73b6 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 29 Aug 2023 17:13:34 +0200 Subject: [PATCH] feat(neon): add SingleValueStorage Signed-off-by: Nikolas Rimikis --- .../neon/neon/lib/src/blocs/accounts.dart | 11 ++++---- .../neon/neon/lib/src/blocs/first_launch.dart | 11 ++++---- .../neon/lib/src/settings/models/storage.dart | 25 +++++++++++++++++++ 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/accounts.dart b/packages/neon/neon/lib/src/blocs/accounts.dart index 76a93fe5..ed251c51 100644 --- a/packages/neon/neon/lib/src/blocs/accounts.dart +++ b/packages/neon/neon/lib/src/blocs/accounts.dart @@ -58,6 +58,8 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState this._globalOptions, this._allAppImplementations, ) { + const lastUsedStorage = SingleValueStorage('last-used-account'); + accounts ..add(loadAccounts(_storage)) ..listen((final as) async { @@ -66,16 +68,16 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState }); activeAccount.listen((final aa) async { if (aa != null) { - await _storage.setString(_keyLastUsedAccount, aa.id); + await lastUsedStorage.setString(aa.id); } else { - await _storage.remove(_keyLastUsedAccount); + await lastUsedStorage.remove(); } }); final as = accounts.value; - if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) { - final lastUsedAccountID = _storage.getString(_keyLastUsedAccount); + if (_globalOptions.rememberLastUsedAccount.value && lastUsedStorage.hasValue()) { + final lastUsedAccountID = lastUsedStorage.getString(); if (lastUsedAccountID != null) { final aa = as.tryFind(lastUsedAccountID); if (aa != null) { @@ -97,7 +99,6 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState late final AppStorage _storage = const AppStorage('accounts'); final GlobalOptions _globalOptions; final Iterable _allAppImplementations; - final _keyLastUsedAccount = 'last-used-account'; final _accountsOptions = {}; final _appsBlocs = {}; diff --git a/packages/neon/neon/lib/src/blocs/first_launch.dart b/packages/neon/neon/lib/src/blocs/first_launch.dart index 6879ac22..bcf83f8b 100644 --- a/packages/neon/neon/lib/src/blocs/first_launch.dart +++ b/packages/neon/neon/lib/src/blocs/first_launch.dart @@ -16,16 +16,15 @@ abstract class FirstLaunchBlocStates { class FirstLaunchBloc extends Bloc implements FirstLaunchBlocEvents, FirstLaunchBlocStates { FirstLaunchBloc({ final bool disabled = false, - }) : _storage = const AppStorage(_keyFirstLaunch) { - if (!disabled && !_storage.containsKey(_keyFirstLaunch)) { + }) { + const storage = SingleValueStorage('first-launch'); + + if (!disabled && !storage.hasValue()) { onFirstLaunch.add(null); - unawaited(_storage.setBool(_keyFirstLaunch, false)); + unawaited(storage.setBool(false)); } } - final AppStorage _storage; - static const _keyFirstLaunch = 'first-launch'; - @override void dispose() { unawaited(onFirstLaunch.close()); diff --git a/packages/neon/neon/lib/src/settings/models/storage.dart b/packages/neon/neon/lib/src/settings/models/storage.dart index 885b4e67..399af196 100644 --- a/packages/neon/neon/lib/src/settings/models/storage.dart +++ b/packages/neon/neon/lib/src/settings/models/storage.dart @@ -46,6 +46,31 @@ final class NeonStorage { } } +@immutable +@internal +final class SingleValueStorage { + const SingleValueStorage(this._id); + + final String _id; + + bool hasValue() => NeonStorage.database.containsKey(_id); + + Future remove() => NeonStorage.database.remove(_id); + + String? getString() => NeonStorage.database.getString(_id); + + Future setString(final String value) => NeonStorage.database.setString(_id, value); + + bool? getBool() => NeonStorage.database.getBool(_id); + + // ignore: avoid_positional_boolean_parameters + Future setBool(final bool value) => NeonStorage.database.setBool(_id, value); + + List? getStringList() => NeonStorage.database.getStringList(_id); + + Future setStringList(final List value) => NeonStorage.database.setStringList(_id, value); +} + @immutable @internal class AppStorage implements SettingsStorage {