mirror of
https://github.com/lemeow125/StudE-Frontend.git
synced 2024-11-17 06:19:25 +08:00
Fixed group message notifications always triggering
This commit is contained in:
parent
2461f2c404
commit
fa07743a90
1 changed files with 34 additions and 22 deletions
|
@ -17,16 +17,26 @@ const FETCH_GROUP_MESSAGES = "GROUP_MESSAGES_TASK";
|
|||
TaskManager.defineTask(FETCH_GROUP_MESSAGES, async () => {
|
||||
const data = await GetStudyGroupMessages();
|
||||
if (data[0] && data[1]) {
|
||||
let messages_prev = JSON.parse(
|
||||
(await AsyncStorage.getItem("messages")) || "{}"
|
||||
let messages_prev = await JSON.parse(
|
||||
(await AsyncStorage.getItem("messages")) || "[]"
|
||||
);
|
||||
if (!messages_prev) {
|
||||
await AsyncStorage.setItem("messages", JSON.stringify(data[1]));
|
||||
} else {
|
||||
let message_curr = data[1];
|
||||
let difference: Array<any> = messages_prev
|
||||
.filter((x: any) => !message_curr.includes(x))
|
||||
.concat(message_curr.filter((x: any) => !messages_prev.includes(x)));
|
||||
.filter(
|
||||
(x: any) =>
|
||||
!message_curr.some(
|
||||
(y: any) => JSON.stringify(y) === JSON.stringify(x)
|
||||
)
|
||||
)
|
||||
.concat(
|
||||
message_curr.filter(
|
||||
(x: any) =>
|
||||
!messages_prev.some(
|
||||
(y: any) => JSON.stringify(y) === JSON.stringify(x)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if (difference.length > 0) {
|
||||
console.log(`${difference.length} unread messages`);
|
||||
|
@ -40,7 +50,6 @@ TaskManager.defineTask(FETCH_GROUP_MESSAGES, async () => {
|
|||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log(data[1].response.data);
|
||||
}
|
||||
|
@ -52,7 +61,7 @@ TaskManager.defineTask(FETCH_STUDENT_STATUS, async () => {
|
|||
const data = await GetStudyGroupListFiltered();
|
||||
const student_status_data = await GetStudentStatus();
|
||||
if (data[0] && data[1]) {
|
||||
console.log("Background Fetch", data[1]);
|
||||
console.log("Fetching nearby study groups...");
|
||||
const entryWithLeastDistance = data[1].reduce(
|
||||
(prev: StudyGroupType, curr: StudyGroupType) => {
|
||||
return prev.distance < curr.distance ? prev : curr;
|
||||
|
@ -63,6 +72,9 @@ TaskManager.defineTask(FETCH_STUDENT_STATUS, async () => {
|
|||
student_status_data[1].study_group == null ||
|
||||
student_status_data[1].study_group == ""
|
||||
) {
|
||||
console.log(
|
||||
"User has no study group yet. Found nearby groups, pushing notification"
|
||||
);
|
||||
Notifications.scheduleNotificationAsync({
|
||||
content: {
|
||||
title: "Students are studying nearby",
|
||||
|
|
Loading…
Reference in a new issue