From 44f7a99bea98a4aafa8149625199673fde5f0d66 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Thu, 16 Apr 2020 14:45:03 +0200 Subject: [PATCH] Fixed crash on iOS and inject javascript instead of sending messages --- src/components/Custom/AutoHideComponent.js | 2 ++ src/components/Screens/WebViewScreen.js | 5 +++-- src/screens/Websites/PlanexScreen.js | 23 ++++++++++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/components/Custom/AutoHideComponent.js b/src/components/Custom/AutoHideComponent.js index 3d2a51b..978df7c 100644 --- a/src/components/Custom/AutoHideComponent.js +++ b/src/components/Custom/AutoHideComponent.js @@ -30,6 +30,8 @@ export default class AutoHideComponent extends React.Component { } onScroll({nativeEvent}: Object) { + if (nativeEvent.velocity === undefined) + return; if (nativeEvent.velocity.y > 0.2) { // Go down if (!this.isAnimationDownPlaying) { this.isAnimationDownPlaying = true; diff --git a/src/components/Screens/WebViewScreen.js b/src/components/Screens/WebViewScreen.js index ee0c4f2..2e808b6 100644 --- a/src/components/Screens/WebViewScreen.js +++ b/src/components/Screens/WebViewScreen.js @@ -113,8 +113,9 @@ class WebViewScreen extends React.PureComponent { onOpenClicked = () => Linking.openURL(this.props.url); - postMessage = (message: string) => { - this.webviewRef.current.getNode().postMessage(message); + injectJavaScript = (script: string) => { + // console.log(this.webviewRef.current.getNode().webViewRef.current); + this.webviewRef.current.getNode().injectJavaScript(script); } /** diff --git a/src/screens/Websites/PlanexScreen.js b/src/screens/Websites/PlanexScreen.js index fcf8cc6..64ceb06 100644 --- a/src/screens/Websites/PlanexScreen.js +++ b/src/screens/Websites/PlanexScreen.js @@ -105,15 +105,15 @@ calendar.option({ } });`; -const LISTEN_TO_MESSAGES = ` -document.addEventListener("message", function(event) { - //alert(event.data); +const EXEC_COMMAND = ` +function execCommand(event) { + alert(JSON.stringify(event)); var data = JSON.parse(event.data); if (data.action === "setGroup") displayAde(data.data); else $('#calendar').fullCalendar(data.action, data.data); -}, false);` +};` const CUSTOM_CSS = "body>.container{padding-top:20px; padding-bottom: 50px}header,#entite,#groupe_visibility,#calendar .fc-left,#calendar .fc-right{display:none}.fc-toolbar .fc-center{width:100%}.fc-toolbar .fc-center>*{float:none;width:100%;margin:0}#entite{margin-bottom:5px!important}#entite,#groupe{width:calc(100% - 20px);margin:0 10px}#groupe_visibility{width:100%}#calendar .fc-agendaWeek-view .fc-content-skeleton .fc-title{font-size:.6rem}#calendar .fc-agendaWeek-view .fc-content-skeleton .fc-time{font-size:.5rem}#calendar .fc-month-view .fc-content-skeleton .fc-title{font-size:.6rem}#calendar .fc-month-view .fc-content-skeleton .fc-time{font-size:.7rem}.fc-axis{font-size:.8rem;width:15px!important}.fc-day-header{font-size:.8rem}.fc-unthemed td.fc-today{background:#be1522; opacity:0.4}"; const CUSTOM_CSS_DARK = "body{background-color:#121212}.fc-unthemed .fc-content,.fc-unthemed .fc-divider,.fc-unthemed .fc-list-heading td,.fc-unthemed .fc-list-view,.fc-unthemed .fc-popover,.fc-unthemed .fc-row,.fc-unthemed tbody,.fc-unthemed td,.fc-unthemed th,.fc-unthemed thead{border-color:#222}.fc-toolbar .fc-center>*,h2,table{color:#fff}.fc-event-container{color:#121212}.fc-event-container .fc-bg{opacity:0.2;background-color:#000}.fc-unthemed td.fc-today{background:#be1522; opacity:0.4}"; @@ -195,15 +195,17 @@ class PlanexScreen extends React.Component { + FULL_CALENDAR_SETTINGS + "displayAde(" + groupID + ");" // Reset Ade + (DateManager.isWeekend(new Date()) ? "calendar.next()" : "") - + LISTEN_TO_MESSAGES + INJECT_STYLE; if (ThemeManager.getNightMode()) this.customInjectedJS += "$('head').append('');"; this.customInjectedJS += - 'removeAlpha();' + - '});true;'; // Prevents crash on ios + 'removeAlpha();' + + '});' + + EXEC_COMMAND + + "function cc(msg) {alert(msg)};" + + 'true;'; // Prevents crash on ios } // componentWillUpdate(prevProps: Props) { @@ -234,7 +236,12 @@ class PlanexScreen extends React.Component { }; sendMessage = (action: string, data: any) => { - this.webScreenRef.current.postMessage(JSON.stringify({action: action, data: data})); + let command; + if (action === "setGroup") + command = "displayAde(" + data + ")"; + else + command = "$('#calendar').fullCalendar('" + action + "', '" + data + "')"; + this.webScreenRef.current.injectJavaScript(command + ';true;'); } onMessage = (event: Object) => {