Skip to content

Commit

Permalink
TW-1520 Add some logic for redirection to 'Notifications' page
Browse files Browse the repository at this point in the history
  • Loading branch information
keshan3262 committed Sep 30, 2024
1 parent c4c9a9c commit 0d60833
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 117 deletions.
15 changes: 6 additions & 9 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,10 @@ PODS:
- React-RCTImage
- RNScrypt (1.2.1):
- React
- RNSentry (5.9.1):
- RNSentry (5.33.1):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- Sentry/HybridSDK (= 8.10.0)
- Sentry/HybridSDK (= 8.36.0)
- RNSha256 (1.4.10):
- React-Core
- RNSVG (12.4.4):
Expand All @@ -765,9 +766,7 @@ PODS:
- SDWebImageWebPCoder (0.8.5):
- libwebp (~> 1.0)
- SDWebImage/Core (~> 5.10)
- Sentry/HybridSDK (8.10.0):
- SentryPrivate (= 8.10.0)
- SentryPrivate (8.10.0)
- Sentry/HybridSDK (8.36.0)
- SocketRocket (0.6.1)
- Yoga (1.14.0)
- YogaKit (1.18.1):
Expand Down Expand Up @@ -922,7 +921,6 @@ SPEC REPOS:
- SDWebImage
- SDWebImageWebPCoder
- Sentry
- SentryPrivate
- SocketRocket
- YogaKit

Expand Down Expand Up @@ -1207,13 +1205,12 @@ SPEC CHECKSUMS:
RNReanimated: 3ffa3d63576ecd26a4f8772d03029cccaf41b5d7
RNScreens: 85d3880b52d34db7b8eeebe2f1a0e807c05e69fa
RNScrypt: 95fdef077b482e9fdf8a761120cb69d129ef4016
RNSentry: 0a1daa8ee81e2776f977ae8c66e67c8d85587828
RNSentry: d84b9265d0c7379104a5fb3c80aa451029e1ace0
RNSha256: e1bc64e9e50b293d5282bb4caa1b2043931f1c9d
RNSVG: ecd661f380a07ba690c9c5929c475a44f432d674
SDWebImage: a7f831e1a65eb5e285e3fb046a23fcfbf08e696d
SDWebImageWebPCoder: 908b83b6adda48effe7667cd2b7f78c897e5111d
Sentry: 71cd4427146ac56eab6e70401ac7a24384c3d3b5
SentryPrivate: 9334613897c85a9e30f2c9d7a331af8aaa4fe71f
Sentry: f8374b5415bc38dfb5645941b3ae31230fbeae57
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: 87e59f6d458e5061d2421086c5de994b3f7cd151
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@react-navigation/native": "6.1.8",
"@react-navigation/stack": "6.3.18",
"@reduxjs/toolkit": "^1.8.5",
"@sentry/react-native": "5.9.1",
"@sentry/react-native": "^5.33.1",
"@shopify/flash-list": "^1.6.1",
"@taquito/local-forging": "19.0.2",
"@taquito/rpc": "19.0.2",
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/main-hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import { useCurrentAccountPkhSelector } from 'src/store/wallet/wallet-selectors'
import { shouldMoveToSoftwareInV1 } from 'src/utils/keychain.utils';

import { useMetadataLoading } from './use-metadata-loading';
import { useNotificationRedirection } from './use-notification-redirection';

