Browse Source

Added webview communication and display popup on planex event click

Arnaud Vergnet 4 years ago
parent
commit
c684872a54

+ 6
- 0
src/components/Screens/WebViewScreen.js View File

@@ -17,6 +17,7 @@ type Props = {
17 17
     url: string,
18 18
     customJS: string,
19 19
     collapsibleStack: Object,
20
+    onMessage: Function,
20 21
 }
21 22
 
22 23
 const AnimatedWebView = Animated.createAnimatedComponent(WebView);
@@ -111,6 +112,10 @@ class WebViewScreen extends React.PureComponent<Props> {
111 112
 
112 113
     onOpenClicked = () => Linking.openURL(this.props.url);
113 114
 
115
+    postMessage = (message: string) => {
116
+        this.webviewRef.current.getNode().postMessage(message);
117
+    }
118
+
114 119
     /**
115 120
      * Gets the loading indicator
116 121
      *
@@ -167,6 +172,7 @@ class WebViewScreen extends React.PureComponent<Props> {
167 172
                 onNavigationStateChange={navState => {
168 173
                     this.canGoBack = navState.canGoBack;
169 174
                 }}
175
+                onMessage={this.props.onMessage}
170 176
                 // Animations
171 177
                 onScroll={onScroll}
172 178
             />

+ 7
- 1
src/screens/Proxiwash/ProxiwashScreen.js View File

@@ -14,6 +14,7 @@ import CustomModal from "../../components/Custom/CustomModal";
14 14
 import AprilFoolsManager from "../../managers/AprilFoolsManager";
15 15
 import MaterialHeaderButtons, {Item} from "../../components/Custom/HeaderButton";
16 16
 import ProxiwashSectionHeader from "../../components/Lists/ProxiwashSectionHeader";
17
+import {withCollapsible} from "../../utils/withCollapsible";
17 18
 
18 19
 const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/washinsa/washinsa.json";
19 20
 
@@ -25,6 +26,7 @@ const LIST_ITEM_HEIGHT = 64;
25 26
 type Props = {
26 27
     navigation: Object,
27 28
     theme: Object,
29
+    collapsibleStack: Object,
28 30
 }
29 31
 
30 32
 type State = {
@@ -416,9 +418,13 @@ class ProxiwashScreen extends React.Component<Props, State> {
416 418
 
417 419
     render() {
418 420
         const nav = this.props.navigation;
421
+        const {containerPaddingTop} = this.props.collapsibleStack;
419 422
         return (
420 423
             <View>
421 424
                 <Banner
425
+                    style={{
426
+                    marginTop: this.state.bannerVisible ? containerPaddingTop : 0,
427
+                }}
422 428
                     visible={this.state.bannerVisible}
423 429
                     actions={[
424 430
                         {
@@ -450,4 +456,4 @@ class ProxiwashScreen extends React.Component<Props, State> {
450 456
     }
451 457
 }
452 458
 
453
-export default withTheme(ProxiwashScreen);
459
+export default withCollapsible(withTheme(ProxiwashScreen));

+ 83
- 5
src/screens/Websites/PlanexScreen.js View File

@@ -7,13 +7,21 @@ import {Avatar, Banner} from "react-native-paper";
7 7
 import i18n from "i18n-js";
8 8
 import {View} from "react-native";
9 9
 import AsyncStorageManager from "../../managers/AsyncStorageManager";
10
+import AlertDialog from "../../components/Dialog/AlertDialog";
11
+import {withCollapsible} from "../../utils/withCollapsible";
12
+import {dateToString, getTimeOnlyString} from "../../utils/Planning";
13
+import DateManager from "../../managers/DateManager";
10 14
 
11 15
 type Props = {
12 16
     navigation: Object,
17
+    collapsibleStack: Object,
13 18
 }
14 19
 
15 20
 type State = {
16 21
     bannerVisible: boolean,
22
+    dialogVisible: boolean,
23
+    dialogTitle: string,
24
+    dialogMessage: string,
17 25
 }
18 26
 
19 27
 
@@ -79,11 +87,34 @@ const OBSERVE_MUTATIONS_INJECTED =
79 87
     '$(".fc-event-container .fc-event").each(function(index) {\n' +
80 88
     '    removeAlpha($(this));\n' +
81 89
     '});';
90
+
91
+const FULL_CALENDAR_SETTINGS =
92
+    `var calendar = $('#calendar').fullCalendar('getCalendar');
93
+    calendar.option({
94
+      eventClick: function (data, event, view) {
95
+          var message = {
96
+          title: data.title,
97
+          color: data.color,
98
+          start: data.start._d,
99
+          end: data.end._d,
100
+        };
101
+       window.ReactNativeWebView.postMessage(JSON.stringify(message));
102
+      }
103
+    });`;
104
+
105
+const LISTEN_TO_MESSAGES =
106
+    `document.addEventListener("message", function(event) {
107
+      console.log("Received post message", event);//Get Event from React Native
108
+      alert(event.data);
109
+    }, false);`
110
+
82 111
 /**
83 112
  * Class defining the app's Planex screen.
84 113
  * This screen uses a webview to render the page
85 114
  */
86
-export default class PlanexScreen extends React.Component<Props, State> {
115
+class PlanexScreen extends React.Component<Props, State> {
116
+
117
+    webScreenRef: Object;
87 118
 
88 119
     customInjectedJS: string;
89 120
     onHideBanner: Function;
@@ -92,6 +123,9 @@ export default class PlanexScreen extends React.Component<Props, State> {
92 123
         bannerVisible:
93 124
             AsyncStorageManager.getInstance().preferences.planexShowBanner.current === '1' &&
94 125
             AsyncStorageManager.getInstance().preferences.defaultStartScreen.current !== 'Planex',
126
+        dialogVisible: false,
127
+        dialogTitle: "",
128
+        dialogMessage: "",
95 129
     };
96 130
 
97 131
     /**
@@ -99,11 +133,14 @@ export default class PlanexScreen extends React.Component<Props, State> {
99 133
      */
100 134
     constructor() {
101 135
         super();
136
+        this.webScreenRef = React.createRef();
102 137
         this.customInjectedJS =
103
-            '$(document).ready(function() {' +
138
+            "$(document).ready(function() {" +
104 139
             OBSERVE_MUTATIONS_INJECTED +
105
-            '$("head").append(\'<meta name="viewport" content="width=device-width, initial-scale=0.9">\');' +
106
-            '$("head").append(\'<link rel="stylesheet" href="' + CUSTOM_CSS_GENERAL + '" type="text/css"/>\');';
140
+            FULL_CALENDAR_SETTINGS +
141
+            LISTEN_TO_MESSAGES +
142
+            "$('head').append('<meta name=\"viewport\" content=\"width=device-width, initial-scale=0.9\">');" +
143
+            "$('head').append('<link rel=\"stylesheet\" href=\"" + CUSTOM_CSS_GENERAL + '" type="text/css"/>\');';
107 144
 
108 145
         if (ThemeManager.getNightMode())
109 146
             this.customInjectedJS += '$("head").append(\'<link rel="stylesheet" href="' + CUSTOM_CSS_NIGHTMODE + '" type="text/css"/>\');';
@@ -137,13 +174,45 @@ export default class PlanexScreen extends React.Component<Props, State> {
137 174
         this.props.navigation.navigate('settings');
138 175
     }
139 176
 
177
+    sendMessage = () => {
178
+        let data= 'coucou'
179
+        this.webScreenRef.current.postMessage(data);
180
+    }
181
+
182
+    onMessage = (event: Object) => {
183
+        let data = JSON.parse(event.nativeEvent.data);
184
+        let startDate = dateToString(new Date(data.start), true);
185
+        let endDate = dateToString(new Date(data.end), true);
186
+        let msg = DateManager.getInstance().getTranslatedDate(startDate) + "\n";
187
+        msg += getTimeOnlyString(startDate) + ' - ' + getTimeOnlyString(endDate);
188
+        this.showDialog(data.title, msg)
189
+    };
190
+
191
+    showDialog = (title: string, message: string) => {
192
+        this.setState({
193
+            dialogVisible: true,
194
+            dialogTitle: title,
195
+            dialogMessage: message,
196
+        });
197
+    };
198
+
199
+    hideDialog = () => {
200
+        this.setState({
201
+            dialogVisible: false,
202
+        });
203
+    };
204
+
140 205
     render() {
141 206
         const nav = this.props.navigation;
207
+        const {containerPaddingTop} = this.props.collapsibleStack;
142 208
         return (
143 209
             <View style={{
144 210
                 height: '100%'
145 211
             }}>
146 212
                 <Banner
213
+                    style={{
214
+                        marginTop: this.state.bannerVisible ? containerPaddingTop : 0,
215
+                    }}
147 216
                     visible={this.state.bannerVisible}
148 217
                     actions={[
149 218
                         {
@@ -163,12 +232,21 @@ export default class PlanexScreen extends React.Component<Props, State> {
163 232
                 >
164 233
                     {i18n.t('planexScreen.enableStartScreen')}
165 234
                 </Banner>
235
+                <AlertDialog
236
+                    visible={this.state.dialogVisible}
237
+                    onDismiss={this.hideDialog}
238
+                    title={this.state.dialogTitle}
239
+                    message={this.state.dialogMessage}/>
166 240
                 <WebViewScreen
241
+                    ref={this.webScreenRef}
167 242
                     navigation={nav}
168 243
                     url={PLANEX_URL}
169
-                    customJS={this.customInjectedJS}/>
244
+                    customJS={this.customInjectedJS}
245
+                    onMessage={this.onMessage}
246
+                />
170 247
             </View>
171 248
         );
172 249
     }
173 250
 }
174 251
 
252
+export default withCollapsible(PlanexScreen);

+ 3
- 2
src/utils/Planning.js View File

@@ -119,11 +119,12 @@ export function stringToDate(dateString: string): Date | null {
119 119
  * @param date The date object to convert
120 120
  * @return {string} The converted string
121 121
  */
122
-export function dateToString(date: Date): string {
122
+export function dateToString(date: Date, isUTC: boolean): string {
123 123
     const day = String(date.getDate()).padStart(2, '0');
124 124
     const month = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
125 125
     const year = date.getFullYear();
126
-    const hours = String(date.getHours()).padStart(2, '0');
126
+    const h = isUTC ? date.getUTCHours() : date.getHours();
127
+    const hours = String(h).padStart(2, '0');
127 128
     const minutes = String(date.getMinutes()).padStart(2, '0');
128 129
     return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
129 130
 }

+ 3
- 3
src/utils/withCollapsible.js View File

@@ -2,7 +2,7 @@ import React from 'react';
2 2
 import {useCollapsibleStack} from "react-navigation-collapsible";
3 3
 
4 4
 export const withCollapsible = (Component: any) => {
5
-    return (props: any) => {
6
-        return <Component collapsibleStack={useCollapsibleStack()} {...props} />;
7
-    };
5
+    return React.forwardRef((props: any, ref: any) => {
6
+        return <Component collapsibleStack={useCollapsibleStack()} ref={ref} {...props} />;
7
+    });
8 8
 };

Loading…
Cancel
Save