From a74d869e584db79f225eba75fe3477e7e1e38df5 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Fri, 29 Sep 2023 07:48:40 +0200 Subject: [PATCH] refactor(neon,neon_files,neon_news,neon_notes,neon_notifications): Move app support check to app implementations Signed-off-by: jld3103 --- packages/neon/neon/lib/src/blocs/apps.dart | 33 +++++++++---------- .../lib/src/models/app_implementation.dart | 15 +++++++++ packages/neon/neon/lib/src/pages/home.dart | 9 ++--- packages/neon/neon_files/lib/neon_files.dart | 7 ++++ packages/neon/neon_news/lib/neon_news.dart | 7 ++++ packages/neon/neon_notes/lib/neon_notes.dart | 9 +++++ .../lib/neon_notifications.dart | 7 ++++ 7 files changed, 66 insertions(+), 21 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/apps.dart b/packages/neon/neon/lib/src/blocs/apps.dart index 4fe57871..2d1feae5 100644 --- a/packages/neon/neon/lib/src/blocs/apps.dart +++ b/packages/neon/neon/lib/src/blocs/apps.dart @@ -36,7 +36,7 @@ abstract interface class AppsBlocStates { BehaviorSubject get openNotifications; - BehaviorSubject?> get appVersions; + BehaviorSubject> get appVersions; } @internal @@ -116,24 +116,23 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates return; } - final appIds = { - 'core', - ...apps.requireData.map((final a) => a.id), - }; + final notSupported = {}; - final notSupported = <(String, Object?)>[]; + try { + final (coreSupported, coreMinimumVersion) = _account.client.core.isSupported(capabilities.requireData); + if (!coreSupported) { + notSupported['core'] = coreMinimumVersion.toString(); + } + } catch (e, s) { + debugPrint(e.toString()); + debugPrint(s.toString()); + } - for (final id in appIds) { + for (final app in apps.requireData) { try { - final (supported, minVersion) = switch (id) { - 'core' => _account.client.core.isSupported(capabilities.requireData), - AppIDs.news => await _account.client.news.isSupported(), - AppIDs.notes => _account.client.notes.isSupported(capabilities.requireData), - _ => (true, null), - }; - - if (!supported) { - notSupported.add((id, minVersion)); + final (supported, minimumVersion) = await app.isSupported(_account, capabilities.requireData); + if (!(supported ?? true)) { + notSupported[app.id] = minimumVersion; } } catch (e, s) { debugPrint(e.toString()); @@ -193,7 +192,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates BehaviorSubject openNotifications = BehaviorSubject(); @override - BehaviorSubject?> appVersions = BehaviorSubject(); + BehaviorSubject> appVersions = BehaviorSubject(); @override Future refresh() async { diff --git a/packages/neon/neon/lib/src/models/app_implementation.dart b/packages/neon/neon/lib/src/models/app_implementation.dart index 75c5d769..0f5e14e0 100644 --- a/packages/neon/neon/lib/src/models/app_implementation.dart +++ b/packages/neon/neon/lib/src/models/app_implementation.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; @@ -12,6 +14,7 @@ import 'package:neon/src/settings/models/options_collection.dart'; import 'package:neon/src/settings/models/storage.dart'; import 'package:neon/src/utils/provider.dart'; import 'package:neon/src/widgets/drawer_destination.dart'; +import 'package:nextcloud/nextcloud.dart'; import 'package:provider/provider.dart'; import 'package:rxdart/rxdart.dart'; import 'package:vector_graphics/vector_graphics.dart'; @@ -33,6 +36,18 @@ abstract class AppImplementation @mustBeOverridden R get options; + /// Checks if the app is supported on the server of the [account]. + /// + /// A `supported` value of `null` means that it can not be known if the app is supported. + /// This is the case for apps that depend on the server version like files and we assume that the app is supported. + /// The server support is handled differently. + /// + /// The first value of the record is the supported status and the second value is the supported minimum version. + FutureOr<(bool? supported, String? minimumVersion)> isSupported( + final Account account, + final CoreOcsGetCapabilitiesResponseApplicationJson_Ocs_Data capabilities, + ); + final blocsCache = AccountCache(); T getBloc(final Account account) => blocsCache[account] ??= buildBloc(account); diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index 7cca87aa..7b92e574 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -36,7 +36,7 @@ class _HomePageState extends State { late GlobalOptions _globalOptions; late AccountsBloc _accountsBloc; late AppsBloc _appsBloc; - late StreamSubscription?> _versionCheckSubscription; + late StreamSubscription> _versionCheckSubscription; @override void initState() { @@ -47,7 +47,7 @@ class _HomePageState extends State { _appsBloc = _accountsBloc.activeAppsBloc; _versionCheckSubscription = _appsBloc.appVersions.listen((final values) { - if (values == null || !mounted) { + if (!mounted) { return; } @@ -55,8 +55,9 @@ class _HomePageState extends State { final buffer = StringBuffer()..writeln(); - for (final error in values) { - final (appId, minVersion) = error; + for (final error in values.entries) { + final appId = error.key; + final minVersion = error.value; final appName = l10n.appImplementationName(appId); if (appName.isNotEmpty && minVersion != null) { diff --git a/packages/neon/neon_files/lib/neon_files.dart b/packages/neon/neon_files/lib/neon_files.dart index f2eda221..1e7985c9 100644 --- a/packages/neon/neon_files/lib/neon_files.dart +++ b/packages/neon/neon_files/lib/neon_files.dart @@ -71,4 +71,11 @@ class FilesApp extends AppImplementation { @override final RouteBase route = $filesAppRoute; + + @override + (bool? supported, String? minimumVersion) isSupported( + final Account account, + final CoreOcsGetCapabilitiesResponseApplicationJson_Ocs_Data capabilities, + ) => + const (null, null); } diff --git a/packages/neon/neon_news/lib/neon_news.dart b/packages/neon/neon_news/lib/neon_news.dart index 286323fe..778b637a 100644 --- a/packages/neon/neon_news/lib/neon_news.dart +++ b/packages/neon/neon_news/lib/neon_news.dart @@ -81,4 +81,11 @@ class NewsApp extends AppImplementation { @override BehaviorSubject getUnreadCounter(final NewsBloc bloc) => bloc.unreadCounter; + + @override + Future<(bool? supported, String? minimumVersion)> isSupported( + final Account account, + final CoreOcsGetCapabilitiesResponseApplicationJson_Ocs_Data capabilities, + ) => + account.client.news.isSupported(); } diff --git a/packages/neon/neon_notes/lib/neon_notes.dart b/packages/neon/neon_notes/lib/neon_notes.dart index aa8054b7..bb1ff6ff 100644 --- a/packages/neon/neon_notes/lib/neon_notes.dart +++ b/packages/neon/neon_notes/lib/neon_notes.dart @@ -67,4 +67,13 @@ class NotesApp extends AppImplementation { @override final RouteBase route = $notesAppRoute; + + @override + (bool? supported, String? minimumVersion) isSupported( + final Account account, + final CoreOcsGetCapabilitiesResponseApplicationJson_Ocs_Data capabilities, + ) { + final (supported, minimumVersion) = account.client.notes.isSupported(capabilities); + return (supported, minimumVersion.toString()); + } } diff --git a/packages/neon/neon_notifications/lib/neon_notifications.dart b/packages/neon/neon_notifications/lib/neon_notifications.dart index 18ea3347..b02ea9f5 100644 --- a/packages/neon/neon_notifications/lib/neon_notifications.dart +++ b/packages/neon/neon_notifications/lib/neon_notifications.dart @@ -52,4 +52,11 @@ class NotificationsApp extends AppImplementation getUnreadCounter(final NotificationsBloc bloc) => bloc.unreadCounter; + + @override + (bool? supported, String? minimumVersion) isSupported( + final Account account, + final CoreOcsGetCapabilitiesResponseApplicationJson_Ocs_Data capabilities, + ) => + const (null, null); }