export const useMainHooks = (isLocked: boolean) => {
export const useMainHooks = (isLocked: boolean, isAuthorised: boolean) => {
const dispatch = useDispatch();

const selectedAccountPkh = useCurrentAccountPkhSelector();
Expand All @@ -49,6 +50,7 @@ export const useMainHooks = (isLocked: boolean) => {
useAppLockTimer();
useBeaconHandler();
useNFTDynamicLinks();
useNotificationRedirection(isLocked, isAuthorised);

const blockSubscription = useBlockSubscription();

Expand Down
22 changes: 22 additions & 0 deletions src/hooks/main-hooks/use-notification-redirection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';

import { ScreensEnum } from 'src/navigator/enums/screens.enum';
import { useNavigation } from 'src/navigator/hooks/use-navigation.hook';
import { setShouldRedirectToNotificationsAction } from 'src/store/notifications/notifications-actions';
import { useShouldRedirectToNotificationsSelector } from 'src/store/notifications/notifications-selectors';

export const useNotificationRedirection = (isLocked: boolean, isAuthorised: boolean) => {
const { navigate } = useNavigation();
const dispatch = useDispatch();
const shouldRedirectToNotifications = useShouldRedirectToNotificationsSelector();

useEffect(() => {
if (isLocked || !isAuthorised || !shouldRedirectToNotifications) {
return;
}

dispatch(setShouldRedirectToNotificationsAction(false));
navigate(ScreensEnum.Notifications);
}, [dispatch, isAuthorised, isLocked, navigate, shouldRedirectToNotifications]);
};
19 changes: 17 additions & 2 deletions src/hooks/root-hooks/use-push-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,31 @@ import { useCallback, useEffect } from 'react';
import { PermissionsAndroid } from 'react-native';
/* eslint-disable-next-line import/no-named-as-default */
import PushNotification from 'react-native-push-notification';
import { useDispatch } from 'react-redux';

import { isAndroid } from 'src/config/system';
import { setShouldRedirectToNotificationsAction } from 'src/store/notifications/notifications-actions';
import { isDefined } from 'src/utils/is-defined';

export const usePushNotifications = () => {
const dispatch = useDispatch();

useEffect(() => {
requestUserPermission(getFcmToken);
const unsubscribe = handleForegroundNotifications();
const unsubscribeFromFgNotifications = handleForegroundNotifications();

const handleRemoteMessage = () => void dispatch(setShouldRedirectToNotificationsAction(true));

const unsubscribeFromNotificationOpenedApp = messaging().onNotificationOpenedApp(handleRemoteMessage);
messaging()
.getInitialNotification()
.then(message => message && handleRemoteMessage())
.catch(error => console.error(error));

return unsubscribe;
return () => {
unsubscribeFromFgNotifications();
unsubscribeFromNotificationOpenedApp();
};
}, []);

const getFcmToken = useCallback(async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/navigator/main-stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const MainStackScreen = memo(() => {

const { metadata } = useNetworkInfo();

useMainHooks(isLocked);
useMainHooks(isLocked, isAuthorised);

const shouldShowUnauthorizedScreens = !isAuthorised;
const shouldShowAuthorizedScreens = isAuthorised && !isLocked;
Expand Down
7 changes: 6 additions & 1 deletion src/store/notifications/notifications-actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createAction } from '@reduxjs/toolkit';

import { NotificationInterface } from '../../interfaces/notification.interface';
import { NotificationInterface } from 'src/interfaces/notification.interface';

import { createActions } from '../create-actions';

export const loadNotificationsAction = createActions<void, NotificationInterface[]>('notifications/LOAD_NOTIFICATIONS');
Expand All @@ -9,3 +10,7 @@ export const viewAllNotificationsAction = createAction<void>('notifications/VIEW
export const readNotificationsItemAction = createAction<number>('notifications/READ_NOTIFICATIONS_ITEM');

export const setIsNewsEnabledAction = createAction<boolean>('notifications/SET_IS_NEWS_ENABLED');

export const setShouldRedirectToNotificationsAction = createAction<boolean>(
'notifications/SET_SHOULD_REDIRECT_TO_NOTIFICATIONS'
);
12 changes: 9 additions & 3 deletions src/store/notifications/notifications-reducers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { createReducer } from '@reduxjs/toolkit';

import { NotificationStatus } from '../../enums/notification-status.enum';
import { isDefined } from '../../utils/is-defined';
import { NotificationStatus } from 'src/enums/notification-status.enum';
import { isDefined } from 'src/utils/is-defined';

import { createEntity } from '../create-entity';

import {
loadNotificationsAction,
setIsNewsEnabledAction,
readNotificationsItemAction,
viewAllNotificationsAction
viewAllNotificationsAction,
setShouldRedirectToNotificationsAction
} from './notifications-actions';
import { notificationsInitialState, NotificationsState } from './notifications-state';

Expand Down Expand Up @@ -76,4 +78,8 @@ export const notificationsReducers = createReducer<NotificationsState>(notificat
...state,
isNewsEnabled
}));

builder.addCase(setShouldRedirectToNotificationsAction, (state, { payload: shouldRedirectToNotifications }) => {
state.shouldRedirectToNotifications = shouldRedirectToNotifications;
});
});
3 changes: 3 additions & 0 deletions src/store/notifications/notifications-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ export const useIsNewNotificationsAvailableSelector = () =>
);

export const useIsNewsEnabledSelector = () => useSelector(({ notifications }) => notifications.isNewsEnabled);

export const useShouldRedirectToNotificationsSelector = () =>
useSelector(({ notifications }) => notifications.shouldRedirectToNotifications);
3 changes: 2 additions & 1 deletion src/store/notifications/notifications-state.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import { NotificationsState } from './notifications-state';
export const mockNotificationsState: NotificationsState = {
startFromTime: new Date('2022-11-29T13:00:00.000Z').getTime(),
list: createEntity([]),
isNewsEnabled: true
isNewsEnabled: true,
shouldRedirectToNotifications: false
};
7 changes: 5 additions & 2 deletions src/store/notifications/notifications-state.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { NotificationInterface } from '../../interfaces/notification.interface';
import { NotificationInterface } from 'src/interfaces/notification.interface';

import { createEntity } from '../create-entity';
import { LoadableEntityState } from '../types';

export interface NotificationsState {
startFromTime: number;
list: LoadableEntityState<NotificationInterface[]>;
isNewsEnabled: boolean;
shouldRedirectToNotifications: boolean;
}

export const notificationsInitialState: NotificationsState = {
startFromTime: new Date().getTime(),
list: createEntity([]),
isNewsEnabled: true
isNewsEnabled: true,
shouldRedirectToNotifications: false
};
Loading

0 comments on commit 0d60833

Please sign in to comment.