2021-02-28 22:49:55 +01:00
|
|
|
import Icon from '@components/Icon'
|
|
|
|
import { StyleConstants } from '@utils/styles/constants'
|
|
|
|
import { useTheme } from '@utils/styles/ThemeManager'
|
2021-05-09 21:59:03 +02:00
|
|
|
import React, { RefObject } from 'react'
|
2021-04-09 21:43:12 +02:00
|
|
|
import { AccessibilityInfo } from 'react-native'
|
2022-12-05 23:15:07 +01:00
|
|
|
import FlashMessage, { MessageType, showMessage } from 'react-native-flash-message'
|
2021-02-28 22:49:55 +01:00
|
|
|
import haptics from './haptics'
|
|
|
|
|
|
|
|
const displayMessage = ({
|
2021-05-09 21:59:03 +02:00
|
|
|
ref,
|
2021-03-01 00:28:14 +01:00
|
|
|
duration = 'short',
|
2021-02-28 22:49:55 +01:00
|
|
|
autoHide = true,
|
|
|
|
message,
|
|
|
|
description,
|
|
|
|
onPress,
|
|
|
|
type
|
2022-12-05 23:15:07 +01:00
|
|
|
}: {
|
|
|
|
ref?: RefObject<FlashMessage>
|
|
|
|
duration?: 'short' | 'long'
|
|
|
|
autoHide?: boolean
|
|
|
|
message: string
|
|
|
|
description?: string
|
|
|
|
onPress?: () => void
|
|
|
|
type?: MessageType
|
|
|
|
}) => {
|
2021-04-09 21:43:12 +02:00
|
|
|
AccessibilityInfo.announceForAccessibility(message + '.' + description)
|
|
|
|
|
2022-12-05 23:15:07 +01:00
|
|
|
if (type && type === 'danger') {
|
2021-02-28 22:49:55 +01:00
|
|
|
haptics('Error')
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:59:03 +02:00
|
|
|
if (ref) {
|
|
|
|
ref.current?.showMessage({
|
2022-12-05 23:15:07 +01:00
|
|
|
duration: type === 'danger' ? 8000 : duration === 'short' ? 3000 : 5000,
|
2021-05-09 21:59:03 +02:00
|
|
|
autoHide,
|
|
|
|
message,
|
|
|
|
description,
|
|
|
|
onPress,
|
2022-12-05 23:15:07 +01:00
|
|
|
type
|
2021-05-09 21:59:03 +02:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
showMessage({
|
2022-12-05 23:15:07 +01:00
|
|
|
duration: type === 'danger' ? 8000 : duration === 'short' ? 3000 : 5000,
|
2021-05-09 21:59:03 +02:00
|
|
|
autoHide,
|
|
|
|
message,
|
|
|
|
description,
|
|
|
|
onPress,
|
2022-12-05 23:15:07 +01:00
|
|
|
type
|
2021-05-09 21:59:03 +02:00
|
|
|
})
|
|
|
|
}
|
2021-02-28 22:49:55 +01:00
|
|
|
}
|
|
|
|
|
2021-05-09 21:59:03 +02:00
|
|
|
const Message = React.forwardRef<FlashMessage>((_, ref) => {
|
2022-02-12 14:51:01 +01:00
|
|
|
const { colors, theme } = useTheme()
|
2021-02-28 22:49:55 +01:00
|
|
|
|
2022-12-05 23:15:07 +01:00
|
|
|
enum iconMapping {
|
|
|
|
success = 'CheckCircle',
|
|
|
|
danger = 'XCircle',
|
|
|
|
warning = 'AlertCircle',
|
|
|
|
none = '',
|
|
|
|
default = '',
|
|
|
|
info = '',
|
|
|
|
auto = ''
|
|
|
|
}
|
|
|
|
enum colorMapping {
|
|
|
|
success = 'blue',
|
|
|
|
danger = 'red',
|
|
|
|
warning = 'secondary',
|
|
|
|
none = 'secondary',
|
|
|
|
default = 'secondary',
|
|
|
|
info = 'secondary',
|
|
|
|
auto = 'secondary'
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:59:03 +02:00
|
|
|
return (
|
|
|
|
<FlashMessage
|
|
|
|
ref={ref}
|
|
|
|
icon='auto'
|
2022-12-05 23:15:07 +01:00
|
|
|
renderFlashMessageIcon={type => {
|
|
|
|
return typeof type === 'string' && ['success', 'danger', 'warning'].includes(type) ? (
|
|
|
|
<Icon
|
|
|
|
name={iconMapping[type]}
|
|
|
|
size={StyleConstants.Font.LineHeight.M}
|
|
|
|
color={colors[colorMapping[type]]}
|
|
|
|
style={{ marginRight: StyleConstants.Spacing.S }}
|
|
|
|
/>
|
|
|
|
) : null
|
|
|
|
}}
|
2021-05-09 21:59:03 +02:00
|
|
|
position='top'
|
|
|
|
floating
|
|
|
|
style={{
|
2022-02-12 14:51:01 +01:00
|
|
|
backgroundColor: colors.backgroundDefault,
|
|
|
|
shadowColor: colors.primaryDefault,
|
2021-05-09 21:59:03 +02:00
|
|
|
shadowOffset: { width: 0, height: 0 },
|
2022-02-12 14:51:01 +01:00
|
|
|
shadowOpacity: theme === 'light' ? 0.16 : 0.24,
|
2022-07-09 11:29:47 +02:00
|
|
|
shadowRadius: 4,
|
2023-01-04 00:06:37 +01:00
|
|
|
paddingRight: StyleConstants.Spacing.M * 2
|
2021-05-09 21:59:03 +02:00
|
|
|
}}
|
|
|
|
titleStyle={{
|
2022-02-12 14:51:01 +01:00
|
|
|
color: colors.primaryDefault,
|
2021-05-09 21:59:03 +02:00
|
|
|
...StyleConstants.FontStyle.M,
|
|
|
|
fontWeight: StyleConstants.Font.Weight.Bold
|
|
|
|
}}
|
|
|
|
textStyle={{
|
2022-02-12 14:51:01 +01:00
|
|
|
color: colors.primaryDefault,
|
2021-05-09 21:59:03 +02:00
|
|
|
...StyleConstants.FontStyle.S
|
|
|
|
}}
|
|
|
|
// @ts-ignore
|
|
|
|
textProps={{ numberOfLines: 2 }}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})
|
2021-02-28 22:49:55 +01:00
|
|
|
|
2022-12-05 23:15:07 +01:00
|
|
|
export { Message, displayMessage }
|