Committed by
GitHub
Merge pull request #611 from Grohden/refactor/route-observer
refactor: Fix route-observer null logs in some situations
Showing
1 changed file
with
110 additions
and
74 deletions
@@ -34,139 +34,175 @@ class Routing { | @@ -34,139 +34,175 @@ class Routing { | ||
34 | } | 34 | } |
35 | } | 35 | } |
36 | 36 | ||
37 | -class GetObserver extends NavigatorObserver { | ||
38 | - final Function(Routing) routing; | 37 | +/// Extracts the name of a route based on it's instance type |
38 | +/// or null if not possible. | ||
39 | +String _extractRouteName(Route route) { | ||
40 | + if (route?.settings?.name != null) { | ||
41 | + return route.settings.name; | ||
42 | + } | ||
39 | 43 | ||
40 | - GetObserver([this.routing, this._routeSend]); | 44 | + if (route is GetPageRoute) { |
45 | + return route.routeName; | ||
46 | + } | ||
41 | 47 | ||
42 | - final Routing _routeSend; | 48 | + if (route is GetDialogRoute) { |
49 | + return route.name; | ||
50 | + } | ||
43 | 51 | ||
44 | - Route<dynamic> route; | ||
45 | - bool isBack; | ||
46 | - bool isSnackbar; | ||
47 | - bool isBottomSheet; | ||
48 | - bool isDialog; | ||
49 | - String current; | ||
50 | - String previous; | ||
51 | - dynamic args; | 52 | + if (route is GetModalBottomSheetRoute) { |
53 | + return route.name; | ||
54 | + } | ||
52 | 55 | ||
53 | - // String previousArgs; | ||
54 | - String removed; | 56 | + return null; |
57 | +} | ||
55 | 58 | ||
56 | - String name(Route<dynamic> route) { | ||
57 | - if (route?.settings?.name != null) { | ||
58 | - return route.settings.name; | ||
59 | - } else if (route is GetPageRoute) { | ||
60 | - return route.routeName; | ||
61 | - } else if (route is GetDialogRoute) { | ||
62 | - return route.name; | ||
63 | - } else if (route is GetModalBottomSheetRoute) { | ||
64 | - return route.name; | ||
65 | - } else { | ||
66 | - return route?.settings?.name; | ||
67 | - } | 59 | +/// This is basically a util for rules about 'what a route is' |
60 | +class _RouteData { | ||
61 | + final bool isGetPageRoute; | ||
62 | + final bool isSnackbar; | ||
63 | + final bool isBottomSheet; | ||
64 | + final bool isDialog; | ||
65 | + final String name; | ||
66 | + | ||
67 | + _RouteData({ | ||
68 | + @required this.name, | ||
69 | + @required this.isGetPageRoute, | ||
70 | + @required this.isSnackbar, | ||
71 | + @required this.isBottomSheet, | ||
72 | + @required this.isDialog, | ||
73 | + }); | ||
74 | + | ||
75 | + factory _RouteData.ofRoute(Route route) { | ||
76 | + return _RouteData( | ||
77 | + name: _extractRouteName(route), | ||
78 | + isGetPageRoute: route is GetPageRoute, | ||
79 | + isSnackbar: route is SnackRoute, | ||
80 | + isDialog: route is GetDialogRoute, | ||
81 | + isBottomSheet: route is GetModalBottomSheetRoute, | ||
82 | + ); | ||
68 | } | 83 | } |
84 | +} | ||
85 | + | ||
86 | +class GetObserver extends NavigatorObserver { | ||
87 | + final Function(Routing) routing; | ||
88 | + | ||
89 | + GetObserver([this.routing, this._routeSend]); | ||
90 | + | ||
91 | + final Routing _routeSend; | ||
69 | 92 | ||
70 | @override | 93 | @override |
71 | - void didPush(Route<dynamic> route, Route<dynamic> previousRoute) { | ||
72 | - var isGetPageRoute = route is GetPageRoute; | ||
73 | - var isSnackbar = route is SnackRoute; | ||
74 | - var isDialog = route is GetDialogRoute; | ||
75 | - var isBottomSheet = route is GetModalBottomSheetRoute; | ||
76 | - var routeName = name(route); | ||
77 | - | ||
78 | - if (isSnackbar) { | ||
79 | - GetConfig.log("OPEN SNACKBAR $routeName"); | ||
80 | - } else if (isBottomSheet || isDialog) { | ||
81 | - GetConfig.log("OPEN $routeName"); | ||
82 | - } else if (isGetPageRoute) { | ||
83 | - GetConfig.log("GOING TO ROUTE $routeName"); | 94 | + void didPush(Route route, Route previousRoute) { |
95 | + super.didPush(route, previousRoute); | ||
96 | + final newRoute = _RouteData.ofRoute(route); | ||
97 | + | ||
98 | + if (newRoute.isSnackbar) { | ||
99 | + GetConfig.log("OPEN SNACKBAR ${newRoute.name}"); | ||
100 | + } else if (newRoute.isBottomSheet || newRoute.isDialog) { | ||
101 | + GetConfig.log("OPEN ${newRoute.name}"); | ||
102 | + } else if (newRoute.isGetPageRoute) { | ||
103 | + GetConfig.log("GOING TO ROUTE ${newRoute.name}"); | ||
84 | } | 104 | } |
85 | - GetConfig.currentRoute = routeName; | ||
86 | 105 | ||
106 | + GetConfig.currentRoute = newRoute.name; | ||
87 | _routeSend?.update((value) { | 107 | _routeSend?.update((value) { |
88 | - if (route is PageRoute) value.current = routeName; | 108 | + // Only PageRoute is allowed to change current value |
109 | + if (route is PageRoute) { | ||
110 | + value.current = newRoute.name ?? ''; | ||
111 | + } | ||
112 | + | ||
89 | value.args = route?.settings?.arguments; | 113 | value.args = route?.settings?.arguments; |
90 | value.route = route; | 114 | value.route = route; |
91 | value.isBack = false; | 115 | value.isBack = false; |
92 | value.removed = ''; | 116 | value.removed = ''; |
93 | - value.previous = previousRoute?.settings?.name ?? ''; | ||
94 | - value.isSnackbar = isSnackbar; | ||
95 | - value.isBottomSheet = isBottomSheet; | ||
96 | - value.isDialog = isDialog; | 117 | + value.previous = _extractRouteName(previousRoute) ?? ''; |
118 | + value.isSnackbar = newRoute.isSnackbar; | ||
119 | + value.isBottomSheet = newRoute.isBottomSheet; | ||
120 | + value.isDialog = newRoute.isDialog; | ||
97 | }); | 121 | }); |
98 | - if (routing != null) routing(_routeSend); | 122 | + |
123 | + if (routing != null) { | ||
124 | + routing(_routeSend); | ||
125 | + } | ||
99 | } | 126 | } |
100 | 127 | ||
101 | @override | 128 | @override |
102 | void didPop(Route route, Route previousRoute) { | 129 | void didPop(Route route, Route previousRoute) { |
103 | super.didPop(route, previousRoute); | 130 | super.didPop(route, previousRoute); |
104 | - | ||
105 | - var isGetPageRoute = route is GetPageRoute; | ||
106 | - var isSnackbar = route is SnackRoute; | ||
107 | - var isDialog = route is GetDialogRoute; | ||
108 | - var isBottomSheet = route is GetModalBottomSheetRoute; | ||
109 | - var routeName = name(route); | ||
110 | - | ||
111 | - if (isSnackbar) { | ||
112 | - GetConfig.log("CLOSE SNACKBAR $routeName"); | ||
113 | - } else if (isBottomSheet || isDialog) { | ||
114 | - GetConfig.log("CLOSE $routeName"); | ||
115 | - } else if (isGetPageRoute) { | ||
116 | - GetConfig.log("CLOSE TO ROUTE $routeName"); | 131 | + final newRoute = _RouteData.ofRoute(route); |
132 | + | ||
133 | + if (newRoute.isSnackbar) { | ||
134 | + GetConfig.log("CLOSE SNACKBAR ${newRoute.name}"); | ||
135 | + } else if (newRoute.isBottomSheet || newRoute.isDialog) { | ||
136 | + GetConfig.log("CLOSE ${newRoute.name}"); | ||
137 | + } else if (newRoute.isGetPageRoute) { | ||
138 | + GetConfig.log("CLOSE TO ROUTE ${newRoute.name}"); | ||
117 | } | 139 | } |
118 | - GetConfig.currentRoute = routeName; | ||
119 | 140 | ||
141 | + GetConfig.currentRoute = newRoute.name; | ||
142 | + // Here we use a 'inverse didPush set', meaning that we use | ||
143 | + // previous route instead of 'route' because this is | ||
144 | + // a 'inverse push' | ||
120 | _routeSend?.update((value) { | 145 | _routeSend?.update((value) { |
146 | + // Only PageRoute is allowed to change current value | ||
121 | if (previousRoute is PageRoute) { | 147 | if (previousRoute is PageRoute) { |
122 | - value.current = previousRoute?.settings?.name ?? ''; | 148 | + value.current = _extractRouteName(previousRoute) ?? ''; |
123 | } | 149 | } |
150 | + | ||
124 | value.args = route?.settings?.arguments; | 151 | value.args = route?.settings?.arguments; |
125 | value.route = previousRoute; | 152 | value.route = previousRoute; |
126 | value.isBack = true; | 153 | value.isBack = true; |
127 | value.removed = ''; | 154 | value.removed = ''; |
128 | - value.previous = route?.settings?.name ?? ''; | 155 | + value.previous = newRoute.name ?? ''; |
129 | value.isSnackbar = false; | 156 | value.isSnackbar = false; |
130 | value.isBottomSheet = false; | 157 | value.isBottomSheet = false; |
131 | value.isDialog = false; | 158 | value.isDialog = false; |
132 | }); | 159 | }); |
133 | - if (routing != null) routing(_routeSend); | 160 | + |
161 | + routing?.call(_routeSend); | ||
134 | } | 162 | } |
135 | 163 | ||
136 | @override | 164 | @override |
137 | void didReplace({Route newRoute, Route oldRoute}) { | 165 | void didReplace({Route newRoute, Route oldRoute}) { |
138 | super.didReplace(newRoute: newRoute, oldRoute: oldRoute); | 166 | super.didReplace(newRoute: newRoute, oldRoute: oldRoute); |
167 | + final newName = _extractRouteName(newRoute); | ||
168 | + final oldName = _extractRouteName(oldRoute); | ||
139 | 169 | ||
140 | - GetConfig.log("REPLACE ROUTE ${oldRoute?.settings?.name}"); | ||
141 | - GetConfig.log("NEW ROUTE ${newRoute?.settings?.name}"); | ||
142 | - | ||
143 | - GetConfig.currentRoute = name(newRoute); | 170 | + GetConfig.log("REPLACE ROUTE $oldName"); |
171 | + GetConfig.log("NEW ROUTE $newName"); | ||
144 | 172 | ||
173 | + GetConfig.currentRoute = newName; | ||
145 | _routeSend?.update((value) { | 174 | _routeSend?.update((value) { |
146 | - if (newRoute is PageRoute) value.current = newRoute?.settings?.name ?? ''; | 175 | + // Only PageRoute is allowed to change current value |
176 | + if (newRoute is PageRoute) { | ||
177 | + value.current = newName ?? ''; | ||
178 | + } | ||
179 | + | ||
147 | value.args = newRoute?.settings?.arguments; | 180 | value.args = newRoute?.settings?.arguments; |
148 | value.route = newRoute; | 181 | value.route = newRoute; |
149 | value.isBack = false; | 182 | value.isBack = false; |
150 | value.removed = ''; | 183 | value.removed = ''; |
151 | - value.previous = '${oldRoute?.settings?.name}'; | 184 | + value.previous = '$oldName'; |
152 | value.isSnackbar = false; | 185 | value.isSnackbar = false; |
153 | value.isBottomSheet = false; | 186 | value.isBottomSheet = false; |
154 | value.isDialog = false; | 187 | value.isDialog = false; |
155 | }); | 188 | }); |
156 | - if (routing != null) routing(_routeSend); | 189 | + |
190 | + routing?.call(_routeSend); | ||
157 | } | 191 | } |
158 | 192 | ||
159 | @override | 193 | @override |
160 | void didRemove(Route route, Route previousRoute) { | 194 | void didRemove(Route route, Route previousRoute) { |
161 | super.didRemove(route, previousRoute); | 195 | super.didRemove(route, previousRoute); |
162 | - GetConfig.log("REMOVING ROUTE ${route?.settings?.name}"); | 196 | + final routeName = _extractRouteName(route); |
163 | 197 | ||
198 | + GetConfig.log("REMOVING ROUTE $routeName"); | ||
164 | _routeSend?.update((value) { | 199 | _routeSend?.update((value) { |
165 | value.route = previousRoute; | 200 | value.route = previousRoute; |
166 | value.isBack = false; | 201 | value.isBack = false; |
167 | - value.removed = route?.settings?.name ?? ''; | ||
168 | - value.previous = route?.settings?.name ?? ''; | 202 | + value.removed = routeName ?? ''; |
203 | + value.previous = routeName ?? ''; | ||
169 | }); | 204 | }); |
170 | - if (routing != null) routing(_routeSend); | 205 | + |
206 | + routing?.call(_routeSend); | ||
171 | } | 207 | } |
172 | } | 208 | } |
-
Please register or login to post a comment