Browse Source

Fix eslint errors

First files rewritten to match the new eslint config
Arnaud Vergnet 3 years ago
parent
commit
be1f61b671
4 changed files with 423 additions and 389 deletions
  1. 187
    186
      App.js
  2. 16
    22
      src/managers/DashboardManager.js
  3. 184
    159
      src/utils/URLHandler.js
  4. 36
    22
      src/utils/Utils.js

+ 187
- 186
App.js View File

@@ -1,210 +1,211 @@
1 1
 // @flow
2 2
 
3 3
 import * as React from 'react';
4
-import {LogBox, Platform, SafeAreaView, StatusBar, View} from 'react-native';
4
+import {LogBox, Platform, SafeAreaView, View} from 'react-native';
5
+import {NavigationContainer} from '@react-navigation/native';
6
+import {Provider as PaperProvider} from 'react-native-paper';
7
+import {setSafeBounceHeight} from 'react-navigation-collapsible';
8
+import SplashScreen from 'react-native-splash-screen';
9
+import {OverflowMenuProvider} from 'react-navigation-header-buttons';
5 10
 import LocaleManager from './src/managers/LocaleManager';
6
-import AsyncStorageManager from "./src/managers/AsyncStorageManager";
7
-import CustomIntroSlider from "./src/components/Overrides/CustomIntroSlider";
8
-import type {CustomTheme} from "./src/managers/ThemeManager";
11
+import AsyncStorageManager from './src/managers/AsyncStorageManager';
12
+import CustomIntroSlider from './src/components/Overrides/CustomIntroSlider';
13
+import type {CustomTheme} from './src/managers/ThemeManager';
9 14
 import ThemeManager from './src/managers/ThemeManager';
10
-import {NavigationContainer} from '@react-navigation/native';
11 15
 import MainNavigator from './src/navigation/MainNavigator';
12
-import {Provider as PaperProvider} from 'react-native-paper';
13
-import AprilFoolsManager from "./src/managers/AprilFoolsManager";
14
-import Update from "./src/constants/Update";
15
-import ConnectionManager from "./src/managers/ConnectionManager";
16
-import URLHandler from "./src/utils/URLHandler";
17
-import {setSafeBounceHeight} from "react-navigation-collapsible";
18
-import SplashScreen from 'react-native-splash-screen'
19
-import {OverflowMenuProvider} from "react-navigation-header-buttons";
16
+import AprilFoolsManager from './src/managers/AprilFoolsManager';
17
+import Update from './src/constants/Update';
18
+import ConnectionManager from './src/managers/ConnectionManager';
19
+import type {ParsedUrlDataType} from './src/utils/URLHandler';
20
+import URLHandler from './src/utils/URLHandler';
21
+import {setupStatusBar} from './src/utils/Utils';
20 22
 
21 23
 // Native optimizations https://reactnavigation.org/docs/react-native-screens
22 24
 // Crashes app when navigating away from webview on android 9+
23 25
 // enableScreens(true);
24 26
 
