Browse Source

Improved doc and typing, improved API connection handling

Arnaud Vergnet 1 year ago
parent
commit
b813aa0b83

+ 4
- 2
src/constants/Update.js View File

@@ -1,3 +1,5 @@
1
+// @flow
2
+
1 3
 import i18n from "i18n-js";
2 4
 
3 5
 /**
@@ -43,8 +45,8 @@ export default class Update {
43 45
         this.titleList = [];
44 46
         this.descriptionList = [];
45 47
         for (let i = 0; i < Update.slidesNumber; i++) {
46
-            this.titleList.push(i18n.t('intro.updateSlide'+ i + '.title'))
47
-            this.descriptionList.push(i18n.t('intro.updateSlide'+ i + '.text'))
48
+            this.titleList.push(i18n.t('intro.updateSlide' + i + '.title'))
49
+            this.descriptionList.push(i18n.t('intro.updateSlide' + i + '.text'))
48 50
         }
49 51
     }
50 52
 

+ 37
- 10
src/managers/AprilFoolsManager.js View File

@@ -1,14 +1,13 @@
1 1
 // @flow
2 2
 
3
+import type {Machine} from "../screens/Proxiwash/ProxiwashScreen";
4
+
3 5
 /**
4
- * Singleton class used to manage themes
6
+ * Singleton class used to manage april fools
5 7
  */
