2019-11-02 15:44:19 +01:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
import {Linking, Platform, View} from 'react-native';
|
2019-11-08 02:37:38 +01:00
|
|
|
import {Spinner, Footer, Right, Left, Body, Tab, TabHeading, Text, Tabs} from 'native-base';
|
2019-11-02 15:44:19 +01:00
|
|
|
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";
|
2019-11-08 03:56:29 +01:00
|
|
|
import {ScreenOrientation} from 'expo';
|
|
|
|
import {NavigationActions} from 'react-navigation';
|
2019-11-02 15:44:19 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
navigation: Object,
|
2019-11-08 02:37:38 +01:00
|
|
|
data: Array<{
|
|
|
|
url: string,
|
|
|
|
icon: string,
|
|
|
|
name: string,
|
|
|
|
customJS: string
|
|
|
|
}>,
|
2019-11-02 15:44:19 +01:00
|
|
|
headerTitle: string,
|
|
|
|
hasHeaderBackButton: boolean,
|
|
|
|
hasSideMenu: boolean,
|
2019-11-07 10:57:58 +01:00
|
|
|
hasFooter: boolean,
|
2019-11-02 15:44:19 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 03:56:29 +01:00
|
|
|
type State = {
|
|
|
|
isLandscape: boolean,
|
|
|
|
}
|
|
|
|
|
2019-11-02 15:44:19 +01:00
|
|
|
/**
|
|
|
|
* Class defining a webview screen.
|
|
|
|
*/
|
2019-11-08 03:56:29 +01:00
|
|
|
export default class WebViewScreen extends React.Component<Props, State> {
|
2019-11-02 15:44:19 +01:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
hasBackButton: false,
|
|
|
|
hasSideMenu: true,
|
2019-11-07 10:57:58 +01:00
|
|
|
hasFooter: true,
|
2019-11-02 15:44:19 +01:00
|
|
|
};
|
|
|
|
|
2019-11-08 03:56:29 +01:00
|
|
|
state = {
|
|
|
|
isLandscape: false,
|
|
|
|
};
|
|
|
|
|
2019-11-08 02:37:38 +01:00
|
|
|
webviewArray: Array<WebView> = [];
|
2019-11-08 03:56:29 +01:00
|
|
|
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();
|
|
|
|
}
|
2019-11-02 15:44:19 +01:00
|
|
|
|
2019-11-08 02:37:38 +01:00
|
|
|
openWebLink(url: string) {
|
|
|
|
Linking.openURL(url).catch((err) => console.error('Error opening link', err));
|
2019-11-02 15:44:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getHeaderButton(clickAction: Function, icon: string) {
|
|
|
|
return (
|
|
|
|
<Touchable
|
|
|
|
style={{padding: 6}}
|
|
|
|
onPress={() => clickAction()}>
|
|
|
|
<CustomMaterialIcon
|
|
|
|
color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
|
|
|
|
icon={icon}/>
|
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getRefreshButton() {
|
|
|
|
return (
|
|
|
|
<View style={{flexDirection: 'row'}}>
|
|
|
|
{this.getHeaderButton(() => this.refreshWebview(), 'refresh')}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
refreshWebview() {
|
2019-11-08 02:37:38 +01:00
|
|
|
for (let view of this.webviewArray) {
|
|
|
|
view.reload();
|
|
|
|
}
|
2019-11-02 15:44:19 +01:00
|
|
|
}
|
|
|
|
|
2019-11-07 10:57:58 +01:00
|
|
|
goBackWebview() {
|
2019-11-08 02:37:38 +01:00
|
|
|
for (let view of this.webviewArray) {
|
|
|
|
view.goBack();
|
|
|
|
}
|
2019-11-07 10:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
goForwardWebview() {
|
2019-11-08 02:37:38 +01:00
|
|
|
for (let view of this.webviewArray) {
|
|
|
|
view.goForward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getWebview(obj: Object) {
|
|
|
|
return (
|
|
|
|
<WebView
|
|
|
|
ref={ref => (this.webviewArray.push(ref))}
|
|
|
|
source={{uri: obj['url']}}
|
|
|
|
style={{
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
}}
|
|
|
|
startInLoadingState={true}
|
|
|
|
injectedJavaScript={obj['customJS']}
|
|
|
|
javaScriptEnabled={true}
|
|
|
|
renderLoading={() =>
|
|
|
|
<View style={{
|
|
|
|
backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor,
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
flex: 1,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
}}>
|
|
|
|
<Spinner/>
|
|
|
|
</View>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getTabbedWebview() {
|
|
|
|
let tabbedView = [];
|
|
|
|
for (let i = 0; i < this.props.data.length; i++) {
|
|
|
|
tabbedView.push(
|
|
|
|
<Tab heading={
|
|
|
|
<TabHeading>
|
|
|
|
<CustomMaterialIcon
|
|
|
|
icon={this.props.data[i]['icon']}
|
|
|
|
color={ThemeManager.getCurrentThemeVariables().tabIconColor}
|
|
|
|
fontSize={20}
|
|
|
|
/>
|
|
|
|
<Text>{this.props.data[i]['name']}</Text>
|
|
|
|
</TabHeading>}
|
|
|
|
key={this.props.data[i]['url']}
|
|
|
|
style={{backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor}}>
|
|
|
|
{this.getWebview(this.props.data[i])}
|
|
|
|
</Tab>);
|
|
|
|
}
|
|
|
|
return tabbedView;
|
2019-11-07 10:57:58 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 15:44:19 +01:00
|
|
|
render() {
|
|
|
|
const nav = this.props.navigation;
|
|
|
|
return (
|
|
|
|
<BaseContainer
|
|
|
|
navigation={nav}
|
|
|
|
headerTitle={this.props.headerTitle}
|
|
|
|
headerRightButton={this.getRefreshButton()}
|
|
|
|
hasBackButton={this.props.hasHeaderBackButton}
|
2019-11-08 03:56:29 +01:00
|
|
|
hasSideMenu={this.props.hasSideMenu}
|
|
|
|
isHeaderVisible={!this.state.isLandscape}>
|
2019-11-08 02:37:38 +01:00
|
|
|
{this.props.data.length === 1 ?
|
|
|
|
this.getWebview(this.props.data[0]) :
|
|
|
|
<Tabs
|
|
|
|
tabContainerStyle={{
|
|
|
|
elevation: 0, // Fix for android shadow
|
|
|
|
}}
|
|
|
|
locked={true}
|
|
|
|
>
|
|
|
|
{this.getTabbedWebview()}
|
|
|
|
</Tabs>}
|
|
|
|
{this.props.hasFooter && this.props.data.length === 1 ?
|
2019-11-07 10:57:58 +01:00
|
|
|
<Footer>
|
|
|
|
<Left style={{
|
|
|
|
paddingLeft: 6,
|
|
|
|
}}>
|
2019-11-08 02:37:38 +01:00
|
|
|
{this.getHeaderButton(() => this.openWebLink(this.props.data[0]['url']), 'open-in-new')}
|
2019-11-07 10:57:58 +01:00
|
|
|
</Left>
|
|
|
|
<Body/>
|
|
|
|
<Right style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'flex-end',
|
|
|
|
paddingRight: 6
|
|
|
|
}}>
|
|
|
|
<View style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
marginRight: 0,
|
|
|
|
marginLeft: 'auto'
|
|
|
|
}}>
|
|
|
|
{this.getHeaderButton(() => this.goBackWebview(), 'chevron-left')}
|
|
|
|
{this.getHeaderButton(() => this.goForwardWebview(), 'chevron-right')}
|
|
|
|
</View>
|
|
|
|
</Right>
|
|
|
|
</Footer> : <View/>}
|
2019-11-02 15:44:19 +01:00
|
|
|
</BaseContainer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|