Skip to content

Commit

Permalink
Modify unused DeviceNotifDismissInfo type
Browse files Browse the repository at this point in the history
Repurpose this into a generic `DeviceNotifState` / `DeviceNotifInfo.`
Maintaining a number of distinct props in an attempt to be more flexible for future iterations/use cases.
  • Loading branch information
Jon-edge committed Nov 1, 2024
1 parent e71aac8 commit d22b526
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
38 changes: 33 additions & 5 deletions src/actions/DeviceSettingsActions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { makeReactNativeDisklet } from 'disklet'

import { asDeviceSettings, DefaultScreen, DeviceNotifDismissInfo, DeviceSettings } from '../types/types'
import { asDeviceNotifInfo, asDeviceSettings, DefaultScreen, DeviceNotifInfo, DeviceNotifState, DeviceSettings } from '../types/types'

const disklet = makeReactNativeDisklet()
const DEVICE_SETTINGS_FILENAME = 'DeviceSettings.json'
Expand Down Expand Up @@ -60,14 +60,42 @@ export const writeForceLightAccountCreate = async (forceLightAccountCreate: bool
}

/**
* Track the state of whether particular one-time notifications associated with
* the device were interacted with or dismissed.
* Manage the state of local notifications, used by both `NotificationView` and
* `NotificationCenterScene`
**/
export const writeDeviceNotifDismissInfo = async (deviceNotifDismissInfo: DeviceNotifDismissInfo) => {
const updatedSettings: DeviceSettings = { ...deviceSettings, deviceNotifDismissInfo }
const writeDeviceNotifState = async (deviceNotifState: DeviceNotifState) => {
const updatedSettings: DeviceSettings = { ...deviceSettings, deviceNotifState }
return await writeDeviceSettings(updatedSettings)
}

/**
* Overwrite the values of local notifications or create new values per specific
* `deviceNotifState` key *with default values* unioned with provided
* `deviceNotifInfo.`
* Used by both `NotificationView` and `NotificationCenterScene`
**/
export const createDeviceNotifInfo = async (deviceNotifStateKey: string, deviceNotifInfo: DeviceNotifInfo = {}) => {
return await writeDeviceNotifState({ ...deviceSettings.deviceNotifState, [deviceNotifStateKey]: { ...asDeviceNotifInfo(deviceNotifInfo) } })
}

/**
* Modify existing state of local notifications per specific `deviceNotifState`
* key with *existing values* unioned with provided `deviceNotifInfo.`
*
* If the particular key does not exist, it is created with default values.
*
* Used by both `NotificationView` and `NotificationCenterScene`
**/
export const modifyDeviceNotifInfo = async (deviceNotifStateKey: string, deviceNotifInfo: Partial<DeviceNotifInfo> = {}) => {
if (deviceSettings.deviceNotifState[deviceNotifStateKey] == null) {
console.warn('modifyDeviceNotifInfo: deviceNotifStateKey does not exist. Creating with default values.')
}
return await createDeviceNotifInfo(deviceNotifStateKey, {
...deviceSettings.deviceNotifState[deviceNotifStateKey],
...asDeviceNotifInfo(deviceNotifInfo)
})
}

/**
* Track the state of whether the "How did you Discover Edge" modal was shown.
**/
Expand Down
49 changes: 46 additions & 3 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,58 @@ const asLocalAccountSettingsInner = asObject({
tokenWarningsShown: asMaybe(asTokenWarningsShown, [])
})

const asDeviceNotifDismissInfo = asObject({})
export type DeviceNotifDismissInfo = ReturnType<typeof asDeviceNotifDismissInfo>
export const asDeviceNotifInfo = asObject({
dateReceived: asMaybe(asNumber, Date.now()),
isBannerHidden: asMaybe(asBoolean, false),
isCompleted: asMaybe(asBoolean, false),
isPriority: asMaybe(asBoolean, false),
isShown: asMaybe(asBoolean, true)
})
const asDeviceNotifState = asObject(asDeviceNotifInfo)
/**
* - dateReceived: Timestamp that this notification was detected locally. May
* accept server-side date values in future iterations.
* - isBannerHidden: Whether the user has dismissed the bottom
* `NotificationView` banner
* - isCompleted: True if user "completed" the requested action. This may be
* as simple as tapping and reading the notification, but may be more complex,
* such as requiring 2FA toggled on. Incomplete notifications are represented
* with a red dot in the `NotificationCenterScene`
* - isPriority: True to pin this notification to the top of the
* `NotificationCenterScene`
* - isShown: False if hidden from *both* the `NotificationCenterScene` and the
* `NotificationView`
*/
export interface DeviceNotifInfo {
dateReceived?: number
isBannerHidden?: boolean
isCompleted?: boolean
isPriority?: boolean
isShown?: boolean
}

/**
* - dateReceived: Timestamp that this notification was detected locally. May
* accept server-side date values in future iterations.
* - isBannerHidden: Whether the user has dismissed the bottom
* `NotificationView` banner
* - isCompleted: True if user "completed" the requested action. This may be
* as simple as tapping and reading the notification, but may be more complex,
* such as requiring 2FA toggled on. Incomplete notifications are represented
* with a red dot in the `NotificationCenterScene`
* - isPriority: True to pin this notification to the top of the
* `NotificationCenterScene`
* - isShown: False if hidden from *both* the `NotificationCenterScene` and the
* `NotificationView`
*/
export type DeviceNotifState = ReturnType<typeof asDeviceNotifState>

export const asDefaultScreen = asValue('home', 'assets')

const asDeviceSettingsInner = asObject({
defaultScreen: asMaybe(asDefaultScreen, 'home'),
developerPluginUri: asMaybe(asString),
deviceNotifDismissInfo: asMaybe(asDeviceNotifDismissInfo, asDeviceNotifDismissInfo({})),
deviceNotifState: asMaybe(asDeviceNotifState, asDeviceNotifState({})),
disableAnimations: asMaybe(asBoolean, false),
forceLightAccountCreate: asMaybe(asBoolean, false),
isSurveyDiscoverShown: asMaybe(asBoolean, false)
Expand Down

0 comments on commit d22b526

Please sign in to comment.