Skip to content

Commit

Permalink
refactor: goFurter args
Browse files Browse the repository at this point in the history
  • Loading branch information
nesquikm committed Oct 26, 2023
1 parent 0efff69 commit 2654e47
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 113 deletions.
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ linter:
rules:
public_member_api_docs: false
analyzer:
exclude:
- lib/**/*.g.dart
plugins:
- custom_lint
21 changes: 0 additions & 21 deletions lib/app/router/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,5 @@ GoRouter getRouter(RouterPersistence routerPersistence) {
}
});

// final saveLocation = router.routerDelegate.currentConfiguration.last.route is GoRoute &&

// final currentConfiguration = router.routerDelegate.currentConfiguration;
// final routes = currentConfiguration.matches
// .where((match) => match.route is GoRoute)
// .map((match) => match.route as GoRoute);

// final saveLocation = routes.every(
// (route) => AppRoute.getByPath(route.path)?.saveLocation ?? false,
// );

// if (saveLocation) {
// WidgetsBinding.instance.addPostFrameCallback((_) {
// routerPersistence.setState(
// RouterPersistenceState(
// fullPath: currentConfiguration.fullPath,
// ),
// );
// });
// }

return router;
}
91 changes: 5 additions & 86 deletions lib/app/router/routes/app_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,90 +43,8 @@ enum AppRoute {
(e) => e.path == path,
);
}

// /// Returns path segments from [fullPath]. It's a wrapper for
// /// [Uri.pathSegments], but for the first segment adds slash.
// /// Also it filters and joins segments that contains parameters.
// /// We are strongly recommend to use this method instead
// /// of [Uri.pathSegments].
// static List<String> pathSegments({required String fullPath}) {
// final uri = Uri.parse(fullPath);
// if (uri.pathSegments.isEmpty) return [];

// final segments = [...uri.pathSegments]
// ..replaceRange(0, 1, ['/${uri.pathSegments.first}']);

// final filteredSegments =
// segments.fold(<String>[], (previousValue, element) {
// if (element.startsWith(':')) {
// if (previousValue.isEmpty) {
// // Don't know what to do with this case
// return [element];
// }
// final [...rest, last] = previousValue;

// return [...rest, '$last/$element'];
// }

// return [...previousValue, element];
// });

// return filteredSegments
// .where((segment) => AppRoute.getByPath(segment) != null)
// .toList();
// }
}

// /// Get first segment from [fullPath].
// String getRootPath({required String fullPath}) {
// final segments = AppRoute.pathSegments(fullPath: fullPath);
// if (segments.isEmpty) {
// AppRoute._log.severe('getRootPath: no root location found');

// return AppRoute.defaultRoute.path;
// }

// return segments.first;
// }

// /// Get last segment from [fullPath].
// String getCurrentPath({required String? fullPath}) {
// if (fullPath == null) {
// return AppRoute.defaultRoute.path;
// }

// final segments = AppRoute.pathSegments(fullPath: fullPath);
// if (segments.isEmpty) {
// AppRoute._log.severe('getCurrentPath: no current location found');

// return AppRoute.defaultRoute.path;
// }

// return segments.last;
// }

// /// Get first segment from [fullPath] and return [AppRoute].
// AppRoute getRootAppRoute({required String fullPath}) {
// return AppRoute.getByPath(getRootPath(fullPath: fullPath)) ??
// AppRoute.defaultRoute;
// }

// /// Get last segment from [fullPath] and return [AppRoute].
// AppRoute getCurrentAppRoute({required String? fullPath}) {
// return fullPath != null
// ? AppRoute.getByPath(getCurrentPath(fullPath: fullPath)) ??
// AppRoute.getByPath(fullPath) ??
// AppRoute.defaultRoute
// : AppRoute.defaultRoute;
// }

// /// Returns true, if every segment from [fullPath] can be saved in
// /// NavigationService.
// bool canSaveLocation({required String fullPath}) {
// return AppRoute.pathSegments(fullPath: fullPath)
// .every((segment) => AppRoute.getByPath(segment)?.isSaveLocation ?? false);
// }

extension NavigationHelper on WidgetRef {
/// Navigate to a location above current.
/// Instead of:
Expand All @@ -141,7 +59,7 @@ extension NavigationHelper on WidgetRef {
/// onPressed: () => context.goFurther(AppRoute.multiuse.path),
/// ```
void goFurther(
String location, {
AppRoute appRoute, {
bool preserveQueryParams = false,
}) {
if (!context.mounted) return;
Expand All @@ -153,7 +71,7 @@ extension NavigationHelper on WidgetRef {
// We have query params in old path that we should preserve, so we must
// update it manually
if (resultLocation.hasQuery && preserveQueryParams) {
final newLocation = Uri.parse(location);
final newLocation = Uri.parse(appRoute.path);
final query = <String, dynamic>{}
..addAll(resultLocation.queryParameters)
..addAll(newLocation.queryParameters);
Expand All @@ -164,8 +82,9 @@ extension NavigationHelper on WidgetRef {
);
} else {
// old location do not have query, new one may have it, we dont care
resultLocation =
resultLocation.replace(path: '${resultLocation.path}/$location');
resultLocation = resultLocation.replace(
path: '${resultLocation.path}/${appRoute.path}',
);
}

return GoRouter.of(context).go(
Expand Down
4 changes: 2 additions & 2 deletions lib/features/library/page/library_authors_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class _LibraryAuthorsPageState extends ConsumerState<LibraryAuthorsPage> {
const Text('LibraryAuthors'),
TextButton(
onPressed: () {
ref.goFurther(AppRoute.reader.path);
ref.goFurther(AppRoute.reader);
},
child: const Text('Reader'),
),
TextButton(
onPressed: () {
ref.goFurther(AppRoute.settings.path);
ref.goFurther(AppRoute.settings);
},
child: const Text('Settings'),
),
Expand Down
4 changes: 2 additions & 2 deletions lib/features/library/page/library_collections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class _LibraryCollectionsPageState
const Text('LibraryCollections'),
TextButton(
onPressed: () {
ref.goFurther(AppRoute.reader.path);
ref.goFurther(AppRoute.reader);
},
child: const Text('Reader'),
),
TextButton(
onPressed: () {
ref.goFurther(AppRoute.settings.path);
ref.goFurther(AppRoute.settings);
},
child: const Text('Settings'),
),
Expand Down
4 changes: 2 additions & 2 deletions lib/features/library/page/library_tomes_pages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class _LibraryTomesPageState extends ConsumerState<LibraryTomesPage> {
const Text('LibraryTomes'),
TextButton(
onPressed: () {
ref.goFurther(AppRoute.reader.path);
ref.goFurther(AppRoute.reader);
},
child: const Text('Reader'),
),
TextButton(
onPressed: () {
ref.goFurther(AppRoute.settings.path);
ref.goFurther(AppRoute.settings);
},
child: const Text('Settings'),
),
Expand Down

0 comments on commit 2654e47

Please sign in to comment.