Browse Source

refactor(neon,neon_files,neon_news,neon_notes,neon_notifications): deprecate AppImplementation.page

Migrates NotificationsApp to a separate route

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
feature/nested-router
Nikolas Rimikis 1 year ago
parent
commit
ebc20a1a4c
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 8
      packages/neon/neon/lib/src/blocs/apps.dart
  2. 2
      packages/neon/neon/lib/src/models/app_implementation.dart
  3. 8
      packages/neon/neon/lib/src/router.dart
  4. 44
      packages/neon/neon/lib/src/widgets/app_bar.dart
  5. 3
      packages/neon/neon_files/lib/neon_files.dart
  6. 3
      packages/neon/neon_news/lib/neon_news.dart
  7. 3
      packages/neon/neon_notes/lib/neon_notes.dart
  8. 3
      packages/neon/neon_notifications/lib/neon_notifications.dart
  9. 44
      packages/neon/neon_notifications/lib/routes.dart

8
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. /// 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. /// When using [skipAlreadySet] nothing will be done if there already is an active app.
void setActiveApp(final String appID, {final bool skipAlreadySet = false}); void setActiveApp(final String appID, {final bool skipAlreadySet = false});
/// Navigates to the active app.
void navigateActiveApp();
} }
abstract class AppsBlocStates { abstract class AppsBlocStates {
@ -228,6 +231,11 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
} }
} }
@override
void navigateActiveApp() {
_router.go(activeApp.value.initialPath);
}
T getAppBloc<T extends Bloc>(final AppImplementation<T, dynamic> appImplementation) => T getAppBloc<T extends Bloc>(final AppImplementation<T, dynamic> appImplementation) =>
appImplementation.getBloc(_account); appImplementation.getBloc(_account);

2
packages/neon/neon/lib/src/models/app_implementation.dart

