Skip to content

Commit

Permalink
Merge branch 'PapillonApp:development' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
amolinarius authored Mar 30, 2024
2 parents bfea69e + a9ff700 commit 75c9785
Show file tree
Hide file tree
Showing 25 changed files with 1,691 additions and 3,070 deletions.
4 changes: 2 additions & 2 deletions App.old.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ function InsetSettings() {
name="About"
component={AboutScreen}
options={{
headerTitle: 'A propos de Papillon',
headerTitle: 'À propos de Papillon',
headerBackTitle: 'Préférences',
}}
/>
Expand All @@ -344,7 +344,7 @@ function InsetSettings() {
component={DonorsScreen}
options={{
headerTitle: 'Donateurs',
headerBackTitle: 'A propos',
headerBackTitle: 'À propos',
}}
/>
<Stack.Screen
Expand Down
9 changes: 9 additions & 0 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ function App() {
headerBackVisible: false,
},
},
{
name: 'ConsentScreenWithoutAcceptation',
component: require('./views/ConsentScreenWithoutAcceptation').default,
options: {
presentation: 'modal',
headerTitle: 'Termes & conditions',
headerBackVisible: false,
},
},
];

return (
Expand Down
4 changes: 3 additions & 1 deletion fetch/BackgroundFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { BackgroundFetchResult } from 'expo-background-fetch';
import fetchLessons from './background/Lessons';
import fetchHomeworks from './background/Homeworks';
import fetchGrades from './background/Grades';
import fetchCours from './background/BagReminder';
import selfReminder from './background/SelfReminder';

const backgroundFetch = async () => {
console.log('[background fetch] Running background fetch');

await Promise.all([fetchLessons(), fetchHomeworks(), fetchGrades()]);
await Promise.all([fetchLessons(), fetchHomeworks(), fetchGrades(), fetchCours(), selfReminder()]);

return BackgroundFetchResult.NewData;
};
Expand Down
67 changes: 67 additions & 0 deletions fetch/background/BagReminder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {getContextValues} from '../../utils/AppContext';
import notifee from '@notifee/react-native';
import {PapillonLesson} from '../types/timetable';
import {checkCanNotify, DidNotified, SetNotified} from './Helper';
import formatCoursName from '../../utils/FormatCoursName';

const now = new Date();

const fetchCours = async () => {
if (now.getHours() >= 18 && now.getHours() <= 20) {
console.log('[background fetch] Running cours fetch');
const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
tomorrow.setHours(0, 0, 0, 0);
const tommorowAsString = tomorrow.toISOString().split('T')[0];
let dataInstance = await getContextValues().dataProvider;
await dataInstance.getTimetable(tomorrow).then(timetable => {
console.log('[background fetch] fetched cours');
const cours = timetable.filter(cours => {
if (cours.isCancelled) return false;
if (cours.start.split('T')[0] !== tommorowAsString) return false;
return true;
});
if (cours.length > 0) {
remindBag(cours);
}
});
} else {
console.log('[background fetch] Skipping cours fetch');
}
};

const remindBag = async (lesson: PapillonLesson[]) => {
const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
tomorrow.setHours(0, 0, 0, 0);

const canNotify: boolean = await checkCanNotify('notifications_BagReminderEnabled');
const didNotify: boolean = await DidNotified('hw_' + tomorrow.getTime());
if (!canNotify || didNotify) return;
var body = '';
var isFirst = true;
lesson.forEach(cours => {
if (!body.includes(cours.subject)) {
let start = new Date(cours.start);
let end = new Date(cours.end);
if (!isFirst) body += '\n';
isFirst = false;
body += `${('0' + start.getHours()).slice(-2)}:${('0' + start.getMinutes()).slice(-2)} - ${('0' + end.getHours()).slice(-2)}:${('0' + end.getMinutes()).slice(-2)}${formatCoursName(cours.subject.name)}`;
}
});
await notifee.displayNotification({
id: 'hw_' + tomorrow.getTime(),
title: '🎒 Il est temps de préparer votre sac pour demain !',
body: body,
android: {
channelId: 'bag-remind',
},
ios: {
sound: 'papillon_ding.wav',
threadId: 'notifications_BagReminderEnabled',
},
});
await SetNotified('hw_' + tomorrow.getTime());
};

export default fetchCours;
15 changes: 8 additions & 7 deletions fetch/background/Grades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,24 @@ const sendGradesToSharedGroup = async (grades: PapillonGrades) => {
};

const notifyGrades = async (grades: PapillonGrades[]) => {
let oldGrades = await AsyncStorage.getItem('oldGrades');
let oldGradesData = await AsyncStorage.getItem('oldGrades');
const fullGrades = grades.grades;
const avg = await calculateSubjectAverage(fullGrades);

if (oldGrades === null) {
if (oldGradesData === null) {
await AsyncStorage.setItem('oldGrades', JSON.stringify(fullGrades));
return true;
}

oldGrades = JSON.parse(oldGrades);
const oldGrades = JSON.parse(oldGradesData);

if (oldGrades.length === fullGrades.length) {
return true;
}

// find the difference between the two arrays
const lastGrades = fullGrades.filter((grade) => !oldGrades.includes(grade));
// make a list of the new grades
const lastGrades = fullGrades.filter((grade) => {
return !oldGrades.some((oldGrade) => oldGrade.subject.name === grade.subject.name && oldGrade.date === grade.date && oldGrade.grade.value.value === grade.grade.value.value);
});

for (let i = 0; i < lastGrades.length; i++) {
let lastGrade = lastGrades[i];
Expand All @@ -128,7 +129,7 @@ const notifyGrades = async (grades: PapillonGrades[]) => {
let bdy = `Vous avez eu ${lastGrade.grade.value.value}/${lastGrade.grade.out_of.value} ${goodGrade ? '! 👏' : ''}`;

if(lastGrade.grade.value.value === undefined || lastGrade.grade.value.value === null || lastGrade.grade.value.value < 0) {
bdy = 'Vous n\'avez pas été noté(e) pour ce cours.';
bdy = 'Vous n\'avez pas été noté(e) pour cette évaluation.';
}

notifee.displayNotification({
Expand Down
26 changes: 20 additions & 6 deletions fetch/background/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const checkCanNotify = async (type = 'notificationsEnabled') => {
}

console.log('[background fetch] authorized:', authorized, 'canNotify:', canNotify, 'enabled:', enabled);
if(authorized) RegisterNotifChannel()
if(authorized) RegisterNotifChannel();
return authorized && canNotify && enabled;
};

Expand All @@ -46,7 +46,7 @@ const SetNotified = async (id: string) => {
};

const RegisterNotifChannel = async () => {
if(Platform.OS === "ios") return;
if(Platform.OS === 'ios') return;
let groups = [
{
name: 'Nouvelles données disponibles',
Expand Down Expand Up @@ -107,10 +107,24 @@ const RegisterNotifChannel = async () => {
description: 'Indique quand une nouvelle actualité est disponible',
importance: AndroidImportance.HIGH
},
]
await notifee.createChannelGroups(groups)
await notifee.createChannels(channels)
}
{
name: 'Rappels de faire son sac',
id: 'remind-bag',
groupId: 'remind-group',
description: 'Notification de rappel de faire son sac',
importance: AndroidImportance.DEFAULT
},
{
name: 'Rappels de self',
id: 'remind-self',
groupId: 'remind-group',
description: 'Notification de rappel de réserver le self',
importance: AndroidImportance.DEFAULT
}
];
await notifee.createChannelGroups(groups);
await notifee.createChannels(channels);
};

export {
checkCanNotify,
Expand Down
6 changes: 6 additions & 0 deletions fetch/background/Lessons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ const notifyLessons = async (lessons: PapillonLesson[]) => {
const canNotify : boolean = await checkCanNotify('notifications_CoursEnabled');
if (!canNotify) return;

// remove all notifications
for (const lesson of lessons) {
const lessonID = (lesson.subject?.name ? lesson.subject.name : '') + new Date(lesson.start).getTime();
notifee.cancelNotification(lessonID);
}

// get all lessons with status set
const lessonsWithStatus = lessons.filter(lesson => lesson.status !== undefined);

Expand Down
40 changes: 40 additions & 0 deletions fetch/background/SelfReminder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import notifee from '@notifee/react-native';
import {getContextValues} from '../../utils/AppContext';
import {checkCanNotify, DidNotified} from './Helper';

const SelfReminder = async () => {
console.log('[background fetch] Running self reminder');
const now = new Date();

const canNotify: boolean = await checkCanNotify('notifications_BagReminderEnabled');
const didNotify: boolean = await DidNotified('hw_' + now.getTime());
if (!canNotify || didNotify) return;

if (now.getHours() >= 8 && now.getHours() <= 10) {
let dataInstance = await getContextValues().dataProvider;
await dataInstance.getTimetable(now).then(async (timetable) => {
console.log('[background fetch] fetched cours');
const cours = timetable.filter(cours => {
if (cours.isCancelled) return false;
if (cours.start.split('T')[0] !== now.toISOString().split('T')[0]) return false;
return true;
});
if (cours.length > 0) {
await notifee.displayNotification({
id: 'self_' + now.getTime(),
title: '🍽️ Pense à réserver le self !',
body: 'Il est temps d\'aller réserver le self pour ce midi.',
android: {
channelId: 'self-remind',
},
ios: {
sound: 'papillon_ding.wav',
threadId: 'notifications_SelfReminderEnabled',
},
});
}
});
}
};

export default SelfReminder;
Loading

0 comments on commit 75c9785

Please sign in to comment.