Browse Source

neon: Cleanup finding account

pull/257/head
jld3103 2 years ago
parent
commit
e6d16de211
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 8
      packages/neon/neon/lib/src/blocs/accounts.dart
  2. 9
      packages/neon/neon/lib/src/pages/home.dart
  3. 11
      packages/neon/neon/lib/src/pages/login.dart
  4. 6
      packages/neon/neon/lib/src/utils/settings_export_helper.dart

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

@ -38,13 +38,13 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
final as = accounts.value; final as = accounts.value;
if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) { if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) {
final lastUsedAccountID = _storage.getString(_keyLastUsedAccount); final lastUsedAccountID = _storage.getString(_keyLastUsedAccount);
activeAccount.add(as.singleWhere((final account) => account.id == lastUsedAccountID)); activeAccount.add(lastUsedAccountID != null ? as.find(lastUsedAccountID) : null);
} else { } else {
unawaited( unawaited(
_globalOptions.initialAccount.stream.first.then((final lastAccount) { _globalOptions.initialAccount.stream.first.then((final lastAccount) {
final matches = as.where((final account) => account.id == lastAccount).toList(); final account = lastAccount != null ? as.find(lastAccount) : null;
if (matches.isNotEmpty) { if (account != null) {
activeAccount.add(matches[0]); activeAccount.add(account);
} }
}), }),
); );

9
packages/neon/neon/lib/src/pages/home.dart

@ -216,7 +216,7 @@ class _HomePageState extends State<HomePage> {
builder: (final context) { builder: (final context) {
if (accountsSnapshot.hasData) { if (accountsSnapshot.hasData) {
final accounts = accountsSnapshot.data!; 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 isQuickBar = navigationMode == NavigationMode.quickBar;
final drawer = Drawer( final drawer = Drawer(
@ -326,11 +326,8 @@ class _HomePageState extends State<HomePage> {
) )
.toList(), .toList(),
onChanged: (final id) { onChanged: (final id) {
for (final account in accounts) { if (id != null) {
if (account.id == id) { _accountsBloc.setActiveAccount(accounts.find(id));
_accountsBloc.setActiveAccount(account);
break;
}
} }
}, },
), ),

11
packages/neon/neon/lib/src/pages/login.dart

@ -61,12 +61,11 @@ class _LoginPageState extends State<LoginPage> {
_accountsBloc.updateAccount(account); _accountsBloc.updateAccount(account);
Navigator.of(context).pop(); Navigator.of(context).pop();
} else { } else {
for (final a in _accountsBloc.accounts.value) { final existingAccount = _accountsBloc.accounts.value.find(account.id);
if (a.id == account.id) { if (existingAccount != null) {
NeonException.showSnackbar(context, AppLocalizations.of(context).errorAccountAlreadyExists); NeonException.showSnackbar(context, AppLocalizations.of(context).errorAccountAlreadyExists);
await _loginBloc.refresh(); await _loginBloc.refresh();
return; return;
}
} }
_accountsBloc _accountsBloc
..addAccount(account) ..addAccount(account)

6
packages/neon/neon/lib/src/utils/settings_export_helper.dart

@ -33,13 +33,13 @@ class SettingsExportHelper {
final accountsData = data['accounts'] as Map<String, dynamic>; final accountsData = data['accounts'] as Map<String, dynamic>;
for (final accountId in accountsData.keys) { for (final accountId in accountsData.keys) {
final matchingAccounts = accountSpecificOptions.keys.where((final account) => account.id == accountId).toList(); final account = accountSpecificOptions.keys.toList().find(accountId);
if (matchingAccounts.length != 1) { if (account == null) {
return; return;
} }
final accountData = accountsData[accountId]! as Map<String, dynamic>; final accountData = accountsData[accountId]! as Map<String, dynamic>;
await _applyOptionsMapToOptions( await _applyOptionsMapToOptions(
accountSpecificOptions[matchingAccounts[0]]!, accountSpecificOptions[account]!,
accountData, accountData,
); );
} }

Loading…
Cancel
Save