25
-
26
-LogBox.ignoreLogs([ // collapsible headers cause this warning, just ignore as it is not an issue
27
-    'Non-serializable values were found in the navigation state',
28
-    'Cannot update a component from inside the function body of a different component',
27
+LogBox.ignoreLogs([
28
+  // collapsible headers cause this warning, just ignore as it is not an issue
29
+  'Non-serializable values were found in the navigation state',
30
+  'Cannot update a component from inside the function body of a different component',
29 31
 ]);
30 32
 
31
-type Props = {};
32
-
33
-type State = {
34
-    isLoading: boolean,
35
-    showIntro: boolean,
36
-    showUpdate: boolean,
37
-    showAprilFools: boolean,
38
-    currentTheme: CustomTheme | null,
33
+type StateType = {
34
+  isLoading: boolean,
35
+  showIntro: boolean,
36
+  showUpdate: boolean,
37
+  showAprilFools: boolean,
38
+  currentTheme: CustomTheme | null,
39 39
 };
40 40
 
41
-export default class App extends React.Component<Props, State> {
42
-
43
-    state = {
44
-        isLoading: true,
45
-        showIntro: true,
46
-        showUpdate: true,
47
-        showAprilFools: false,
48
-        currentTheme: null,
49
-    };
50
-
51
-    navigatorRef: { current: null | NavigationContainer };
52
-
53
-    defaultHomeRoute: string | null;
54
-    defaultHomeData: { [key: string]: any }
55
-
56
-    createDrawerNavigator: () => React.Node;
41
+export default class App extends React.Component<null, StateType> {
42
+  navigatorRef: {current: null | NavigationContainer};
57 43
 
58
-    urlHandler: URLHandler;
44
+  defaultHomeRoute: string | null;
59 45
 
60
-    constructor() {
61
-        super();
62
-        LocaleManager.initTranslations();
63
-        this.navigatorRef = React.createRef();
64
-        this.defaultHomeRoute = null;
65
-        this.defaultHomeData = {};
66
-        this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
67
-        this.urlHandler.listen();
68
-        setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
69
-        this.loadAssetsAsync().then(() => {
70
-            this.onLoadFinished();
71
-        });
72
-    }
73
-
74
-    /**
75
-     * The app has been started by an url, and it has been parsed.
76
-     * Set a new default start route based on the data parsed.
77
-     *
78
-     * @param parsedData The data parsed from the url
79
-     */
80
-    onInitialURLParsed = (parsedData: { route: string, data: { [key: string]: any } }) => {
81
-        this.defaultHomeRoute = parsedData.route;
82
-        this.defaultHomeData = parsedData.data;
83
-    };
46
+  defaultHomeData: {[key: string]: string};
84 47
 
85
-    /**
86
-     * An url has been opened and parsed while the app was active.
87
-     * Redirect the user to the screen according to parsed data.
88
-     *
89
-     * @param parsedData The data parsed from the url
90
-     */
91
-    onDetectURL = (parsedData: { route: string, data: { [key: string]: any } }) => {
92
-        // Navigate to nested navigator and pass data to the index screen
93
-        if (this.navigatorRef.current != null) {
94
-            this.navigatorRef.current.navigate('home', {
95
-                screen: 'index',
96
-                params: {nextScreen: parsedData.route, data: parsedData.data}
97
-            });
98
-        }
99
-    };
48
+  urlHandler: URLHandler;
100 49
 
101
-    /**
102
-     * Updates the current theme
103
-     */
104
-    onUpdateTheme = () => {
105
-        this.setState({
106
-            currentTheme: ThemeManager.getCurrentTheme()
107
-        });
108
-        this.setupStatusBar();
50
+  constructor() {
51
+    super();
52
+    this.state = {
53
+      isLoading: true,
54
+      showIntro: true,
55
+      showUpdate: true,
56
+      showAprilFools: false,
57
+      currentTheme: null,
109 58
     };
110
-
111
-    /**
112
-     * Updates status bar content color if on iOS only,
113
-     * as the android status bar is always set to black.
114
-     */
115
-    setupStatusBar() {
116
-        if (ThemeManager.getNightMode()) {
117
-            StatusBar.setBarStyle('light-content', true);
118
-        } else {
119
-            StatusBar.setBarStyle('dark-content', true);
120
-        }
121
-        if (Platform.OS === "android")
122
-            StatusBar.setBackgroundColor(ThemeManager.getCurrentTheme().colors.surface, true);
59
+    LocaleManager.initTranslations();
60
+    this.navigatorRef = React.createRef();
61
+    this.defaultHomeRoute = null;
62
+    this.defaultHomeData = {};
63
+    this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
64
+    this.urlHandler.listen();
65
+    setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
66
+    this.loadAssetsAsync().finally(() => {
67
+      this.onLoadFinished();
68
+    });
69
+  }
70
+
71
+  /**
72
+   * The app has been started by an url, and it has been parsed.
73
+   * Set a new default start route based on the data parsed.
74
+   *
75
+   * @param parsedData The data parsed from the url
76
+   */
77
+  onInitialURLParsed = (parsedData: ParsedUrlDataType) => {
78
+    this.defaultHomeRoute = parsedData.route;
79
+    this.defaultHomeData = parsedData.data;
80
+  };
81
+
82
+  /**
83
+   * An url has been opened and parsed while the app was active.
84
+   * Redirect the user to the screen according to parsed data.
85
+   *
86
+   * @param parsedData The data parsed from the url
87
+   */
88
+  onDetectURL = (parsedData: ParsedUrlDataType) => {
89
+    // Navigate to nested navigator and pass data to the index screen
90
+    const nav = this.navigatorRef.current;
91
+    if (nav != null) {
92
+      nav.navigate('home', {
93
+        screen: 'index',
94
+        params: {nextScreen: parsedData.route, data: parsedData.data},
95
+      });
123 96
     }
124
-
125
-    /**
126
-     * Callback when user ends the intro. Save in preferences to avoid showing back the introSlides
127
-     */
128
-    onIntroDone = () => {
129
-        this.setState({
130
-            showIntro: false,
131
-            showUpdate: false,
132
-            showAprilFools: false,
133
-        });
134
-        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.showIntro.key, false);
135
-        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.updateNumber.key, Update.number);
136
-        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key, false);
137
-    };
138
-
139
-    /**
140
-     * Loads every async data
141
-     *
142
-     * @returns {Promise<void>}
143
-     */
144
-    loadAssetsAsync = async () => {
145
-        await AsyncStorageManager.getInstance().loadPreferences();
146
-        try {
147
-            await ConnectionManager.getInstance().recoverLogin();
148
-        } catch (e) {
149
-        }
97
+  };
98
+
99
+  /**
100
+   * Updates the current theme
101
+   */
102
+  onUpdateTheme = () => {
103
+    this.setState({
104
+      currentTheme: ThemeManager.getCurrentTheme(),
105
+    });
106
+    setupStatusBar();
107
+  };
108
+
109
+  /**
110
+   * Callback when user ends the intro. Save in preferences to avoid showing back the introSlides
111
+   */
112
+  onIntroDone = () => {
113
+    this.setState({
114
+      showIntro: false,
115
+      showUpdate: false,
116
+      showAprilFools: false,
117
+    });
118
+    AsyncStorageManager.set(
119
+      AsyncStorageManager.PREFERENCES.showIntro.key,
120
+      false,
121
+    );
122
+    AsyncStorageManager.set(
123
+      AsyncStorageManager.PREFERENCES.updateNumber.key,
124
+      Update.number,
125
+    );
126
+    AsyncStorageManager.set(
127
+      AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key,
128
+      false,
129
+    );
130
+  };
131
+
132
+  /**
133
+   * Async loading is done, finish processing startup data
134
+   */
135
+  onLoadFinished() {
136
+    // Only show intro if this is the first time starting the app
137
+    ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
138
+    // Status bar goes dark if set too fast on ios
139
+    if (Platform.OS === 'ios') setTimeout(setupStatusBar, 1000);
140
+    else setupStatusBar();
141
+
142
+    this.setState({
143
+      isLoading: false,
144
+      currentTheme: ThemeManager.getCurrentTheme(),
145
+      showIntro: AsyncStorageManager.getBool(
146
+        AsyncStorageManager.PREFERENCES.showIntro.key,
147
+      ),
148
+      showUpdate:
149
+        AsyncStorageManager.getNumber(
150
+          AsyncStorageManager.PREFERENCES.updateNumber.key,
151
+        ) !== Update.number,
152
+      showAprilFools:
153
+        AprilFoolsManager.getInstance().isAprilFoolsEnabled() &&
154
+        AsyncStorageManager.getBool(
155
+          AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key,
156
+        ),
157
+    });
158
+    SplashScreen.hide();
159
+  }
160
+
161
+  /**
162
+   * Loads every async data
163
+   *
164
+   * @returns {Promise<void>}
165
+   */
166
+  loadAssetsAsync = async () => {
167
+    await AsyncStorageManager.getInstance().loadPreferences();
168
+    await ConnectionManager.getInstance().recoverLogin();
169
+  };
170
+
171
+  /**
172
+   * Renders the app based on loading state
173
+   */
174
+  render(): React.Node {
175
+    const {state} = this;
176
+    if (state.isLoading) {
177
+      return null;
150 178
     }
151
-
152
-    /**
153
-     * Async loading is done, finish processing startup data
154
-     */
155
-    onLoadFinished() {
156
-        // Only show intro if this is the first time starting the app
157
-        this.createDrawerNavigator = () => <MainNavigator
158
-            defaultHomeRoute={this.defaultHomeRoute}
159
-            defaultHomeData={this.defaultHomeData}
160
-        />;
161
-        ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
162
-        // Status bar goes dark if set too fast on ios
163
-        if (Platform.OS === 'ios')
164
-            setTimeout(this.setupStatusBar, 1000);
165
-        else
166
-            this.setupStatusBar();
167
-        this.setState({
168
-            isLoading: false,
169
-            currentTheme: ThemeManager.getCurrentTheme(),
170
-            showIntro: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.showIntro.key),
171
-            showUpdate: AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.updateNumber.key)
172
-                !== Update.number,
173
-            showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled()
174
-                && AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key),
175
-        });
176
-        SplashScreen.hide();
177
-    }
178
-
179
-    /**
180
-     * Renders the app based on loading state
181
-     */
182
-    render() {
183
-        if (this.state.isLoading) {
184
-            return null;
185
-        } else if (this.state.showIntro || this.state.showUpdate || this.state.showAprilFools) {
186
-            return <CustomIntroSlider
187
-                onDone={this.onIntroDone}
188
-                isUpdate={this.state.showUpdate && !this.state.showIntro}
189
-                isAprilFools={this.state.showAprilFools && !this.state.showIntro}
190
-            />;
191
-        } else {
192
-            return (
193
-                <PaperProvider theme={this.state.currentTheme}>
194
-                    <OverflowMenuProvider>
195
-                        <View style={{backgroundColor: ThemeManager.getCurrentTheme().colors.background, flex: 1}}>
196
-                            <SafeAreaView style={{flex: 1}}>
197
-                                <NavigationContainer theme={this.state.currentTheme} ref={this.navigatorRef}>
198
-                                    <MainNavigator
199
-                                        defaultHomeRoute={this.defaultHomeRoute}
200
-                                        defaultHomeData={this.defaultHomeData}
201
-                                    />
202
-                                </NavigationContainer>
203
-                            </SafeAreaView>
204
-                        </View>
205
-                    </OverflowMenuProvider>
206
-                </PaperProvider>
207
-            );
208
-        }
179
+    if (state.showIntro || state.showUpdate || state.showAprilFools) {
180
+      return (
181
+        <CustomIntroSlider
182
+          onDone={this.onIntroDone}
183
+          isUpdate={state.showUpdate && !state.showIntro}
184
+          isAprilFools={state.showAprilFools && !state.showIntro}
185
+        />
186
+      );
209 187
     }
188
+    return (
189
+      <PaperProvider theme={state.currentTheme}>
190
+        <OverflowMenuProvider>
191
+          <View
192
+            style={{
193
+              backgroundColor: ThemeManager.getCurrentTheme().colors.background,
194
+              flex: 1,
195
+            }}>
196
+            <SafeAreaView style={{flex: 1}}>
197
+              <NavigationContainer
198
+                theme={state.currentTheme}
199
+                ref={this.navigatorRef}>
200
+                <MainNavigator
201
+                  defaultHomeRoute={this.defaultHomeRoute}
202
+                  defaultHomeData={this.defaultHomeData}
203
+                />
204
+              </NavigationContainer>
205
+            </SafeAreaView>
206
+          </View>
207
+        </OverflowMenuProvider>
208
+      </PaperProvider>
209
+    );
210
+  }
210 211
 }

+ 16
- 22
src/managers/DashboardManager.js View File

@@ -1,27 +1,21 @@
1 1
 // @flow
2 2
 
3
-import type {ServiceItem} from "./ServicesManager";
4
-import ServicesManager from "./ServicesManager";
5
-import {StackNavigationProp} from "@react-navigation/stack";
6
-import {getSublistWithIds} from "../utils/Utils";
7
-import AsyncStorageManager from "./AsyncStorageManager";
8
-
3
+import type {ServiceItem} from './ServicesManager';
4
+import ServicesManager from './ServicesManager';
5
+import {getSublistWithIds} from '../utils/Utils';
6
+import AsyncStorageManager from './AsyncStorageManager';
9 7
 
10 8
 export default class DashboardManager extends ServicesManager {
11
-
12
-    constructor(nav: StackNavigationProp) {
13
-        super(nav)
14
-    }
15
-
16
-    getCurrentDashboard(): Array<ServiceItem> {
17
-        const dashboardIdList = AsyncStorageManager
18
-            .getObject(AsyncStorageManager.PREFERENCES.dashboardItems.key);
19
-        const allDatasets = [
20
-            ...this.amicaleDataset,
21
-            ...this.studentsDataset,
22
-            ...this.insaDataset,
23
-            ...this.specialDataset,
24
-        ];
25
-        return getSublistWithIds(dashboardIdList, allDatasets);
26
-    }
9
+  getCurrentDashboard(): Array<ServiceItem | null> {
10
+    const dashboardIdList = AsyncStorageManager.getObject(
11
+      AsyncStorageManager.PREFERENCES.dashboardItems.key,
12
+    );
13
+    const allDatasets = [
14
+      ...this.amicaleDataset,
15
+      ...this.studentsDataset,
16
+      ...this.insaDataset,
17
+      ...this.specialDataset,
18
+    ];
19
+    return getSublistWithIds(dashboardIdList, allDatasets);
20
+  }
27 21
 }

+ 184
- 159
src/utils/URLHandler.js View File

@@ -2,174 +2,199 @@
2 2
 
3 3
 import {Linking} from 'react-native';
4 4
 
5
+export type ParsedUrlDataType = {
6
+  route: string,
7
+  data: {[key: string]: string},
8
+};
9
+
10
+export type ParsedUrlCallbackType = (parsedData: ParsedUrlDataType) => void;
11
+
12
+type RawParsedUrlDataType = {
13
+  path: string,
14
+  queryParams: {[key: string]: string},
15
+};
16
+
5 17
 /**
6 18
  * Class use to handle depp links scanned or clicked.
7 19
  */
8 20
 export default class URLHandler {
9
-
10
-    static SCHEME = "campus-insat://"; // Urls beginning with this string will be opened in the app
11
-
12
-    static CLUB_INFO_URL_PATH = "club";
13
-    static EVENT_INFO_URL_PATH = "event";
14
-
15
-    static CLUB_INFO_ROUTE = "club-information";
16
-    static EVENT_INFO_ROUTE = "planning-information";
17
-
18
-    onInitialURLParsed: Function;
19
-    onDetectURL: Function;
20
-
21
-    constructor(onInitialURLParsed: Function, onDetectURL: Function) {
22
-        this.onInitialURLParsed = onInitialURLParsed;
23
-        this.onDetectURL = onDetectURL;
24
-    }
25
-
26
-    /**
27
-     * Parses the given url to retrieve the corresponding app path and associated arguments.
28
-     *
29
-     * @param url The url to parse
30
-     * @returns {{path: string, queryParams: {}}}
31
-     */
32
-    static parseUrl(url: string) {
33
-        let params = {};
34
-        let path = "";
35
-        let temp = url.replace(URLHandler.SCHEME, "");
36
-        if (temp != null) {
37
-            let array = temp.split("?");
38
-            if (array != null && array.length > 0) {
39
-                path = array[0];
40
-            }
41
-            if (array != null && array.length > 1) {
42
-                let tempParams = array[1].split("&");
43
-                for (let i = 0; i < tempParams.length; i++) {
44
-                    let paramsArray = tempParams[i].split("=");
45
-                    if (paramsArray.length > 1) {
46
-                        params[paramsArray[0]] = paramsArray[1];
47
-                    }
48
-                }
49
-            }
50
-        }
51
-        return {path: path, queryParams: params};
21
+  static SCHEME = 'campus-insat://'; // Urls beginning with this string will be opened in the app
22
+
23
+  static CLUB_INFO_URL_PATH = 'club';
24
+
25
+  static EVENT_INFO_URL_PATH = 'event';
26
+
27
+  static CLUB_INFO_ROUTE = 'club-information';
28
+
29
+  static EVENT_INFO_ROUTE = 'planning-information';
30
+
31
+  onInitialURLParsed: ParsedUrlCallbackType;
32
+
33
+  onDetectURL: ParsedUrlCallbackType;
34
+
35
+  constructor(
36
+    onInitialURLParsed: ParsedUrlCallbackType,
37
+    onDetectURL: ParsedUrlCallbackType,
38
+  ) {
39
+    this.onInitialURLParsed = onInitialURLParsed;
40
+    this.onDetectURL = onDetectURL;
41
+  }
42
+
43
+  /**
44
+   * Parses the given url to retrieve the corresponding app path and associated arguments.
45
+   *
46
+   * @param url The url to parse
47
+   * @returns {{path: string, queryParams: {}}}
48
+   */
49
+  static parseUrl(url: string): RawParsedUrlDataType | null {
50
+    let parsedData: RawParsedUrlDataType | null = null;
51
+    const urlNoScheme = url.replace(URLHandler.SCHEME, '');
52
+    if (urlNoScheme != null) {
53
+      const params = {};
54
+      const [path, fullParamsString] = urlNoScheme.split('?');
55
+      if (fullParamsString != null) {
56
+        const paramsStringArray = fullParamsString.split('&');
57
+        paramsStringArray.forEach((paramString: string) => {
58
+          const [key, value] = paramString.split('=');
59
+          if (value != null) {
60
+            params[key] = value;
61
+          }
62
+        });
63
+      }
64
+      if (path != null) parsedData = {path, queryParams: params};
52 65
     }
53
-
54
-    /**
55
-     * Gets routing data corresponding to the given url.
56
-     * If the url does not match any existing route, null will be returned.
57
-     *
58
-     * @param path Url path
59
-     * @param queryParams Url parameters
60
-     * @returns {null}
61
-     */
62
-    static getUrlData({path, queryParams}: { path: string, queryParams: { [key: string]: string } }) {
63
-        let data = null;
64
-        if (path !== null) {
65
-            if (URLHandler.isClubInformationLink(path))
66
-                data = URLHandler.generateClubInformationData(queryParams);
67
-            else if (URLHandler.isPlanningInformationLink(path))
68
-                data = URLHandler.generatePlanningInformationData(queryParams);
69
-        }
70
-        return data;
71
-    }
72
-
73
-    /**
74
-     * Checks if the given url is in a valid format
75
-     *
76
-     * @param url The url to check
77
-     * @returns {boolean}
78
-     */
79
-    static isUrlValid(url: string) {
80
-        return this.getUrlData(URLHandler.parseUrl(url)) !== null;
81
-    }
82
-
83
-    /**
84
-     * Check if the given path links to the club information screen
85
-     *
86
-     * @param path The url to check
87
-     * @returns {boolean}
88
-     */
89
-    static isClubInformationLink(path: string) {
90
-        return path === URLHandler.CLUB_INFO_URL_PATH;
66
+    return parsedData;
67
+  }
68
+
69
+  /**
70
+   * Gets routing data corresponding to the given url.
71
+   * If the url does not match any existing route, null will be returned.
72
+   *
73
+   * @param rawParsedUrlData The data just parsed
74
+   * @returns {null}
75
+   */
76
+  static getUrlData(
77
+    rawParsedUrlData: RawParsedUrlDataType | null,
78
+  ): ParsedUrlDataType | null {
79
+    let parsedData: null | ParsedUrlDataType = null;
80
+    if (rawParsedUrlData != null) {
81
+      const {path} = rawParsedUrlData;
82
+      const {queryParams} = rawParsedUrlData;
83
+      if (URLHandler.isClubInformationLink(path))
84
+        parsedData = URLHandler.generateClubInformationData(queryParams);
85
+      else if (URLHandler.isPlanningInformationLink(path))
86
+        parsedData = URLHandler.generatePlanningInformationData(queryParams);
91 87
     }
92 88
 
93
-    /**
94
-     * Check if the given path links to the planning information screen
95
-     *
96
-     * @param path The url to check
97
-     * @returns {boolean}
98
-     */
99
-    static isPlanningInformationLink(path: string) {
100
-        return path === URLHandler.EVENT_INFO_URL_PATH;
89
+    return parsedData;
90
+  }
91
+
92
+  /**
93
+   * Checks if the given url is in a valid format
94
+   *
95
+   * @param url The url to check
96
+   * @returns {boolean}
97
+   */
98
+  static isUrlValid(url: string): boolean {
99
+    return this.getUrlData(URLHandler.parseUrl(url)) !== null;
100
+  }
101
+
102
+  /**
103
+   * Check if the given path links to the club information screen
104
+   *
105
+   * @param path The url to check
106
+   * @returns {boolean}
107
+   */
108
+  static isClubInformationLink(path: string): boolean {
109
+    return path === URLHandler.CLUB_INFO_URL_PATH;
110
+  }
111
+
112
+  /**
113
+   * Check if the given path links to the planning information screen
114
+   *
115
+   * @param path The url to check
116
+   * @returns {boolean}
117
+   */
118
+  static isPlanningInformationLink(path: string): boolean {
119
+    return path === URLHandler.EVENT_INFO_URL_PATH;
120
+  }
121
+
122
+  /**
123
+   * Generates data formatted for the club information screen from the url parameters.
124
+   *
125
+   * @param params Url parameters to convert
126
+   * @returns {null|{route: string, data: {clubId: number}}}
127
+   */
128
+  static generateClubInformationData(params: {
129
+    [key: string]: string,
130
+  }): ParsedUrlDataType | null {
131
+    if (params.id != null) {
132
+      const id = parseInt(params.id, 10);
133
+      if (!Number.isNaN(id))
134
+        return {
135
+          route: URLHandler.CLUB_INFO_ROUTE,
136
+          data: {clubId: id.toString()},
137
+        };
101 138
     }
102
-
103
-    /**
104
-     * Generates data formatted for the club information screen from the url parameters.
105
-     *
106
-     * @param params Url parameters to convert
107
-     * @returns {null|{route: string, data: {clubId: number}}}
108
-     */
109
-    static generateClubInformationData(params: Object): Object | null {
110
-        if (params !== undefined && params.id !== undefined) {
111
-            let id = parseInt(params.id);
112
-            if (!isNaN(id)) {
113
-                return {route: URLHandler.CLUB_INFO_ROUTE, data: {clubId: id}};
114
-            }
115
-        }
116
-        return null;
139
+    return null;
140
+  }
141
+
142
+  /**
143
+   * Generates data formatted for the planning information screen from the url parameters.
144
+   *
145
+   * @param params Url parameters to convert
146
+   * @returns {null|{route: string, data: {clubId: number}}}
147
+   */
148
+  static generatePlanningInformationData(params: {
149
+    [key: string]: string,
150
+  }): ParsedUrlDataType | null {
151
+    if (params.id != null) {
152
+      const id = parseInt(params.id, 10);
153
+      if (!Number.isNaN(id)) {
154
+        return {
155
+          route: URLHandler.EVENT_INFO_ROUTE,
156
+          data: {eventId: id.toString()},
157
+        };
158
+      }
117 159
     }
118
-
119
-    /**
120
-     * Generates data formatted for the planning information screen from the url parameters.
121
-     *
122
-     * @param params Url parameters to convert
123
-     * @returns {null|{route: string, data: {clubId: number}}}
124
-     */
125
-    static generatePlanningInformationData(params: Object): Object | null {
126
-        if (params !== undefined && params.id !== undefined) {
127
-            let id = parseInt(params.id);
128
-            if (!isNaN(id)) {
129
-                return {route: URLHandler.EVENT_INFO_ROUTE, data: {eventId: id}};
130
-            }
131
-        }
132
-        return null;
160
+    return null;
161
+  }
162
+
163
+  /**
164
+   * Starts listening to events.
165
+   *
166
+   * There are 2 types of event.
167
+   *
168
+   * A classic event, triggered while the app is active.
169
+   * An initial event, called when the app was opened by clicking on a link
170
+   *
171
+   */
172
+  listen() {
173
+    Linking.addEventListener('url', this.onUrl);
174
+    Linking.getInitialURL().then(this.onInitialUrl);
175
+  }
176
+
177
+  /**
178
+   * Gets data from the given url and calls the classic callback with it.
179
+   *
180
+   * @param url The url detected
181
+   */
182
+  onUrl = ({url}: {url: string}) => {
183
+    if (url != null) {
184
+      const data = URLHandler.getUrlData(URLHandler.parseUrl(url));
185
+      if (data != null) this.onDetectURL(data);
133 186
     }
134
-
135
-    /**
136
-     * Starts listening to events.
137
-     *
138
-     * There are 2 types of event.
139
-     *
140
-     * A classic event, triggered while the app is active.
141
-     * An initial event, called when the app was opened by clicking on a link
142
-     *
143
-     */
144
-    listen() {
145
-        Linking.addEventListener('url', this.onUrl);
146
-        Linking.getInitialURL().then(this.onInitialUrl);
187
+  };
188
+
189
+  /**
190
+   * Gets data from the given url and calls the initial callback with it.
191
+   *
192
+   * @param url The url detected
193
+   */
194
+  onInitialUrl = (url: ?string) => {
195
+    if (url != null) {
196
+      const data = URLHandler.getUrlData(URLHandler.parseUrl(url));
197
+      if (data != null) this.onInitialURLParsed(data);
147 198
     }
148
-
149
-    /**
150
-     * Gets data from the given url and calls the classic callback with it.
151
-     *
152
-     * @param url The url detected
153
-     */
154
-    onUrl = ({url}: { url: string }) => {
155
-        if (url != null) {
156
-            let data = URLHandler.getUrlData(URLHandler.parseUrl(url));
157
-            if (data !== null)
158
-                this.onDetectURL(data);
159
-        }
160
-    };
161
-
162
-    /**
163
-     * Gets data from the given url and calls the initial callback with it.
164
-     *
165
-     * @param url The url detected
166
-     */
167
-    onInitialUrl = (url: ?string) => {
168
-        if (url != null) {
169
-            let data = URLHandler.getUrlData(URLHandler.parseUrl(url));
170
-            if (data !== null)
171
-                this.onInitialURLParsed(data);
172
-        }
173
-    };
174
-
199
+  };
175 200
 }

+ 36
- 22
src/utils/Utils.js View File

@@ -1,5 +1,7 @@
1 1
 // @flow
2 2
 
3
+import {Platform, StatusBar} from 'react-native';
4
+import ThemeManager from '../managers/ThemeManager';
3 5
 
4 6
 /**
5 7
  * Gets a sublist of the given list with items of the given ids only
@@ -10,28 +12,40 @@
10 12
  * @param originalList The original list
11 13
  * @returns {[]}
12 14
  */
13
-export function getSublistWithIds(
14
-    idList: Array<string>,
15
-    originalList: Array<{ key: string, [key: string]: any }>
16
-): Array<{ key: string, [key: string]: any }> {
17
-    let subList = [];
18
-    for (let i = 0; i < subList.length; i++) {
19
-        subList.push(null);
20
-    }
21
-    let itemsAdded = 0;
22
-    for (let i = 0; i < originalList.length; i++) {
23
-        const item = originalList[i];
24
-        if (idList.includes(item.key)) {
25
-            subList[idList.indexOf(item.key)] = item;
26
-            itemsAdded++;
27
-            if (itemsAdded === idList.length)
28
-                break;
29
-        }
30
-    }
31
-    for (let i = 0; i < subList.length; i++) {
32
-        if (subList[i] == null)
33
-            subList.splice(i, 1);
15
+export function getSublistWithIds<T>(
16
+  idList: Array<string>,
17
+  originalList: Array<{key: string, ...T}>,
18
+): Array<{key: string, ...T} | null> {
19
+  const subList = [];
20
+  for (let i = 0; i < idList.length; i += 1) {
21
+    subList.push(null);
22
+  }
23
+  let itemsAdded = 0;
24
+  for (let i = 0; i < originalList.length; i += 1) {
25
+    const item = originalList[i];
26
+    if (idList.includes(item.key)) {
27
+      subList[idList.indexOf(item.key)] = item;
28
+      itemsAdded += 1;
29
+      if (itemsAdded === idList.length) break;
34 30
     }
31
+  }
32
+  return subList;
33
+}
35 34
 
36
-    return subList;
35
+/**
36
+ * Updates status bar content color if on iOS only,
37
+ * as the android status bar is always set to black.
38
+ */
39
+export function setupStatusBar() {
40
+  if (ThemeManager.getNightMode()) {
41
+    StatusBar.setBarStyle('light-content', true);
42
+  } else {
43
+    StatusBar.setBarStyle('dark-content', true);
44
+  }
45
+  if (Platform.OS === 'android') {
46
+    StatusBar.setBackgroundColor(
47
+      ThemeManager.getCurrentTheme().colors.surface,
48
+      true,
49
+    );
50
+  }
37 51
 }

Loading…
Cancel
Save