// @flow import * as React from 'react'; import WebView from "react-native-webview"; import BasicLoadingScreen from "../Custom/BasicLoadingScreen"; import ErrorView from "../Custom/ErrorView"; import {ERROR_TYPE} from "../../utils/WebData"; import MaterialHeaderButtons, {Item} from '../Custom/HeaderButton'; import {HiddenItem} from "react-navigation-header-buttons"; import {Linking} from "expo"; import i18n from 'i18n-js'; import {BackHandler} from "react-native"; type Props = { navigation: Object, url: string, customJS: string, } /** * Class defining a webview screen. */ class WebViewScreen extends React.PureComponent { static defaultProps = { customJS: '', }; webviewRef: Object; canGoBack: boolean; constructor() { super(); this.webviewRef = React.createRef(); this.canGoBack = false; } /** * Creates refresh button after mounting */ componentDidMount() { const rightButton = this.getRefreshButton.bind(this); this.props.navigation.setOptions({ headerRight: rightButton, }); this.props.navigation.addListener( 'focus', () => BackHandler.addEventListener( 'hardwareBackPress', this.onBackButtonPressAndroid ) ); this.props.navigation.addListener( 'blur', () => BackHandler.removeEventListener( 'hardwareBackPress', this.onBackButtonPressAndroid ) ); } onBackButtonPressAndroid = () => { if (this.canGoBack){ this.onGoBackClicked(); return true; } return false; }; /** * Gets a header refresh button * * @return {*} */ getRefreshButton() { return ( ); }; /** * Callback to use when refresh button is clicked. Reloads the webview. */ onRefreshClicked = () => this.webviewRef.current.reload(); onGoBackClicked = () => this.webviewRef.current.goBack(); onGoForwardClicked = () => this.webviewRef.current.goForward(); onOpenClicked = () => Linking.openURL(this.props.url); /** * Gets the loading indicator * * @return {*} */ getRenderLoading = () => ; render() { return ( } onNavigationStateChange={navState => { this.canGoBack = navState.canGoBack; }} /> ); } } export default WebViewScreen;