6 8
 export default class AprilFoolsManager {
7 9
 
8 10
     static instance: AprilFoolsManager | null = null;
9
-
10
-    aprilFoolsEnabled: boolean;
11
-
12 11
     static fakeMachineNumber = [
13 12
         "",
14 13
         "cos(ln(1))",
@@ -24,6 +23,7 @@ export default class AprilFoolsManager {
24 23
         "1×10¹+1×10⁰",
25 24
         "Re(√192e^(iπ/6))",
26 25
     ];
26
+    aprilFoolsEnabled: boolean;
27 27
 
28 28
     constructor() {
29 29
         let today = new Date();
@@ -40,7 +40,13 @@ export default class AprilFoolsManager {
40 40
             AprilFoolsManager.instance;
41 41
     }
42 42
 
43
-    static getFakeMenuItem(menu: Object) {
43
+    /**
44
+     * Adds fake menu entries
45
+     *
46
+     * @param menu
47
+     * @returns {Object}
48
+     */
49
+    static getFakeMenuItem(menu: Array<{dishes: Array<{name: string}>}>) {
44 50
         menu[1]["dishes"].splice(4, 0, {name: "Coq au vin"});
45 51
         menu[1]["dishes"].splice(2, 0, {name: "Bat'Soupe"});
46 52
         menu[1]["dishes"].splice(1, 0, {name: "Pave de loup"});
@@ -49,16 +55,26 @@ export default class AprilFoolsManager {
49 55
         return menu;
50 56
     }
51 57
 
52
-    static getNewProxiwashDryerOrderedList(dryers: Array<Object>) {
53
-        if (dryers !== undefined) {
58
+    /**
59
+     * Changes proxiwash dryers order
60
+     *
61
+     * @param dryers
62
+     */
63
+    static getNewProxiwashDryerOrderedList(dryers: Array<Machine> | null) {
64
+        if (dryers != null) {
54 65
             let second = dryers[1];
55 66
             dryers.splice(1, 1);
56 67
             dryers.push(second);
57 68
         }
58 69
     }
59 70
 
60
-    static getNewProxiwashWasherOrderedList(washers: Array<Object>) {
61
-        if (washers !== undefined) {
71
+    /**
72
+     * Changes proxiwash washers order
73
+     *
74
+     * @param washers
75
+     */
76
+    static getNewProxiwashWasherOrderedList(washers: Array<Machine> | null) {
77
+        if (washers != null) {
62 78
             let first = washers[0];
63 79
             let second = washers[1];
64 80
             let fifth = washers[4];
@@ -67,14 +83,25 @@ export default class AprilFoolsManager {
67 83
             washers.splice(4, 1, ninth);
68 84
             washers.splice(1, 1, first);
69 85
             washers.splice(0, 1, fifth);
70
-            // washers.push(fifth);
71 86
         }
72 87
     }
73 88
 
89
+    /**
90
+     * Gets the new display number for the given machine number
91
+     *
92
+     * @param number
93
+     * @returns {string}
94
+     */
74 95
     static getProxiwashMachineDisplayNumber(number: number) {
75 96
         return AprilFoolsManager.fakeMachineNumber[number];
76 97
     }
77 98
 
99
+    /**
100
+     * Gets the new and ugly april fools theme
101
+     *
102
+     * @param currentTheme
103
+     * @returns {{colors: {textDisabled: string, agendaDayTextColor: string, surface: string, background: string, dividerBackground: string, accent: string, agendaBackgroundColor: string, tabIcon: string, card: string, primary: string}}}
104
+     */
78 105
     static getAprilFoolsTheme(currentTheme: Object) {
79 106
         return {
80 107
             ...currentTheme,

+ 64
- 27
src/managers/ConnectionManager.js View File

@@ -23,15 +23,13 @@ export default class ConnectionManager {
23 23
     #email: string;
24 24
     #token: string | null;
25 25
 
26
-    listeners: Array<Function>;
27
-
28 26
     constructor() {
29 27
         this.#token = null;
30
-        this.listeners = [];
31 28
     }
32 29
 
33 30
     /**
34
-     * Get this class instance or create one if none is found
31
+     * Gets this class instance or create one if none is found
32
+     *
35 33
      * @returns {ConnectionManager}
36 34
      */
37 35
     static getInstance(): ConnectionManager {
@@ -40,21 +38,20 @@ export default class ConnectionManager {
40 38
             ConnectionManager.instance;
41 39
     }
42 40
 
43
-    getToken() {
41
+    /**
42
+     * Gets the current token
43
+     *
44
+     * @returns {string | null}
45
+     */
46
+    getToken(): string | null {
44 47
         return this.#token;
45 48
     }
46 49
 
47
-    onLoginStateChange(newState: boolean) {
48
-        for (let i = 0; i < this.listeners.length; i++) {
49
-            if (this.listeners[i] !== undefined)
50
-                this.listeners[i](newState);
51
-        }
52
-    }
53
-
54
-    addLoginStateListener(listener: Function) {
55
-        this.listeners.push(listener);
56
-    }
57
-
50
+    /**
51
+     * Tries to recover login token from the secure keychain
52
+     *
53
+     * @returns {Promise<R>}
54
+     */
58 55
     async recoverLogin() {
59 56
         return new Promise((resolve, reject) => {
60 57
             if (this.getToken() !== null)
@@ -64,7 +61,6 @@ export default class ConnectionManager {
64 61
                     .then((data) => {
65 62
                         if (data) {
66 63
                             this.#token = data.password;
67
-                            this.onLoginStateChange(true);
68 64
                             resolve(this.#token);
69 65
                         } else
70 66
                             reject(false);
@@ -76,17 +72,28 @@ export default class ConnectionManager {
76 72
         });
77 73
     }
78 74
 
75
+    /**
76
+     * Check if the user has a valid token
77
+     *
78
+     * @returns {boolean}
79
+     */
79 80
     isLoggedIn() {
80 81
         return this.getToken() !== null;
81 82
     }
82 83
 
84
+    /**
85
+     * Saves the login token in the secure keychain
86
+     *
87
+     * @param email
88
+     * @param token
89
+     * @returns {Promise<R>}
90
+     */
83 91
     async saveLogin(email: string, token: string) {
84 92
         return new Promise((resolve, reject) => {
85 93
             Keychain.setInternetCredentials(SERVER_NAME, 'token', token)
86 94
                 .then(() => {
87 95
                     this.#token = token;
88 96
                     this.#email = email;
89
-                    this.onLoginStateChange(true);
90 97
                     resolve(true);
91 98
                 })
92 99
                 .catch(() => {
@@ -95,12 +102,16 @@ export default class ConnectionManager {
95 102
         });
96 103
     }
97 104
 
105
+    /**
106
+     * Deletes the login token from the keychain
107
+     *
108
+     * @returns {Promise<R>}
109
+     */
98 110
     async disconnect() {
99 111
         return new Promise((resolve, reject) => {
100 112
             Keychain.resetInternetCredentials(SERVER_NAME)
101 113
                 .then(() => {
102 114
                     this.#token = null;
103
-                    this.onLoginStateChange(false);
104 115
                     resolve(true);
105 116
                 })
106 117
                 .catch(() => {
@@ -109,6 +120,16 @@ export default class ConnectionManager {
109 120
         });
110 121
     }
111 122
 
123
+
124
+    /**
125
+     * Sends the given login and password to the api.
126
+     * If the combination is valid, the login token is received and saved in the secure keychain.
127
+     * If not, the promise is rejected with the corresponding error code.
128
+     *
129
+     * @param email
130
+     * @param password
131
+     * @returns {Promise<R>}
132
+     */
112 133
     async connect(email: string, password: string) {
113 134
         return new Promise((resolve, reject) => {
114 135
             const data = {
@@ -117,19 +138,28 @@ export default class ConnectionManager {
117 138
             };
118 139
             apiRequest(AUTH_PATH, 'POST', data)
119 140
                 .then((response) => {
120
-                    this.saveLogin(email, response.token)
121
-                        .then(() => {
122
-                            resolve(true);
123
-                        })
124
-                        .catch(() => {
125
-                            reject(ERROR_TYPE.TOKEN_SAVE);
126
-                        });
141
+                    if (this.isConnectionResponseValid(response)) {
142
+                        this.saveLogin(email, response.token)
143
+                            .then(() => {
144
+                                resolve(true);
145
+                            })
146
+                            .catch(() => {
147
+                                reject(ERROR_TYPE.TOKEN_SAVE);
148
+                            });
149
+                    } else
150
+                        reject(ERROR_TYPE.SERVER_ERROR);
127 151
                 })
128 152
                 .catch((error) => reject(error));
129 153
         });
130 154
     }
131 155
 
132
-    isConnectionResponseValid(response: Object) {
156
+    /**
157
+     * Checks if the API connection response is correctly formatted
158
+     *
159
+     * @param response
160
+     * @returns {boolean}
161
+     */
162
+    isConnectionResponseValid(response: { [key: string]: any }) {
133 163
         let valid = isResponseValid(response);
134 164
 
135 165
         if (valid && response.error === ERROR_TYPE.SUCCESS)
@@ -140,6 +170,13 @@ export default class ConnectionManager {
140 170
         return valid;
141 171
     }
142 172
 
173
+    /**
174
+     * Sends an authenticated request with the login token to the API
175
+     *
176
+     * @param path
177
+     * @param params
178
+     * @returns {Promise<R>}
179
+     */
143 180
     async authenticatedRequest(path: string, params: Object) {
144 181
         return new Promise((resolve, reject) => {
145 182
             if (this.getToken() !== null) {

+ 4
- 4
src/managers/DateManager.js View File

@@ -45,6 +45,10 @@ export default class DateManager {
45 45
             DateManager.instance;
46 46
     }
47 47
 
48
+    static isWeekend(date: Date) {
49
+        return date.getDay() === 6 || date.getDay() === 0;
50
+    }
51
+
48 52
     /**
49 53
      * Gets a translated string representing the given date.
50 54
      *
@@ -58,8 +62,4 @@ export default class DateManager {
58 62
         return this.daysOfWeek[date.getDay()] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
59 63
     }
60 64
 
61
-    static isWeekend(date: Date) {
62
-        return date.getDay() === 6 || date.getDay() === 0;
63
-    }
64
-
65 65
 }

+ 0
- 4
src/managers/LocaleManager.js View File

@@ -19,8 +19,4 @@ export default class LocaleManager {
19 19
         i18n.translations = {fr, en};
20 20
         i18n.locale = RNLocalize.findBestAvailableLanguage(["en", "fr"]).languageTag;
21 21
     }
22
-
23
-    static getCurrentLocale() {
24
-        return RNLocalize.findBestAvailableLanguage(["en", "fr"]).languageTag;
25
-    }
26 22
 }

+ 29
- 29
src/managers/ThemeManager.js View File

@@ -46,15 +46,15 @@ export type CustomTheme = {
46 46
 
47 47
         // Tetris
48 48
         tetrisBackground: string,
49
-        tetrisBorder:string,
50
-        tetrisScore:string,
51
-        tetrisI : string,
52
-        tetrisO : string,
53
-        tetrisT : string,
54
-        tetrisS : string,
55
-        tetrisZ : string,
56
-        tetrisJ : string,
57
-        tetrisL : string,
49
+        tetrisBorder: string,
50
+        tetrisScore: string,
51
+        tetrisI: string,
52
+        tetrisO: string,
53
+        tetrisT: string,
54
+        tetrisS: string,
55
+        tetrisZ: string,
56
+        tetrisJ: string,
57
+        tetrisL: string,
58 58
     },
59 59
 }
60 60
 
@@ -116,16 +116,16 @@ export default class ThemeManager {
116 116
                 tutorinsaColor: '#f93943',
117 117
 
118 118
                 // Tetris
119
-                tetrisBackground:'#e6e6e6',
120
-                tetrisBorder:'#2f2f2f',
121
-                tetrisScore:'#e2bd33',
122
-                tetrisI : '#3cd9e6',
123
-                tetrisO : '#ffdd00',
124
-                tetrisT : '#a716e5',
125
-                tetrisS : '#09c528',
126
-                tetrisZ : '#ff0009',
127
-                tetrisJ : '#2a67e3',
128
-                tetrisL : '#da742d',
119
+                tetrisBackground: '#e6e6e6',
120
+                tetrisBorder: '#2f2f2f',
121
+                tetrisScore: '#e2bd33',
122
+                tetrisI: '#3cd9e6',
123
+                tetrisO: '#ffdd00',
124
+                tetrisT: '#a716e5',
125
+                tetrisS: '#09c528',
126
+                tetrisZ: '#ff0009',
127
+                tetrisJ: '#2a67e3',
128
+                tetrisL: '#da742d',
129 129
             },
130 130
         };
131 131
     }
@@ -176,16 +176,16 @@ export default class ThemeManager {
176 176
                 tutorinsaColor: '#f93943',
177 177
 
178 178
                 // Tetris
179
-                tetrisBackground:'#2c2c2c',
180
-                tetrisBorder:'#1b1b1b',
181
-                tetrisScore:'#e2d707',
182
-                tetrisI : '#30b3be',
183
-                tetrisO : '#c1a700',
184
-                tetrisT : '#9114c7',
185
-                tetrisS : '#08a121',
186
-                tetrisZ : '#b50008',
187
-                tetrisJ : '#0f37b9',
188
-                tetrisL : '#b96226',
179
+                tetrisBackground: '#2c2c2c',
180
+                tetrisBorder: '#1b1b1b',
181
+                tetrisScore: '#e2d707',
182
+                tetrisI: '#30b3be',
183
+                tetrisO: '#c1a700',
184
+                tetrisT: '#9114c7',
185
+                tetrisS: '#08a121',
186
+                tetrisZ: '#b50008',
187
+                tetrisJ: '#0f37b9',
188
+                tetrisL: '#b96226',
189 189
             },
190 190
         };
191 191
     }

Loading…
Cancel
Save