Skip to content

Commit f926679

Browse files
Add config to package
1 parent fd72ead commit f926679

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

apps/wallet-mobile/src/features/Settings/useCases/changeWalletSettings/ManageNotificationDisplayDuration/ManageNotificationDisplayDurationScreen.tsx

+16-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {Button} from '../../../../../components/Button/Button'
88
import {useFormatNumber} from '../../../../../kernel/i18n'
99
import {parseNumber} from '@yoroi/common'
1010
import {useStrings} from './strings'
11+
import {useNotificationDisplaySettings} from '../Notifications/NotificationsDisplaySettings'
12+
import {useNotificationsConfig} from '@yoroi/notifications'
1113

1214
type ManualChoice = {
1315
id: 'Manual'
@@ -32,21 +34,24 @@ const CHOICES: Readonly<Choice[]> = [
3234
{id: 'Manual'},
3335
] as const
3436

35-
const defaultChoice = CHOICES[1]
36-
3737
export const ManageNotificationDisplayDurationScreen = ({}) => {
3838
const {styles, colors} = useStyles()
3939
const formatNumber = useFormatNumber()
40+
const {data: config} = useNotificationsConfig()
4041

4142
const strings = useStrings()
42-
const [selectedChoiceId, setSelectedChoiceId] = React.useState<ChoiceKind>(defaultChoice.id)
43+
const savedChoice = getChoiceByValue(config.displayDuration)
44+
const [selectedChoiceId, setSelectedChoiceId] = React.useState<ChoiceKind>(savedChoice.id)
4345
const selectedChoice = getChoiceById(selectedChoiceId)
44-
const [inputValue, setInputValue] = React.useState('')
46+
const defaultInputValue = savedChoice.id === 'Manual' ? formatNumber(config.displayDuration) : ''
47+
const [inputValue, setInputValue] = React.useState(defaultInputValue)
4548

4649
const isSelectedChoiceManual = selectedChoiceId === 'Manual'
4750
const isInputEnabled = isSelectedChoiceManual
51+
const isInputEmpty = inputValue === ''
4852
const hasError = isSelectedChoiceManual && !isInputValid(inputValue)
49-
const isButtonDisabled = hasError || (isSelectedChoiceManual && inputValue === '')
53+
const isButtonDisabled = hasError || (isSelectedChoiceManual && isInputEmpty)
54+
const shouldDisplayError = !isInputEmpty && hasError
5055

5156
const handleChoicePress = (id: ChoiceKind) => {
5257
setSelectedChoiceId(id)
@@ -94,7 +99,8 @@ export const ManageNotificationDisplayDurationScreen = ({}) => {
9499
keyboardType="numeric"
95100
selectionColor={colors.cursor}
96101
right={<Text style={styles.percentLabel}>{strings.seconds}</Text>}
97-
error={hasError}
102+
error={shouldDisplayError}
103+
errorText={shouldDisplayError ? 'Hello' : undefined}
98104
/>
99105
</View>
100106
</ScrollView>
@@ -205,6 +211,10 @@ const getChoiceById = (id: ChoiceKind): Choice => {
205211
return CHOICES.find((choice) => choice.id === id) ?? {id: 'Manual'}
206212
}
207213

214+
const getChoiceByValue = (value: number): Choice => {
215+
return CHOICES.find((choice) => choice.id !== 'Manual' && choice.value === value) ?? {id: 'Manual'}
216+
}
217+
208218
const isInputValid = (text: string) => {
209219
const isNumeric = /^[0-9]*$/.test(text)
210220
const parsed = parseNumber(text)

apps/wallet-mobile/src/kernel/i18n/locales/en-US.json

+2
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@
444444
"components.settings.collateral.initialCollateralInfoModalTitle": "Collateral creation",
445445
"components.settings.collateral.initialCollateralInfoModalText": "Collateral is mandatory when interacting with certain smart contracts on Cardano, which requires to make a 0 ADA transaction. ADA will only be deduced from your collateral if transaction validation fails.",
446446
"components.settings.collateral.initialCollateralInfoModalButton": "Add collateral",
447+
"components.settings.notificationDisplayDuration.title": "Display duration",
447448
"components.settings.disableeasyconfirmationscreen.disableButton": "Disable",
448449
"components.settings.disableeasyconfirmationscreen.disableHeading": "By disabling this option, you will be able to spend your assets only with your master password.",
449450
"components.settings.disableeasyconfirmationscreen.title": "Disable easy confirmation",
@@ -490,6 +491,7 @@
490491
"components.settings.walletsettingscreen.walletType": "Wallet type:",
491492
"components.settings.walletsettingscreen.inAppNotifications": "In-app notifications",
492493
"components.settings.walletsettingscreen.allowNotifications": "Allow notifications",
494+
"components.settings.walletsettingscreen.displayDuration": "Display duration",
493495
"components.settings.manageNotificationDisplayDuration.description": "Adjust the display duration of in-app notifications to suit your preferences.",
494496
"components.settings.manageNotificationDisplayDuration.apply": "Apply",
495497
"components.settings.manageNotificationDisplayDuration.displayDuration": "Display duration",

packages/notifications/src/notification-manager.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,14 @@ const eventsManagerMaker = ({
140140
return {events, unreadCounterByGroup$, newEvents$}
141141
}
142142

143+
type ConfigManagerMakerOptions = {storage: App.Storage<true, string>}
143144
const configManagerMaker = ({
144145
storage,
145-
}: {
146-
storage: App.Storage<true, string>
147-
}): Notifications.Manager['config'] => {
146+
}: ConfigManagerMakerOptions): Notifications.Manager['config'] => {
148147
return {
149148
read: async (): Promise<Notifications.Config> => {
150-
return (
151-
(await storage.getItem<ConfigStorageData>('config')) ?? defaultConfig
152-
)
149+
const value = await storage.getItem<Partial<ConfigStorageData>>('config')
150+
return {...defaultConfig, value}
153151
},
154152
save: async (config: Notifications.Config): Promise<void> => {
155153
await storage.setItem('config', config)
@@ -174,6 +172,7 @@ const buildUnreadCounterDefaultValue = (): Map<Notifications.Group, number> => {
174172
}
175173

176174
const defaultConfig: Notifications.Config = {
175+
displayDuration: 4,
177176
[Notifications.Trigger.PrimaryTokenPriceChanged]: {
178177
notify: true,
179178
thresholdInPercent: 10,

packages/types/src/notifications/manager.ts

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ interface NotificationEventBase {
6262
}
6363

6464
export type NotificationConfig = {
65+
displayDuration: number
6566
[NotificationTrigger.PrimaryTokenPriceChanged]: {
6667
notify: boolean
6768
thresholdInPercent: number

0 commit comments

Comments
 (0)