Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebViewScreen.tsx 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import * as React from 'react';
  20. import WebView from 'react-native-webview';
  21. import {
  22. Divider,
  23. HiddenItem,
  24. OverflowMenu,
  25. } from 'react-navigation-header-buttons';
  26. import i18n from 'i18n-js';
  27. import {
  28. Animated,
  29. BackHandler,
  30. Linking,
  31. NativeScrollEvent,
  32. NativeSyntheticEvent,
  33. } from 'react-native';
  34. import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
  35. import {withTheme} from 'react-native-paper';
  36. import {StackNavigationProp} from '@react-navigation/stack';
  37. import {Collapsible} from 'react-navigation-collapsible';
  38. import withCollapsible from '../../utils/withCollapsible';
  39. import MaterialHeaderButtons, {Item} from '../Overrides/CustomHeaderButton';
  40. import {ERROR_TYPE} from '../../utils/WebData';
  41. import ErrorView from './ErrorView';
  42. import BasicLoadingScreen from './BasicLoadingScreen';
  43. type PropsType = {
  44. navigation: StackNavigationProp<any>;
  45. theme: ReactNativePaper.Theme;
  46. url: string;
  47. collapsibleStack: Collapsible;
  48. onMessage: (event: {nativeEvent: {data: string}}) => void;
  49. onScroll: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
  50. customJS?: string;
  51. customPaddingFunction?: null | ((padding: number) => string);
  52. showAdvancedControls?: boolean;
  53. };
  54. const AnimatedWebView = Animated.createAnimatedComponent(WebView);
  55. /**
  56. * Class defining a webview screen.
  57. */
  58. class WebViewScreen extends React.PureComponent<PropsType> {
  59. static defaultProps = {
  60. customJS: '',
  61. showAdvancedControls: true,
  62. customPaddingFunction: null,
  63. };
  64. webviewRef: {current: null | WebView};
  65. canGoBack: boolean;
  66. constructor(props: PropsType) {
  67. super(props);
  68. this.webviewRef = React.createRef();
  69. this.canGoBack = false;
  70. }
  71. /**
  72. * Creates header buttons and listens to events after mounting
  73. */
  74. componentDidMount() {
  75. const {props} = this;
  76. props.navigation.setOptions({
  77. headerRight: props.showAdvancedControls
  78. ? this.getAdvancedButtons
  79. : this.getBasicButton,
  80. });
  81. props.navigation.addListener('focus', () => {
  82. BackHandler.addEventListener(
  83. 'hardwareBackPress',
  84. this.onBackButtonPressAndroid,
  85. );
  86. });
  87. props.navigation.addListener('blur', () => {
  88. BackHandler.removeEventListener(
  89. 'hardwareBackPress',
  90. this.onBackButtonPressAndroid,
  91. );
  92. });
  93. }
  94. /**
  95. * Goes back on the webview or on the navigation stack if we cannot go back anymore
  96. *
  97. * @returns {boolean}
  98. */
  99. onBackButtonPressAndroid = (): boolean => {
  100. if (this.canGoBack) {
  101. this.onGoBackClicked();
  102. return true;
  103. }
  104. return false;
  105. };
  106. /**
  107. * Gets header refresh and open in browser buttons
  108. *
  109. * @return {*}
  110. */
  111. getBasicButton = () => {
  112. return (
  113. <MaterialHeaderButtons>
  114. <Item
  115. title="refresh"
  116. iconName="refresh"
  117. onPress={this.onRefreshClicked}
  118. />
  119. <Item
  120. title={i18n.t('general.openInBrowser')}
  121. iconName="open-in-new"
  122. onPress={this.onOpenClicked}
  123. />
  124. </MaterialHeaderButtons>
  125. );
  126. };
  127. /**
  128. * Creates advanced header control buttons.
  129. * These buttons allows the user to refresh, go back, go forward and open in the browser.
  130. *
  131. * @returns {*}
  132. */
  133. getAdvancedButtons = () => {
  134. const {props} = this;
  135. return (
  136. <MaterialHeaderButtons>
  137. <Item
  138. title="refresh"
  139. iconName="refresh"
  140. onPress={this.onRefreshClicked}
  141. />
  142. <OverflowMenu
  143. style={{marginHorizontal: 10}}
  144. OverflowIcon={
  145. <MaterialCommunityIcons
  146. name="dots-vertical"
  147. size={26}
  148. color={props.theme.colors.text}
  149. />
  150. }>
  151. <HiddenItem
  152. title={i18n.t('general.goBack')}
  153. onPress={this.onGoBackClicked}
  154. />
  155. <HiddenItem
  156. title={i18n.t('general.goForward')}
  157. onPress={this.onGoForwardClicked}
  158. />
  159. <Divider />
  160. <HiddenItem
  161. title={i18n.t('general.openInBrowser')}
  162. onPress={this.onOpenClicked}
  163. />
  164. </OverflowMenu>
  165. </MaterialHeaderButtons>
  166. );
  167. };
  168. /**
  169. * Gets the loading indicator
  170. *
  171. * @return {*}
  172. */
  173. getRenderLoading = () => <BasicLoadingScreen isAbsolute />;
  174. /**
  175. * Gets the javascript needed to generate a padding on top of the page
  176. * This adds padding to the body and runs the custom padding function given in props
  177. *
  178. * @param padding The padding to add in pixels
  179. * @returns {string}
  180. */
  181. getJavascriptPadding(padding: number): string {
  182. const {props} = this;
  183. const customPadding =
  184. props.customPaddingFunction != null
  185. ? props.customPaddingFunction(padding)
  186. : '';
  187. return `document.getElementsByTagName('body')[0].style.paddingTop = '${padding}px';${customPadding}true;`;
  188. }
  189. /**
  190. * Callback to use when refresh button is clicked. Reloads the webview.
  191. */
  192. onRefreshClicked = () => {
  193. if (this.webviewRef.current != null) {
  194. this.webviewRef.current.reload();
  195. }
  196. };
  197. onGoBackClicked = () => {
  198. if (this.webviewRef.current != null) {
  199. this.webviewRef.current.goBack();
  200. }
  201. };
  202. onGoForwardClicked = () => {
  203. if (this.webviewRef.current != null) {
  204. this.webviewRef.current.goForward();
  205. }
  206. };
  207. onOpenClicked = () => {
  208. const {url} = this.props;
  209. Linking.openURL(url);
  210. };
  211. onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
  212. const {onScroll} = this.props;
  213. if (onScroll) {
  214. onScroll(event);
  215. }
  216. };
  217. /**
  218. * Injects the given javascript string into the web page
  219. *
  220. * @param script The script to inject
  221. */
  222. injectJavaScript = (script: string) => {
  223. if (this.webviewRef.current != null) {
  224. this.webviewRef.current.injectJavaScript(script);
  225. }
  226. };
  227. render() {
  228. const {props} = this;
  229. const {containerPaddingTop, onScrollWithListener} = props.collapsibleStack;
  230. return (
  231. <AnimatedWebView
  232. ref={this.webviewRef}
  233. source={{uri: props.url}}
  234. startInLoadingState
  235. injectedJavaScript={props.customJS}
  236. javaScriptEnabled
  237. renderLoading={this.getRenderLoading}
  238. renderError={() => (
  239. <ErrorView
  240. errorCode={ERROR_TYPE.CONNECTION_ERROR}
  241. onRefresh={this.onRefreshClicked}
  242. />
  243. )}
  244. onNavigationStateChange={(navState: {canGoBack: boolean}) => {
  245. this.canGoBack = navState.canGoBack;
  246. }}
  247. onMessage={props.onMessage}
  248. onLoad={() => {
  249. this.injectJavaScript(this.getJavascriptPadding(containerPaddingTop));
  250. }}
  251. // Animations
  252. onScroll={(event) => onScrollWithListener(this.onScroll)(event)}
  253. />
  254. );
  255. }
  256. }
  257. export default withCollapsible(withTheme(WebViewScreen));