Browse Source

Fixed qr code crash

Arnaud Vergnet 4 years ago
parent
commit
6607d9206b
1 changed files with 39 additions and 11 deletions
  1. 39
    11
      src/utils/URLHandler.js

+ 39
- 11
src/utils/URLHandler.js View File

@@ -1,9 +1,11 @@
1 1
 // @flow
2 2
 
3
-import {Linking} from 'expo';
3
+import {Linking} from 'react-native';
4 4
 
5 5
 export default class URLHandler {
6 6
 
7
+    static SCHEME = "campus-insat://";
8
+
7 9
     static CLUB_INFO_URL_PATH = "club";
8 10
     static EVENT_INFO_URL_PATH = "event";
9 11
 
@@ -20,21 +22,47 @@ export default class URLHandler {
20 22
 
21 23
     listen() {
22 24
         Linking.addEventListener('url', this.onUrl);
23
-        Linking.parseInitialURLAsync().then(this.onInitialUrl);
25
+        Linking.getInitialURL().then(this.onInitialUrl);
24 26
     }
25 27
 
26
-    onUrl = ({url}: Object) => {
27
-        let data = URLHandler.getUrlData(Linking.parse(url));
28
-        if (data !== null)
29
-            this.onDetectURL(data);
28
+    onUrl = ({url}: { url: string }) => {
29
+        if (url != null) {
30
+            let data = URLHandler.getUrlData(URLHandler.parseUrl(url));
31
+            if (data !== null)
32
+                this.onDetectURL(data);
33
+        }
30 34
     };
31 35
 
32
-    onInitialUrl = ({path, queryParams}: Object) => {
33
-        let data = URLHandler.getUrlData({path, queryParams});
34
-        if (data !== null)
35
-            this.onInitialURLParsed(data);
36
+    onInitialUrl = (url: ?string) => {
37
+        if (url != null) {
38
+            let data = URLHandler.getUrlData(URLHandler.parseUrl(url));
39
+            if (data !== null)
40
+                this.onInitialURLParsed(data);
41
+        }
36 42
     };
37 43
 
44
+    static parseUrl(url: string) {
45
+        let params = {};
46
+        let path = "";
47
+        let temp = url.replace(URLHandler.SCHEME, "");
48
+        if (temp != null) {
49
+            let array = temp.split("?");
50
+            if (array != null && array.length > 0) {
51
+                path = array[0];
52
+            }
53
+            if (array != null && array.length > 1) {
54
+                let tempParams = array[1].split("&");
55
+                for (let i = 0; i < tempParams.length; i++) {
56
+                    let paramsArray = tempParams[i].split("=");
57
+                    if (paramsArray.length > 1) {
58
+                        params[paramsArray[0]] = paramsArray[1];
59
+                    }
60
+                }
61
+            }
62
+        }
63
+        return {path: path, queryParams: params};
64
+    }
65
+
38 66
     static getUrlData({path, queryParams}: Object) {
39 67
         let data = null;
40 68
         if (path !== null) {
@@ -47,7 +75,7 @@ export default class URLHandler {
47 75
     }
48 76
 
49 77
     static isUrlValid(url: string) {
50
-        return this.getUrlData(Linking.parse(url)) !== null;
78
+        return this.getUrlData(URLHandler.parseUrl(url)) !== null;
51 79
     }
52 80
 
53 81
     static isClubInformationLink(path: string) {

Loading…
Cancel
Save