// @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 {Animated, BackHandler} from "react-native"; import {withCollapsible} from "../../utils/withCollapsible"; import AutoHideHandler from "../../utils/AutoHideHandler"; type Props = { navigation: Object, url: string, customJS: string, collapsibleStack: Object, onMessage: Function, onScroll: Function, } const AnimatedWebView = Animated.createAnimatedComponent(WebView); /** * Class defining a webview screen. */ class WebViewScreen extends React.PureComponent { static defaultProps = { customJS: '', }; webviewRef: Object; hideHandler: AutoHideHandler; canGoBack: boolean; constructor() { super(); this.webviewRef = React.createRef(); this.canGoBack = false; this.hideHandler = new AutoHideHandler(false); this.hideHandler.addListener(this.onHideChange); } /** * 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.getNode().reload(); // Need to call getNode() as we are working with animated components onGoBackClicked = () => this.webviewRef.current.getNode().goBack(); onGoForwardClicked = () => this.webviewRef.current.getNode().goForward(); onOpenClicked = () => Linking.openURL(this.props.url); injectJavaScript = (script: string) => { this.webviewRef.current.getNode().injectJavaScript(script); } /** * Gets the loading indicator * * @return {*} */ getRenderLoading = () => ; getJavascriptPadding(padding: number) { return ( "document.getElementsByTagName('body')[0].style.paddingTop = '" + padding + "px';" + "true;" ); } onScroll = (event: Object) => { this.hideHandler.onScroll(event); if (this.props.onScroll) this.props.onScroll(event); } onHideChange = (shouldHide: boolean) => { this.props.navigation.setParams({hideTabBar: shouldHide}); } render() { const {containerPaddingTop, onScrollWithListener} = this.props.collapsibleStack; return ( } onNavigationStateChange={navState => { this.canGoBack = navState.canGoBack; }} onMessage={this.props.onMessage} onLoad={() => this.injectJavaScript(this.getJavascriptPadding(containerPaddingTop))} // Animations onScroll={onScrollWithListener(this.onScroll)} /> ); } } export default withCollapsible(WebViewScreen);