From 761ab683d18735641b2a1117e55adb0404052dc2 Mon Sep 17 00:00:00 2001 From: Hixie Date: Tue, 21 Jul 2015 14:54:03 -0700 Subject: [PATCH] Rationalise usage of keys in navigator.dart. Route (named routes) no longer have a key, and have their own storage for their names. RouseState no longer has a key, and uses an owner field pointing to a StatefulComponent instead. As such, RouteBase no longer has a key. HistoryEntry no longer uses a global int to ensure uniqueness. Propagated this to stocks app. --- sky/sdk/example/stocks/lib/stock_home.dart | 3 ++- .../example/stocks/lib/stock_settings.dart | 2 +- sky/sdk/lib/widgets/drawer.dart | 3 ++- sky/sdk/lib/widgets/navigator.dart | 25 ++++++++----------- sky/sdk/lib/widgets/popup_menu.dart | 5 ++-- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/sky/sdk/example/stocks/lib/stock_home.dart b/sky/sdk/example/stocks/lib/stock_home.dart index b7a8b8902..4ce3187ad 100644 --- a/sky/sdk/example/stocks/lib/stock_home.dart +++ b/sky/sdk/example/stocks/lib/stock_home.dart @@ -69,7 +69,8 @@ class StockHome extends AnimatedComponent { } void _handleSearchEnd() { - assert(navigator.currentRoute.key == this); + assert(navigator.currentRoute is RouteState); + assert((navigator.currentRoute as RouteState).owner == this); // TODO(ianh): remove cast once analyzer is cleverer navigator.pop(); setState(() { _isSearching = false; diff --git a/sky/sdk/example/stocks/lib/stock_settings.dart b/sky/sdk/example/stocks/lib/stock_settings.dart index a49047c83..76e5c0389 100644 --- a/sky/sdk/example/stocks/lib/stock_settings.dart +++ b/sky/sdk/example/stocks/lib/stock_settings.dart @@ -61,7 +61,7 @@ class StockSettings extends StatefulComponent { break; case StockMode.pessimistic: showModeDialog = true; - navigator.pushState("/settings/confirm", (_) { + navigator.pushState(this, (_) { showModeDialog = false; }); break; diff --git a/sky/sdk/lib/widgets/drawer.dart b/sky/sdk/lib/widgets/drawer.dart index 781970ea1..b45aa9f19 100644 --- a/sky/sdk/lib/widgets/drawer.dart +++ b/sky/sdk/lib/widgets/drawer.dart @@ -143,7 +143,8 @@ class Drawer extends AnimatedComponent { if (_lastStatus != null && status != _lastStatus) { if (status == DrawerStatus.inactive && navigator != null && - navigator.currentRoute.key == this) + navigator.currentRoute is RouteState && + (navigator.currentRoute as RouteState).owner == this) // TODO(ianh): remove cast once analyzer is cleverer navigator.pop(); if (onStatusChanged != null) onStatusChanged(status); diff --git a/sky/sdk/lib/widgets/navigator.dart b/sky/sdk/lib/widgets/navigator.dart index 8254bbef5..1a20b5aeb 100644 --- a/sky/sdk/lib/widgets/navigator.dart +++ b/sky/sdk/lib/widgets/navigator.dart @@ -12,25 +12,24 @@ import 'package:vector_math/vector_math.dart'; typedef Widget Builder(Navigator navigator, RouteBase route); abstract class RouteBase { - RouteBase({ this.key }); - final Object key; Widget build(Navigator navigator, RouteBase route); void popState() { } } class Route extends RouteBase { - Route({ String name, this.builder }) : super(key: name); - String get name => key; + Route({ this.name, this.builder }); + final String name; final Builder builder; Widget build(Navigator navigator, RouteBase route) => builder(navigator, route); } class RouteState extends RouteBase { - RouteState({ this.callback, this.route, Object key }) : super(key: key); + RouteState({ this.callback, this.route, this.owner }); - RouteBase route; Function callback; + RouteBase route; + StatefulComponent owner; Widget build(Navigator navigator, RouteBase route) => null; @@ -144,9 +143,8 @@ class Transition extends AnimatedComponent { } class HistoryEntry { - HistoryEntry({ this.route, this.key }); + HistoryEntry({ this.route }); final RouteBase route; - final int key; bool transitionFinished = false; // TODO(jackson): Keep track of the requested transition } @@ -158,12 +156,11 @@ class NavigationState { if (route.name != null) namedRoutes[route.name] = route; } - history.add(new HistoryEntry(route: routes[0], key: _lastKey++)); + history.add(new HistoryEntry(route: routes[0])); } List history = new List(); int historyIndex = 0; - int _lastKey = 0; Map namedRoutes = new Map(); RouteBase get currentRoute => history[historyIndex].route; @@ -176,7 +173,7 @@ class NavigationState { } void push(RouteBase route) { - HistoryEntry historyEntry = new HistoryEntry(route: route, key: _lastKey++); + HistoryEntry historyEntry = new HistoryEntry(route: route); history.insert(historyIndex + 1, historyEntry); historyIndex++; } @@ -203,9 +200,9 @@ class Navigator extends StatefulComponent { RouteBase get currentRoute => state.currentRoute; - void pushState(Object key, Function callback) { + void pushState(StatefulComponent owner, Function callback) { RouteBase route = new RouteState( - key: key, + owner: owner, callback: callback, route: state.currentRoute ); @@ -245,7 +242,7 @@ class Navigator extends StatefulComponent { if (content == null) continue; Transition transition = new Transition( - key: historyEntry.key.toString(), + key: historyEntry.hashCode.toString(), // TODO(ianh): make it not collide content: content, direction: (i <= state.historyIndex) ? TransitionDirection.forward : TransitionDirection.reverse, interactive: (i == state.historyIndex), diff --git a/sky/sdk/lib/widgets/popup_menu.dart b/sky/sdk/lib/widgets/popup_menu.dart index 85e9bd70c..49d6aa97b 100644 --- a/sky/sdk/lib/widgets/popup_menu.dart +++ b/sky/sdk/lib/widgets/popup_menu.dart @@ -122,8 +122,9 @@ class PopupMenu extends AnimatedComponent { PopupMenuStatus status = _status; if (_lastStatus != null && status != _lastStatus) { if (status == PopupMenuStatus.inactive && - navigator != null && - navigator.currentRoute.key == this) + navigator != null && + navigator.currentRoute is RouteState && + (navigator.currentRoute as RouteState).owner == this) // TODO(ianh): remove cast once analyzer is cleverer navigator.pop(); if (onStatusChanged != null) onStatusChanged(status); -- GitLab