forked from vergnet/application-amicale
Improved notification activation on corner cases
This commit is contained in:
parent
1835fcadf9
commit
aa2fad344a
4 changed files with 47 additions and 30 deletions
|
@ -4,21 +4,29 @@ import {getCleanedMachineWatched, getMachineEndDate, getMachineOfId, isMachineWa
|
||||||
test('getMachineEndDate', () => {
|
test('getMachineEndDate', () => {
|
||||||
jest.spyOn(Date, 'now')
|
jest.spyOn(Date, 'now')
|
||||||
.mockImplementation(() =>
|
.mockImplementation(() =>
|
||||||
new Date('2020-01-14T00:00:00.000Z').getTime()
|
new Date('2020-01-14T15:00:00.000Z').getTime()
|
||||||
);
|
);
|
||||||
let expectDate = new Date('2020-01-14T00:00:00.000Z');
|
let expectDate = new Date('2020-01-14T15:00:00.000Z');
|
||||||
expectDate.setHours(23);
|
expectDate.setHours(23);
|
||||||
expectDate.setMinutes(10);
|
expectDate.setMinutes(10);
|
||||||
expect(getMachineEndDate({endTime: "23:10"}).getTime()).toBe(expectDate.getTime());
|
expect(getMachineEndDate({endTime: "23:10"}).getTime()).toBe(expectDate.getTime());
|
||||||
|
|
||||||
expectDate.setHours(15);
|
expectDate.setHours(16);
|
||||||
expectDate.setMinutes(30);
|
expectDate.setMinutes(30);
|
||||||
expect(getMachineEndDate({endTime: "15:30"}).getTime()).toBe(expectDate.getTime());
|
expect(getMachineEndDate({endTime: "16:30"}).getTime()).toBe(expectDate.getTime());
|
||||||
|
|
||||||
|
expect(getMachineEndDate({endTime: "15:30"})).toBeNull();
|
||||||
|
|
||||||
|
expect(getMachineEndDate({endTime: "13:10"})).toBeNull();
|
||||||
|
|
||||||
|
jest.spyOn(Date, 'now')
|
||||||
|
.mockImplementation(() =>
|
||||||
|
new Date('2020-01-14T23:00:00.000Z').getTime()
|
||||||
|
);
|
||||||
|
expectDate = new Date('2020-01-14T23:00:00.000Z');
|
||||||
expectDate.setHours(0);
|
expectDate.setHours(0);
|
||||||
expectDate.setMinutes(10);
|
expectDate.setMinutes(30);
|
||||||
expectDate.setDate(expectDate.getDate() + 1);
|
expect(getMachineEndDate({endTime: "00:30"}).getTime()).toBe(expectDate.getTime());
|
||||||
expect(getMachineEndDate({endTime: "00:10"}).getTime()).toBe(expectDate.getTime());
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('isMachineWatched', () => {
|
test('isMachineWatched', () => {
|
||||||
|
|
|
@ -18,6 +18,7 @@ import type {CustomTheme} from "../../managers/ThemeManager";
|
||||||
import {Collapsible} from "react-navigation-collapsible";
|
import {Collapsible} from "react-navigation-collapsible";
|
||||||
import {StackNavigationProp} from "@react-navigation/stack";
|
import {StackNavigationProp} from "@react-navigation/stack";
|
||||||
import {getCleanedMachineWatched, getMachineEndDate, isMachineWatched} from "../../utils/Proxiwash";
|
import {getCleanedMachineWatched, getMachineEndDate, isMachineWatched} from "../../utils/Proxiwash";
|
||||||
|
import {Modalize} from "react-native-modalize";
|
||||||
|
|
||||||
const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/washinsa/washinsa.json";
|
const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/washinsa/washinsa.json";
|
||||||
|
|
||||||
|
@ -55,9 +56,12 @@ type State = {
|
||||||
*/
|
*/
|
||||||
class ProxiwashScreen extends React.Component<Props, State> {
|
class ProxiwashScreen extends React.Component<Props, State> {
|
||||||
|
|
||||||
modalRef: Object;
|
modalRef: null | Modalize;
|
||||||
|
|
||||||
fetchedData: Object;
|
fetchedData: {
|
||||||
|
dryers: Array<Machine>,
|
||||||
|
washers: Array<Machine>,
|
||||||
|
};
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
refreshing: false,
|
refreshing: false,
|
||||||
|
@ -95,7 +99,10 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
||||||
*/
|
*/
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.props.navigation.setOptions({
|
this.props.navigation.setOptions({
|
||||||
headerRight: this.getAboutButton,
|
headerRight:
|
||||||
|
<MaterialHeaderButtons>
|
||||||
|
<Item title="information" iconName="information" onPress={this.onAboutPress}/>
|
||||||
|
</MaterialHeaderButtons>,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,16 +112,6 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
||||||
*/
|
*/
|
||||||
onAboutPress = () => this.props.navigation.navigate('proxiwash-about');
|
onAboutPress = () => this.props.navigation.navigate('proxiwash-about');
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the about header button
|
|
||||||
*
|
|
||||||
* @return {*}
|
|
||||||
*/
|
|
||||||
getAboutButton = () =>
|
|
||||||
<MaterialHeaderButtons>
|
|
||||||
<Item title="information" iconName="information" onPress={this.onAboutPress}/>
|
|
||||||
</MaterialHeaderButtons>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts the key for the given item
|
* Extracts the key for the given item
|
||||||
*
|
*
|
||||||
|
@ -123,7 +120,6 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
||||||
*/
|
*/
|
||||||
getKeyExtractor = (item: Machine) => item.number;
|
getKeyExtractor = (item: Machine) => item.number;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setups notifications for the machine with the given ID.
|
* Setups notifications for the machine with the given ID.
|
||||||
* One notification will be sent at the end of the program.
|
* One notification will be sent at the end of the program.
|
||||||
|
@ -141,7 +137,7 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
||||||
this.showNotificationsDisabledWarning();
|
this.showNotificationsDisabledWarning();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
Notifications.setupMachineNotification(machine.number, false)
|
Notifications.setupMachineNotification(machine.number, false, null)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
this.removeNotificationFromState(machine);
|
this.removeNotificationFromState(machine);
|
||||||
});
|
});
|
||||||
|
|
|
@ -62,7 +62,7 @@ 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
|
* @param endDate
|
||||||
*/
|
*/
|
||||||
export async function setupMachineNotification(machineID: string, isEnabled: boolean, endDate?: Date) {
|
export async function setupMachineNotification(machineID: string, isEnabled: boolean, endDate: Date | null) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (isEnabled && endDate != null) {
|
if (isEnabled && endDate != null) {
|
||||||
askPermissions()
|
askPermissions()
|
||||||
|
|
|
@ -4,18 +4,31 @@ import type {Machine} from "../screens/Proxiwash/ProxiwashScreen";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the machine end Date object.
|
* Gets the machine end Date object.
|
||||||
* If the time is before the current time, it will be considered as tomorrow
|
* If the end time is at least 12 hours before the current time,
|
||||||
|
* it will be considered as happening the day after.
|
||||||
|
* If it is before but less than 12 hours, it will be considered invalid (to fix proxiwash delay)
|
||||||
*
|
*
|
||||||
* @param machine The machine to get the date from
|
* @param machine The machine to get the date from
|
||||||
* @returns {Date} The date object representing the end time.
|
* @returns {Date} The date object representing the end time.
|
||||||
*/
|
*/
|
||||||
export function getMachineEndDate(machine: Machine) {
|
export function getMachineEndDate(machine: Machine): Date | null {
|
||||||
const array = machine.endTime.split(":");
|
const array = machine.endTime.split(":");
|
||||||
let date = new Date(Date.now());
|
let endDate = new Date(Date.now());
|
||||||
date.setHours(parseInt(array[0]), parseInt(array[1]));
|
endDate.setHours(parseInt(array[0]), parseInt(array[1]));
|
||||||
if (date < new Date(Date.now()))
|
|
||||||
date.setDate(date.getDate() + 1);
|
let limit = new Date(Date.now());
|
||||||
return date;
|
if (endDate < limit) {
|
||||||
|
if (limit.getHours() > 12) {
|
||||||
|
limit.setHours(limit.getHours() - 12);
|
||||||
|
if (endDate < limit)
|
||||||
|
endDate.setDate(endDate.getDate() + 1);
|
||||||
|
else
|
||||||
|
endDate = null;
|
||||||
|
} else
|
||||||
|
endDate = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return endDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue