Skip to content

Commit

Permalink
[go_router] Refactor internal classes and methods (#2317)
Browse files Browse the repository at this point in the history
* Refactor internal classes and methods

- Separate matching from redirection
- Add RouteRedirector typedef
- Add RouteMatcher class
- Add RouteBuilder class
- Add RouteConfiguration class
- Rename and reorganize internal classes and libraries
- Add todo comments

* format

* Sort imports

* Update changelog

* Address code review comments

- Change name back to GoRouterRefreshStream
- Update toString() methods for new naming
- Make fields final
- Add logging to parser
- Add comments
- add tests
- Move function-scope to new library-scope _addRedirect function
- import widgets instead of material where possible

* remove routing library

* Move classes in go_router.dart into separate libraries

* Move Configuration.validate() into constructor

* Remove comment

* use continue in redirect loop

* Fix comments

* Sort imports

* Fix logging in configuration

* add visibleForTesting annotation

* Updates from merge with main

* Format

* Add TODOs to make Router implementation classes private

* Add copyright headers

* Fix tests

* format

* fix comment

* Update packages/go_router/lib/src/parser.dart

Co-authored-by: Loïc Sharma <737941+loic-sharma@users.noreply.github.com>

* add whitespace

* format

* Hide typedefs that weren't previously exported

* Delete empty file

* add missing import

* Specify version 4.1.2 in pubspec.yaml

* Update packages/go_router/lib/src/builder.dart

Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com>

* Fix comment

* Add isError and error getters to RouteMatchList

* Add issue links to TODO comments

* Add link to issue for TODO

* Re-apply code from #2306 due to merge conflicts

* Add issue references

Co-authored-by: Loïc Sharma <737941+loic-sharma@users.noreply.github.com>
Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 22, 2022
1 parent 199d8c9 commit 75e6e43
Show file tree
Hide file tree
Showing 48 changed files with 1,310 additions and 922 deletions.
30 changes: 30 additions & 0 deletions packages/go_router/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.2.1

- Refactors internal classes and methods

## 4.2.0

- Adds `void replace()` and `replaceNamed` to `GoRouterDelegate`, `GoRouter` and `GoRouterHelper`.
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/example/lib/books/src/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'package:flutter/widgets.dart';

/// A mock authentication service
/// A mock authentication service.
class BookstoreAuth extends ChangeNotifier {
bool _signedIn = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:flutter/material.dart';

import 'package:go_router/go_router.dart';

/// The enum for scaffold tab
/// The enum for scaffold tab.
enum ScaffoldTab {
/// The books tab.
books,
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/example/lib/cupertino.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:go_router/go_router.dart';

void main() => runApp(App());

/// The main app
/// The main app.
class App extends StatelessWidget {
/// Creates an [App].
App({Key? key}) : super(key: key);
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/example/lib/error_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Page2Screen extends StatelessWidget {
);
}

/// The screen of the error page
/// The screen of the error page.
class ErrorScreen extends StatelessWidget {
/// Creates an [ErrorScreen].
const ErrorScreen(this.error, {Key? key}) : super(key: key);
Expand Down
2 changes: 2 additions & 0 deletions packages/go_router/example/lib/router_stream_refresh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class _AppState extends State<App> {
return null;
},
// changes on the listenable will cause the router to refresh it's route
// TODO(johnpryan): Deprecate GoRouterRefreshStream
// See https://github.com/flutter/flutter/issues/108128
refreshListenable: GoRouterRefreshStream(loggedInState.stream),
);
super.initState();
Expand Down
4 changes: 2 additions & 2 deletions packages/go_router/example/lib/shared_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class SharedScaffold extends StatefulWidget {
Key? key,
}) : super(key: key);

/// The selected index
/// The selected index.
final int selectedIndex;

/// The body of the page.
Expand Down Expand Up @@ -173,7 +173,7 @@ class Page2View extends StatelessWidget {

/// The error scaffold.
class ErrorScaffold extends StatelessWidget {
/// Creates an [ErrorScaffold]
/// Creates an [ErrorScaffold].
const ErrorScaffold({
required this.body,
Key? key,
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/example/lib/transitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class ExampleTransitionsScreen extends StatelessWidget {
/// The color of the container.
final Color color;

/// The transition kind
/// The transition kind.
final String kind;

@override
Expand Down
109 changes: 9 additions & 100 deletions packages/go_router/lib/go_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,106 +3,15 @@
// found in the LICENSE file.

/// A declarative router for Flutter based on Navigation 2 supporting
/// deep linking, data-driven routes and more
/// deep linking, data-driven routes and more.
library go_router;

import 'package:flutter/widgets.dart';

import 'src/go_router.dart';

export 'src/custom_transition_page.dart';
export 'src/go_route.dart';
export 'src/go_router.dart';
export 'src/go_router_refresh_stream.dart';
export 'src/go_router_state.dart';
export 'src/inherited_go_router.dart';
export 'src/route_data.dart' show GoRouteData, TypedGoRoute;
export 'src/configuration.dart' show GoRouterState, GoRoute;
export 'src/misc/extensions.dart';
export 'src/misc/inherited_router.dart';
export 'src/misc/refresh_stream.dart';
export 'src/pages/custom_transition_page.dart';
export 'src/platform.dart' show UrlPathStrategy;
export 'src/router.dart';
export 'src/typed_routing.dart' show GoRouteData, TypedGoRoute;
export 'src/typedefs.dart' show GoRouterPageBuilder, GoRouterRedirect;
export 'src/url_path_strategy.dart';

/// Dart extension to add navigation function to a BuildContext object, e.g.
/// context.go('/');
// NOTE: adding this here instead of in /src to work-around a Dart analyzer bug
// and fix: https://github.com/csells/go_router/issues/116
extension GoRouterHelper on BuildContext {
/// Get a location from route name and parameters.
String namedLocation(
String name, {
Map<String, String> params = const <String, String>{},
Map<String, String> queryParams = const <String, String>{},
}) =>
GoRouter.of(this)
.namedLocation(name, params: params, queryParams: queryParams);

/// Navigate to a location.
void go(String location, {Object? extra}) =>
GoRouter.of(this).go(location, extra: extra);

/// Navigate to a named route.
void goNamed(
String name, {
Map<String, String> params = const <String, String>{},
Map<String, String> queryParams = const <String, String>{},
Object? extra,
}) =>
GoRouter.of(this).goNamed(
name,
params: params,
queryParams: queryParams,
extra: extra,
);

/// Push a location onto the page stack.
void push(String location, {Object? extra}) =>
GoRouter.of(this).push(location, extra: extra);

/// Navigate to a named route onto the page stack.
void pushNamed(
String name, {
Map<String, String> params = const <String, String>{},
Map<String, String> queryParams = const <String, String>{},
Object? extra,
}) =>
GoRouter.of(this).pushNamed(
name,
params: params,
queryParams: queryParams,
extra: extra,
);

/// Replaces the top-most page of the page stack with the given URL location
/// w/ optional query parameters, e.g. `/family/f2/person/p1?color=blue`.
///
/// See also:
/// * [go] which navigates to the location.
/// * [push] which pushes the location onto the page stack.
void replace(String location, {Object? extra}) =>
GoRouter.of(this).replace(location, extra: extra);

/// Replaces the top-most page of the page stack with the named route w/
/// optional parameters, e.g. `name='person', params={'fid': 'f2', 'pid':
/// 'p1'}`.
///
/// See also:
/// * [goNamed] which navigates a named route.
/// * [pushNamed] which pushes a named route onto the page stack.
void replaceNamed(
String name, {
Map<String, String> params = const <String, String>{},
Map<String, String> queryParams = const <String, String>{},
Object? extra,
}) =>
GoRouter.of(this).replaceNamed(
name,
params: params,
queryParams: queryParams,
extra: extra,
);

/// Returns `true` if there is more than 1 page on the stack.
bool canPop() => GoRouter.of(this).canPop();

/// Pop the top page off the Navigator's page stack by calling
/// [Navigator.pop].
void pop() => GoRouter.of(this).pop();
}
Loading

0 comments on commit 75e6e43

Please sign in to comment.