// @flow import * as React from 'react'; import {Linking, Platform, View} from 'react-native'; import {Spinner, Footer, Right, Left, Body, Tab, TabHeading, Text, Tabs} from 'native-base'; import WebView from "react-native-webview"; import Touchable from "react-native-platform-touchable"; import CustomMaterialIcon from "../components/CustomMaterialIcon"; import ThemeManager from "../utils/ThemeManager"; import BaseContainer from "../components/BaseContainer"; import {ScreenOrientation} from 'expo'; import {NavigationActions} from 'react-navigation'; type Props = { navigation: Object, data: Array<{ url: string, icon: string, name: string, customJS: string }>, headerTitle: string, hasHeaderBackButton: boolean, hasSideMenu: boolean, hasFooter: boolean, } type State = { isLandscape: boolean, } /** * Class defining a webview screen. */ export default class WebViewScreen extends React.Component { static defaultProps = { hasBackButton: false, hasSideMenu: true, hasFooter: true, }; state = { isLandscape: false, }; webviewArray: Array = []; willFocusSubscription: function; willBlurSubscription: function; /** * Register for blur event to close side menu on screen change */ componentDidMount() { this.willFocusSubscription = this.props.navigation.addListener( 'willFocus', payload => { ScreenOrientation.unlockAsync(); ScreenOrientation.addOrientationChangeListener((OrientationChangeEvent) => { let isLandscape = OrientationChangeEvent.orientationInfo.orientation === ScreenOrientation.Orientation.LANDSCAPE || OrientationChangeEvent.orientationInfo.orientation === ScreenOrientation.Orientation.LANDSCAPE_LEFT || OrientationChangeEvent.orientationInfo.orientation === ScreenOrientation.Orientation.LANDSCAPE_RIGHT; this.setState({isLandscape: isLandscape}); const setParamsAction = NavigationActions.setParams({ params: {showTabBar: !isLandscape}, key: this.props.navigation.state.key, }); this.props.navigation.dispatch(setParamsAction); }); } ); this.willBlurSubscription = this.props.navigation.addListener( 'willBlur', payload => { ScreenOrientation.lockAsync(ScreenOrientation.Orientation.PORTRAIT); } ); } /** * Unregister from event when un-mounting components */ componentWillUnmount() { if (this.willBlurSubscription !== undefined) this.willBlurSubscription.remove(); if (this.willFocusSubscription !== undefined) this.willFocusSubscription.remove(); } openWebLink(url: string) { Linking.openURL(url).catch((err) => console.error('Error opening link', err)); } getHeaderButton(clickAction: Function, icon: string) { return ( clickAction()}> ); } getRefreshButton() { return ( {this.getHeaderButton(() => this.refreshWebview(), 'refresh')} ); }; refreshWebview() { for (let view of this.webviewArray) { view.reload(); } } goBackWebview() { for (let view of this.webviewArray) { view.goBack(); } } goForwardWebview() { for (let view of this.webviewArray) { view.goForward(); } } getWebview(obj: Object) { return ( (this.webviewArray.push(ref))} source={{uri: obj['url']}} style={{ width: '100%', height: '100%', }} startInLoadingState={true} injectedJavaScript={obj['customJS']} javaScriptEnabled={true} renderLoading={() => } /> ); } getTabbedWebview() { let tabbedView = []; for (let i = 0; i < this.props.data.length; i++) { tabbedView.push( {this.props.data[i]['name']} } key={this.props.data[i]['url']} style={{backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor}}> {this.getWebview(this.props.data[i])} ); } return tabbedView; } render() { const nav = this.props.navigation; return ( {this.props.data.length === 1 ? this.getWebview(this.props.data[0]) : {this.getTabbedWebview()} } {this.props.hasFooter && this.props.data.length === 1 ?
{this.getHeaderButton(() => this.openWebLink(this.props.data[0]['url']), 'open-in-new')} {this.getHeaderButton(() => this.goBackWebview(), 'chevron-left')} {this.getHeaderButton(() => this.goForwardWebview(), 'chevron-right')}
: }
); } }