From e6d16de211d929d0e0e838000ed5986c7aec81eb Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 19 Apr 2023 07:26:51 +0200 Subject: [PATCH] neon: Cleanup finding account --- packages/neon/neon/lib/src/blocs/accounts.dart | 8 ++++---- packages/neon/neon/lib/src/pages/home.dart | 9 +++------ packages/neon/neon/lib/src/pages/login.dart | 11 +++++------ .../neon/lib/src/utils/settings_export_helper.dart | 6 +++--- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/accounts.dart b/packages/neon/neon/lib/src/blocs/accounts.dart index b542cfd4..5fc940ec 100644 --- a/packages/neon/neon/lib/src/blocs/accounts.dart +++ b/packages/neon/neon/lib/src/blocs/accounts.dart @@ -38,13 +38,13 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState final as = accounts.value; if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) { final lastUsedAccountID = _storage.getString(_keyLastUsedAccount); - activeAccount.add(as.singleWhere((final account) => account.id == lastUsedAccountID)); + activeAccount.add(lastUsedAccountID != null ? as.find(lastUsedAccountID) : null); } else { unawaited( _globalOptions.initialAccount.stream.first.then((final lastAccount) { - final matches = as.where((final account) => account.id == lastAccount).toList(); - if (matches.isNotEmpty) { - activeAccount.add(matches[0]); + final account = lastAccount != null ? as.find(lastAccount) : null; + if (account != null) { + activeAccount.add(account); } }), ); diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index c2fd12af..6915eac4 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -216,7 +216,7 @@ class _HomePageState extends State { builder: (final context) { if (accountsSnapshot.hasData) { final accounts = accountsSnapshot.data!; - final account = accounts.singleWhere((final account) => account.id == widget.account.id); + final account = accounts.find(widget.account.id)!; final isQuickBar = navigationMode == NavigationMode.quickBar; final drawer = Drawer( @@ -326,11 +326,8 @@ class _HomePageState extends State { ) .toList(), onChanged: (final id) { - for (final account in accounts) { - if (account.id == id) { - _accountsBloc.setActiveAccount(account); - break; - } + if (id != null) { + _accountsBloc.setActiveAccount(accounts.find(id)); } }, ), diff --git a/packages/neon/neon/lib/src/pages/login.dart b/packages/neon/neon/lib/src/pages/login.dart index 48a5ca66..087741e4 100644 --- a/packages/neon/neon/lib/src/pages/login.dart +++ b/packages/neon/neon/lib/src/pages/login.dart @@ -61,12 +61,11 @@ class _LoginPageState extends State { _accountsBloc.updateAccount(account); Navigator.of(context).pop(); } else { - for (final a in _accountsBloc.accounts.value) { - if (a.id == account.id) { - NeonException.showSnackbar(context, AppLocalizations.of(context).errorAccountAlreadyExists); - await _loginBloc.refresh(); - return; - } + final existingAccount = _accountsBloc.accounts.value.find(account.id); + if (existingAccount != null) { + NeonException.showSnackbar(context, AppLocalizations.of(context).errorAccountAlreadyExists); + await _loginBloc.refresh(); + return; } _accountsBloc ..addAccount(account) diff --git a/packages/neon/neon/lib/src/utils/settings_export_helper.dart b/packages/neon/neon/lib/src/utils/settings_export_helper.dart index e62b1a7c..83e9d683 100644 --- a/packages/neon/neon/lib/src/utils/settings_export_helper.dart +++ b/packages/neon/neon/lib/src/utils/settings_export_helper.dart @@ -33,13 +33,13 @@ class SettingsExportHelper { final accountsData = data['accounts'] as Map; for (final accountId in accountsData.keys) { - final matchingAccounts = accountSpecificOptions.keys.where((final account) => account.id == accountId).toList(); - if (matchingAccounts.length != 1) { + final account = accountSpecificOptions.keys.toList().find(accountId); + if (account == null) { return; } final accountData = accountsData[accountId]! as Map; await _applyOptionsMapToOptions( - accountSpecificOptions[matchingAccounts[0]]!, + accountSpecificOptions[account]!, accountData, ); }