Skip to content

Commit

Permalink
clean up logic
Browse files Browse the repository at this point in the history
  • Loading branch information
atn4z7 committed Sep 26, 2024
1 parent b5e33fe commit 10ee98f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/core-mobile/app/services/fcm/AppCheckService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ class AppCheckService {
return await firebase.appCheck().getToken(false)
}
}

export default new AppCheckService()
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function registerDeviceToNotificationSender(
throw error
})
if (response.ok) {
return response.json()
return await response.json()
} else {
throw new Error(`${response.status}:${response.statusText}`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function subscribeForBalanceChange({
throw new Error(error)
})
if (response.ok) {
return response.json()
return await response.json()
} else {
throw new Error(`${response.status}:${response.statusText}`)
}
Expand Down
55 changes: 38 additions & 17 deletions packages/core-mobile/app/store/notifications/listeners/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Logger from 'utils/Logger'
import { AnyAction, isAnyOf, PayloadAction } from '@reduxjs/toolkit'
import { manageForegroundNotificationSubscription } from 'store/notifications/listeners/manageForegroundNotificationSubscription'
import { unsubscribeBalanceChangeNotifications } from 'store/notifications/listeners/unsubscribeBalanceChangeNotifications'
import { setFeatureFlags } from 'store/posthog'
import { setFeatureFlags } from 'store/posthog/slice'
import { FeatureFlags, FeatureGates } from 'services/posthog/types'
import type { Action } from 'redux'
import { ChannelId } from 'services/notifications/channels'
Expand Down Expand Up @@ -83,9 +83,18 @@ export const addNotificationsListeners = (
setAccount,
onFcmTokenChange,
turnOnNotificationsFor,
matcherIsSubscribeBalanceChangeNotificationsEnabled
onBalanceChangeNotificationsEnabled
),
effect: async (_, listenerApi) => {
effect: async (action, listenerApi) => {
if (action.type === setFeatureFlags.type) {
// avoid subscribing when previous flag is already true
const previousFlag =
listenerApi.getOriginalState().posthog.featureFlags[
FeatureGates.BALANCE_CHANGE_NOTIFICATIONS
]
if (previousFlag === true) return
}

await subscribeBalanceChangeNotifications(listenerApi).catch(reason => {
Logger.error(
`[listeners.ts][subscribeBalanceChangeNotifications]${reason}`
Expand All @@ -97,15 +106,25 @@ export const addNotificationsListeners = (
startListening({
matcher: isAnyOf(
onLogOut,
matcherNotificationsTurnedOffForBalanceChange,
matcherIsSubscribeBalanceChangeNotificationsDisabled
onNotificationsTurnedOffForBalanceChange,
onBalanceChangeNotificationsDisabled
),
effect: async () =>
effect: async (action, listenerApi) => {
if (action.type === setFeatureFlags.type) {
const previousFlag =
listenerApi.getOriginalState().posthog.featureFlags[
FeatureGates.BALANCE_CHANGE_NOTIFICATIONS
]
// avoid unsubscribing when previous flag is already false
if (previousFlag === false) return
}

await unsubscribeBalanceChangeNotifications().catch(reason => {
Logger.error(
`[listeners.ts][unsubscribeBalanceChangeNotifications]${reason}`
)
})
}
})

startListening({
Expand Down Expand Up @@ -133,7 +152,18 @@ export const addNotificationsListeners = (
})
})
}
const matcherIsSubscribeBalanceChangeNotificationsEnabled = {

const onNotificationsTurnedOffForBalanceChange = {
match: (action: Action<unknown>): action is PayloadAction => {
return (
action.type === turnOffNotificationsFor.type &&
(action as PayloadAction<{ channelId: ChannelId }>).payload.channelId ===
ChannelId.BALANCE_CHANGES
)
}
}

const onBalanceChangeNotificationsEnabled = {
match: (action: Action<unknown>): action is PayloadAction => {
if (action.type === setFeatureFlags.type) {
const setFeatureFlagsAction = action as PayloadAction<FeatureFlags>
Expand All @@ -144,7 +174,7 @@ const matcherIsSubscribeBalanceChangeNotificationsEnabled = {
return false
}
}
const matcherIsSubscribeBalanceChangeNotificationsDisabled = {
const onBalanceChangeNotificationsDisabled = {
match: (action: Action<unknown>): action is PayloadAction => {
if (action.type === setFeatureFlags.type) {
const setFeatureFlagsAction = action as PayloadAction<FeatureFlags>
Expand All @@ -155,12 +185,3 @@ const matcherIsSubscribeBalanceChangeNotificationsDisabled = {
return false
}
}
const matcherNotificationsTurnedOffForBalanceChange = {
match: (action: Action<unknown>): action is PayloadAction => {
return (
action.type === turnOffNotificationsFor.type &&
(action as PayloadAction<{ channelId: ChannelId }>).payload.channelId ===
ChannelId.BALANCE_CHANGES
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,26 @@ import { subscribeForBalanceChange } from 'services/notifications/balanceChange/
import Logger from 'utils/Logger'
import { ChannelId } from 'services/notifications/channels'
import NotificationsService from 'services/notifications/NotificationsService'
import { selectHasPromptedForBalanceChange } from '../slice'

export async function subscribeBalanceChangeNotifications(
listenerApi: AppListenerEffectAPI
): Promise<void> {
const { getState } = listenerApi
const accounts = selectAccounts(getState())

const state = getState()
const hasPromptedForBalanceChange = selectHasPromptedForBalanceChange(state)

if (!hasPromptedForBalanceChange) {
// skip if user has not been prompted for balance change notifications
return
}

const accounts = selectAccounts(state)
const addresses = Object.values(accounts).map(account => account.addressC)

if (addresses.length === 0) {
//skip if no addresses, means wallet is not yet created
// skip if no addresses, means wallet is not yet created
return
}

Expand Down
1 change: 1 addition & 0 deletions packages/core-mobile/app/utils/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export default async function fetchWithAppCheck(
},
body: bodyJson
}

return fetch(url, options)
}

0 comments on commit 10ee98f

Please sign in to comment.