|
| 1 | +// @flow |
| 2 | +import {Card, CardHeader, CardActions, CardText} from 'material-ui/Card'; |
| 3 | +import AndroidIcon from 'material-ui/svg-icons/action/android'; |
| 4 | +import IconButton from 'material-ui/IconButton'; |
| 5 | +import open from 'open'; |
| 6 | +import RaisedButton from 'material-ui/RaisedButton'; |
| 7 | +import React, {PureComponent} from 'react'; |
| 8 | + |
| 9 | +import {adjustWithCard} from '../../styles/vars/metrics'; |
| 10 | +import {lightInfoMessage, linkStyle} from '../../styles/main'; |
| 11 | +import AndroidRunOptions from './RunOptions'; |
| 12 | +import Animated from '../Base/Animated'; |
| 13 | +import Terminal from '../Terminal/Console'; |
| 14 | + |
| 15 | +class RunAndroid extends PureComponent<$RunAndroidProps, $RunAndroidState> { |
| 16 | + state = { |
| 17 | + options: {}, |
| 18 | + running: false, |
| 19 | + showTerminal: false, |
| 20 | + }; |
| 21 | + terminal: Terminal; |
| 22 | + render = () => ( |
| 23 | + <Animated rubberBand> |
| 24 | + <Card> |
| 25 | + <CardHeader |
| 26 | + actAsExpander |
| 27 | + avatar={<IconButton><AndroidIcon /></IconButton>} |
| 28 | + showExpandableButton |
| 29 | + subtitle="Install your app on Android device (or emulator)" |
| 30 | + title="Run Android" |
| 31 | + /> |
| 32 | + <CardText expandable> |
| 33 | + <AndroidRunOptions |
| 34 | + onChange={this.onChangeOption} |
| 35 | + /> |
| 36 | + </CardText> |
| 37 | + <CardActions> |
| 38 | + <div style={{marginLeft: adjustWithCard}}> |
| 39 | + <RaisedButton |
| 40 | + primary={!this.state.running} |
| 41 | + secondary={this.state.running} |
| 42 | + label={this.state.running ? 'Stop' : 'Run'} |
| 43 | + onClick={this.onActionClick} |
| 44 | + /> |
| 45 | + {this.state.running && ( |
| 46 | + <span> |
| 47 | + <span style={lightInfoMessage}> |
| 48 | + Might not always terminate sub shells |
| 49 | + </span> |
| 50 | + <span style={linkStyle} onClick={this.seeBug1}> |
| 51 | + See issue |
| 52 | + </span> |
| 53 | + </span> |
| 54 | + )} |
| 55 | + </div> |
| 56 | + </CardActions> |
| 57 | + <CardText> |
| 58 | + {this.state.showTerminal && ( |
| 59 | + <Terminal |
| 60 | + command={this.makeCommand()} |
| 61 | + cwd={this.props.app.path} |
| 62 | + onDone={() => this.setState({running: false})} |
| 63 | + onFail={() => this.setState({running: false})} |
| 64 | + ref={(terminal) => { |
| 65 | + if (!this.terminal && terminal) { |
| 66 | + this.terminal = terminal; |
| 67 | + } |
| 68 | + }} |
| 69 | + /> |
| 70 | + )} |
| 71 | + </CardText> |
| 72 | + </Card> |
| 73 | + </Animated> |
| 74 | + ); |
| 75 | + onActionClick = () => { |
| 76 | + if (this.state.running) { |
| 77 | + this.terminal.stop(); |
| 78 | + setTimeout(() => { |
| 79 | + this.setState({running: false}); |
| 80 | + }, 1500); |
| 81 | + } else if (this.state.showTerminal) { |
| 82 | + this.setState({showTerminal: false}, () => { |
| 83 | + setTimeout(() => { |
| 84 | + this.setState({running: true, showTerminal: true}); |
| 85 | + }, 500); |
| 86 | + }); |
| 87 | + } else { |
| 88 | + this.setState({running: true, showTerminal: true}); |
| 89 | + } |
| 90 | + }; |
| 91 | + onChangeOption = (key: string, option: $CliOptions, value: $AnyPrimitive) => this.setState({ |
| 92 | + options: { |
| 93 | + ...this.state.options, |
| 94 | + [key]: value, |
| 95 | + }, |
| 96 | + }); |
| 97 | + makeCommand = () => { |
| 98 | + const cmd = 'react-native run-android'; |
| 99 | + const options = []; |
| 100 | + for (const option in this.state.options) { |
| 101 | + if (typeof this.state.options[option] === 'boolean') { |
| 102 | + options.push(`--${option}`); |
| 103 | + } else { |
| 104 | + options.push(`--${option} "${this.state.options[option]}"`); |
| 105 | + } |
| 106 | + } |
| 107 | + return `${cmd} ${options.join(' ')}`; |
| 108 | + }; |
| 109 | + seeBug1 = () => open('https://github.com/co2-git/ReactNative/issues/35'); |
| 110 | +} |
| 111 | + |
| 112 | +export default RunAndroid; |
0 commit comments