Jonatas

use constraints rather map on parser

... ... @@ -48,10 +48,13 @@ class ParseRouteTree {
final pageMiddlewares = page.middlewares ?? <GetMiddleware>[];
pageMiddlewares.addAll(route.middlewares ?? <GetMiddleware>[]);
result.add(_addChild(page, parentPath, pageMiddlewares));
page.bindings.addAll(route.bindings);
final children = _flattenPage(page);
for (var child in children) {
pageMiddlewares.addAll(child.middlewares ?? <GetMiddleware>[]);
result.add(_addChild(child, parentPath, pageMiddlewares));
page.bindings.addAll(route.bindings);
}
}
return result;
... ... @@ -85,7 +88,7 @@ class ParseRouteTree {
(route) {
return _match(
name,
route.path['regex'] as RegExp,
route.path.regex,
);
},
orElse: () => null,
... ... @@ -94,18 +97,13 @@ class ParseRouteTree {
return route;
}
Map<String, String> _parseParams(
String path, Map<String, dynamic> routePath) {
Map<String, String> _parseParams(String path, PathDecoded routePath) {
final params = <String, String>{};
Match paramsMatch = (routePath['regex'] as RegExp).firstMatch(path);
for (var i = 0; i < (routePath['keys'].length as int); i++) {
String param;
try {
param = Uri.decodeQueryComponent(paramsMatch[i + 1]);
} on Exception catch (_) {
param = paramsMatch[i + 1];
}
params[routePath['keys'][i] as String] = param;
Match paramsMatch = routePath.regex.firstMatch(path);
for (var i = 0; i < routePath.keys.length; i++) {
var param = Uri.decodeQueryComponent(paramsMatch[i + 1]);
params[routePath.keys[i]] = param;
}
return params;
}
... ...
... ... @@ -11,8 +11,8 @@ import 'default_transitions.dart';
import 'transitions_type.dart';
class GetPageRoute<T> extends PageRoute<T> {
GetPageRoute(
{RouteSettings settings,
GetPageRoute({
RouteSettings settings,
this.transitionDuration = const Duration(milliseconds: 300),
this.opaque = true,
this.parameter,
... ... @@ -30,8 +30,8 @@ class GetPageRoute<T> extends PageRoute<T> {
this.barrierLabel,
this.maintainState = true,
bool fullscreenDialog = false,
this.middlewares})
: assert(opaque != null),
this.middlewares,
}) : assert(opaque != null),
assert(barrierDismissible != null),
assert(maintainState != null),
assert(fullscreenDialog != null),
... ...
... ... @@ -5,6 +5,12 @@ import '../../get_navigation.dart';
import 'custom_transition.dart';
import 'transitions_type.dart';
class PathDecoded {
const PathDecoded(this.regex, this.keys);
final RegExp regex;
final List<String> keys;
}
class GetPage {
final String name;
final GetPageBuilder page;
... ... @@ -24,8 +30,8 @@ class GetPage {
final RouteSettings settings;
final List<GetPage> children;
final List<GetMiddleware> middlewares;
final Map<String, dynamic> path;
final List<String> keys;
final PathDecoded path;
GetPage({
@required this.name,
@required this.page,
... ... @@ -39,43 +45,37 @@ class GetPage {
this.transitionDuration,
this.popGesture,
this.binding,
this.bindings,
this.bindings = const [],
this.transition,
this.customTransition,
this.fullscreenDialog = false,
this.children,
this.keys,
this.middlewares,
}) : path = normalize(name, keysList: keys),
}) : path = _nameToRegex(name),
assert(page != null),
assert(name != null),
assert(maintainState != null),
assert(fullscreenDialog != null);
static Map<String, dynamic> normalize(
String path, {
List<String> keysList,
}) {
var keys = List<String>.from(keysList ?? const <String>[]);
var stringPath =
'$path/?'.replaceAllMapped(RegExp(r'(\.)?:(\w+)(\?)?'), (placeholder) {
var replace = StringBuffer('(?:');
static PathDecoded _nameToRegex(String path) {
var keys = <String>[];
if (placeholder[1] != null) {
replace.write('\.');
}
String _replace(Match pattern) {
var buffer = StringBuffer('(?:');
replace.write('([\\w%+-._~!\$&\'()*,;=:@]+))');
if (pattern[1] != null) buffer.write('\.');
buffer.write('([\\w%+-._~!\$&\'()*,;=:@]+))');
if (pattern[3] != null) buffer.write('?');
if (placeholder[3] != null) {
replace.write('?');
keys.add(pattern[2]);
return "$buffer";
}
keys.add(placeholder[2]);
return replace.toString();
}).replaceAll('//', '/');
var stringPath = '$path/?'
.replaceAllMapped(RegExp(r'(\.)?:(\w+)(\?)?'), _replace)
.replaceAll('//', '/');
return {'regex': RegExp('^$stringPath\$'), 'keys': keys};
return PathDecoded(RegExp('^$stringPath\$'), keys);
}
GetPage copyWith({
... ... @@ -95,7 +95,6 @@ class GetPage {
Duration transitionDuration,
bool fullscreenDialog,
RouteSettings settings,
List<String> keys,
List<GetPage> children,
List<GetMiddleware> middlewares,
}) {
... ... @@ -116,7 +115,6 @@ class GetPage {
transitionDuration: transitionDuration ?? this.transitionDuration,
fullscreenDialog: fullscreenDialog ?? this.fullscreenDialog,
settings: settings ?? this.settings,
keys: keys ?? this.keys,
children: children ?? this.children,
middlewares: middlewares ?? this.middlewares,
);
... ...
... ... @@ -102,8 +102,6 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>>
VoidCallback remove;
Object _selector;
List<VoidCallback> _removeToOthers = <VoidCallback>[];
@override
void initState() {
super.initState();
... ...