Browse Source

Merge pull request #257 from provokateurin/cleanup/finding-accounts-apps

Cleanup finding accounts and apps
pull/261/head
Kate 2 years ago committed by GitHub
parent
commit
51e4686d3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 34
      packages/neon/neon/lib/src/app.dart
  2. 8
      packages/neon/neon/lib/src/blocs/accounts.dart
  3. 7
      packages/neon/neon/lib/src/blocs/apps.dart
  4. 13
      packages/neon/neon/lib/src/pages/home.dart
  5. 11
      packages/neon/neon/lib/src/pages/login.dart
  6. 12
      packages/neon/neon/lib/src/utils/app_implementation.dart
  7. 12
      packages/neon/neon/lib/src/utils/settings_export_helper.dart
  8. 14
      packages/neon/neon_notifications/lib/pages/main.dart

34
packages/neon/neon/lib/src/app.dart

@ -171,22 +171,18 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
if (account == null) { if (account == null) {
return; return;
} }
final appImplementation = Provider.of<List<AppImplementation>>(context, listen: false) final app = Provider.of<List<AppImplementation>>(context, listen: false).find('notifications');
.singleWhere((final a) => a.id == 'notifications'); if (app != null) {
await _accountsBloc.getAppsBloc(account).getAppBloc<NotificationsBlocInterface>(appImplementation).refresh(); await _accountsBloc.getAppsBloc(account).getAppBloc<NotificationsBlocInterface>(app).refresh();
}
}; };
Global.onPushNotificationClicked = (final pushNotificationWithAccountID) async { Global.onPushNotificationClicked = (final pushNotificationWithAccountID) async {
final allAppImplementations = Provider.of<List<AppImplementation>>(context, listen: false); final allAppImplementations = Provider.of<List<AppImplementation>>(context, listen: false);
final matchingAppImplementations = final app = (pushNotificationWithAccountID.subject.app != null
allAppImplementations.where((final a) => a.id == pushNotificationWithAccountID.subject.app); ? allAppImplementations.find(pushNotificationWithAccountID.subject.app!)
: null) ??
late AppImplementation appImplementation; allAppImplementations.find('notifications');
if (matchingAppImplementations.isNotEmpty) {
appImplementation = matchingAppImplementations.single;
} else {
appImplementation = allAppImplementations.singleWhere((final a) => a.id == 'notifications');
}
final account = _accountsBloc.accounts.value.find(pushNotificationWithAccountID.accountID); final account = _accountsBloc.accounts.value.find(pushNotificationWithAccountID.accountID);
if (account == null) { if (account == null) {
@ -194,13 +190,15 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
} }
_accountsBloc.setActiveAccount(account); _accountsBloc.setActiveAccount(account);
if (appImplementation.id != 'notifications') { if (app != null) {
_accountsBloc if (app.id != 'notifications') {
.getAppsBloc(account) _accountsBloc
.getAppBloc<NotificationsBlocInterface>(appImplementation) .getAppsBloc(account)
.deleteNotification(pushNotificationWithAccountID.subject.nid!); .getAppBloc<NotificationsBlocInterface>(app)
.deleteNotification(pushNotificationWithAccountID.subject.nid!);
}
await _openAppFromExternal(account, app.id);
} }
await _openAppFromExternal(account, appImplementation.id);
}; };
final details = await localNotificationsPlugin.getNotificationAppLaunchDetails(); final details = await localNotificationsPlugin.getNotificationAppLaunchDetails();

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

@ -37,13 +37,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);
} }
}), }),
); );

7
packages/neon/neon/lib/src/blocs/apps.dart

