Skip to content

Commit

Permalink
feat: add router
Browse files Browse the repository at this point in the history
  • Loading branch information
nesquikm committed Oct 25, 2023
1 parent 1aeddfd commit ff09db6
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/app/router/router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

122 changes: 122 additions & 0 deletions lib/app/router/routes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import 'package:collection/collection.dart';
import 'package:logging/logging.dart';

enum AppRoute {
library(
'library',
'/library',
isSaveLocation: true,
),

reader(
'reader',
'/reader',
isSaveLocation: true,
),

settings(
'settings',
'/settings',
);

const AppRoute(
this.name,
this.path, {
this.isSaveLocation = false,
});

final String name;
final String path;
final bool isSaveLocation;

static AppRoute get defaultRoute => library;
static final _log = Logger('AppRoute');

static AppRoute? getByPath(String path) {
return AppRoute.values.firstWhereOrNull(
(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);
}
10 changes: 9 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ packages:
source: hosted
version: "4.7.0"
collection:
dependency: transitive
dependency: "direct main"
description:
name: collection
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
Expand Down Expand Up @@ -461,6 +461,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.2"
go_router:
dependency: "direct main"
description:
name: go_router
sha256: a206cc4621a644531a2e05e7774616ab4d9d85eab1f3b0e255f3102937fccab1
url: "https://pub.dev"
source: hosted
version: "12.0.0"
graphs:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:
dependencies:
archive: ^3.4.6
async: ^2.11.0
collection: ^1.17.2
convert: ^3.1.1
crypto: ^3.0.3
encrypted_storage:
Expand All @@ -29,6 +30,7 @@ dependencies:
sdk: flutter
flutter_riverpod: ^2.4.4
freezed_annotation: ^2.4.1
go_router: ^12.0.0
image: ^3.3.0
intl: ^0.18.0
json_annotation: ^4.8.1
Expand Down

0 comments on commit ff09db6

Please sign in to comment.