Jonatas

use constraints rather map on parser

@@ -48,10 +48,13 @@ class ParseRouteTree { @@ -48,10 +48,13 @@ class ParseRouteTree {
48 final pageMiddlewares = page.middlewares ?? <GetMiddleware>[]; 48 final pageMiddlewares = page.middlewares ?? <GetMiddleware>[];
49 pageMiddlewares.addAll(route.middlewares ?? <GetMiddleware>[]); 49 pageMiddlewares.addAll(route.middlewares ?? <GetMiddleware>[]);
50 result.add(_addChild(page, parentPath, pageMiddlewares)); 50 result.add(_addChild(page, parentPath, pageMiddlewares));
  51 + page.bindings.addAll(route.bindings);
  52 +
51 final children = _flattenPage(page); 53 final children = _flattenPage(page);
52 for (var child in children) { 54 for (var child in children) {
53 pageMiddlewares.addAll(child.middlewares ?? <GetMiddleware>[]); 55 pageMiddlewares.addAll(child.middlewares ?? <GetMiddleware>[]);
54 result.add(_addChild(child, parentPath, pageMiddlewares)); 56 result.add(_addChild(child, parentPath, pageMiddlewares));
  57 + page.bindings.addAll(route.bindings);
55 } 58 }
56 } 59 }
57 return result; 60 return result;
@@ -85,7 +88,7 @@ class ParseRouteTree { @@ -85,7 +88,7 @@ class ParseRouteTree {
85 (route) { 88 (route) {
86 return _match( 89 return _match(
87 name, 90 name,
88 - route.path['regex'] as RegExp, 91 + route.path.regex,
89 ); 92 );
90 }, 93 },
91 orElse: () => null, 94 orElse: () => null,
@@ -94,18 +97,13 @@ class ParseRouteTree { @@ -94,18 +97,13 @@ class ParseRouteTree {
94 return route; 97 return route;
95 } 98 }
96 99
97 - Map<String, String> _parseParams(  
98 - String path, Map<String, dynamic> routePath) { 100 + Map<String, String> _parseParams(String path, PathDecoded routePath) {
99 final params = <String, String>{}; 101 final params = <String, String>{};
100 - Match paramsMatch = (routePath['regex'] as RegExp).firstMatch(path);  
101 - for (var i = 0; i < (routePath['keys'].length as int); i++) {  
102 - String param;  
103 - try {  
104 - param = Uri.decodeQueryComponent(paramsMatch[i + 1]);  
105 - } on Exception catch (_) {  
106 - param = paramsMatch[i + 1];  
107 - }  
108 - params[routePath['keys'][i] as String] = param; 102 + Match paramsMatch = routePath.regex.firstMatch(path);
  103 +
  104 + for (var i = 0; i < routePath.keys.length; i++) {
  105 + var param = Uri.decodeQueryComponent(paramsMatch[i + 1]);
  106 + params[routePath.keys[i]] = param;
109 } 107 }
110 return params; 108 return params;
111 } 109 }
@@ -11,8 +11,8 @@ import 'default_transitions.dart'; @@ -11,8 +11,8 @@ import 'default_transitions.dart';
11 import 'transitions_type.dart'; 11 import 'transitions_type.dart';
12 12
13 class GetPageRoute<T> extends PageRoute<T> { 13 class GetPageRoute<T> extends PageRoute<T> {
14 - GetPageRoute(  
15 - {RouteSettings settings, 14 + GetPageRoute({
  15 + RouteSettings settings,
16 this.transitionDuration = const Duration(milliseconds: 300), 16 this.transitionDuration = const Duration(milliseconds: 300),
17 this.opaque = true, 17 this.opaque = true,
18 this.parameter, 18 this.parameter,
@@ -30,8 +30,8 @@ class GetPageRoute<T> extends PageRoute<T> { @@ -30,8 +30,8 @@ class GetPageRoute<T> extends PageRoute<T> {
30 this.barrierLabel, 30 this.barrierLabel,
31 this.maintainState = true, 31 this.maintainState = true,
32 bool fullscreenDialog = false, 32 bool fullscreenDialog = false,
33 - this.middlewares})  
34 - : assert(opaque != null), 33 + this.middlewares,
  34 + }) : assert(opaque != null),
35 assert(barrierDismissible != null), 35 assert(barrierDismissible != null),
36 assert(maintainState != null), 36 assert(maintainState != null),
37 assert(fullscreenDialog != null), 37 assert(fullscreenDialog != null),
@@ -5,6 +5,12 @@ import '../../get_navigation.dart'; @@ -5,6 +5,12 @@ import '../../get_navigation.dart';
5 import 'custom_transition.dart'; 5 import 'custom_transition.dart';
6 import 'transitions_type.dart'; 6 import 'transitions_type.dart';
7 7
  8 +class PathDecoded {
  9 + const PathDecoded(this.regex, this.keys);
  10 + final RegExp regex;
  11 + final List<String> keys;
  12 +}
  13 +
8 class GetPage { 14 class GetPage {
9 final String name; 15 final String name;
10 final GetPageBuilder page; 16 final GetPageBuilder page;
@@ -24,8 +30,8 @@ class GetPage { @@ -24,8 +30,8 @@ class GetPage {
24 final RouteSettings settings; 30 final RouteSettings settings;
25 final List<GetPage> children; 31 final List<GetPage> children;
26 final List<GetMiddleware> middlewares; 32 final List<GetMiddleware> middlewares;
27 - final Map<String, dynamic> path;  
28 - final List<String> keys; 33 + final PathDecoded path;
  34 +
29 GetPage({ 35 GetPage({
30 @required this.name, 36 @required this.name,
31 @required this.page, 37 @required this.page,
@@ -39,43 +45,37 @@ class GetPage { @@ -39,43 +45,37 @@ class GetPage {
39 this.transitionDuration, 45 this.transitionDuration,
40 this.popGesture, 46 this.popGesture,
41 this.binding, 47 this.binding,
42 - this.bindings, 48 + this.bindings = const [],
43 this.transition, 49 this.transition,
44 this.customTransition, 50 this.customTransition,
45 this.fullscreenDialog = false, 51 this.fullscreenDialog = false,
46 this.children, 52 this.children,
47 - this.keys,  
48 this.middlewares, 53 this.middlewares,
49 - }) : path = normalize(name, keysList: keys), 54 + }) : path = _nameToRegex(name),
50 assert(page != null), 55 assert(page != null),
51 assert(name != null), 56 assert(name != null),
52 assert(maintainState != null), 57 assert(maintainState != null),
53 assert(fullscreenDialog != null); 58 assert(fullscreenDialog != null);
54 59
55 - static Map<String, dynamic> normalize(  
56 - String path, {  
57 - List<String> keysList,  
58 - }) {  
59 - var keys = List<String>.from(keysList ?? const <String>[]);  
60 - var stringPath =  
61 - '$path/?'.replaceAllMapped(RegExp(r'(\.)?:(\w+)(\?)?'), (placeholder) {  
62 - var replace = StringBuffer('(?:'); 60 + static PathDecoded _nameToRegex(String path) {
  61 + var keys = <String>[];
63 62
64 - if (placeholder[1] != null) {  
65 - replace.write('\.');  
66 - } 63 + String _replace(Match pattern) {
  64 + var buffer = StringBuffer('(?:');
67 65
68 - replace.write('([\\w%+-._~!\$&\'()*,;=:@]+))'); 66 + if (pattern[1] != null) buffer.write('\.');
  67 + buffer.write('([\\w%+-._~!\$&\'()*,;=:@]+))');
  68 + if (pattern[3] != null) buffer.write('?');
69 69
70 - if (placeholder[3] != null) {  
71 - replace.write('?'); 70 + keys.add(pattern[2]);
  71 + return "$buffer";
72 } 72 }
73 73
74 - keys.add(placeholder[2]);  
75 - return replace.toString();  
76 - }).replaceAll('//', '/'); 74 + var stringPath = '$path/?'
  75 + .replaceAllMapped(RegExp(r'(\.)?:(\w+)(\?)?'), _replace)
  76 + .replaceAll('//', '/');
77 77
78 - return {'regex': RegExp('^$stringPath\$'), 'keys': keys}; 78 + return PathDecoded(RegExp('^$stringPath\$'), keys);
79 } 79 }
80 80
81 GetPage copyWith({ 81 GetPage copyWith({
@@ -95,7 +95,6 @@ class GetPage { @@ -95,7 +95,6 @@ class GetPage {
95 Duration transitionDuration, 95 Duration transitionDuration,
96 bool fullscreenDialog, 96 bool fullscreenDialog,
97 RouteSettings settings, 97 RouteSettings settings,
98 - List<String> keys,  
99 List<GetPage> children, 98 List<GetPage> children,
100 List<GetMiddleware> middlewares, 99 List<GetMiddleware> middlewares,
101 }) { 100 }) {
@@ -116,7 +115,6 @@ class GetPage { @@ -116,7 +115,6 @@ class GetPage {
116 transitionDuration: transitionDuration ?? this.transitionDuration, 115 transitionDuration: transitionDuration ?? this.transitionDuration,
117 fullscreenDialog: fullscreenDialog ?? this.fullscreenDialog, 116 fullscreenDialog: fullscreenDialog ?? this.fullscreenDialog,
118 settings: settings ?? this.settings, 117 settings: settings ?? this.settings,
119 - keys: keys ?? this.keys,  
120 children: children ?? this.children, 118 children: children ?? this.children,
121 middlewares: middlewares ?? this.middlewares, 119 middlewares: middlewares ?? this.middlewares,
122 ); 120 );
@@ -102,8 +102,6 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>> @@ -102,8 +102,6 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>>
102 VoidCallback remove; 102 VoidCallback remove;
103 Object _selector; 103 Object _selector;
104 104
105 - List<VoidCallback> _removeToOthers = <VoidCallback>[];  
106 -  
107 @override 105 @override
108 void initState() { 106 void initState() {
109 super.initState(); 107 super.initState();