@ -37,7 +37,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
unawaited( unawaited(
options.initialApp.stream.first.then((var initialApp) async { options.initialApp.stream.first.then((var initialApp) async {
if (initialApp == null) { if (initialApp == null) {
if (result.data!.where((final a) => a.id == 'files').isNotEmpty) { if (result.data!.find('files') != null) {
initialApp = 'files'; initialApp = 'files';
} else if (result.data!.isNotEmpty) { } else if (result.data!.isNotEmpty) {
// This should never happen, because the files app is always installed and can not be removed, but just in // This should never happen, because the files app is always installed and can not be removed, but just in
@ -126,10 +126,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
@override @override
Future setActiveApp(final String? appID) async { Future setActiveApp(final String? appID) async {
if ((await appImplementations.firstWhere((final a) => a.data != null)) if (appID != null && (await appImplementations.firstWhere((final a) => a.data != null)).data!.find(appID) != null) {
.data!
.where((final app) => app.id == appID)
.isNotEmpty) {
if (activeAppID.valueOrNull != appID) { if (activeAppID.valueOrNull != appID) {
activeAppID.add(appID); activeAppID.add(appID);
} }

13
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;
}
} }
}, },
), ),
@ -474,7 +471,7 @@ class _HomePageState extends State<HomePage> {
Flexible( Flexible(
child: Text( child: Text(
appImplementations.data! appImplementations.data!
.singleWhere((final a) => a.id == activeAppIDSnapshot.data!) .find(activeAppIDSnapshot.data!)!
.name(context), .name(context),
), ),
), ),
@ -583,7 +580,7 @@ class _HomePageState extends State<HomePage> {
if (activeAppIDSnapshot.hasData) ...[ if (activeAppIDSnapshot.hasData) ...[
Expanded( Expanded(
child: appImplementations.data! child: appImplementations.data!
.singleWhere((final a) => a.id == activeAppIDSnapshot.data!) .find(activeAppIDSnapshot.data!)!
.buildPage(context, _appsBloc), .buildPage(context, _appsBloc),
), ),
], ],

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)

12
packages/neon/neon/lib/src/utils/app_implementation.dart

@ -46,3 +46,15 @@ abstract class AppImplementation<T extends Bloc, R extends NextcloudAppSpecificO
options.dispose(); options.dispose();
} }
} }
extension AppImplementationFind on List<AppImplementation> {
AppImplementation? find(final String appID) {
for (final app in this) {
if (app.id == appID) {
return app;
}
}
return null;
}
}

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

@ -20,26 +20,26 @@ class SettingsExportHelper {
final appImplementationsData = data['apps'] as Map<String, dynamic>; final appImplementationsData = data['apps'] as Map<String, dynamic>;
for (final appId in appImplementationsData.keys) { for (final appId in appImplementationsData.keys) {
final matchingAppImplementations = appImplementations.where((final app) => app.id == appId).toList(); final app = appImplementations.find(appId);
if (matchingAppImplementations.length != 1) { if (app == null) {
return; return;
} }
final appImplementationData = appImplementationsData[appId]! as Map<String, dynamic>; final appImplementationData = appImplementationsData[appId]! as Map<String, dynamic>;
await _applyOptionsMapToOptions( await _applyOptionsMapToOptions(
matchingAppImplementations[0].options.options, app.options.options,
appImplementationData, appImplementationData,
); );
} }
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,
); );
} }

14
packages/neon/neon_notifications/lib/pages/main.dart

@ -50,9 +50,7 @@ class _NotificationsMainPageState extends State<NotificationsMainPage> {
final BuildContext context, final BuildContext context,
final NextcloudNotificationsNotification notification, final NextcloudNotificationsNotification notification,
) { ) {
final matchingAppImplementations = Provider.of<List<AppImplementation>>(context, listen: false) final app = Provider.of<List<AppImplementation>>(context, listen: false).find(notification.app);
.where((final a) => a.id == notification.app)
.toList();
return ListTile( return ListTile(
title: Text(notification.subject), title: Text(notification.subject),
@ -73,8 +71,8 @@ class _NotificationsMainPageState extends State<NotificationsMainPage> {
), ),
], ],
), ),
leading: matchingAppImplementations.isNotEmpty leading: app != null
? matchingAppImplementations.single.buildIcon( ? app.buildIcon(
context, context,
width: 40, width: 40,
height: 40, height: 40,
@ -93,11 +91,9 @@ class _NotificationsMainPageState extends State<NotificationsMainPage> {
if (notification.app == 'notifications') { if (notification.app == 'notifications') {
return; return;
} }
final allAppImplementations = Provider.of<List<AppImplementation>>(context, listen: false); if (app != null) {
final matchingAppImplementations = allAppImplementations.where((final a) => a.id == notification.app);
if (matchingAppImplementations.isNotEmpty) {
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false); final accountsBloc = Provider.of<AccountsBloc>(context, listen: false);
await accountsBloc.getAppsBloc(accountsBloc.activeAccount.value!).setActiveApp(notification.app); await accountsBloc.getAppsBloc(accountsBloc.activeAccount.value!).setActiveApp(app.id);
} else { } else {
await showDialog( await showDialog(
context: context, context: context,

Loading…
Cancel
Save