From c0c268945439c91a80cc90578524c5cb4688e6ce Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 6 Jun 2023 20:05:21 +0200 Subject: [PATCH 1/4] neon: inject bloc for every app via provider --- packages/neon/neon/lib/src/blocs/apps.dart | 4 ++++ packages/neon/neon/lib/src/pages/home.dart | 10 +++++++++- .../neon/neon/lib/src/utils/app_implementation.dart | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/neon/neon/lib/src/blocs/apps.dart b/packages/neon/neon/lib/src/blocs/apps.dart index 4e7a341a..3654250f 100644 --- a/packages/neon/neon/lib/src/blocs/apps.dart +++ b/packages/neon/neon/lib/src/blocs/apps.dart @@ -144,4 +144,8 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates return _blocs[appImplementation.id] = appImplementation.buildBloc(_account.client) as T; } + + List? getAppProviders() => appImplementations.valueOrNull?.data + ?.map((final appImplementation) => appImplementation.blocProvider(_account.client)) + .toList(); } diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index a3cc9f7b..68d9916b 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -504,7 +504,7 @@ class _HomePageState extends State { ], ); - final body = Builder( + Widget body = Builder( builder: (final context) => Row( children: [ if (navigationMode == NavigationMode.quickBar) ...[ @@ -540,6 +540,14 @@ class _HomePageState extends State { ), ); + final appProviders = _appsBloc.getAppProviders(); + if (appProviders != null) { + body = MultiProvider( + providers: appProviders, + child: body, + ); + } + return WillPopScope( onWillPop: () async { if (_scaffoldKey.currentState!.isDrawerOpen) { diff --git a/packages/neon/neon/lib/src/utils/app_implementation.dart b/packages/neon/neon/lib/src/utils/app_implementation.dart index aea3331e..d04279f9 100644 --- a/packages/neon/neon/lib/src/utils/app_implementation.dart +++ b/packages/neon/neon/lib/src/utils/app_implementation.dart @@ -24,6 +24,10 @@ abstract class AppImplementation blocProvider(final NextcloudClient client) => Provider( + create: (final _) => buildBloc(client), + ); + BehaviorSubject? getUnreadCounter(final AppsBloc appsBloc); Widget buildPage(final BuildContext context, final AppsBloc appsBloc); From 0e0753decc825d7a74ec146d69e26db0fb44d3e6 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 6 Jun 2023 20:06:07 +0200 Subject: [PATCH 2/4] neon_files, neon_news, neon_notes, neon_notifications: use injecet bloc --- packages/neon/neon_files/lib/neon_files.dart | 10 +++++++--- packages/neon/neon_news/lib/neon_news.dart | 10 +++++++--- packages/neon/neon_notes/lib/neon_notes.dart | 10 +++++++--- .../neon_notifications/lib/neon_notifications.dart | 10 +++++++--- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/neon/neon_files/lib/neon_files.dart b/packages/neon/neon_files/lib/neon_files.dart index 88ab2f7a..a0ef69ce 100644 --- a/packages/neon/neon_files/lib/neon_files.dart +++ b/packages/neon/neon_files/lib/neon_files.dart @@ -62,9 +62,13 @@ class FilesApp extends AppImplementation { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => FilesMainPage( - bloc: appsBloc.getAppBloc(this), - ); + Widget buildPage(final BuildContext context, final AppsBloc appsBloc) { + final bloc = Provider.of(context, listen: false); + + return FilesMainPage( + bloc: bloc, + ); + } @override BehaviorSubject? getUnreadCounter(final AppsBloc appsBloc) => null; diff --git a/packages/neon/neon_news/lib/neon_news.dart b/packages/neon/neon_news/lib/neon_news.dart index d9d239d2..6ee7237c 100644 --- a/packages/neon/neon_news/lib/neon_news.dart +++ b/packages/neon/neon_news/lib/neon_news.dart @@ -68,9 +68,13 @@ class NewsApp extends AppImplementation { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => NewsMainPage( - bloc: appsBloc.getAppBloc(this), - ); + Widget buildPage(final BuildContext context, final AppsBloc appsBloc) { + final bloc = Provider.of(context, listen: false); + + return NewsMainPage( + bloc: bloc, + ); + } @override BehaviorSubject getUnreadCounter(final AppsBloc appsBloc) => appsBloc.getAppBloc(this).unreadCounter; diff --git a/packages/neon/neon_notes/lib/neon_notes.dart b/packages/neon/neon_notes/lib/neon_notes.dart index 65bb522e..2989bbc9 100644 --- a/packages/neon/neon_notes/lib/neon_notes.dart +++ b/packages/neon/neon_notes/lib/neon_notes.dart @@ -58,9 +58,13 @@ class NotesApp extends AppImplementation { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => NotesMainPage( - bloc: appsBloc.getAppBloc(this), - ); + Widget buildPage(final BuildContext context, final AppsBloc appsBloc) { + final bloc = Provider.of(context, listen: false); + + return NotesMainPage( + bloc: bloc, + ); + } @override BehaviorSubject? getUnreadCounter(final AppsBloc appsBloc) => null; diff --git a/packages/neon/neon_notifications/lib/neon_notifications.dart b/packages/neon/neon_notifications/lib/neon_notifications.dart index 976804c4..45909d57 100644 --- a/packages/neon/neon_notifications/lib/neon_notifications.dart +++ b/packages/neon/neon_notifications/lib/neon_notifications.dart @@ -37,9 +37,13 @@ class NotificationsApp extends NotificationsAppInterface { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => NotificationsMainPage( - bloc: appsBloc.getAppBloc(this), - ); + Widget buildPage(final BuildContext context, final AppsBloc appsBloc) { + final bloc = Provider.of(context, listen: false); + + return NotificationsMainPage( + bloc: bloc, + ); + } @override BehaviorSubject getUnreadCounter(final AppsBloc appsBloc) => From 00c3235fa1ea343c7b4785ec36f738123d43ec89 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 6 Jun 2023 20:12:53 +0200 Subject: [PATCH 3/4] neon_files, neon_news, neon_notes, neon_notifications: make buildPage bloc independent --- packages/neon/neon_files/lib/neon_files.dart | 8 +------ packages/neon/neon_files/lib/pages/main.dart | 20 +++++++++--------- packages/neon/neon_news/lib/neon_news.dart | 8 +------ packages/neon/neon_news/lib/pages/main.dart | 21 +++++++++---------- packages/neon/neon_notes/lib/neon_notes.dart | 8 +------ packages/neon/neon_notes/lib/pages/main.dart | 16 +++++++------- .../lib/neon_notifications.dart | 8 +------ .../neon_notifications/lib/pages/main.dart | 17 ++++++++------- 8 files changed, 41 insertions(+), 65 deletions(-) diff --git a/packages/neon/neon_files/lib/neon_files.dart b/packages/neon/neon_files/lib/neon_files.dart index a0ef69ce..b5e5abb4 100644 --- a/packages/neon/neon_files/lib/neon_files.dart +++ b/packages/neon/neon_files/lib/neon_files.dart @@ -62,13 +62,7 @@ class FilesApp extends AppImplementation { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) { - final bloc = Provider.of(context, listen: false); - - return FilesMainPage( - bloc: bloc, - ); - } + Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => const FilesMainPage(); @override BehaviorSubject? getUnreadCounter(final AppsBloc appsBloc) => null; diff --git a/packages/neon/neon_files/lib/pages/main.dart b/packages/neon/neon_files/lib/pages/main.dart index 20c361eb..a9317fb6 100644 --- a/packages/neon/neon_files/lib/pages/main.dart +++ b/packages/neon/neon_files/lib/pages/main.dart @@ -2,22 +2,22 @@ part of '../neon_files.dart'; class FilesMainPage extends StatefulWidget { const FilesMainPage({ - required this.bloc, super.key, }); - final FilesBloc bloc; - @override State createState() => _FilesMainPageState(); } class _FilesMainPageState extends State { + late FilesBloc bloc; + @override void initState() { super.initState(); + bloc = Provider.of(context, listen: false); - widget.bloc.errors.listen((final error) { + bloc.errors.listen((final error) { NeonException.showSnackbar(context, error); }); } @@ -25,10 +25,10 @@ class _FilesMainPageState extends State { @override Widget build(final BuildContext context) => Scaffold( body: FilesBrowserView( - bloc: widget.bloc.browser, - filesBloc: widget.bloc, + bloc: bloc.browser, + filesBloc: bloc, onPickFile: (final details) async { - final sizeWarning = widget.bloc.options.downloadSizeWarning.value; + final sizeWarning = bloc.options.downloadSizeWarning.value; if (sizeWarning != null && details.size > sizeWarning) { // ignore: use_build_context_synchronously if (!(await showConfirmationDialog( @@ -42,7 +42,7 @@ class _FilesMainPageState extends State { return; } } - widget.bloc.openFile(details.path, details.etag!, details.mimeType); + bloc.openFile(details.path, details.etag!, details.mimeType); }, ), floatingActionButton: FloatingActionButton( @@ -50,8 +50,8 @@ class _FilesMainPageState extends State { await showDialog( context: context, builder: (final context) => FilesChooseCreateDialog( - bloc: widget.bloc, - basePath: widget.bloc.browser.path.value, + bloc: bloc, + basePath: bloc.browser.path.value, ), ); }, diff --git a/packages/neon/neon_news/lib/neon_news.dart b/packages/neon/neon_news/lib/neon_news.dart index 6ee7237c..8964428b 100644 --- a/packages/neon/neon_news/lib/neon_news.dart +++ b/packages/neon/neon_news/lib/neon_news.dart @@ -68,13 +68,7 @@ class NewsApp extends AppImplementation { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) { - final bloc = Provider.of(context, listen: false); - - return NewsMainPage( - bloc: bloc, - ); - } + Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => const NewsMainPage(); @override BehaviorSubject getUnreadCounter(final AppsBloc appsBloc) => appsBloc.getAppBloc(this).unreadCounter; diff --git a/packages/neon/neon_news/lib/pages/main.dart b/packages/neon/neon_news/lib/pages/main.dart index 95cd00e4..64e11df1 100644 --- a/packages/neon/neon_news/lib/pages/main.dart +++ b/packages/neon/neon_news/lib/pages/main.dart @@ -2,24 +2,23 @@ part of '../neon_news.dart'; class NewsMainPage extends StatefulWidget { const NewsMainPage({ - required this.bloc, super.key, }); - final NewsBloc bloc; - @override State createState() => _NewsMainPageState(); } class _NewsMainPageState extends State { - late int _index = widget.bloc.options.defaultCategoryOption.value.index; + late NewsBloc bloc; + late int _index = bloc.options.defaultCategoryOption.value.index; @override void initState() { super.initState(); + final bloc = Provider.of(context, listen: false); - widget.bloc.errors.listen((final error) { + bloc.errors.listen((final error) { NeonException.showSnackbar(context, error); }); } @@ -28,21 +27,21 @@ class _NewsMainPageState extends State { Widget build(final BuildContext context) { final views = [ NewsArticlesView( - bloc: widget.bloc.mainArticlesBloc, - newsBloc: widget.bloc, + bloc: bloc.mainArticlesBloc, + newsBloc: bloc, ), NewsFoldersView( - bloc: widget.bloc, + bloc: bloc, ), NewsFeedsView( - bloc: widget.bloc, + bloc: bloc, ), ]; final floatingActionButtons = [ null, - NewsFolderFloatingActionButton(bloc: widget.bloc), - NewsFeedFloatingActionButton(bloc: widget.bloc), + NewsFolderFloatingActionButton(bloc: bloc), + NewsFeedFloatingActionButton(bloc: bloc), ]; return Scaffold( diff --git a/packages/neon/neon_notes/lib/neon_notes.dart b/packages/neon/neon_notes/lib/neon_notes.dart index 2989bbc9..e62d42a3 100644 --- a/packages/neon/neon_notes/lib/neon_notes.dart +++ b/packages/neon/neon_notes/lib/neon_notes.dart @@ -58,13 +58,7 @@ class NotesApp extends AppImplementation { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) { - final bloc = Provider.of(context, listen: false); - - return NotesMainPage( - bloc: bloc, - ); - } + Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => const NotesMainPage(); @override BehaviorSubject? getUnreadCounter(final AppsBloc appsBloc) => null; diff --git a/packages/neon/neon_notes/lib/pages/main.dart b/packages/neon/neon_notes/lib/pages/main.dart index 2ec42ca6..eecab6b3 100644 --- a/packages/neon/neon_notes/lib/pages/main.dart +++ b/packages/neon/neon_notes/lib/pages/main.dart @@ -2,24 +2,24 @@ part of '../neon_notes.dart'; class NotesMainPage extends StatefulWidget { const NotesMainPage({ - required this.bloc, super.key, }); - final NotesBloc bloc; - @override State createState() => _NotesMainPageState(); } class _NotesMainPageState extends State { - late int _index = widget.bloc.options.defaultCategoryOption.value.index; + late NotesBloc bloc; + late int _index = bloc.options.defaultCategoryOption.value.index; @override void initState() { super.initState(); - widget.bloc.errors.listen((final error) { + bloc = Provider.of(context, listen: false); + + bloc.errors.listen((final error) { handleNotesException(context, error); }); } @@ -28,15 +28,15 @@ class _NotesMainPageState extends State { Widget build(final BuildContext context) { final views = [ NotesView( - bloc: widget.bloc, + bloc: bloc, ), NotesCategoriesView( - bloc: widget.bloc, + bloc: bloc, ), ]; final floatingActionButtons = [ - NotesFloatingActionButton(bloc: widget.bloc), + NotesFloatingActionButton(bloc: bloc), null, ]; diff --git a/packages/neon/neon_notifications/lib/neon_notifications.dart b/packages/neon/neon_notifications/lib/neon_notifications.dart index 45909d57..02797eda 100644 --- a/packages/neon/neon_notifications/lib/neon_notifications.dart +++ b/packages/neon/neon_notifications/lib/neon_notifications.dart @@ -37,13 +37,7 @@ class NotificationsApp extends NotificationsAppInterface { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) { - final bloc = Provider.of(context, listen: false); - - return NotificationsMainPage( - bloc: bloc, - ); - } + Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => const NotificationsMainPage(); @override BehaviorSubject getUnreadCounter(final AppsBloc appsBloc) => diff --git a/packages/neon/neon_notifications/lib/pages/main.dart b/packages/neon/neon_notifications/lib/pages/main.dart index ab9b73fd..6412354b 100644 --- a/packages/neon/neon_notifications/lib/pages/main.dart +++ b/packages/neon/neon_notifications/lib/pages/main.dart @@ -2,34 +2,35 @@ part of '../neon_notifications.dart'; class NotificationsMainPage extends StatefulWidget { const NotificationsMainPage({ - required this.bloc, super.key, }); - final NotificationsBloc bloc; - @override State createState() => _NotificationsMainPageState(); } class _NotificationsMainPageState extends State { + late NotificationsBloc bloc; + @override void initState() { super.initState(); - widget.bloc.errors.listen((final error) { + bloc = Provider.of(context, listen: false); + + bloc.errors.listen((final error) { NeonException.showSnackbar(context, error); }); } @override Widget build(final BuildContext context) => ResultBuilder>( - stream: widget.bloc.notifications, + stream: bloc.notifications, builder: (final context, final notifications) => Scaffold( resizeToAvoidBottomInset: false, floatingActionButton: FloatingActionButton( onPressed: () async { - widget.bloc.deleteAllNotifications(); + bloc.deleteAllNotifications(); }, tooltip: AppLocalizations.of(context).notificationsDismissAll, child: const Icon(MdiIcons.checkAll), @@ -40,7 +41,7 @@ class _NotificationsMainPageState extends State { items: notifications.data, isLoading: notifications.loading, error: notifications.error, - onRefresh: widget.bloc.refresh, + onRefresh: bloc.refresh, builder: _buildNotification, ), ), @@ -113,7 +114,7 @@ class _NotificationsMainPageState extends State { } }, onLongPress: () { - widget.bloc.deleteNotification(notification.notificationId); + bloc.deleteNotification(notification.notificationId); }, ); } From e0b5dae11ec741f6131aa0a7cd4ea6c5d2df06a2 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 6 Jun 2023 20:16:23 +0200 Subject: [PATCH 4/4] neon: make AppImplementation provide a widget getter as a main page --- packages/neon/neon/lib/src/pages/home.dart | 6 ++---- packages/neon/neon/lib/src/utils/app_implementation.dart | 2 +- packages/neon/neon_files/lib/neon_files.dart | 2 +- packages/neon/neon_news/lib/neon_news.dart | 2 +- packages/neon/neon_notes/lib/neon_notes.dart | 2 +- .../neon/neon_notifications/lib/neon_notifications.dart | 2 +- 6 files changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index 68d9916b..ede90702 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -150,7 +150,7 @@ class _HomePageState extends State { ], ), ), - body: app.buildPage(context, _appsBloc), + body: app.page, ), ), ); @@ -526,9 +526,7 @@ class _HomePageState extends State { ] else ...[ if (activeAppIDSnapshot.hasData) ...[ Expanded( - child: appImplementations.data! - .find(activeAppIDSnapshot.data!)! - .buildPage(context, _appsBloc), + child: appImplementations.data!.find(activeAppIDSnapshot.data!)!.page, ), ], ], diff --git a/packages/neon/neon/lib/src/utils/app_implementation.dart b/packages/neon/neon/lib/src/utils/app_implementation.dart index d04279f9..61a75194 100644 --- a/packages/neon/neon/lib/src/utils/app_implementation.dart +++ b/packages/neon/neon/lib/src/utils/app_implementation.dart @@ -30,7 +30,7 @@ abstract class AppImplementation? getUnreadCounter(final AppsBloc appsBloc); - Widget buildPage(final BuildContext context, final AppsBloc appsBloc); + Widget get page; Widget buildIcon( final BuildContext context, { diff --git a/packages/neon/neon_files/lib/neon_files.dart b/packages/neon/neon_files/lib/neon_files.dart index b5e5abb4..66a5c320 100644 --- a/packages/neon/neon_files/lib/neon_files.dart +++ b/packages/neon/neon_files/lib/neon_files.dart @@ -62,7 +62,7 @@ class FilesApp extends AppImplementation { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => const FilesMainPage(); + Widget get page => const FilesMainPage(); @override BehaviorSubject? getUnreadCounter(final AppsBloc appsBloc) => null; diff --git a/packages/neon/neon_news/lib/neon_news.dart b/packages/neon/neon_news/lib/neon_news.dart index 8964428b..030a11fc 100644 --- a/packages/neon/neon_news/lib/neon_news.dart +++ b/packages/neon/neon_news/lib/neon_news.dart @@ -68,7 +68,7 @@ class NewsApp extends AppImplementation { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => const NewsMainPage(); + Widget get page => const NewsMainPage(); @override BehaviorSubject getUnreadCounter(final AppsBloc appsBloc) => appsBloc.getAppBloc(this).unreadCounter; diff --git a/packages/neon/neon_notes/lib/neon_notes.dart b/packages/neon/neon_notes/lib/neon_notes.dart index e62d42a3..c3bf0383 100644 --- a/packages/neon/neon_notes/lib/neon_notes.dart +++ b/packages/neon/neon_notes/lib/neon_notes.dart @@ -58,7 +58,7 @@ class NotesApp extends AppImplementation { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => const NotesMainPage(); + Widget get page => const NotesMainPage(); @override BehaviorSubject? getUnreadCounter(final AppsBloc appsBloc) => null; diff --git a/packages/neon/neon_notifications/lib/neon_notifications.dart b/packages/neon/neon_notifications/lib/neon_notifications.dart index 02797eda..f5a9b61f 100644 --- a/packages/neon/neon_notifications/lib/neon_notifications.dart +++ b/packages/neon/neon_notifications/lib/neon_notifications.dart @@ -37,7 +37,7 @@ class NotificationsApp extends NotificationsAppInterface { ); @override - Widget buildPage(final BuildContext context, final AppsBloc appsBloc) => const NotificationsMainPage(); + Widget get page => const NotificationsMainPage(); @override BehaviorSubject getUnreadCounter(final AppsBloc appsBloc) =>