2019-11-02 15:44:19 +01:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2020-03-06 23:15:01 +01:00
|
|
|
import {View} from 'react-native';
|
2019-11-02 15:44:19 +01:00
|
|
|
import WebView from "react-native-webview";
|
2020-03-09 23:15:13 +01:00
|
|
|
import {ActivityIndicator, withTheme} from 'react-native-paper';
|
2020-03-07 11:49:32 +01:00
|
|
|
import HeaderButton from "./HeaderButton";
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class defining a webview screen.
|
|
|
|
*/
|
2020-03-09 23:15:13 +01:00
|
|
|
class WebViewScreen extends React.PureComponent<Props> {
|
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
|
|
|
};
|
2020-03-09 23:15:13 +01:00
|
|
|
webviewRef: Object;
|
2019-11-02 15:44:19 +01:00
|
|
|
|
2020-02-23 21:47:36 +01:00
|
|
|
onRefreshClicked: Function;
|
|
|
|
onWebviewRef: Function;
|
|
|
|
onGoBackWebview: Function;
|
|
|
|
onGoForwardWebview: Function;
|
2020-03-09 23:15:13 +01:00
|
|
|
getRenderLoading: Function;
|
2020-02-23 21:47:36 +01:00
|
|
|
|
2020-03-09 23:15:13 +01:00
|
|
|
colors: Object;
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-02-23 21:47:36 +01:00
|
|
|
this.onRefreshClicked = this.onRefreshClicked.bind(this);
|
|
|
|
this.onWebviewRef = this.onWebviewRef.bind(this);
|
|
|
|
this.onGoBackWebview = this.onGoBackWebview.bind(this);
|
|
|
|
this.onGoForwardWebview = this.onGoForwardWebview.bind(this);
|
2020-03-09 23:15:13 +01:00
|
|
|
this.getRenderLoading = this.getRenderLoading.bind(this);
|
|
|
|
this.colors = props.theme.colors;
|
2020-02-23 21:47:36 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 09:12:56 +01:00
|
|
|
componentDidMount() {
|
|
|
|
const rightButton = this.getRefreshButton.bind(this);
|
|
|
|
this.props.navigation.setOptions({
|
|
|
|
headerRight: rightButton,
|
|
|
|
});
|
2019-11-02 15:44:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getHeaderButton(clickAction: Function, icon: string) {
|
|
|
|
return (
|
2020-03-07 11:49:32 +01:00
|
|
|
<HeaderButton icon={icon} onPress={clickAction}/>
|
2019-11-02 15:44:19 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getRefreshButton() {
|
|
|
|
return (
|
2020-03-06 09:12:56 +01:00
|
|
|
<View style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
marginRight: 10
|
|
|
|
}}>
|
2020-02-23 21:47:36 +01:00
|
|
|
{this.getHeaderButton(this.onRefreshClicked, 'refresh')}
|
2019-11-02 15:44:19 +01:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-02-23 21:47:36 +01:00
|
|
|
onRefreshClicked() {
|
2020-03-09 23:15:13 +01:00
|
|
|
if (this.webviewRef !== null)
|
|
|
|
this.webviewRef.reload();
|
2019-11-02 15:44:19 +01:00
|
|
|
}
|
|
|
|
|
2020-02-23 21:47:36 +01:00
|
|
|
onGoBackWebview() {
|
2020-03-09 23:15:13 +01:00
|
|
|
if (this.webviewRef !== null)
|
|
|
|
this.webviewRef.goBack();
|
2019-11-07 10:57:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-23 21:47:36 +01:00
|
|
|
onGoForwardWebview() {
|
2020-03-09 23:15:13 +01:00
|
|
|
if (this.webviewRef !== null)
|
|
|
|
this.webviewRef.goForward();
|
2020-02-23 21:47:36 +01:00
|
|
|
}
|
|
|
|
|
2020-03-09 23:15:13 +01:00
|
|
|
onWebviewRef(ref: Object) {
|
|
|
|
this.webviewRef = ref
|
2020-02-23 21:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getRenderLoading() {
|
|
|
|
return (
|
|
|
|
<View style={{
|
2020-03-09 23:15:13 +01:00
|
|
|
backgroundColor: this.colors.background,
|
2020-02-23 21:47:36 +01:00
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
flex: 1,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
}}>
|
2020-03-06 23:15:01 +01:00
|
|
|
<ActivityIndicator
|
|
|
|
animating={true}
|
|
|
|
size={'large'}
|
2020-03-09 23:15:13 +01:00
|
|
|
color={this.colors.primary}/>
|
2020-02-23 21:47:36 +01:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-09 23:15:13 +01:00
|
|
|
render() {
|
|
|
|
// console.log("rendering WebViewScreen");
|
2019-11-08 02:37:38 +01:00
|
|
|
return (
|
|
|
|
<WebView
|
2020-02-23 21:47:36 +01:00
|
|
|
ref={this.onWebviewRef}
|
2020-03-09 23:15:13 +01:00
|
|
|
source={{uri: this.props.data[0]['url']}}
|
2019-11-08 02:37:38 +01:00
|
|
|
style={{
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
}}
|
|
|
|
startInLoadingState={true}
|
2020-03-09 23:15:13 +01:00
|
|
|
injectedJavaScript={this.props.data[0]['customJS']}
|
2019-11-08 02:37:38 +01:00
|
|
|
javaScriptEnabled={true}
|
2020-02-23 21:47:36 +01:00
|
|
|
renderLoading={this.getRenderLoading}
|
2019-11-08 02:37:38 +01:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2019-11-02 15:44:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-09 23:15:13 +01:00
|
|
|
export default withTheme(WebViewScreen);
|