|
| 1 | +/** |
| 2 | + * The examples provided by Facebook are for non-commercial testing and |
| 3 | + * evaluation purposes only. |
| 4 | + * |
| 5 | + * Facebook reserves all rights not expressly granted. |
| 6 | + * |
| 7 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 8 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL |
| 10 | + * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 11 | + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 12 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 13 | + * |
| 14 | + * @providesModule AppStateIOSExample |
| 15 | + * @flow |
| 16 | + */ |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +var React = require('react'); |
| 20 | +var ReactNative = require('react-native'); |
| 21 | +var { |
| 22 | + AppStateIOS, |
| 23 | + Text, |
| 24 | + View |
| 25 | +} = ReactNative; |
| 26 | + |
| 27 | +var AppStateSubscription = React.createClass({ |
| 28 | + getInitialState() { |
| 29 | + return { |
| 30 | + appState: AppStateIOS.currentState, |
| 31 | + previousAppStates: [], |
| 32 | + memoryWarnings: 0, |
| 33 | + }; |
| 34 | + }, |
| 35 | + componentDidMount: function() { |
| 36 | + AppStateIOS.addEventListener('change', this._handleAppStateChange); |
| 37 | + AppStateIOS.addEventListener('memoryWarning', this._handleMemoryWarning); |
| 38 | + }, |
| 39 | + componentWillUnmount: function() { |
| 40 | + AppStateIOS.removeEventListener('change', this._handleAppStateChange); |
| 41 | + AppStateIOS.removeEventListener('memoryWarning', this._handleMemoryWarning); |
| 42 | + }, |
| 43 | + _handleMemoryWarning: function() { |
| 44 | + this.setState({memoryWarnings: this.state.memoryWarnings + 1}); |
| 45 | + }, |
| 46 | + _handleAppStateChange: function(appState) { |
| 47 | + var previousAppStates = this.state.previousAppStates.slice(); |
| 48 | + previousAppStates.push(this.state.appState); |
| 49 | + this.setState({ |
| 50 | + appState, |
| 51 | + previousAppStates, |
| 52 | + }); |
| 53 | + }, |
| 54 | + render() { |
| 55 | + if (this.props.showMemoryWarnings) { |
| 56 | + return ( |
| 57 | + <View> |
| 58 | + <Text>{this.state.memoryWarnings}</Text> |
| 59 | + </View> |
| 60 | + ); |
| 61 | + } |
| 62 | + if (this.props.showCurrentOnly) { |
| 63 | + return ( |
| 64 | + <View> |
| 65 | + <Text>{this.state.appState}</Text> |
| 66 | + </View> |
| 67 | + ); |
| 68 | + } |
| 69 | + return ( |
| 70 | + <View> |
| 71 | + <Text>{JSON.stringify(this.state.previousAppStates)}</Text> |
| 72 | + </View> |
| 73 | + ); |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +exports.title = 'AppStateIOS'; |
| 78 | +exports.description = 'iOS app background status'; |
| 79 | +exports.examples = [ |
| 80 | + { |
| 81 | + title: 'AppStateIOS.currentState', |
| 82 | + description: 'Can be null on app initialization', |
| 83 | + render() { return <Text>{AppStateIOS.currentState}</Text>; } |
| 84 | + }, |
| 85 | + { |
| 86 | + title: 'Subscribed AppStateIOS:', |
| 87 | + description: 'This changes according to the current state, so you can only ever see it rendered as "active"', |
| 88 | + render(): ReactElement { return <AppStateSubscription showCurrentOnly={true} />; } |
| 89 | + }, |
| 90 | + { |
| 91 | + title: 'Previous states:', |
| 92 | + render(): ReactElement { return <AppStateSubscription showCurrentOnly={false} />; } |
| 93 | + }, |
| 94 | + { |
| 95 | + title: 'Memory Warnings', |
| 96 | + description: 'In the simulator, hit Shift+Command+M to simulate a memory warning.', |
| 97 | + render(): ReactElement { return <AppStateSubscription showMemoryWarnings={true} />; } |
| 98 | + }, |
| 99 | +]; |
0 commit comments