Browse Source

feat(neon): add SingleValueStorage

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/674/head
Nikolas Rimikis 1 year ago
parent
commit
91a904df0d
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 11
      packages/neon/neon/lib/src/blocs/accounts.dart
  2. 11
      packages/neon/neon/lib/src/blocs/first_launch.dart
  3. 25
      packages/neon/neon/lib/src/settings/models/storage.dart

11
packages/neon/neon/lib/src/blocs/accounts.dart

@ -58,6 +58,8 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
this._globalOptions, this._globalOptions,
this._allAppImplementations, this._allAppImplementations,
) { ) {
const lastUsedStorage = SingleValueStorage('last-used-account');
accounts accounts
..add(loadAccounts(_storage)) ..add(loadAccounts(_storage))
..listen((final as) async { ..listen((final as) async {
@ -66,16 +68,16 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
}); });
activeAccount.listen((final aa) async { activeAccount.listen((final aa) async {
if (aa != null) { if (aa != null) {
await _storage.setString(_keyLastUsedAccount, aa.id); await lastUsedStorage.setString(aa.id);
} else { } else {
await _storage.remove(_keyLastUsedAccount); await lastUsedStorage.remove();
} }
}); });
final as = accounts.value; final as = accounts.value;
if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) { if (_globalOptions.rememberLastUsedAccount.value && lastUsedStorage.hasValue()) {
final lastUsedAccountID = _storage.getString(_keyLastUsedAccount); final lastUsedAccountID = lastUsedStorage.getString();
if (lastUsedAccountID != null) { if (lastUsedAccountID != null) {
final aa = as.tryFind(lastUsedAccountID); final aa = as.tryFind(lastUsedAccountID);
if (aa != null) { if (aa != null) {
@ -97,7 +99,6 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
late final AppStorage _storage = const AppStorage('accounts'); late final AppStorage _storage = const AppStorage('accounts');
final GlobalOptions _globalOptions; final GlobalOptions _globalOptions;
final Iterable<AppImplementation> _allAppImplementations; final Iterable<AppImplementation> _allAppImplementations;
final _keyLastUsedAccount = 'last-used-account';
final _accountsOptions = <String, AccountSpecificOptions>{}; final _accountsOptions = <String, AccountSpecificOptions>{};
final _appsBlocs = <String, AppsBloc>{}; final _appsBlocs = <String, AppsBloc>{};

11
packages/neon/neon/lib/src/blocs/first_launch.dart

@ -16,16 +16,15 @@ abstract class FirstLaunchBlocStates {
class FirstLaunchBloc extends Bloc implements FirstLaunchBlocEvents, FirstLaunchBlocStates { class FirstLaunchBloc extends Bloc implements FirstLaunchBlocEvents, FirstLaunchBlocStates {
FirstLaunchBloc({ FirstLaunchBloc({
final bool disabled = false, 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); onFirstLaunch.add(null);
unawaited(_storage.setBool(_keyFirstLaunch, false)); unawaited(storage.setBool(false));
} }
} }
final AppStorage _storage;
static const _keyFirstLaunch = 'first-launch';
@override @override
void dispose() { void dispose() {
unawaited(onFirstLaunch.close()); unawaited(onFirstLaunch.close());

25
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<bool> 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<String>? getStringList() => NeonStorage.database.getStringList(_id);
Future setStringList(final List<String> value) => NeonStorage.database.setStringList(_id, value);
}
@immutable @immutable
@internal @internal
class AppStorage implements SettingsStorage { class AppStorage implements SettingsStorage {

Loading…
Cancel
Save