Browse Source

Merge pull request #342 from Leptopoda/fix/go_router

Fix/go router
pull/345/head
Nikolas Rimikis 2 years ago committed by GitHub
parent
commit
5af28337b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      packages/neon/neon/lib/src/pages/account_settings.dart
  2. 2
      packages/neon/neon/lib/src/pages/login.dart
  3. 2
      packages/neon/neon/lib/src/pages/settings.dart
  4. 25
      packages/neon/neon/lib/src/router.dart
  5. 23
      packages/neon/neon/lib/src/router.g.dart

10
packages/neon/neon/lib/src/pages/account_settings.dart

@ -28,7 +28,17 @@ class AccountSettingsPage extends StatelessWidget {
// ignore: use_build_context_synchronously
AppLocalizations.of(context).accountOptionsRemoveConfirm(account.client.humanReadableID),
)) {
final isActive = bloc.activeAccount.value == account;
bloc.removeAccount(account);
if (isActive) {
// ignore: use_build_context_synchronously
const HomeRoute().go(context);
} else {
// ignore: use_build_context_synchronously
Navigator.of(context).pop();
}
}
},
tooltip: AppLocalizations.of(context).accountOptionsRemove,

2
packages/neon/neon/lib/src/pages/login.dart

@ -84,6 +84,8 @@ class _LoginPageState extends State<LoginPage> {
..addAccount(account)
..setActiveAccount(account);
}
// ignore: use_build_context_synchronously
const HomeRoute().go(context);
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());

2
packages/neon/neon/lib/src/pages/settings.dart

@ -183,7 +183,7 @@ class _SettingsPageState extends State<SettingsPage> {
CustomSettingsTile(
title: ElevatedButton.icon(
onPressed: () {
const LoginRoute().go(context);
const AddAccountRoute().go(context);
},
icon: const Icon(MdiIcons.accountPlus),
label: Text(AppLocalizations.of(context).globalOptionsAccountsAdd),

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

@ -1,5 +1,6 @@
// ignore: prefer_mixin
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';
import 'package:neon/neon.dart';
import 'package:provider/provider.dart';
@ -11,20 +12,18 @@ class AppRouter extends GoRouter {
required final GlobalKey<NavigatorState> navigatorKey,
required final AccountsBloc accountsBloc,
}) : super(
debugLogDiagnostics: kDebugMode,
refreshListenable: StreamListenable.behaviorSubject(accountsBloc.activeAccount),
navigatorKey: navigatorKey,
initialLocation: const HomeRoute().location,
redirect: (final context, final state) {
final account = accountsBloc.activeAccount.valueOrNull;
// redirect to loginscreen when no account is logged in
if (account == null) {
return const LoginRoute().location;
}
if (state.location == const LoginRoute().location) {
return const HomeRoute().location;
}
return null;
},
routes: $appRoutes,
@ -60,9 +59,13 @@ class AccountSettingsRoute extends GoRouteData {
name: 'Settings',
routes: [
TypedGoRoute<NextcloudAppSettingsRoute>(
path: ':appid',
path: 'apps/:appid',
name: 'NextcloudAppSettings',
),
TypedGoRoute<AddAccountRoute>(
path: 'account/add',
name: 'addAccount',
),
TypedGoRoute<AccountSettingsRoute>(
path: 'account/:accountid',
name: 'AccountSettings',
@ -98,6 +101,14 @@ class LoginRoute extends GoRouteData {
Widget build(final BuildContext context, final GoRouterState state) => LoginPage(serverURL: server);
}
@immutable
class AddAccountRoute extends GoRouteData {
const AddAccountRoute();
@override
Widget build(final BuildContext context, final GoRouterState state) => const LoginPage();
}
@immutable
class NextcloudAppSettingsRoute extends GoRouteData {
const NextcloudAppSettingsRoute({
@ -109,7 +120,7 @@ class NextcloudAppSettingsRoute extends GoRouteData {
@override
Widget build(final BuildContext context, final GoRouterState state) {
final appImplementations = Provider.of<List<AppImplementation>>(context, listen: false);
final appImplementation = appImplementations.firstWhere((final app) => app.id == appid);
final appImplementation = appImplementations.find(appid)!;
return NextcloudAppSettingsPage(appImplementation: appImplementation);
}

23
packages/neon/neon/lib/src/router.g.dart

@ -22,10 +22,15 @@ RouteBase get $homeRoute => GoRouteData.$route(
factory: $SettingsRouteExtension._fromState,
routes: [
GoRouteData.$route(
path: ':appid',
path: 'apps/:appid',
name: 'NextcloudAppSettings',
factory: $NextcloudAppSettingsRouteExtension._fromState,
),
GoRouteData.$route(
path: 'account/add',
name: 'addAccount',
factory: $AddAccountRouteExtension._fromState,
),
GoRouteData.$route(
path: 'account/:accountid',
name: 'AccountSettings',
@ -70,7 +75,21 @@ extension $NextcloudAppSettingsRouteExtension on NextcloudAppSettingsRoute {
);
String get location => GoRouteData.$location(
'/settings/${Uri.encodeComponent(appid)}',
'/settings/apps/${Uri.encodeComponent(appid)}',
);
void go(BuildContext context) => context.go(location);
Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) => context.pushReplacement(location);
}
extension $AddAccountRouteExtension on AddAccountRoute {
static AddAccountRoute _fromState(GoRouterState state) => const AddAccountRoute();
String get location => GoRouteData.$location(
'/settings/account/add',
);
void go(BuildContext context) => context.go(location);

Loading…
Cancel
Save