Browse Source

feat(neon): give add relationship between AppsBloc and AppRouter

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
feature/nested-router
Nikolas Rimikis 1 year ago
parent
commit
adab3c1722
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 10
      packages/neon/neon/lib/src/app.dart
  2. 7
      packages/neon/neon/lib/src/blocs/accounts.dart
  3. 3
      packages/neon/neon/lib/src/blocs/apps.dart
  4. 8
      packages/neon/neon/lib/src/models/app_implementation.dart
  5. 18
      packages/neon/neon/lib/src/router.dart

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

@ -42,15 +42,12 @@ class NeonApp extends StatefulWidget {
// ignore: prefer_mixin
class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.TrayListener, WindowListener {
final _appRegex = RegExp(r'^app_([a-z]+)$', multiLine: true);
final _navigatorKey = GlobalKey<NavigatorState>();
late final GlobalKey<NavigatorState> _navigatorKey;
late final Iterable<AppImplementation> _appImplementations;
late final NeonPlatform _platform;
late final GlobalOptions _globalOptions;
late final AccountsBloc _accountsBloc;
late final _routerDelegate = AppRouter(
navigatorKey: _navigatorKey,
accountsBloc: _accountsBloc,
);
late final AppRouter _routerDelegate;
Rect? _lastBounds;
@ -63,6 +60,9 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
_globalOptions = Provider.of<GlobalOptions>(context, listen: false);
_accountsBloc = Provider.of<AccountsBloc>(context, listen: false);
_routerDelegate = _accountsBloc.router;
_navigatorKey = _routerDelegate.navigatorKey;
WidgetsBinding.instance.addObserver(this);
if (_platform.canUseSystemTray) {
tray.trayManager.addListener(this);

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

@ -10,6 +10,7 @@ import 'package:neon/src/blocs/user_statuses.dart';
import 'package:neon/src/models/account.dart';
import 'package:neon/src/models/app_implementation.dart';
import 'package:neon/src/platform/platform.dart';
import 'package:neon/src/router.dart';
import 'package:neon/src/settings/models/storage.dart';
import 'package:neon/src/utils/account_options.dart';
import 'package:neon/src/utils/global_options.dart';
@ -63,6 +64,10 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
this._globalOptions,
this._allAppImplementations,
) {
router = AppRouter(
accountsBloc: this,
appImplementations: _allAppImplementations,
);
accounts
..add(loadAccounts(_storage))
..listen((final as) async {
@ -105,6 +110,7 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
final SharedPreferences _sharedPreferences;
final GlobalOptions _globalOptions;
final Iterable<AppImplementation> _allAppImplementations;
late final AppRouter router;
final _keyLastUsedAccount = 'last-used-account';
final _accountsOptions = <String, AccountSpecificOptions>{};
@ -239,6 +245,7 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
this,
account,
_allAppImplementations,
router,
);
}

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

@ -10,6 +10,7 @@ import 'package:neon/src/models/account.dart';
import 'package:neon/src/models/app_ids.dart';
import 'package:neon/src/models/app_implementation.dart';
import 'package:neon/src/models/notifications_interface.dart';
import 'package:neon/src/router.dart';
import 'package:neon/src/settings/models/nextcloud_app_options.dart';
import 'package:neon/src/utils/request_manager.dart';
import 'package:nextcloud/nextcloud.dart';
@ -46,6 +47,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
this._accountsBloc,
this._account,
this._allAppImplementations,
this._router,
) {
apps.listen((final result) {
appImplementations
@ -154,6 +156,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
final AccountsBloc _accountsBloc;
final Account _account;
final Iterable<AppImplementation> _allAppImplementations;
final AppRouter _router;
@override
void dispose() {

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

@ -80,17 +80,17 @@ abstract class AppImplementation<T extends Bloc, R extends NextcloudAppOptions>
/// Route for the app.
///
/// All pages of the app must be specified as subroutes.
/// If this is not [GoRoute] an inital route name must be specified by overriding [initialRouteName].
/// If this is not a [GoRoute] an inital path must be specified by overriding [initialPath].
RouteBase get route;
/// Name of the initial route for this app.
///
/// Subclasses that don't provide a [GoRoute] for [route] must override this.
String get initialRouteName {
String get initialPath {
final route = this.route;
if (route is GoRoute && route.name != null) {
return route.name!;
if (route is GoRoute) {
return route.path;
}
throw FlutterError('No name for the initial route provided.');

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

@ -25,9 +25,21 @@ part 'router.g.dart';
@internal
class AppRouter extends GoRouter {
AppRouter({
required final GlobalKey<NavigatorState> navigatorKey,
factory AppRouter({
required final AccountsBloc accountsBloc,
required final Iterable<AppImplementation> appImplementations,
final GlobalKey<NavigatorState>? navigatorKey,
}) =>
AppRouter._(
navigatorKey: navigatorKey ?? GlobalKey<NavigatorState>(),
accountsBloc: accountsBloc,
appImplementations: appImplementations,
);
AppRouter._({
required this.navigatorKey,
required final AccountsBloc accountsBloc,
required final Iterable<AppImplementation> appImplementations,
}) : super(
debugLogDiagnostics: kDebugMode,
refreshListenable: StreamListenable(accountsBloc.activeAccount),
@ -52,6 +64,8 @@ class AppRouter extends GoRouter {
},
routes: $appRoutes,
);
final GlobalKey<NavigatorState> navigatorKey;
}
@immutable

Loading…
Cancel
Save