diff --git a/packages/neon/neon/lib/src/blocs/apps.dart b/packages/neon/neon/lib/src/blocs/apps.dart index 222de428..538aa885 100644 --- a/packages/neon/neon/lib/src/blocs/apps.dart +++ b/packages/neon/neon/lib/src/blocs/apps.dart @@ -23,6 +23,9 @@ abstract class AppsBlocEvents { /// If the app is already the active app nothing will happen. /// When using [skipAlreadySet] nothing will be done if there already is an active app. void setActiveApp(final String appID, {final bool skipAlreadySet = false}); + + /// Navigates to the active app. + void navigateActiveApp(); } abstract class AppsBlocStates { @@ -228,6 +231,11 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates } } + @override + void navigateActiveApp() { + _router.go(activeApp.value.initialPath); + } + T getAppBloc(final AppImplementation appImplementation) => appImplementation.getBloc(_account); diff --git a/packages/neon/neon/lib/src/models/app_implementation.dart b/packages/neon/neon/lib/src/models/app_implementation.dart index c7390cad..733052bc 100644 --- a/packages/neon/neon/lib/src/models/app_implementation.dart +++ b/packages/neon/neon/lib/src/models/app_implementation.dart @@ -54,8 +54,6 @@ abstract class AppImplementation BehaviorSubject? getUnreadCounter(final T bloc) => null; - Widget get page; - NeonNavigationDestination destination(final BuildContext context) { final accountsBloc = Provider.of(context, listen: false); final account = accountsBloc.activeAccount.value!; diff --git a/packages/neon/neon/lib/src/router.dart b/packages/neon/neon/lib/src/router.dart index 527d6ffa..b527da5d 100644 --- a/packages/neon/neon/lib/src/router.dart +++ b/packages/neon/neon/lib/src/router.dart @@ -2,6 +2,7 @@ import 'dart:async'; +import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'package:go_router/go_router.dart'; @@ -9,6 +10,7 @@ import 'package:meta/meta.dart'; import 'package:neon/src/blocs/accounts.dart'; import 'package:neon/src/models/account.dart'; import 'package:neon/src/models/app_implementation.dart'; +import 'package:neon/src/models/notifications_interface.dart'; import 'package:neon/src/pages/account_settings.dart'; import 'package:neon/src/pages/home.dart'; import 'package:neon/src/pages/login.dart'; @@ -64,9 +66,13 @@ class AppRouter extends GoRouter { }, routes: [ ShellRoute( - routes: appImplementations.map((final a) => a.route).toList(), + routes: appImplementations + .whereNot((final a) => a is NotificationsAppInterface) + .map((final a) => a.route) + .toList(), builder: _mainViewBuilder, ), + ...appImplementations.whereType().map((final a) => a.route), ...$appRoutes, ], ); diff --git a/packages/neon/neon/lib/src/widgets/app_bar.dart b/packages/neon/neon/lib/src/widgets/app_bar.dart index f008a1c6..9d9a7ae6 100644 --- a/packages/neon/neon/lib/src/widgets/app_bar.dart +++ b/packages/neon/neon/lib/src/widgets/app_bar.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; import 'package:meta/meta.dart'; import 'package:neon/l10n/localizations.dart'; import 'package:neon/src/bloc/result_builder.dart'; @@ -100,7 +101,6 @@ class NotificationIconButton extends StatefulWidget { class _NotificationIconButtonState extends State { late AccountsBloc _accountsBloc; late AppsBloc _appsBloc; - late List _accounts; late Account _account; late StreamSubscription notificationSubscription; @@ -109,13 +109,12 @@ class _NotificationIconButtonState extends State { super.initState(); _accountsBloc = Provider.of(context, listen: false); _appsBloc = _accountsBloc.activeAppsBloc; - _accounts = _accountsBloc.accounts.value; _account = _accountsBloc.activeAccount.value!; - notificationSubscription = _appsBloc.openNotifications.listen((final _) async { + notificationSubscription = _appsBloc.openNotifications.listen((final _) { final notificationsAppImplementation = _appsBloc.notificationsAppImplementation.valueOrNull; if (notificationsAppImplementation != null && notificationsAppImplementation.hasData) { - await _openNotifications(notificationsAppImplementation.data!); + _openNotifications(notificationsAppImplementation.data!); } }); } @@ -127,37 +126,8 @@ class _NotificationIconButtonState extends State { super.dispose(); } - // TODO: migrate to go_router with a separate page - Future _openNotifications( - final NotificationsAppInterface app, - ) async { - final page = Scaffold( - resizeToAvoidBottomInset: false, - appBar: AppBar( - title: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text(app.name(context)), - if (_accounts.length > 1) ...[ - Text( - _account.humanReadableID, - style: Theme.of(context).textTheme.bodySmall, - ), - ], - ], - ), - ), - body: app.page, - ); - - await Navigator.of(context).push( - MaterialPageRoute( - builder: (final context) => Provider( - create: (final context) => app.getBloc(_account), - child: page, - ), - ), - ); + void _openNotifications(final NotificationsAppInterface app) { + GoRouter.of(context).go(app.initialPath); } @override @@ -177,8 +147,8 @@ class _NotificationIconButtonState extends State { final unreadCount = unreadCounterSnapshot.data ?? 0; return IconButton( key: Key('app-${notificationsImplementationData.id}'), - onPressed: () async { - await _openNotifications(notificationsImplementationData); + onPressed: () { + _openNotifications(notificationsImplementationData); }, tooltip: AppLocalizations.of(context).appImplementationName(notificationsImplementationData.id), icon: NeonAppImplementationIcon( diff --git a/packages/neon/neon_files/lib/neon_files.dart b/packages/neon/neon_files/lib/neon_files.dart index 0c0abcd4..d996f7bc 100644 --- a/packages/neon/neon_files/lib/neon_files.dart +++ b/packages/neon/neon_files/lib/neon_files.dart @@ -66,9 +66,6 @@ class FilesApp extends AppImplementation { platform, ); - @override - Widget get page => const FilesMainPage(); - @override RouteBase get route => $filesAppRoute; } diff --git a/packages/neon/neon_news/lib/neon_news.dart b/packages/neon/neon_news/lib/neon_news.dart index 412ab5d1..13c2b685 100644 --- a/packages/neon/neon_news/lib/neon_news.dart +++ b/packages/neon/neon_news/lib/neon_news.dart @@ -73,9 +73,6 @@ class NewsApp extends AppImplementation { account, ); - @override - Widget get page => const NewsMainPage(); - @override RouteBase get route => $newsAppRoute; diff --git a/packages/neon/neon_notes/lib/neon_notes.dart b/packages/neon/neon_notes/lib/neon_notes.dart index 5cd6dec2..c2a1199c 100644 --- a/packages/neon/neon_notes/lib/neon_notes.dart +++ b/packages/neon/neon_notes/lib/neon_notes.dart @@ -63,9 +63,6 @@ class NotesApp extends AppImplementation { account, ); - @override - Widget get page => const NotesMainPage(); - @override RouteBase get route => $notesAppRoute; } diff --git a/packages/neon/neon_notifications/lib/neon_notifications.dart b/packages/neon/neon_notifications/lib/neon_notifications.dart index 7760a412..1d737b68 100644 --- a/packages/neon/neon_notifications/lib/neon_notifications.dart +++ b/packages/neon/neon_notifications/lib/neon_notifications.dart @@ -43,9 +43,6 @@ class NotificationsApp extends AppImplementation const NotificationsMainPage(); - @override RouteBase get route => $notificationsAppRoute; diff --git a/packages/neon/neon_notifications/lib/routes.dart b/packages/neon/neon_notifications/lib/routes.dart index c2f57b75..c00b5f13 100644 --- a/packages/neon/neon_notifications/lib/routes.dart +++ b/packages/neon/neon_notifications/lib/routes.dart @@ -1,8 +1,11 @@ -import 'package:flutter/widgets.dart'; +import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; +import 'package:neon/blocs.dart'; +import 'package:neon/l10n/localizations.dart'; import 'package:neon/models.dart'; import 'package:neon/utils.dart'; import 'package:neon_notifications/neon_notifications.dart'; +import 'package:provider/provider.dart'; part 'routes.g.dart'; @@ -15,5 +18,42 @@ class NotificationsAppRoute extends NeonAppRoute { const NotificationsAppRoute(); @override - Widget build(final BuildContext context, final GoRouterState state) => const NotificationsMainPage(); + Widget build(final BuildContext context, final GoRouterState state) { + final accountsBloc = Provider.of(context, listen: false); + final appsBloc = accountsBloc.activeAppsBloc; + final app = appsBloc.notificationsAppImplementation.value.data!; + final accounts = accountsBloc.accounts.value; + final account = accountsBloc.activeAccount.value!; + + Widget title = Text(AppLocalizations.of(context).appImplementationName(AppIDs.notifications)); + + if (accounts.length > 1) { + title = Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + title, + Text( + account.humanReadableID, + style: Theme.of(context).textTheme.bodySmall, + ), + ], + ); + } + + final page = Scaffold( + resizeToAvoidBottomInset: false, + appBar: AppBar( + leading: BackButton( + onPressed: appsBloc.navigateActiveApp, + ), + title: title, + ), + body: const NotificationsMainPage(), + ); + + return Provider( + create: (final context) => app.getBloc(account), + child: page, + ); + } }