@ -54,8 +54,6 @@ abstract class AppImplementation<T extends Bloc, R extends NextcloudAppOptions>
BehaviorSubject<int>? getUnreadCounter(final T bloc) => null; BehaviorSubject<int>? getUnreadCounter(final T bloc) => null;
Widget get page;
NeonNavigationDestination destination(final BuildContext context) { NeonNavigationDestination destination(final BuildContext context) {
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false); final accountsBloc = Provider.of<AccountsBloc>(context, listen: false);
final account = accountsBloc.activeAccount.value!; final account = accountsBloc.activeAccount.value!;

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

@ -2,6 +2,7 @@
import 'dart:async'; import 'dart:async';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.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/blocs/accounts.dart';
import 'package:neon/src/models/account.dart'; import 'package:neon/src/models/account.dart';
import 'package:neon/src/models/app_implementation.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/account_settings.dart';
import 'package:neon/src/pages/home.dart'; import 'package:neon/src/pages/home.dart';
import 'package:neon/src/pages/login.dart'; import 'package:neon/src/pages/login.dart';
@ -64,9 +66,13 @@ class AppRouter extends GoRouter {
}, },
routes: [ routes: [
ShellRoute( 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, builder: _mainViewBuilder,
), ),
...appImplementations.whereType<NotificationsAppInterface>().map((final a) => a.route),
...$appRoutes, ...$appRoutes,
], ],
); );

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

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:neon/l10n/localizations.dart'; import 'package:neon/l10n/localizations.dart';
import 'package:neon/src/bloc/result_builder.dart'; import 'package:neon/src/bloc/result_builder.dart';
@ -100,7 +101,6 @@ class NotificationIconButton extends StatefulWidget {
class _NotificationIconButtonState extends State<NotificationIconButton> { class _NotificationIconButtonState extends State<NotificationIconButton> {
late AccountsBloc _accountsBloc; late AccountsBloc _accountsBloc;
late AppsBloc _appsBloc; late AppsBloc _appsBloc;
late List<Account> _accounts;
late Account _account; late Account _account;
late StreamSubscription notificationSubscription; late StreamSubscription notificationSubscription;
@ -109,13 +109,12 @@ class _NotificationIconButtonState extends State<NotificationIconButton> {
super.initState(); super.initState();
_accountsBloc = Provider.of<AccountsBloc>(context, listen: false); _accountsBloc = Provider.of<AccountsBloc>(context, listen: false);
_appsBloc = _accountsBloc.activeAppsBloc; _appsBloc = _accountsBloc.activeAppsBloc;
_accounts = _accountsBloc.accounts.value;
_account = _accountsBloc.activeAccount.value!; _account = _accountsBloc.activeAccount.value!;
notificationSubscription = _appsBloc.openNotifications.listen((final _) async { notificationSubscription = _appsBloc.openNotifications.listen((final _) {
final notificationsAppImplementation = _appsBloc.notificationsAppImplementation.valueOrNull; final notificationsAppImplementation = _appsBloc.notificationsAppImplementation.valueOrNull;
if (notificationsAppImplementation != null && notificationsAppImplementation.hasData) { if (notificationsAppImplementation != null && notificationsAppImplementation.hasData) {
await _openNotifications(notificationsAppImplementation.data!); _openNotifications(notificationsAppImplementation.data!);
} }
}); });
} }
@ -127,37 +126,8 @@ class _NotificationIconButtonState extends State<NotificationIconButton> {
super.dispose(); super.dispose();
} }
// TODO: migrate to go_router with a separate page void _openNotifications(final NotificationsAppInterface app) {
Future<void> _openNotifications( GoRouter.of(context).go(app.initialPath);
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<NotificationsBlocInterface>(
create: (final context) => app.getBloc(_account),
child: page,
),
),
);
} }
@override @override
@ -177,8 +147,8 @@ class _NotificationIconButtonState extends State<NotificationIconButton> {
final unreadCount = unreadCounterSnapshot.data ?? 0; final unreadCount = unreadCounterSnapshot.data ?? 0;
return IconButton( return IconButton(
key: Key('app-${notificationsImplementationData.id}'), key: Key('app-${notificationsImplementationData.id}'),
onPressed: () async { onPressed: () {
await _openNotifications(notificationsImplementationData); _openNotifications(notificationsImplementationData);
}, },
tooltip: AppLocalizations.of(context).appImplementationName(notificationsImplementationData.id), tooltip: AppLocalizations.of(context).appImplementationName(notificationsImplementationData.id),
icon: NeonAppImplementationIcon( icon: NeonAppImplementationIcon(

3
packages/neon/neon_files/lib/neon_files.dart

@ -66,9 +66,6 @@ class FilesApp extends AppImplementation<FilesBloc, FilesAppSpecificOptions> {
platform, platform,
); );
@override
Widget get page => const FilesMainPage();
@override @override
RouteBase get route => $filesAppRoute; RouteBase get route => $filesAppRoute;
} }

3
packages/neon/neon_news/lib/neon_news.dart

@ -73,9 +73,6 @@ class NewsApp extends AppImplementation<NewsBloc, NewsAppSpecificOptions> {
account, account,
); );
@override
Widget get page => const NewsMainPage();
@override @override
RouteBase get route => $newsAppRoute; RouteBase get route => $newsAppRoute;

3
packages/neon/neon_notes/lib/neon_notes.dart

@ -63,9 +63,6 @@ class NotesApp extends AppImplementation<NotesBloc, NotesAppSpecificOptions> {
account, account,
); );
@override
Widget get page => const NotesMainPage();
@override @override
RouteBase get route => $notesAppRoute; RouteBase get route => $notesAppRoute;
} }

3
packages/neon/neon_notifications/lib/neon_notifications.dart

@ -43,9 +43,6 @@ class NotificationsApp extends AppImplementation<NotificationsBloc, Notification
account, account,
); );
@override
Widget get page => const NotificationsMainPage();
@override @override
RouteBase get route => $notificationsAppRoute; RouteBase get route => $notificationsAppRoute;

44
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:go_router/go_router.dart';
import 'package:neon/blocs.dart';
import 'package:neon/l10n/localizations.dart';
import 'package:neon/models.dart'; import 'package:neon/models.dart';
import 'package:neon/utils.dart'; import 'package:neon/utils.dart';
import 'package:neon_notifications/neon_notifications.dart'; import 'package:neon_notifications/neon_notifications.dart';
import 'package:provider/provider.dart';
part 'routes.g.dart'; part 'routes.g.dart';
@ -15,5 +18,42 @@ class NotificationsAppRoute extends NeonAppRoute {
const NotificationsAppRoute(); const NotificationsAppRoute();
@override @override
Widget build(final BuildContext context, final GoRouterState state) => const NotificationsMainPage(); Widget build(final BuildContext context, final GoRouterState state) {
final accountsBloc = Provider.of<AccountsBloc>(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<NotificationsBlocInterface>(
create: (final context) => app.getBloc(account),
child: page,
);
}
} }

Loading…
Cancel
Save