Browse Source

neon: make find extension methods null aware

pull/411/head
Nikolas Rimikis 1 year ago
parent
commit
64aba14826
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 10
      packages/neon/neon/lib/src/app.dart
  2. 4
      packages/neon/neon/lib/src/blocs/accounts.dart
  3. 4
      packages/neon/neon/lib/src/blocs/apps.dart
  4. 2
      packages/neon/neon/lib/src/blocs/push_notifications.dart
  5. 3
      packages/neon/neon/lib/src/models/account.dart
  6. 2
      packages/neon/neon/lib/src/pages/home.dart
  7. 2
      packages/neon/neon/lib/src/pages/login.dart
  8. 4
      packages/neon/neon/lib/src/router.dart
  9. 3
      packages/neon/neon/lib/src/utils/app_implementation.dart
  10. 2
      packages/neon/neon/lib/src/utils/push_utils.dart
  11. 4
      packages/neon/neon/lib/src/utils/settings_export_helper.dart
  12. 2
      packages/neon/neon/lib/src/widgets/app_bar.dart
  13. 2
      packages/neon/neon_notifications/lib/pages/main.dart

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

@ -109,13 +109,13 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
if (_platform.canUsePushNotifications) { if (_platform.canUsePushNotifications) {
final localNotificationsPlugin = await PushUtils.initLocalNotifications(); final localNotificationsPlugin = await PushUtils.initLocalNotifications();
Global.onPushNotificationReceived = (final accountID) async { Global.onPushNotificationReceived = (final accountID) async {
final account = _accountsBloc.accounts.value.find(accountID); final account = _accountsBloc.accounts.value.tryFind(accountID);
if (account == null) { if (account == null) {
return; return;
} }
final allAppImplementations = Provider.of<Iterable<AppImplementation>>(context, listen: false); final allAppImplementations = Provider.of<Iterable<AppImplementation>>(context, listen: false);
final app = allAppImplementations.find('notifications') as NotificationsAppInterface?; final app = allAppImplementations.tryFind('notifications') as NotificationsAppInterface?;
if (app == null) { if (app == null) {
return; return;
@ -124,7 +124,7 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
await _accountsBloc.getAppsBlocFor(account).getAppBloc<NotificationsBlocInterface>(app).refresh(); await _accountsBloc.getAppsBlocFor(account).getAppBloc<NotificationsBlocInterface>(app).refresh();
}; };
Global.onPushNotificationClicked = (final pushNotificationWithAccountID) async { Global.onPushNotificationClicked = (final pushNotificationWithAccountID) async {
final account = _accountsBloc.accounts.value.find(pushNotificationWithAccountID.accountID); final account = _accountsBloc.accounts.value.tryFind(pushNotificationWithAccountID.accountID);
if (account == null) { if (account == null) {
return; return;
} }
@ -132,7 +132,7 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
final allAppImplementations = Provider.of<Iterable<AppImplementation>>(context, listen: false); final allAppImplementations = Provider.of<Iterable<AppImplementation>>(context, listen: false);
final notificationsApp = allAppImplementations.find('notifications') as NotificationsAppInterface?; final notificationsApp = allAppImplementations.tryFind('notifications') as NotificationsAppInterface?;
if (notificationsApp != null) { if (notificationsApp != null) {
_accountsBloc _accountsBloc
.getAppsBlocFor(account) .getAppsBlocFor(account)
@ -140,7 +140,7 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
.deleteNotification(pushNotificationWithAccountID.subject.nid!); .deleteNotification(pushNotificationWithAccountID.subject.nid!);
} }
final app = allAppImplementations.find(pushNotificationWithAccountID.subject.app!) ?? notificationsApp; final app = allAppImplementations.tryFind(pushNotificationWithAccountID.subject.app) ?? notificationsApp;
if (app == null) { if (app == null) {
return; return;
} }

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

@ -65,7 +65,7 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) { if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) {
final lastUsedAccountID = _storage.getString(_keyLastUsedAccount); final lastUsedAccountID = _storage.getString(_keyLastUsedAccount);
if (lastUsedAccountID != null) { if (lastUsedAccountID != null) {
final aa = as.find(lastUsedAccountID); final aa = as.tryFind(lastUsedAccountID);
if (aa != null) { if (aa != null) {
setActiveAccount(aa); setActiveAccount(aa);
} }
@ -74,7 +74,7 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
unawaited( unawaited(
_globalOptions.initialAccount.stream.first.then((final lastAccount) { _globalOptions.initialAccount.stream.first.then((final lastAccount) {
final account = lastAccount != null ? as.find(lastAccount) : null; final account = as.tryFind(lastAccount);
if (activeAccount.valueOrNull == null) { if (activeAccount.valueOrNull == null) {
if (account != null) { if (account != null) {
setActiveAccount(account); setActiveAccount(account);

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

@ -39,7 +39,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.requireData.find('files') != null) { if (result.requireData.tryFind('files') != null) {
initialApp = 'files'; initialApp = 'files';
} else if (result.requireData.isNotEmpty) { } else if (result.requireData.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
@ -177,7 +177,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
@override @override
Future setActiveApp(final String? appID) async { Future setActiveApp(final String? appID) async {
if (appID != null && if (appID != null &&
(await appImplementations.firstWhere((final a) => a.hasData)).requireData.find(appID) != null) { (await appImplementations.firstWhere((final a) => a.hasData)).requireData.tryFind(appID) != null) {
if (activeAppID.valueOrNull != appID) { if (activeAppID.valueOrNull != appID) {
activeAppID.add(appID); activeAppID.add(appID);
} }

2
packages/neon/neon/lib/src/blocs/push_notifications.dart

@ -52,7 +52,7 @@ class PushNotificationsBloc extends Bloc implements PushNotificationsBlocEvents,
Future _setupUnifiedPush() async { Future _setupUnifiedPush() async {
await UnifiedPush.initialize( await UnifiedPush.initialize(
onNewEndpoint: (final endpoint, final instance) async { onNewEndpoint: (final endpoint, final instance) async {
final account = _accountsBloc.accounts.value.find(instance); final account = _accountsBloc.accounts.value.tryFind(instance);
if (account == null) { if (account == null) {
debugPrint('Account for $instance not found, can not process endpoint'); debugPrint('Account for $instance not found, can not process endpoint');
return; return;

3
packages/neon/neon/lib/src/models/account.dart

@ -105,5 +105,6 @@ extension NextcloudClientHelpers on NextcloudClient {
} }
extension AccountFind on Iterable<Account> { extension AccountFind on Iterable<Account> {
Account? find(final String accountID) => firstWhereOrNull((final account) => account.id == accountID); Account? tryFind(final String? accountID) => firstWhereOrNull((final account) => account.id == accountID);
Account find(final String accountID) => firstWhere((final account) => account.id == accountID);
} }

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

@ -126,7 +126,7 @@ class _HomePageState extends State<HomePage> {
] else ...[ ] else ...[
if (activeAppIDSnapshot.hasData) ...[ if (activeAppIDSnapshot.hasData) ...[
Expanded( Expanded(
child: appImplementations.data!.find(activeAppIDSnapshot.data!)!.page, child: appImplementations.data!.find(activeAppIDSnapshot.data!).page,
), ),
], ],
], ],

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

@ -74,7 +74,7 @@ class _LoginPageState extends State<LoginPage> {
if (widget.serverURL != null) { if (widget.serverURL != null) {
_accountsBloc.updateAccount(account); _accountsBloc.updateAccount(account);
} else { } else {
final existingAccount = _accountsBloc.accounts.value.find(account.id); final existingAccount = _accountsBloc.accounts.value.tryFind(account.id);
if (existingAccount != null) { if (existingAccount != null) {
NeonException.showSnackbar(context, AppLocalizations.of(context).errorAccountAlreadyExists); NeonException.showSnackbar(context, AppLocalizations.of(context).errorAccountAlreadyExists);
await _loginBloc.refresh(); await _loginBloc.refresh();

4
packages/neon/neon/lib/src/router.dart

@ -41,7 +41,7 @@ class AccountSettingsRoute extends GoRouteData {
@override @override
Widget build(final BuildContext context, final GoRouterState state) { Widget build(final BuildContext context, final GoRouterState state) {
final bloc = Provider.of<AccountsBloc>(context, listen: false); final bloc = Provider.of<AccountsBloc>(context, listen: false);
final account = bloc.accounts.value.find(accountid)!; final account = bloc.accounts.value.find(accountid);
return AccountSettingsPage( return AccountSettingsPage(
bloc: bloc, bloc: bloc,
@ -120,7 +120,7 @@ class NextcloudAppSettingsRoute extends GoRouteData {
@override @override
Widget build(final BuildContext context, final GoRouterState state) { Widget build(final BuildContext context, final GoRouterState state) {
final appImplementations = Provider.of<Iterable<AppImplementation>>(context, listen: false); final appImplementations = Provider.of<Iterable<AppImplementation>>(context, listen: false);
final appImplementation = appImplementations.find(appid)!; final appImplementation = appImplementations.tryFind(appid)!;
return NextcloudAppSettingsPage(appImplementation: appImplementation); return NextcloudAppSettingsPage(appImplementation: appImplementation);
} }

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

@ -74,5 +74,6 @@ abstract class AppImplementation<T extends Bloc, R extends NextcloudAppSpecificO
} }
extension AppImplementationFind on Iterable<AppImplementation> { extension AppImplementationFind on Iterable<AppImplementation> {
AppImplementation? find(final String appID) => firstWhereOrNull((final app) => app.id == appID); AppImplementation? tryFind(final String? appID) => firstWhereOrNull((final app) => app.id == appID);
AppImplementation find(final String appID) => firstWhere((final app) => app.id == appID);
} }

2
packages/neon/neon/lib/src/utils/push_utils.dart

@ -82,7 +82,7 @@ class PushUtils {
AndroidBitmap<Object>? largeIconBitmap; AndroidBitmap<Object>? largeIconBitmap;
try { try {
accounts = loadAccounts(AppStorage('accounts', sharedPreferences)); accounts = loadAccounts(AppStorage('accounts', sharedPreferences));
account = accounts.find(instance); account = accounts.tryFind(instance);
if (account != null) { if (account != null) {
notification = notification =
(await account.client.notifications.getNotification(id: pushNotification.subject.nid!)).ocs.data; (await account.client.notifications.getNotification(id: pushNotification.subject.nid!)).ocs.data;

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

@ -20,7 +20,7 @@ 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 app = appImplementations.find(appId); final app = appImplementations.tryFind(appId);
if (app == null) { if (app == null) {
return; return;
} }
@ -33,7 +33,7 @@ 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 account = accountSpecificOptions.keys.find(accountId); final account = accountSpecificOptions.keys.tryFind(accountId);
if (account == null) { if (account == null) {
return; return;
} }

2
packages/neon/neon/lib/src/widgets/app_bar.dart

@ -34,7 +34,7 @@ class NeonAppBar extends StatelessWidget implements PreferredSizeWidget {
if (appImplementations.hasData && activeAppIDSnapshot.hasData) ...[ if (appImplementations.hasData && activeAppIDSnapshot.hasData) ...[
Flexible( Flexible(
child: Text( child: Text(
appImplementations.requireData.find(activeAppIDSnapshot.data!)!.name(context), appImplementations.requireData.find(activeAppIDSnapshot.data!).name(context),
), ),
), ),
], ],

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

@ -51,7 +51,7 @@ class _NotificationsMainPageState extends State<NotificationsMainPage> {
final BuildContext context, final BuildContext context,
final NextcloudNotificationsNotification notification, final NextcloudNotificationsNotification notification,
) { ) {
final app = Provider.of<Iterable<AppImplementation>>(context, listen: false).find(notification.app); final app = Provider.of<Iterable<AppImplementation>>(context, listen: false).tryFind(notification.app);
return ListTile( return ListTile(
title: Text(notification.subject), title: Text(notification.subject),

Loading…
Cancel
Save