Improve utils files to match linter

This commit is contained in:
Arnaud Vergnet 2020-08-05 18:52:18 +02:00
parent fcbc70956b
commit 569e659779
3 changed files with 91 additions and 71 deletions

View file

@ -3,7 +3,7 @@
import i18n from 'i18n-js'; import i18n from 'i18n-js';
import type {DeviceType} from '../screens/Amicale/Equipment/EquipmentListScreen'; import type {DeviceType} from '../screens/Amicale/Equipment/EquipmentListScreen';
import DateManager from '../managers/DateManager'; import DateManager from '../managers/DateManager';
import type {CustomTheme} from '../managers/ThemeManager'; import type {CustomThemeType} from '../managers/ThemeManager';
import type {MarkedDatesObjectType} from '../screens/Amicale/Equipment/EquipmentRentScreen'; import type {MarkedDatesObjectType} from '../screens/Amicale/Equipment/EquipmentRentScreen';
/** /**
@ -161,7 +161,7 @@ export function getValidRange(
*/ */
export function generateMarkedDates( export function generateMarkedDates(
isSelection: boolean, isSelection: boolean,
theme: CustomTheme, theme: CustomThemeType,
range: Array<string>, range: Array<string>,
): MarkedDatesObjectType { ): MarkedDatesObjectType {
const markedDates = {}; const markedDates = {};

View file

@ -1,10 +1,14 @@
// @flow // @flow
import {checkNotifications, requestNotifications, RESULTS} from 'react-native-permissions'; import {
import AsyncStorageManager from "../managers/AsyncStorageManager"; checkNotifications,
import i18n from "i18n-js"; requestNotifications,
RESULTS,
} from 'react-native-permissions';
import i18n from 'i18n-js';
import AsyncStorageManager from '../managers/AsyncStorageManager';
const PushNotification = require("react-native-push-notification"); const PushNotification = require('react-native-push-notification');
// Used to multiply the normal notification id to create the reminder one. It allows to find it back easily // Used to multiply the normal notification id to create the reminder one. It allows to find it back easily
const reminderIdFactor = 100; const reminderIdFactor = 100;
@ -13,25 +17,21 @@ const reminderIdFactor = 100;
* Async function asking permission to send notifications to the user. * Async function asking permission to send notifications to the user.
* Used on ios. * Used on ios.
* *
* @returns {Promise} * @returns {Promise<void>}
*/ */
export async function askPermissions() { export async function askPermissions(): Promise<void> {
return new Promise(((resolve, reject) => { return new Promise((resolve: () => void, reject: () => void) => {
checkNotifications().then(({status}) => { checkNotifications().then(({status}: {status: string}) => {
if (status === RESULTS.GRANTED) if (status === RESULTS.GRANTED) resolve();
resolve(); else if (status === RESULTS.BLOCKED) reject();
else if (status === RESULTS.BLOCKED) else {
reject() requestNotifications().then((result: {status: string}) => {
else { if (result.status === RESULTS.GRANTED) resolve();
requestNotifications().then(({status}) => { else reject();
if (status === RESULTS.GRANTED)
resolve();
else
reject();
});
}
}); });
})); }
});
});
} }
/** /**
@ -43,27 +43,33 @@ export async function askPermissions() {
* @param date The date to trigger the notification at * @param date The date to trigger the notification at
*/ */
function createNotifications(machineID: string, date: Date) { function createNotifications(machineID: string, date: Date) {
let reminder = AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key); const reminder = AsyncStorageManager.getNumber(
if (!isNaN(reminder) && reminder > 0) { AsyncStorageManager.PREFERENCES.proxiwashNotifications.key,
let id = reminderIdFactor * parseInt(machineID); );
let reminderDate = new Date(date); if (!Number.isNaN(reminder) && reminder > 0) {
reminderDate.setMinutes(reminderDate.getMinutes() - reminder); const id = reminderIdFactor * parseInt(machineID, 10);
PushNotification.localNotificationSchedule({ const reminderDate = new Date(date);
title: i18n.t("screens.proxiwash.notifications.machineRunningTitle", {time: reminder}), reminderDate.setMinutes(reminderDate.getMinutes() - reminder);
message: i18n.t("screens.proxiwash.notifications.machineRunningBody", {number: machineID}),
id: id.toString(),
date: reminderDate,
});
console.log("Setting up notifications for ", date, " and reminder for ", reminderDate);
} else
console.log("Setting up notifications for ", date);
PushNotification.localNotificationSchedule({ PushNotification.localNotificationSchedule({
title: i18n.t("screens.proxiwash.notifications.machineFinishedTitle"), title: i18n.t('screens.proxiwash.notifications.machineRunningTitle', {
message: i18n.t("screens.proxiwash.notifications.machineFinishedBody", {number: machineID}), time: reminder,
id: machineID, }),
date: date, message: i18n.t('screens.proxiwash.notifications.machineRunningBody', {
number: machineID,
}),
id: id.toString(),
date: reminderDate,
}); });
}
PushNotification.localNotificationSchedule({
title: i18n.t('screens.proxiwash.notifications.machineFinishedTitle'),
message: i18n.t('screens.proxiwash.notifications.machineFinishedBody', {
number: machineID,
}),
id: machineID,
date,
});
} }
/** /**
@ -76,22 +82,26 @@ function createNotifications(machineID: string, date: Date) {
* @param isEnabled True to enable notifications, false to disable * @param isEnabled True to enable notifications, false to disable
* @param endDate The trigger date, or null if disabling notifications * @param endDate The trigger date, or null if disabling notifications
*/ */
export async function setupMachineNotification(machineID: string, isEnabled: boolean, endDate: Date | null) { export async function setupMachineNotification(
return new Promise((resolve, reject) => { machineID: string,
if (isEnabled && endDate != null) { isEnabled: boolean,
askPermissions() endDate: Date | null,
.then(() => { ): Promise<void> {
createNotifications(machineID, endDate); return new Promise((resolve: () => void, reject: () => void) => {
resolve(); if (isEnabled && endDate != null) {
}) askPermissions()
.catch(() => { .then(() => {
reject(); createNotifications(machineID, endDate);
}); resolve();
} else { })
PushNotification.cancelLocalNotifications({id: machineID}); .catch(() => {
let reminderId = reminderIdFactor * parseInt(machineID); reject();
PushNotification.cancelLocalNotifications({id: reminderId.toString()}); });
resolve(); } else {
} PushNotification.cancelLocalNotifications({id: machineID});
}); const reminderId = reminderIdFactor * parseInt(machineID, 10);
PushNotification.cancelLocalNotifications({id: reminderId.toString()});
resolve();
}
});
} }

View file

@ -1,5 +1,7 @@
import React from 'react'; // @flow
import {useCollapsibleStack} from "react-navigation-collapsible";
import * as React from 'react';
import {useCollapsibleStack} from 'react-navigation-collapsible';
/** /**
* Function used to manipulate Collapsible Hooks from a class. * Function used to manipulate Collapsible Hooks from a class.
@ -14,12 +16,20 @@ import {useCollapsibleStack} from "react-navigation-collapsible";
* @param Component The component to use Collapsible with * @param Component The component to use Collapsible with
* @returns {React.ComponentType<any>} * @returns {React.ComponentType<any>}
*/ */
export const withCollapsible = (Component: React.ComponentType<any>) => { export default function withCollapsible(
return React.forwardRef((props: any, ref: any) => { // eslint-disable-next-line flowtype/no-weak-types
return <Component Component: React.ComponentType<any>,
collapsibleStack={useCollapsibleStack()} // eslint-disable-next-line flowtype/no-weak-types
ref={ref} ): React$AbstractComponent<any, any> {
{...props} // eslint-disable-next-line flowtype/no-weak-types
/>; return React.forwardRef((props: any, ref: any): React.Node => {
}); return (
}; <Component
collapsibleStack={useCollapsibleStack()}
ref={ref}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
/>
);
});
}