Browse Source

Merge pull request #338 from Leptopoda/feature/routing

Feature/routing
pull/345/head
Nikolas Rimikis 2 years ago committed by GitHub
parent
commit
d47461f445
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      packages/app/pubspec.lock
  2. 3
      packages/neon/neon/lib/neon.dart
  3. 2
      packages/neon/neon/lib/src/app.dart
  4. 2
      packages/neon/neon/lib/src/pages/account_settings.dart
  5. 79
      packages/neon/neon/lib/src/pages/home.dart
  6. 1
      packages/neon/neon/lib/src/pages/login.dart
  7. 29
      packages/neon/neon/lib/src/pages/settings.dart
  8. 137
      packages/neon/neon/lib/src/router.dart
  9. 122
      packages/neon/neon/lib/src/router.g.dart
  10. 8
      packages/neon/neon/lib/src/utils/global_popups.dart
  11. 38
      packages/neon/neon/lib/src/utils/stream_listenable.dart
  12. 14
      packages/neon/neon/lib/src/widgets/exception.dart
  13. 4
      packages/neon/neon/pubspec.yaml

16
packages/app/pubspec.lock

@ -317,6 +317,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
go_router:
dependency: transitive
description:
name: go_router
sha256: "00d1b67d6e9fa443331da229084dd3eb04407f5a2dff22940bd7bba6af5722c3"
url: "https://pub.dev"
source: hosted
version: "7.1.1"
html:
dependency: transitive
description:
@ -434,6 +442,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.2"
logging:
dependency: transitive
description:
name: logging
sha256: "04094f2eb032cbb06c6f6e8d3607edcfcb0455e2bb6cbc010cb01171dcb64e6d"
url: "https://pub.dev"
source: hosted
version: "1.1.1"
markdown:
dependency: transitive
description:

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

@ -23,6 +23,7 @@ import 'package:material_design_icons_flutter/material_design_icons_flutter.dart
import 'package:neon/l10n/localizations.dart';
import 'package:neon/src/models/account.dart';
import 'package:neon/src/models/push_notification.dart';
import 'package:neon/src/router.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path/path.dart' as p;
@ -67,7 +68,6 @@ part 'src/platform/abstract.dart';
part 'src/platform/android.dart';
part 'src/platform/linux.dart';
part 'src/platform/platform.dart';
part 'src/router.dart';
part 'src/utils/account_options.dart';
part 'src/utils/app_implementation.dart';
part 'src/utils/bloc.dart';
@ -89,6 +89,7 @@ part 'src/utils/settings_export_helper.dart';
part 'src/utils/sort_box_builder.dart';
part 'src/utils/sort_box_order_option_values.dart';
part 'src/utils/storage.dart';
part 'src/utils/stream_listenable.dart';
part 'src/utils/theme.dart';
part 'src/utils/validators.dart';
part 'src/widgets/account_avatar.dart';

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

@ -295,7 +295,7 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
keepOriginalAccentColor: nextcloudTheme == null || (themeKeepOriginalAccentColor ?? false),
oledAsDark: themeOLEDAsDark,
),
routerDelegate: _routerDelegate,
routerConfig: _routerDelegate,
);
},
);

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

@ -29,8 +29,6 @@ class AccountSettingsPage extends StatelessWidget {
AppLocalizations.of(context).accountOptionsRemoveConfirm(account.client.humanReadableID),
)) {
bloc.removeAccount(account);
// ignore: use_build_context_synchronously
Navigator.of(context).pop();
}
},
tooltip: AppLocalizations.of(context).accountOptionsRemove,

79
packages/neon/neon/lib/src/pages/home.dart

