2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-03-24 22:48:27 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
2017-05-06 03:50:47 +00:00
|
|
|
* @providesModule RNTesterApp
|
2016-02-23 00:15:35 +00:00
|
|
|
* @flow
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-05 23:51:34 +00:00
|
|
|
const AsyncStorage = require('AsyncStorage');
|
2017-03-07 05:41:51 +00:00
|
|
|
const BackHandler = require('BackHandler');
|
2016-05-05 23:51:34 +00:00
|
|
|
const Linking = require('Linking');
|
2016-04-09 03:36:40 +00:00
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
2017-05-06 03:50:47 +00:00
|
|
|
const RNTesterActions = require('./RNTesterActions');
|
|
|
|
const RNTesterExampleContainer = require('./RNTesterExampleContainer');
|
|
|
|
const RNTesterExampleList = require('./RNTesterExampleList');
|
|
|
|
const RNTesterList = require('./RNTesterList.ios');
|
|
|
|
const RNTesterNavigationReducer = require('./RNTesterNavigationReducer');
|
2016-04-04 13:04:54 +00:00
|
|
|
const URIActionMap = require('./URIActionMap');
|
2016-02-23 00:15:35 +00:00
|
|
|
|
2016-02-20 00:55:07 +00:00
|
|
|
const {
|
2017-02-28 23:39:24 +00:00
|
|
|
Button,
|
2015-02-19 14:57:05 +00:00
|
|
|
AppRegistry,
|
2016-02-23 00:15:35 +00:00
|
|
|
SnapshotViewIOS,
|
2015-01-30 01:10:49 +00:00
|
|
|
StyleSheet,
|
2017-02-28 23:39:24 +00:00
|
|
|
Text,
|
2016-02-03 14:40:39 +00:00
|
|
|
View,
|
2017-11-29 16:51:08 +00:00
|
|
|
SafeAreaView
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2016-02-29 21:21:43 +00:00
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
import type { RNTesterExample } from './RNTesterList.ios';
|
|
|
|
import type { RNTesterAction } from './RNTesterActions';
|
|
|
|
import type { RNTesterNavigationState } from './RNTesterNavigationReducer';
|
2016-02-23 00:15:35 +00:00
|
|
|
|
2016-04-04 13:04:54 +00:00
|
|
|
type Props = {
|
|
|
|
exampleFromAppetizeParams: string,
|
|
|
|
};
|
2016-02-23 00:15:44 +00:00
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
const APP_STATE_KEY = 'RNTesterAppState.v2';
|
2016-02-23 00:15:44 +00:00
|
|
|
|
2017-05-26 05:54:51 +00:00
|
|
|
const Header = ({ onBack, title }: { onBack?: () => mixed, title: string }) => (
|
2017-11-29 16:51:08 +00:00
|
|
|
<SafeAreaView style={styles.headerContainer}>
|
|
|
|
<View style={styles.header}>
|
|
|
|
<View style={styles.headerCenter}>
|
|
|
|
<Text style={styles.title}>{title}</Text>
|
|
|
|
</View>
|
|
|
|
{onBack && <View style={styles.headerLeft}>
|
|
|
|
<Button title="Back" onPress={onBack} />
|
|
|
|
</View>}
|
2017-02-28 23:39:24 +00:00
|
|
|
</View>
|
2017-11-29 16:51:08 +00:00
|
|
|
</SafeAreaView>
|
2017-02-28 23:39:24 +00:00
|
|
|
);
|
2016-05-27 22:30:59 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
|
2018-02-08 18:26:45 +00:00
|
|
|
UNSAFE_componentWillMount() {
|
2017-03-07 05:41:51 +00:00
|
|
|
BackHandler.addEventListener('hardwareBackPress', this._handleBack);
|
|
|
|
}
|
|
|
|
|
2016-04-04 13:04:54 +00:00
|
|
|
componentDidMount() {
|
2016-05-05 23:51:34 +00:00
|
|
|
Linking.getInitialURL().then((url) => {
|
2016-05-27 22:30:59 +00:00
|
|
|
AsyncStorage.getItem(APP_STATE_KEY, (err, storedString) => {
|
2016-05-05 23:51:34 +00:00
|
|
|
const exampleAction = URIActionMap(this.props.exampleFromAppetizeParams);
|
|
|
|
const urlAction = URIActionMap(url);
|
|
|
|
const launchAction = exampleAction || urlAction;
|
|
|
|
if (err || !storedString) {
|
|
|
|
const initialAction = launchAction || {type: 'InitialAction'};
|
2017-05-06 03:50:47 +00:00
|
|
|
this.setState(RNTesterNavigationReducer(undefined, initialAction));
|
2016-05-05 23:51:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const storedState = JSON.parse(storedString);
|
|
|
|
if (launchAction) {
|
2017-05-06 03:50:47 +00:00
|
|
|
this.setState(RNTesterNavigationReducer(storedState, launchAction));
|
2016-05-05 23:51:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState(storedState);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Linking.addEventListener('url', (url) => {
|
|
|
|
this._handleAction(URIActionMap(url));
|
|
|
|
});
|
2016-02-23 00:15:35 +00:00
|
|
|
}
|
2016-05-05 23:51:34 +00:00
|
|
|
|
2017-02-28 23:39:24 +00:00
|
|
|
_handleBack = () => {
|
2017-05-06 03:50:47 +00:00
|
|
|
this._handleAction(RNTesterActions.Back());
|
2017-02-28 23:39:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
_handleAction = (action: ?RNTesterAction) => {
|
2016-05-05 23:51:34 +00:00
|
|
|
if (!action) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-06 03:50:47 +00:00
|
|
|
const newState = RNTesterNavigationReducer(this.state, action);
|
2016-05-05 23:51:34 +00:00
|
|
|
if (this.state !== newState) {
|
2017-02-09 22:14:56 +00:00
|
|
|
this.setState(
|
|
|
|
newState,
|
|
|
|
() => AsyncStorage.setItem(APP_STATE_KEY, JSON.stringify(this.state))
|
|
|
|
);
|
2016-04-04 13:04:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-05 23:51:34 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
if (!this.state) {
|
2016-02-23 00:15:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
2017-02-28 23:39:24 +00:00
|
|
|
if (this.state.openExample) {
|
2017-05-06 03:50:47 +00:00
|
|
|
const Component = RNTesterList.Modules[this.state.openExample];
|
2017-02-28 23:39:24 +00:00
|
|
|
if (Component.external) {
|
|
|
|
return (
|
|
|
|
<Component
|
|
|
|
onExampleExit={this._handleBack}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<View style={styles.exampleContainer}>
|
|
|
|
<Header onBack={this._handleBack} title={Component.title} />
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterExampleContainer module={Component} />
|
2017-02-28 23:39:24 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-02-23 00:15:35 +00:00
|
|
|
|
2017-02-28 23:39:24 +00:00
|
|
|
}
|
2016-03-24 22:48:27 +00:00
|
|
|
return (
|
2017-02-28 23:39:24 +00:00
|
|
|
<View style={styles.exampleContainer}>
|
2017-05-06 03:50:47 +00:00
|
|
|
<Header title="RNTester" />
|
2017-08-29 21:54:33 +00:00
|
|
|
{/* $FlowFixMe(>=0.53.0 site=react_native_fb,react_native_oss) This
|
|
|
|
* comment suppresses an error when upgrading Flow's support for
|
|
|
|
* React. To see the error delete this comment and run Flow. */}
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterExampleList
|
2016-05-05 23:51:34 +00:00
|
|
|
onNavigate={this._handleAction}
|
2017-05-06 03:50:47 +00:00
|
|
|
list={RNTesterList}
|
2016-02-23 00:15:35 +00:00
|
|
|
/>
|
2017-02-28 23:39:24 +00:00
|
|
|
</View>
|
|
|
|
);
|
2016-02-23 00:15:35 +00:00
|
|
|
}
|
2016-02-20 00:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2017-11-29 16:51:08 +00:00
|
|
|
headerContainer: {
|
2017-02-28 23:39:24 +00:00
|
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
|
|
borderBottomColor: '#96969A',
|
|
|
|
backgroundColor: '#F5F5F6',
|
2017-11-29 16:51:08 +00:00
|
|
|
},
|
|
|
|
header: {
|
|
|
|
height: 40,
|
|
|
|
flexDirection: 'row'
|
2017-02-28 23:39:24 +00:00
|
|
|
},
|
|
|
|
headerLeft: {
|
|
|
|
},
|
|
|
|
headerCenter: {
|
2015-01-30 01:10:49 +00:00
|
|
|
flex: 1,
|
2017-02-28 23:39:24 +00:00
|
|
|
position: 'absolute',
|
2017-11-29 16:51:08 +00:00
|
|
|
top: 7,
|
2017-02-28 23:39:24 +00:00
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
fontSize: 19,
|
|
|
|
fontWeight: '600',
|
|
|
|
textAlign: 'center',
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
2016-02-23 00:15:35 +00:00
|
|
|
exampleContainer: {
|
|
|
|
flex: 1,
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
2015-11-12 19:40:02 +00:00
|
|
|
});
|
|
|
|
|
2016-02-23 00:15:35 +00:00
|
|
|
AppRegistry.registerComponent('SetPropertiesExampleApp', () => require('./SetPropertiesExampleApp'));
|
|
|
|
AppRegistry.registerComponent('RootViewSizeFlexibilityExampleApp', () => require('./RootViewSizeFlexibilityExampleApp'));
|
2017-05-06 03:50:47 +00:00
|
|
|
AppRegistry.registerComponent('RNTesterApp', () => RNTesterApp);
|
2016-02-23 00:15:35 +00:00
|
|
|
|
|
|
|
// Register suitable examples for snapshot tests
|
2017-05-06 03:50:47 +00:00
|
|
|
RNTesterList.ComponentExamples.concat(RNTesterList.APIExamples).forEach((Example: RNTesterExample) => {
|
2016-02-23 00:15:35 +00:00
|
|
|
const ExampleModule = Example.module;
|
|
|
|
if (ExampleModule.displayName) {
|
2017-08-18 01:36:54 +00:00
|
|
|
class Snapshotter extends React.Component<{}> {
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2016-02-23 00:15:35 +00:00
|
|
|
return (
|
|
|
|
<SnapshotViewIOS>
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterExampleContainer module={ExampleModule} />
|
2016-02-23 00:15:35 +00:00
|
|
|
</SnapshotViewIOS>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 00:15:35 +00:00
|
|
|
AppRegistry.registerComponent(ExampleModule.displayName, () => Snapshotter);
|
|
|
|
}
|
|
|
|
});
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
module.exports = RNTesterApp;
|