Skip to content

Commit 1b92c6f

Browse files
committed
fix(UIExplorerApp): Update UIExplorerApp
Ensures the UIExplorer app is compatible with the recent rebase.
1 parent e705e34 commit 1b92c6f

File tree

1 file changed

+104
-61
lines changed

1 file changed

+104
-61
lines changed

Examples/UIExplorer/UIExplorerApp.windows.js

Lines changed: 104 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
29
* The examples provided by Facebook are for non-commercial testing and
310
* evaluation purposes only.
411
*
@@ -16,48 +23,66 @@
1623
*/
1724
'use strict';
1825

26+
const AppRegistry = require('AppRegistry');
27+
const AsyncStorage = require('AsyncStorage');
28+
const BackAndroid = require('BackAndroid');
29+
const Dimensions = require('Dimensions');
30+
const SplitViewWindows = require('SplitViewWindows');
31+
const Linking = require('Linking');
1932
const React = require('react');
20-
const ReactNative = require('react-native');
21-
const {
22-
AppRegistry,
23-
BackAndroid,
24-
Dimensions,
25-
NavigationExperimental,
26-
SplitViewWindows,
27-
StyleSheet,
28-
View,
29-
StatusBar,
30-
} = ReactNative;
31-
const {
32-
RootContainer: NavigationRootContainer,
33-
} = NavigationExperimental;
34-
const UIExplorerActions = require('./UIExplorerActions');
33+
const StatusBar = require('StatusBar');
34+
const StyleSheet = require('StyleSheet');
3535
const UIExplorerExampleList = require('./UIExplorerExampleList');
3636
const UIExplorerList = require('./UIExplorerList');
3737
const UIExplorerNavigationReducer = require('./UIExplorerNavigationReducer');
3838
const UIExplorerStateTitleMap = require('./UIExplorerStateTitleMap');
3939
const UIExplorerHeaderWindows = require('./UIExplorerHeaderWindows');
40+
const URIActionMap = require('./URIActionMap');
41+
const View = require('View');
42+
43+
const DRAWER_WIDTH_LEFT = 56;
4044

41-
var DRAWER_WIDTH_LEFT = 56;
45+
type Props = {
46+
exampleFromAppetizeParams: string,
47+
};
48+
49+
type State = {
50+
initialExampleUri: ?string,
51+
};
4252