@ -4,12 +4,9 @@ const kQuickBarWidth = kAvatarSize + 20;
class HomePage extends StatefulWidget {
const HomePage({
required this.account,
super.key,
});
final Account account;
@override
State<HomePage> createState() => _HomePageState();
}
@ -19,6 +16,7 @@ class _HomePageState extends State<HomePage> {
final _scaffoldKey = GlobalKey<ScaffoldState>();
final drawerScrollController = ScrollController();
late Account _account;
late GlobalOptions _globalOptions;
late AccountsBloc _accountsBloc;
late AppsBloc _appsBloc;
@ -27,11 +25,11 @@ class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
_globalOptions = Provider.of<GlobalOptions>(context, listen: false);
_accountsBloc = Provider.of<AccountsBloc>(context, listen: false);
_appsBloc = _accountsBloc.getAppsBloc(widget.account);
_capabilitiesBloc = _accountsBloc.getCapabilitiesBloc(widget.account);
_account = _accountsBloc.activeAccount.value!;
_appsBloc = _accountsBloc.getAppsBloc(_account);
_capabilitiesBloc = _accountsBloc.getCapabilitiesBloc(_account);
_appsBloc.openNotifications.listen((final _) async {
final notificationsAppImplementation = _appsBloc.notificationsAppImplementation.valueOrNull;
@ -61,9 +59,9 @@ class _HomePageState extends State<HomePage> {
]) {
try {
final (supported, _) = switch (id) {
'core' => await widget.account.client.core.isSupported(result.data),
'news' => await widget.account.client.news.isSupported(),
'notes' => await widget.account.client.notes.isSupported(result.data),
'core' => await _account.client.core.isSupported(result.data),
'news' => await _account.client.news.isSupported(),
'notes' => await _account.client.notes.isSupported(result.data),
_ => (true, null),
};
if (supported || !mounted) {
@ -92,7 +90,7 @@ class _HomePageState extends State<HomePage> {
Future _checkMaintenanceMode() async {
try {
final status = await widget.account.client.core.getStatus();
final status = await _account.client.core.getStatus();
if (status.maintenance) {
if (!mounted) {
return;
@ -129,14 +127,6 @@ class _HomePageState extends State<HomePage> {
);
}
Future _openSettings() async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (final context) => const SettingsPage(),
),
);
}
Future _openNotifications(
final NotificationsAppInterface app,
final List<Account> accounts,
@ -207,10 +197,11 @@ class _HomePageState extends State<HomePage> {
builder: (final context) {
if (accountsSnapshot.hasData) {
final accounts = accountsSnapshot.data!;
final account = accounts.find(widget.account.id)!;
final account = accounts.find(_account.id);
if (account != null) {
final isQuickBar = navigationMode == NavigationMode.quickBar;
final drawer = Drawer(
final drawer = Builder(
builder: (final context) => Drawer(
width: isQuickBar ? kQuickBarWidth : null,
child: Container(
padding: isQuickBar ? const EdgeInsets.all(5) : null,
@ -272,15 +263,18 @@ class _HomePageState extends State<HomePage> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (capabilities.data != null) ...[
if (capabilities.data!.capabilities.theming?.name != null) ...[
if (capabilities.data!.capabilities.theming?.name !=
null) ...[
Text(
capabilities.data!.capabilities.theming!.name!,
style: DefaultTextStyle.of(context).style.copyWith(
color: Theme.of(context).appBarTheme.foregroundColor,
color:
Theme.of(context).appBarTheme.foregroundColor,
),
),
],
if (capabilities.data!.capabilities.theming?.logo != null) ...[
if (capabilities.data!.capabilities.theming?.logo !=
null) ...[
Flexible(
child: NeonCachedUrlImage(
url: capabilities.data!.capabilities.theming!.logo!,
@ -303,7 +297,7 @@ class _HomePageState extends State<HomePage> {
dropdownColor: Theme.of(context).colorScheme.primary,
iconEnabledColor:
Theme.of(context).colorScheme.onBackground,
value: widget.account.id,
value: _account.id,
items: accounts
.map<DropdownMenuItem<String>>(
(final account) => DropdownMenuItem<String>(
@ -311,8 +305,9 @@ class _HomePageState extends State<HomePage> {
child: NeonAccountTile(
account: account,
dense: true,
textColor:
Theme.of(context).appBarTheme.foregroundColor,
textColor: Theme.of(context)
.appBarTheme
.foregroundColor,
),
),
)
@ -382,13 +377,11 @@ class _HomePageState extends State<HomePage> {
minLeadingWidth: 0,
onTap: () async {
await _appsBloc.setActiveApp(appImplementation.id);
if (navigationMode == NavigationMode.drawer) {
// Don't pop when the drawer is always shown
if (!mounted) {
return;
}
Navigator.of(context).pop();
}
Scaffold.maybeOf(context)?.closeDrawer();
},
);
},
@ -401,7 +394,7 @@ class _HomePageState extends State<HomePage> {
),
if (isQuickBar) ...[
IconButton(
onPressed: _openSettings,
onPressed: () => const SettingsRoute().go(context),
tooltip: AppLocalizations.of(context).settings,
icon: Icon(
Icons.settings,
@ -415,16 +408,15 @@ class _HomePageState extends State<HomePage> {
leading: const Icon(Icons.settings),
minLeadingWidth: 0,
onTap: () async {
if (navigationMode == NavigationMode.drawer) {
Navigator.of(context).pop();
}
await _openSettings();
Scaffold.maybeOf(context)?.closeDrawer();
const SettingsRoute().go(context);
},
),
],
],
),
),
),
);
return Row(
@ -526,15 +518,8 @@ class _HomePageState extends State<HomePage> {
),
],
IconButton(
onPressed: () async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (final context) => AccountSettingsPage(
bloc: _accountsBloc,
account: account,
),
),
);
onPressed: () {
AccountSettingsRoute(accountid: account.id).go(context);
},
tooltip: AppLocalizations.of(context).settingsAccount,
icon: IntrinsicWidth(
@ -558,7 +543,8 @@ class _HomePageState extends State<HomePage> {
Expanded(
child: Center(
child: Text(
AppLocalizations.of(context).errorNoCompatibleNextcloudAppsFound,
AppLocalizations.of(context)
.errorNoCompatibleNextcloudAppsFound,
textAlign: TextAlign.center,
),
),
@ -583,6 +569,7 @@ class _HomePageState extends State<HomePage> {
],
);
}
}
return Container();
},
),

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

@ -73,7 +73,6 @@ class _LoginPageState extends State<LoginPage> {
if (widget.serverURL != null) {
_accountsBloc.updateAccount(account);
Navigator.of(context).pop();
} else {
final existingAccount = _accountsBloc.accounts.value.find(account.id);
if (existingAccount != null) {

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

@ -80,14 +80,8 @@ class _SettingsPageState extends State<SettingsPage> {
CustomSettingsTile(
leading: appImplementation.buildIcon(context),
title: Text(appImplementation.name(context)),
onTap: () async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (final context) => NextcloudAppSettingsPage(
appImplementation: appImplementation,
),
),
);
onTap: () {
NextcloudAppSettingsRoute(appid: appImplementation.id).go(context);
},
),
],
@ -181,26 +175,15 @@ class _SettingsPageState extends State<SettingsPage> {
for (final account in accountsSnapshot.data!) ...[
NeonAccountSettingsTile(
account: account,
onTap: () async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (final context) => AccountSettingsPage(
bloc: accountsBloc,
account: account,
),
),
);
onTap: () {
AccountSettingsRoute(accountid: account.id).go(context);
},
),
],
CustomSettingsTile(
title: ElevatedButton.icon(
onPressed: () async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (final context) => const LoginPage(),
),
);
onPressed: () {
const LoginRoute().go(context);
},
icon: const Icon(MdiIcons.accountPlus),
label: Text(AppLocalizations.of(context).globalOptionsAccountsAdd),

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

@ -1,41 +1,124 @@
part of '../neon.dart';
// ignore: prefer_mixin
class AppRouter extends RouterDelegate<Account> with ChangeNotifier, PopNavigatorRouterDelegateMixin<Account> {
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:neon/neon.dart';
import 'package:provider/provider.dart';
part 'router.g.dart';
class AppRouter extends GoRouter {
AppRouter({
required this.navigatorKey,
required this.accountsBloc,
});
required final GlobalKey<NavigatorState> navigatorKey,
required final AccountsBloc accountsBloc,
}) : super(
refreshListenable: StreamListenable.behaviorSubject(accountsBloc.activeAccount),
navigatorKey: navigatorKey,
initialLocation: const HomeRoute().location,
redirect: (final context, final state) {
final account = accountsBloc.activeAccount.valueOrNull;
final AccountsBloc accountsBloc;
if (account == null) {
return const LoginRoute().location;
}
@override
final GlobalKey<NavigatorState> navigatorKey;
if (state.location == const LoginRoute().location) {
return const HomeRoute().location;
}
@override
Future setNewRoutePath(final Account? configuration) async {}
return null;
},
routes: $appRoutes,
);
}
@override
Account? get currentConfiguration => accountsBloc.activeAccount.valueOrNull;
@immutable
class AccountSettingsRoute extends GoRouteData {
const AccountSettingsRoute({
required this.accountid,
});
final String accountid;
@override
Widget build(final BuildContext context) => Navigator(
key: navigatorKey,
onPopPage: (final route, final result) => route.didPop(result),
pages: [
if (currentConfiguration == null) ...[
const MaterialPage(
child: LoginPage(),
),
] else ...[
MaterialPage(
Widget build(final BuildContext context, final GoRouterState state) {
final bloc = Provider.of<AccountsBloc>(context, listen: false);
final account = bloc.accounts.value.find(accountid)!;
return AccountSettingsPage(
bloc: bloc,
account: account,
);
}
}
@TypedGoRoute<HomeRoute>(
path: '/',
name: 'home',
child: HomePage(
key: Key(currentConfiguration!.id),
account: currentConfiguration!,
routes: [
TypedGoRoute<SettingsRoute>(
path: 'settings',
name: 'Settings',
routes: [
TypedGoRoute<NextcloudAppSettingsRoute>(
path: ':appid',
name: 'NextcloudAppSettings',
),
TypedGoRoute<AccountSettingsRoute>(
path: 'account/:accountid',
name: 'AccountSettings',
),
],
)
],
);
)
@immutable
class HomeRoute extends GoRouteData {
const HomeRoute();
@override
Widget build(final BuildContext context, final GoRouterState state) {
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false);
final account = accountsBloc.activeAccount.valueOrNull!;
return HomePage(key: Key(account.id));
}
}
@TypedGoRoute<LoginRoute>(
path: '/login',
name: 'login',
)
@immutable
class LoginRoute extends GoRouteData {
const LoginRoute({this.server});
final String? server;
@override
Widget build(final BuildContext context, final GoRouterState state) => LoginPage(serverURL: server);
}
@immutable
class NextcloudAppSettingsRoute extends GoRouteData {
const NextcloudAppSettingsRoute({
required this.appid,
});
final String appid;
@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);
return NextcloudAppSettingsPage(appImplementation: appImplementation);
}
}
@immutable
class SettingsRoute extends GoRouteData {
const SettingsRoute();
@override
Widget build(final BuildContext context, final GoRouterState state) => const SettingsPage();
}

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

@ -0,0 +1,122 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'router.dart';
// **************************************************************************
// GoRouterGenerator
// **************************************************************************
List<RouteBase> get $appRoutes => [
$homeRoute,
$loginRoute,
];
RouteBase get $homeRoute => GoRouteData.$route(
path: '/',
name: 'home',
factory: $HomeRouteExtension._fromState,
routes: [
GoRouteData.$route(
path: 'settings',
name: 'Settings',
factory: $SettingsRouteExtension._fromState,
routes: [
GoRouteData.$route(
path: ':appid',
name: 'NextcloudAppSettings',
factory: $NextcloudAppSettingsRouteExtension._fromState,
),
GoRouteData.$route(
path: 'account/:accountid',
name: 'AccountSettings',
factory: $AccountSettingsRouteExtension._fromState,
),
],
),
],
);
extension $HomeRouteExtension on HomeRoute {
static HomeRoute _fromState(GoRouterState state) => const HomeRoute();
String get location => GoRouteData.$location(
'/',
);
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 $SettingsRouteExtension on SettingsRoute {
static SettingsRoute _fromState(GoRouterState state) => const SettingsRoute();
String get location => GoRouteData.$location(
'/settings',
);
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 $NextcloudAppSettingsRouteExtension on NextcloudAppSettingsRoute {
static NextcloudAppSettingsRoute _fromState(GoRouterState state) => NextcloudAppSettingsRoute(
appid: state.pathParameters['appid']!,
);
String get location => GoRouteData.$location(
'/settings/${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 $AccountSettingsRouteExtension on AccountSettingsRoute {
static AccountSettingsRoute _fromState(GoRouterState state) => AccountSettingsRoute(
accountid: state.pathParameters['accountid']!,
);
String get location => GoRouteData.$location(
'/settings/account/${Uri.encodeComponent(accountid)}',
);
void go(BuildContext context) => context.go(location);
Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) => context.pushReplacement(location);
}
RouteBase get $loginRoute => GoRouteData.$route(
path: '/login',
name: 'login',
factory: $LoginRouteExtension._fromState,
);
extension $LoginRouteExtension on LoginRoute {
static LoginRoute _fromState(GoRouterState state) => LoginRoute(
server: state.queryParameters['server'],
);
String get location => GoRouteData.$location(
'/login',
queryParams: {
if (server != null) 'server': server,
},
);
void go(BuildContext context) => context.go(location);
Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) => context.pushReplacement(location);
}

8
packages/neon/neon/lib/src/utils/global_popups.dart

@ -27,12 +27,8 @@ class GlobalPopups {
content: Text(AppLocalizations.of(context).firstLaunchGoToSettingsToEnablePushNotifications),
action: SnackBarAction(
label: AppLocalizations.of(context).settings,
onPressed: () async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (final context) => const SettingsPage(),
),
);
onPressed: () {
const SettingsRoute().go(context);
},
),
),

38
packages/neon/neon/lib/src/utils/stream_listenable.dart

@ -0,0 +1,38 @@
part of '../../neon.dart';
/// Listenable Stream
///
/// A class that implements [Listenable] for a stream.
/// Objects need to be manually disposed.
class StreamListenable extends ChangeNotifier {
/// Listenable Stream
///
/// Implementation for all types of [Stream]s.
/// For an implementation tailored towards [BehaviorSubject] have a look at [StreamListenable.behaviorSubject].
StreamListenable(final Stream<dynamic> stream) {
notifyListeners();
_subscription = stream.asBroadcastStream().listen((final value) {
notifyListeners();
});
}
/// Listenable BehaviorSubject
///
/// Implementation for a [BehaviorSubject]. It ensures to not unececcary notify listeners.
/// For an implementation tailored towards otnher kinds of [Stream] have a look at [StreamListenable].
StreamListenable.behaviorSubject(final BehaviorSubject<dynamic> subject) {
_subscription = subject.listen((final value) {
notifyListeners();
});
}
late final StreamSubscription<dynamic> _subscription;
@override
void dispose() {
unawaited(_subscription.cancel());
super.dispose();
}
}

14
packages/neon/neon/lib/src/widgets/exception.dart

@ -62,7 +62,7 @@ class NeonException extends StatelessWidget {
: AppLocalizations.of(context).actionRetry,
onPressed: () async {
if (details.isUnauthorized) {
await _openLoginPage(context);
_openLoginPage(context);
} else {
onRetry();
}
@ -177,14 +177,10 @@ class NeonException extends StatelessWidget {
);
}
static Future _openLoginPage(final BuildContext context) async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (final context) => LoginPage(
serverURL: Provider.of<AccountsBloc>(context, listen: false).activeAccount.value!.serverURL,
),
),
);
static void _openLoginPage(final BuildContext context) {
LoginRoute(
server: Provider.of<AccountsBloc>(context, listen: false).activeAccount.value!.serverURL,
).go(context);
}
}

4
packages/neon/neon/pubspec.yaml

@ -21,6 +21,7 @@ dependencies:
sdk: flutter
flutter_native_splash: ^2.2.19
flutter_svg: ^2.0.5
go_router: ^7.1.1
http: ^0.13.6
intl: ^0.18.0
json_annotation: ^4.8.1
@ -56,7 +57,8 @@ dependencies:
xml: ^6.3.0
dev_dependencies:
build_runner: ^2.4.2
build_runner: ^2.4.4
go_router_builder: ^2.0.1
json_serializable: ^6.6.2
nit_picking:
git:

Loading…
Cancel
Save