From 761d528153e26390e4d6d3caabc70341812a8617 Mon Sep 17 00:00:00 2001 From: Eric Vicenti Date: Tue, 28 Feb 2017 15:39:24 -0800 Subject: [PATCH] Remove NavExperimental from UIExplorer Summary: Simplify the UIExplorer setup, and remove dependency on NavigationExperimental, which is being phased out in favor of React Navigation. Reviewed By: mkonicek Differential Revision: D4627131 fbshipit-source-id: 6294623a885074a73c831b0d817770fbe8a90221 --- Examples/UIExplorer/js/UIExplorerActions.js | 32 +++- .../UIExplorer/js/UIExplorerApp.android.js | 112 ++++++------- Examples/UIExplorer/js/UIExplorerApp.ios.js | 157 ++++++++---------- .../UIExplorer/js/UIExplorerExampleList.js | 4 +- .../js/UIExplorerNavigationReducer.js | 150 +++-------------- .../UIExplorer/js/UIExplorerStateTitleMap.js | 41 ----- Examples/UIExplorer/js/URIActionMap.js | 8 +- 7 files changed, 164 insertions(+), 340 deletions(-) delete mode 100644 Examples/UIExplorer/js/UIExplorerStateTitleMap.js diff --git a/Examples/UIExplorer/js/UIExplorerActions.js b/Examples/UIExplorer/js/UIExplorerActions.js index 8a6b1ee8c..d1983e907 100644 --- a/Examples/UIExplorer/js/UIExplorerActions.js +++ b/Examples/UIExplorer/js/UIExplorerActions.js @@ -23,22 +23,35 @@ */ 'use strict'; -export type UIExplorerListWithFilterAction = { - type: 'UIExplorerListWithFilterAction', - filter: ?string; +export type UIExplorerBackAction = { + type: 'UIExplorerBackAction', +}; + +export type UIExplorerListAction = { + type: 'UIExplorerListAction', }; export type UIExplorerExampleAction = { type: 'UIExplorerExampleAction', - openExample: string; + openExample: string, }; -export type UIExplorerAction = UIExplorerListWithFilterAction | UIExplorerExampleAction; +export type UIExplorerAction = ( + UIExplorerBackAction | + UIExplorerListAction | + UIExplorerExampleAction +); -function ExampleListWithFilter(filter: ?string): UIExplorerListWithFilterAction { + +function Back(): UIExplorerBackAction { return { - type: 'UIExplorerListWithFilterAction', - filter, + type: 'UIExplorerBackAction', + }; +} + +function ExampleList(): UIExplorerListAction { + return { + type: 'UIExplorerListAction', }; } @@ -50,7 +63,8 @@ function ExampleAction(openExample: string): UIExplorerExampleAction { } const UIExplorerActions = { - ExampleListWithFilter, + Back, + ExampleList, ExampleAction, }; diff --git a/Examples/UIExplorer/js/UIExplorerApp.android.js b/Examples/UIExplorer/js/UIExplorerApp.android.js index 35c96cb55..683f53b13 100644 --- a/Examples/UIExplorer/js/UIExplorerApp.android.js +++ b/Examples/UIExplorer/js/UIExplorerApp.android.js @@ -33,18 +33,18 @@ const React = require('react'); const StatusBar = require('StatusBar'); const StyleSheet = require('StyleSheet'); const ToolbarAndroid = require('ToolbarAndroid'); +const UIExplorerActions = require('./UIExplorerActions'); const UIExplorerExampleContainer = require('./UIExplorerExampleContainer'); const UIExplorerExampleList = require('./UIExplorerExampleList'); const UIExplorerList = require('./UIExplorerList'); const UIExplorerNavigationReducer = require('./UIExplorerNavigationReducer'); -const UIExplorerStateTitleMap = require('./UIExplorerStateTitleMap'); const UIManager = require('UIManager'); const URIActionMap = require('./URIActionMap'); const View = require('View'); const nativeImageSource = require('nativeImageSource'); -import type {UIExplorerNavigationState} from './UIExplorerNavigationReducer'; +import type { UIExplorerNavigationState } from './UIExplorerNavigationReducer'; UIManager.setLayoutAnimationEnabledExperimental(true); @@ -54,27 +54,31 @@ type Props = { exampleFromAppetizeParams: string, }; -type State = UIExplorerNavigationState & { - externalExample: ?string, -}; +const APP_STATE_KEY = 'UIExplorerAppState.v2'; + +const HEADER_LOGO_ICON = nativeImageSource({ + android: 'launcher_icon', + width: 132, + height: 144 +}); + +const HEADER_NAV_ICON = nativeImageSource({ + android: 'ic_menu_black_24dp', + width: 48, + height: 48 +}); class UIExplorerApp extends React.Component { - _handleAction: Function; - _renderDrawerContent: Function; - state: State; - constructor(props: Props) { - super(props); - this._handleAction = this._handleAction.bind(this); - this._renderDrawerContent = this._renderDrawerContent.bind(this); - } + props: Props; + state: UIExplorerNavigationState; componentWillMount() { - BackAndroid.addEventListener('hardwareBackPress', this._handleBackButtonPress.bind(this)); + BackAndroid.addEventListener('hardwareBackPress', this._handleBackButtonPress); } componentDidMount() { Linking.getInitialURL().then((url) => { - AsyncStorage.getItem('UIExplorerAppState', (err, storedString) => { + AsyncStorage.getItem(APP_STATE_KEY, (err, storedString) => { const exampleAction = URIActionMap(this.props.exampleFromAppetizeParams); const urlAction = URIActionMap(url); const launchAction = exampleAction || urlAction; @@ -116,7 +120,7 @@ class UIExplorerApp extends React.Component { ); } - _renderDrawerContent() { + _renderDrawerContent = () => { return ( ); - } + }; _renderApp() { const { - externalExample, - stack, + openExample, } = this.state; - if (externalExample) { - const Component = UIExplorerList.Modules[externalExample]; - return ( - { - this._handleAction({ type: 'BackAction' }); - }} - ref={(example) => { this._exampleRef = example; }} - /> - ); - } - const title = UIExplorerStateTitleMap(stack.routes[stack.index]); - const index = stack.routes.length <= 1 ? 1 : stack.index; - if (stack && stack.routes[index]) { - const {key} = stack.routes[index]; - const ExampleModule = UIExplorerList.Modules[key]; - if (ExampleModule) { + if (openExample) { + const ExampleModule = UIExplorerList.Modules[openExample]; + if (ExampleModule.external) { + return ( + { + this._handleAction(UIExplorerActions.Back()); + }} + ref={(example) => { this._exampleRef = example; }} + /> + ); + } else if (ExampleModule) { return ( this.drawer.openDrawer()} style={styles.toolbar} - title={title} + title={ExampleModule.title} /> this.drawer.openDrawer()} style={styles.toolbar} - title={title} + title="UIExplorer" /> ); } - _handleAction(action: Object): boolean { + _handleAction = (action: Object): boolean => { this.drawer && this.drawer.closeDrawer(); const newState = UIExplorerNavigationReducer(this.state, action); if (this.state !== newState) { this.setState( newState, - () => AsyncStorage.setItem('UIExplorerAppState', JSON.stringify(this.state)) + () => AsyncStorage.setItem(APP_STATE_KEY, JSON.stringify(this.state)) ); return true; } return false; - } + }; - _handleBackButtonPress() { + _handleBackButtonPress = () => { if (this._overrideBackPressForDrawerLayout) { // This hack is necessary because drawer layout provides an imperative API // with open and close methods. This code would be cleaner if the drawer @@ -231,8 +213,8 @@ class UIExplorerApp extends React.Component { ) { return true; } - return this._handleAction({ type: 'BackAction' }); - } + return this._handleAction(UIExplorerActions.Back()); + }; } const styles = StyleSheet.create({ diff --git a/Examples/UIExplorer/js/UIExplorerApp.ios.js b/Examples/UIExplorer/js/UIExplorerApp.ios.js index c755f7d76..802413c04 100644 --- a/Examples/UIExplorer/js/UIExplorerApp.ios.js +++ b/Examples/UIExplorer/js/UIExplorerApp.ios.js @@ -27,62 +27,46 @@ const AsyncStorage = require('AsyncStorage'); const Linking = require('Linking'); const React = require('react'); const ReactNative = require('react-native'); +const UIExplorerActions = require('./UIExplorerActions'); const UIExplorerExampleContainer = require('./UIExplorerExampleContainer'); const UIExplorerExampleList = require('./UIExplorerExampleList'); const UIExplorerList = require('./UIExplorerList.ios'); const UIExplorerNavigationReducer = require('./UIExplorerNavigationReducer'); -const UIExplorerStateTitleMap = require('./UIExplorerStateTitleMap'); const URIActionMap = require('./URIActionMap'); const { + Button, AppRegistry, - NavigationExperimental, SnapshotViewIOS, StyleSheet, + Text, View, } = ReactNative; -const { - CardStack: NavigationCardStack, - Header: NavigationHeader, -} = NavigationExperimental; - -import type { NavigationSceneRendererProps } from 'NavigationTypeDefinition'; - -import type { UIExplorerNavigationState } from './UIExplorerNavigationReducer'; - import type { UIExplorerExample } from './UIExplorerList.ios'; +import type { UIExplorerAction } from './UIExplorerActions'; +import type { UIExplorerNavigationState } from './UIExplorerNavigationReducer'; type Props = { exampleFromAppetizeParams: string, }; -type State = UIExplorerNavigationState & { - externalExample?: string, -}; +const APP_STATE_KEY = 'UIExplorerAppState.v2'; -const APP_STATE_KEY = 'UIExplorerAppState.v1'; +const Header = ({ onBack, title}) => ( + + + {title} + + {onBack && +