diff --git a/API.js b/API.js index 29ba4ad..5ce7d16 100644 --- a/API.js +++ b/API.js @@ -310,6 +310,13 @@ const API = { } return postCall("/user/myProfile", {profile, data}); }, + markNotificationsViewed() { + if (CurrentProfile) { + delete userNameCache[CurrentProfile]; + delete failedProfileCache[CurrentProfile]; + } + return postCall("/user/notifications/viewed", {}); + }, searchProfiles(query){ return getCall("/user/search", query ? {query} : {}).then((data)=>{ if(data.status == "ok"){ diff --git a/App.js b/App.js index be5dbf3..a78b081 100644 --- a/App.js +++ b/App.js @@ -273,6 +273,12 @@ const MainNavigation = ({ route }) => { } export default function App() { + const appState = useSnapshot(GlobalState); + const viewer = appState.me || {}; + const hasUnviewedNotifications = Array.isArray(viewer?.notifications) + ? viewer.notifications.some((n) => n && n.viewed !== true) + : false; + return ( , @@ -295,7 +301,25 @@ export default function App() { props.navigation.navigate("SongPlayer"); }} /> { props.navigation.navigate("Search") }} /> - { props.navigation.navigate("Notifications") }} /> + ( + + + {hasUnviewedNotifications ? ( + + ) : <>} + + )} + onPress={() => { props.navigation.navigate("Notifications") }} + /> ) }, diff --git a/Views/NotificationsView.js b/Views/NotificationsView.js index 4086584..c7ce00e 100644 --- a/Views/NotificationsView.js +++ b/Views/NotificationsView.js @@ -6,10 +6,32 @@ import SinglePost from '../components/SinglePostComponent'; import Moment from 'moment'; import { useSnapshot } from 'valtio'; import GlobalState from '../contexts/GlobalState.js'; +import API from '../API.js'; +import { useFocusEffect } from '@react-navigation/native'; let NotificationsView = ({ navigation, route }) => { const gState = useSnapshot(GlobalState); const viewer = gState.me; + + useFocusEffect( + React.useCallback(() => { + let active = true; + const markViewed = async () => { + const notifications = Array.isArray(viewer?.notifications) ? viewer.notifications : []; + const hasUnviewed = notifications.some((n) => n && n.viewed !== true); + if (!hasUnviewed) return; + const result = await API.markNotificationsViewed(); + if (!active || result?.status !== "ok") return; + if (!GlobalState.me.notifications) GlobalState.me.notifications = []; + GlobalState.me.notifications = GlobalState.me.notifications.map((n) => ({ ...n, viewed: true })); + }; + markViewed(); + return () => { + active = false; + }; + }, [viewer?.notifications?.length]) + ); + const renderNotification = (({ item }) => { const gotToPost = () => { navigation.navigate('SinglePost', { postid: item.postid }); @@ -17,7 +39,7 @@ let NotificationsView = ({ navigation, route }) => { return ( - {item.body} + {item.body} {" " + Moment(item.ts).fromNow()}