Skip to content

Commit

Permalink
Merge pull request #281 from Kgeek33/feat/accuseNews
Browse files Browse the repository at this point in the history
Feat(News): intégration "J'ai pris connaissance..." + changes
  • Loading branch information
ecnivtwelve authored Nov 26, 2024
2 parents 1ae6d47 + 9487cdc commit 0e7024a
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 55 deletions.
2 changes: 0 additions & 2 deletions src/components/Global/PapillonPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ const PapillonPicker: React.FC<PapillonPickerProps> = ({
styles.picker,
direction === "left" ? {
left: 0,
transformOrigin: "left top",
} : {
right: 0,
transformOrigin: "left right",
},
{
backgroundColor: Platform.OS === "ios" ? theme.colors.card + 50 : theme.colors.card,
Expand Down
1 change: 1 addition & 0 deletions src/router/screens/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default [
headerTitle: "Item",
presentation: "modal",
headerShown: false,
animation: "slide_from_right",
}),
createScreen("AddonLogs", AddonLogs, {
headerTitle: "Logs",
Expand Down
6 changes: 2 additions & 4 deletions src/utils/magic/categorizeMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const categorizeMessages = (messages: Information[]): CategorizedMessages
message.title = "";
}

if (matchCount > 0 && !read) {
if (matchCount > 0 && !read && importantMessages.length < 3) {
importantMessages.push({ ...message, matchCount, matchingWords, important: true });

// Log the matching words or phrases for this message
Expand All @@ -52,7 +52,5 @@ export const categorizeMessages = (messages: Information[]): CategorizedMessages
}
}

const limitedImportantMessages = importantMessages.slice(0, 3);

return { importantMessages: limitedImportantMessages, normalMessages };
return { importantMessages, normalMessages };
};
2 changes: 1 addition & 1 deletion src/views/account/News/Atoms/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const NewsListItem: React.FC<NewsListItemProps> = ({ index, message, navigation,
{message.author}
</NativeText>

{!message.read && isED && (
{!message.read && !isED && (
<View style={{
width: 8,
height: 8,
Expand Down
142 changes: 103 additions & 39 deletions src/views/account/News/Document.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import InsetsBottomView from "@/components/Global/InsetsBottomView";
import { NativeItem, NativeList, NativeListHeader, NativeText } from "@/components/Global/NativeComponents";
import {
NativeItem,
NativeList,
NativeListHeader,
NativeText,
} from "@/components/Global/NativeComponents";
import InitialIndicator from "@/components/News/InitialIndicator";
import { Information } from "@/services/shared/Information";
import formatDate from "@/utils/format/format_date_complets";
Expand All @@ -11,26 +16,32 @@ import {
Link,
MoreHorizontal,
} from "lucide-react-native";
import React, { useEffect, useLayoutEffect } from "react";
import {View, Dimensions, Linking, TouchableOpacity, type GestureResponderEvent, StyleSheet} from "react-native";
import React, { useEffect, useLayoutEffect, useState } from "react";
import {View, Dimensions, Linking, TouchableOpacity, type GestureResponderEvent, Text, StyleSheet} from "react-native";
import { ScrollView } from "react-native-gesture-handler";
import HTMLView from "react-native-htmlview";
import { PapillonModernHeader} from "@/components/Global/PapillonModernHeader";
import {LinearGradient} from "expo-linear-gradient";
import {setNewsRead} from "@/services/news";
import {useCurrentAccount} from "@/stores/account";
import { PapillonModernHeader } from "@/components/Global/PapillonModernHeader";
import { LinearGradient } from "expo-linear-gradient";
import { setNewsRead } from "@/services/news";
import { useCurrentAccount } from "@/stores/account";
import PapillonPicker from "@/components/Global/PapillonPicker";
import {Screen} from "@/router/helpers/types";
import {AttachmentType} from "@/services/shared/Attachment";
import PapillonCheckbox from "@/components/Global/PapillonCheckbox";
import { newsInformationAcknowledge } from "pawnote";
import parse_initials from "@/utils/format/format_pronote_initials";
import { selectColorSeed } from "@/utils/format/select_color_seed";
import { AccountService } from "@/stores/account/types";
import { useSafeAreaInsets } from "react-native-safe-area-context";

const NewsItem: Screen<"NewsItem"> = ({ route, navigation }) => {
let message = JSON.parse(route.params.message) as Information;
const [message, setMessage] = useState<Information>(JSON.parse(route.params.message) as Information);
const important = route.params.important;
const isED = route.params.isED;
const account = useCurrentAccount((store) => store.account!);

const insets = useSafeAreaInsets();

const theme = useTheme();
const stylesText = StyleSheet.create({
body: {
Expand All @@ -54,7 +65,10 @@ const NewsItem: Screen<"NewsItem"> = ({ route, navigation }) => {

useEffect(() => {
setNewsRead(account, message, true);
message.read = true;
setMessage((prev) => ({
...prev,
read: true,
}));
}, [account.instance]);

const tagsStyles = {
Expand All @@ -73,14 +87,14 @@ const NewsItem: Screen<"NewsItem"> = ({ route, navigation }) => {

const renderersProps = {
a: {
onPress: onPress
}
onPress: onPress,
},
};

return (
<View style={{flex: 1}}>
<View style={{ flex: 1 }}>
<PapillonModernHeader native height={110} outsideNav={true}>
<View style={{flexDirection: "row", gap: 12, alignItems: "center"}}>
<View style={{ flexDirection: "row", gap: 12, alignItems: "center" }}>
<InitialIndicator
initial={parse_initials(message.author)}
color={selectColorSeed(message.author)}
Expand All @@ -89,30 +103,41 @@ const NewsItem: Screen<"NewsItem"> = ({ route, navigation }) => {
<NativeText variant="title" numberOfLines={1}>{message.title === "" ? message.author : message.title}</NativeText>
<NativeText variant="subtitle" numberOfLines={1}>{message.title === "" ? formatDate(message.date.toDateString()) : message.author}</NativeText>
</View>
{isED && <PapillonPicker
animated
direction="right"
delay={0}
data={[
{
icon: message.read ? <EyeOff /> : <Eye />,
label: message.read ? "Marquer comme non lu" : "Marquer comme lu",
onPress: () => {
setNewsRead(account, message, !message.read);
message.read = !message.read;
}
}
]}
>
<TouchableOpacity>
<MoreHorizontal size={24} color={theme.colors.text} />
</TouchableOpacity>
</PapillonPicker>}
{!isED && (
<PapillonPicker
animated
direction="right"
delay={0}
data={[
{
icon: message.read ? <EyeOff /> : <Eye />,
label: message.read
? "Marquer comme non lu"
: "Marquer comme lu",
onPress: () => {
setNewsRead(account, message, !message.read);
setMessage((prev) => ({
...prev,
read: !prev.read,
}));
},
},
]}
>
<TouchableOpacity>
<MoreHorizontal size={24} color={theme.colors.text} />
</TouchableOpacity>
</PapillonPicker>
)}
</View>
</PapillonModernHeader>
{important && (
<LinearGradient
colors={!theme.dark ? [theme.colors.card, "#BFF6EF"] : [theme.colors.card, "#2C2C2C"]}
colors={
!theme.dark
? [theme.colors.card, "#BFF6EF"]
: [theme.colors.card, "#2C2C2C"]
}
start={[0, 0]}
end={[2, 2]}
style={{
Expand All @@ -130,13 +155,52 @@ const NewsItem: Screen<"NewsItem"> = ({ route, navigation }) => {
<ScrollView
style={{
flex: 1,
}}
contentContainerStyle={{
paddingBottom: 16,
paddingTop: 106,
paddingBottom: 16 + insets.bottom,
paddingTop: 106 - 16,
}}
>
<View style={{paddingHorizontal: 16}}>
<View
style={{
paddingHorizontal: 16,
}}
>

{account.service === AccountService.Pronote && message.ref.needToAcknowledge && (
<NativeList inline
style={{
marginBottom: 16,
}}
>
<NativeItem
leading={
<PapillonCheckbox
checked={message.acknowledged}
onPress={async () => {
if (!message.acknowledged && account.instance) {
await newsInformationAcknowledge(
account.instance,
message.ref
);

setMessage((prev) => ({
...prev,
read: true,
acknowledged: true,
}));
}
}}
color="green"
/>
}
>
<NativeText variant="body">
J'ai lu et pris connaissance
</NativeText>
</NativeItem>
</NativeList>
)}


<HTMLView
value={`<body>${message.content}</body`}
stylesheet={stylesText}
Expand Down Expand Up @@ -167,7 +231,7 @@ const NewsItem: Screen<"NewsItem"> = ({ route, navigation }) => {
</ScrollView>}

{message.attachments.length > 0 && (
<View style={{paddingHorizontal: 16}}>
<View style={{ paddingHorizontal: 16 }}>
<NativeListHeader label="Pièces jointes" />
<NativeList>
{message.attachments.map((attachment, index) => (
Expand Down
51 changes: 42 additions & 9 deletions src/views/account/News/News.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const NewsScreen: Screen<"News"> = ({ route, navigation }) => {
const [isLoading, setIsLoading] = useState(false);
const [importantMessages, setImportantMessages] = useState<NewsItem[]>([]);
const [sortedMessages, setSortedMessages] = useState<NewsItem[]>([]);
const [isED, setIsED] = useState(false);

useLayoutEffect(() => {
navigation.setOptions({
Expand All @@ -43,22 +44,52 @@ const NewsScreen: Screen<"News"> = ({ route, navigation }) => {
}, [account]);

useEffect(() => {
navigation.addListener("focus", () => fetchData(true));
fetchData();
}, [account.instance]);
if (account.service === AccountService.EcoleDirecte) setIsED(true);
if (sortedMessages.length === 0) {
navigation.addListener("focus", () => fetchData(true));
fetchData();
}
}, [sortedMessages, account.instance]);

useEffect(() => {
if (informations) {
if (account.personalization?.magicEnabled) {
const { importantMessages, normalMessages } = categorizeMessages(informations);
setImportantMessages(importantMessages.map(message => ({ ...message, date: message.date.toString() })));
setSortedMessages(normalMessages.map(message => ({ ...message, date: message.date.toString(), important: false })).sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()));
if (account.personalization.MagicNews) {
const { importantMessages, normalMessages } =
categorizeMessages(informations);
setImportantMessages(
importantMessages.map((message) => ({
...message,
date: message.date.toString(),
}))
);
setSortedMessages(
normalMessages
.map((message) => ({
...message,
date: message.date.toString(),
important: false,
}))
.sort(
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
)
);
} else {
setImportantMessages([]);
setSortedMessages(informations.map(info => ({ ...info, date: info.date.toString(), title: info.title || "", important: false })).sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()));
setSortedMessages(
informations
.map((info) => ({
...info,
date: info.date.toString(),
title: info.title || "",
important: false,
}))
.sort(
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
)
);
}
}
}, [informations, account.personalization?.magicEnabled]);
}, [informations, account.personalization.MagicNews]);

const renderItem: ListRenderItem<NewsItem> = useCallback(({ item, index }) => (
<NewsListItem
Expand Down Expand Up @@ -123,6 +154,7 @@ const NewsScreen: Screen<"News"> = ({ route, navigation }) => {
data={importantMessages}
renderItem={renderItem}
keyExtractor={(_, index) => `important-${index}`}
scrollEnabled={false}
/>
</LinearGradient>
</NativeList>
Expand All @@ -140,6 +172,7 @@ const NewsScreen: Screen<"News"> = ({ route, navigation }) => {
data={sortedMessages}
renderItem={renderItem}
keyExtractor={(_, index) => `sorted-${index}`}
scrollEnabled={false}
/>
</NativeList>
</Reanimated.View>
Expand Down

0 comments on commit 0e7024a

Please sign in to comment.