4353
class UIExplorerApp extends React.Component {
54+
_handleAction: Function;
55+
_renderPaneContent: Function;
56+
state: State;
57+
constructor(props: Props) {
58+
super(props);
59+
this._handleAction = this._handleAction.bind(this);
60+
this._renderPaneContent = this._renderPaneContent.bind(this);
61+
}
62+
4463
componentWillMount() {
4564
BackAndroid.addEventListener('hardwareBackPress', this._handleBackButtonPress.bind(this));
4665
}
47-
48-
render() {
49-
return (
50-
<NavigationRootContainer
51-
persistenceKey="UIExplorerStateNavState"
52-
ref={navRootRef => { this._navigationRootRef = navRootRef; }}
53-
reducer={UIExplorerNavigationReducer}
54-
renderNavigation={this._renderApp.bind(this)}
55-
/>
56-
);
66+
67+
componentDidMount() {
68+
AsyncStorage.getItem('UIExplorerAppState', (err, storedString) => {
69+
const exampleAction = URIActionMap(this.props.exampleFromAppetizeParams);
70+
if (err || !storedString) {
71+
const initialAction = exampleAction || {type: 'InitialAction'};
72+
this.setState(UIExplorerNavigationReducer(null, initialAction));
73+
return;
74+
}
75+
const storedState = JSON.parse(storedString);
76+
if (exampleAction) {
77+
this.setState(UIExplorerNavigationReducer(storedState, exampleAction));
78+
return;
79+
}
80+
this.setState(storedState);
81+
});
5782
}
58-
59-
_renderApp(navigationState, onNavigate) {
60-
if (!navigationState) {
83+
84+
render() {
85+
if (!this.state) {
6186
return null;
6287
}
6388
return (
@@ -72,49 +97,51 @@ class UIExplorerApp extends React.Component {
7297
this._overrideBackPressForSplitView = false;
7398
}}
7499
ref={(splitView) => { this.splitView = splitView; }}
75-
renderPaneView={this._renderPaneContent.bind(this, onNavigate)}>
76-
{this._renderNavigation(navigationState, onNavigate)}
100+
renderPaneView={this._renderPaneContent}
101+
statusBarBackgroundColor="#589c90">
102+
{this._renderApp()}
77103
</SplitViewWindows>
78104
);
79105
}
80-
81-
_renderPaneContent(onNavigate) {
106+
107+
_renderPaneContent() {
82108
return (
83-
<UIExplorerExampleList
84-
list={UIExplorerList}
85-
displayTitleRow={true}
86-
disableSearch={true}
87-
onNavigate={(action) => {
88-
this.splitView && this.splitView.closePane();
89-
onNavigate(action);
90-
}}
91-
/>
109+
<View style={styles.paneContentWrapper}>
110+
<UIExplorerExampleList
111+
list={UIExplorerList}
112+
displayTitleRow={true}
113+
disableSearch={true}
114+
onNavigate={this._handleAction}
115+
/>
116+
</View>
92117
);
93118
}
94119

95-
_renderNavigation(navigationState, onNavigate) {
96-
if (navigationState.externalExample) {
97-
var Component = UIExplorerList.Modules[navigationState.externalExample];
120+
_renderApp() {
121+
const {
122+
externalExample,
123+
stack,
124+
} = this.state;
125+
if (externalExample) {
126+
const Component = UIExplorerList.Modules[externalExample];
98127
return (
99128
<Component
100129
onExampleExit={() => {
101-
onNavigate(NavigationRootContainer.getBackAction());
130+
this._handleAction({ type: 'BackAction' });
102131
}}
103132
ref={(example) => { this._exampleRef = example; }}
104133
/>
105134
);
106135
}
107-
const {stack} = navigationState;
108136
const title = UIExplorerStateTitleMap(stack.children[stack.index]);
109-
if (stack && stack.children[1]) {
110-
const {key} = stack.children[1];
137+
const index = stack.children.length <= 1 ? 1 : stack.index;
138+
139+
if (stack && stack.children[index]) {
140+
const {key} = stack.children[index];
111141
const ExampleModule = UIExplorerList.Modules[key];
112142
const ExampleComponent = UIExplorerExampleList.makeRenderable(ExampleModule);
113143
return (
114144
<View style={styles.container}>
115-
<StatusBar
116-
backgroundColor="#589c90"
117-
/>
118145
<UIExplorerHeaderWindows
119146
onPress={() => this.splitView.openPane()}
120147
title={title}
@@ -128,36 +155,47 @@ class UIExplorerApp extends React.Component {
128155
}
129156
return (
130157
<View style={styles.container}>
131-
<StatusBar
132-
backgroundColor="#589c90"
133-
/>
134158
<UIExplorerHeaderWindows
135159
onPress={() => this.splitView.openPane()}
136160
title={title}
137161
style={styles.header}
138162
/>
139163
<UIExplorerExampleList
164+
onNavigate={this._handleAction}
140165
list={UIExplorerList}
141166
{...stack.children[0]}
142167
/>
143168
</View>
144169
);
145170
}
146171

172+
_handleAction(action: Object): boolean {
173+
this.splitView && this.splitView.closePane();
174+
const newState = UIExplorerNavigationReducer(this.state, action);
175+
if (this.state !== newState) {
176+
this.setState(newState);
177+
AsyncStorage.setItem('UIExplorerAppState', JSON.stringify(newState));
178+
return true;
179+
}
180+
return false;
181+
}
182+
147183
_handleBackButtonPress() {
184+
if (this._overrideBackPressForSplitView) {
185+
// This hack is necessary because split view provides an imperative API
186+
// with open and close methods. This code would be cleaner if the split
187+
// view provided an `isOpen` prop and allowed us to pass a `onPaneClose` handler.
188+
this.splitView && this.splitView.closePane();
189+
return true;
190+
}
148191
if (
149192
this._exampleRef &&
150193
this._exampleRef.handleBackAction &&
151194
this._exampleRef.handleBackAction()
152195
) {
153196
return true;
154197
}
155-
if (this._navigationRootRef) {
156-
return this._navigationRootRef.handleNavigation(
157-
NavigationRootContainer.getBackAction()
158-
);
159-
}
160-
return false;
198+
return this._handleAction({ type: 'BackAction' });
161199
}
162200
}
163201

@@ -168,6 +206,11 @@ const styles = StyleSheet.create({
168206
header: {
169207
backgroundColor: '#E9EAED',
170208
},
209+
paneContentWrapper: {
210+
flex: 1,
211+
paddingTop: StatusBar.currentHeight,
212+
backgroundColor: 'white',
213+
},
171214
});
172215

173216
AppRegistry.registerComponent('UIExplorerApp', () => UIExplorerApp);

0 commit comments

Comments
 (0)