Skip to content

Commit

Permalink
feat(notifications): add notification preferences (#433)
Browse files Browse the repository at this point in the history
Closes #44

---------

Co-authored-by: Umberto Pepato <[email protected]>
  • Loading branch information
lpezzolla and umbopepato authored Feb 1, 2024
1 parent 491caad commit 7a8447c
Show file tree
Hide file tree
Showing 38 changed files with 872 additions and 374 deletions.
6 changes: 5 additions & 1 deletion assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"campus": "Campus",
"cancel": "Cancel",
"capacity": "Capacity",
"career": "Career",
"cfu": "CFU",
"classroomsNotSpecified": "Classroom not specified",
"cleanCourseFiles": "Remove all course files",
Expand Down Expand Up @@ -522,7 +523,10 @@
"reservationPresence": "Reservations"
},
"notificationsScreen": {
"title": "Notifications archive"
"title": "Notifications",
"perCourseTitle": "Courses",
"globalTitle": "Global",
"globalSubtitle": "Important notifications are always active"
},
"offeringBachelorScreen": {
"title": "Bachelor degree"
Expand Down
8 changes: 6 additions & 2 deletions assets/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"campus": "Sede",
"cancel": "Annulla",
"capacity": "Capacità",
"career": "Carriera",
"cfu": "CFU",
"classroomsNotSpecified": "Luogo non specificato",
"cleanCourseFiles": "Rimuovi file dei corsi",
Expand Down Expand Up @@ -514,15 +515,18 @@
"eventStartTime": "Inizio evento",
"information": "Informazioni",
"logo": "Logo",
"title": "Avvisi & News"
"title": "News & Eventi"
},
"notifications": {
"events": "Eventi e iniziative",
"important": "Importanti",
"reservationPresence": "Prenotazione presenza"
},
"notificationsScreen": {
"title": "Archivio notifiche"
"title": "Notifiche",
"perCourseTitle": "Corsi",
"globalTitle": "Globali",
"globalSubtitle": "Le notifiche importanti sono sempre attive"
},
"offeringBachelorScreen": {
"title": "Lauree triennali"
Expand Down
1 change: 1 addition & 0 deletions lib/ui/components/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface ListItemProps extends TouchableHighlightProps {
titleProps?: TextProps;
multilineTitle?: boolean;
unread?: boolean;
isInVisibleRange?: boolean;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions lib/ui/components/SectionHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { Text } from './Text';
interface Props {
title: string;
titleStyle?: StyleProp<TextStyle>;
subtitle?: string;
subtitleStyle?: StyleProp<TextStyle>;
ellipsizeTitle?: boolean;
linkTo?: To<any>;
linkToMoreCount?: number;
Expand All @@ -35,6 +37,8 @@ interface Props {
export const SectionHeader = ({
title,
titleStyle,
subtitle,
subtitleStyle,
ellipsizeTitle = true,
accessibilityLabel = undefined,
linkTo,
Expand Down Expand Up @@ -67,6 +71,17 @@ export const SectionHeader = ({
>
{title}
</Text>
{subtitle && (
<Text
accessible={false}
variant="secondaryText"
style={subtitleStyle}
accessibilityRole="header"
{...ellipsis}
>
{subtitle}
</Text>
)}
</View>
{trailingItem
? trailingItem
Expand Down
9 changes: 6 additions & 3 deletions lib/ui/components/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ export const Switch = ({ children, ...rest }: SwitchProps) => {
<RNSwitch
trackColor={{
true: Platform.select({
ios: palettes.secondary[500],
android: palettes.secondary[dark ? 700 : 300],
ios: palettes.primary[dark ? 400 : 500],
android: palettes.primary[400],
}),
false:
Platform.OS === 'android' && !dark ? palettes.muted[200] : undefined,
}}
thumbColor={Platform.select({ android: palettes.secondary[500] })}
thumbColor={Platform.select({
android: palettes.primary[500],
})}
style={{ opacity: 1 }}
{...rest}
>
{children}
Expand Down
1 change: 1 addition & 0 deletions lib/ui/components/TopTabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const TopTabBar = ({
testID={options.tabBarTestID}
onPress={onPress}
onLongPress={onLongPress}
badge={options.tabBarBadge?.() as string | number}
>
{label as string}
</Tab>
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@miblanchard/react-native-slider": "^2.2.0",
"@openspacelabs/react-native-zoomable-view": "^2.1.5",
"@orama/orama": "^2.0.0-beta.8",
"@polito/api-client": "^1.0.0-ALPHA.56",
"@polito/api-client": "^1.0.0-ALPHA.57",
"@react-native-async-storage/async-storage": "^1.17.11",
"@react-native-clipboard/clipboard": "^1.12.1",
"@react-native-community/blur": "^4.3.0",
Expand Down
4 changes: 2 additions & 2 deletions src/core/components/RootNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { UserNavigator } from '../../features/user/components/UserNavigator';
import { tabBarStyle } from '../../utils/tab-bar';
import { usePreferencesContext } from '../contexts/PreferencesContext';
import { useInitFirebaseMessaging } from '../hooks/messaging';
import { usePushNotifications } from '../hooks/usePushNotifications';
import { useNotifications } from '../hooks/useNotifications';
import { useGetSites } from '../queries/placesHooks';
import { useGetModalMessages, useGetStudent } from '../queries/studentHooks';
import { ONBOARDING_STEPS } from '../screens/OnboardingModal';
Expand All @@ -45,7 +45,7 @@ export const RootNavigator = () => {
const { data: student } = useGetStudent();
const { onboardingStep, updatePreference } = usePreferencesContext();
const navigation = useNavigation<NativeStackNavigationProp<RootParamList>>();
const { getUnreadsCount } = usePushNotifications();
const { getUnreadsCount } = useNotifications();
const campus = useGetCurrentCampus();
const { data: sites } = useGetSites();

Expand Down
14 changes: 0 additions & 14 deletions src/core/contexts/PreferencesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { PlaceOverview } from '@polito/api-client';
import { PersonOverview } from '@polito/api-client/models';

import { AgendaTypesFilterState } from '../../features/agenda/types/AgendaTypesFilterState';
import { UnreadNotifications } from '../types/notifications';

export const editablePreferenceKeys = [
'lastInstalledVersion',
Expand All @@ -16,7 +15,6 @@ export const editablePreferenceKeys = [
'notifications',
'favoriteServices',
'peopleSearched',
'unreadNotifications',
'onboardingStep',
'emailGuideRead',
'placesSearched',
Expand All @@ -31,7 +29,6 @@ export const objectPreferenceKeys = [
'notifications',
'favoriteServices',
'peopleSearched',
'unreadNotifications',
'onboardingStep',
'emailGuideRead',
'placesSearched',
Expand Down Expand Up @@ -59,17 +56,6 @@ export interface PreferencesContextBase {
onboardingStep?: number;
emailGuideRead?: boolean;
placesSearched: PlaceOverview[];
/**
* A map whose keys represent a path to an object or area where the update
* occurred and leaf values are the number of unread updates
*
* @example
* {
* "teaching": { "courses": { "12345": { "notices": { "12345": 1 } } } },
* "services": { "tickets": { "12345": 2 } },
* }
*/
unreadNotifications: UnreadNotifications;
agendaScreen: {
layout: 'weekly' | 'daily';
filters: AgendaTypesFilterState;
Expand Down
15 changes: 10 additions & 5 deletions src/core/hooks/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { useEffect } from 'react';
import { PERMISSIONS, RESULTS, check, request } from 'react-native-permissions';

import messaging from '@react-native-firebase/messaging';
import { useQueryClient } from '@tanstack/react-query';

import { isEnvProduction } from '../../utils/env';
import { useUpdateDevicePreferences } from '../queries/studentHooks';
import {
NOTIFICATIONS_QUERY_KEY,
useUpdateDevicePreferences,
} from '../queries/studentHooks';
import { RemoteMessage } from '../types/notifications';
import { usePushNotifications } from './usePushNotifications';
import { useNotifications } from './useNotifications';

const requestNotificationPermission = async () => {
return await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS);
Expand All @@ -19,7 +23,8 @@ const isNotificationPermissionGranted = async () => {
};

export const useInitFirebaseMessaging = () => {
const { navigateToUpdate, updateUnreadStatus } = usePushNotifications();
const queryClient = useQueryClient();
const { navigateToUpdate } = useNotifications();
const preferencesQuery = useUpdateDevicePreferences();

if (isEnvProduction) {
Expand Down Expand Up @@ -56,11 +61,11 @@ export const useInitFirebaseMessaging = () => {
});

const unsubscribeOnMessage = messaging().onMessage(remoteMessage =>
updateUnreadStatus(remoteMessage as RemoteMessage),
queryClient.invalidateQueries(NOTIFICATIONS_QUERY_KEY),
);

messaging().setBackgroundMessageHandler(async remoteMessage => {
updateUnreadStatus(remoteMessage as RemoteMessage);
queryClient.invalidateQueries(NOTIFICATIONS_QUERY_KEY);
});

return () =>
Expand Down
Loading

0 comments on commit 7a8447c

Please sign in to comment.