tooot/src/screens/Tabs/Me/Push.tsx

148 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-02-27 16:33:54 +01:00
import { MenuContainer, MenuRow } from '@components/Menu'
import { updateInstancePush } from '@utils/slices/instances/updatePush'
import { updateInstancePushAlert } from '@utils/slices/instances/updatePushAlert'
import { updateInstancePushDecode } from '@utils/slices/instances/updatePushDecode'
2021-03-23 23:16:01 +01:00
import { clearPushLoading, getInstancePush } from '@utils/slices/instancesSlice'
2021-02-27 16:33:54 +01:00
import * as WebBrowser from 'expo-web-browser'
2021-03-01 01:27:26 +01:00
import * as Notifications from 'expo-notifications'
import React, { useEffect, useMemo, useState } from 'react'
2021-02-27 16:33:54 +01:00
import { useTranslation } from 'react-i18next'
import { ScrollView } from 'react-native-gesture-handler'
import { useDispatch, useSelector } from 'react-redux'
2021-03-01 01:27:26 +01:00
import layoutAnimation from '@utils/styles/layoutAnimation'
import Button from '@components/Button'
import { StyleConstants } from '@utils/styles/constants'
import { AppState, Linking } from 'react-native'
2021-02-27 16:33:54 +01:00
const ScreenMeSettingsPush: React.FC = () => {
2021-03-28 23:31:10 +02:00
const { t } = useTranslation('screenTabs')
2021-03-01 01:27:26 +01:00
2021-03-02 01:17:06 +01:00
const dispatch = useDispatch()
const instancePush = useSelector(getInstancePush)
2021-03-01 01:27:26 +01:00
const [pushEnabled, setPushEnabled] = useState<boolean>()
const [pushCanAskAgain, setPushCanAskAgain] = useState<boolean>()
2021-03-02 01:17:06 +01:00
const checkPush = async () => {
const settings = await Notifications.getPermissionsAsync()
layoutAnimation()
setPushEnabled(settings.granted)
setPushCanAskAgain(settings.canAskAgain)
}
2021-03-01 01:27:26 +01:00
useEffect(() => {
checkPush()
AppState.addEventListener('change', checkPush)
2021-03-02 01:17:06 +01:00
return () => {
AppState.removeEventListener('change', checkPush)
2021-03-02 01:17:06 +01:00
}
}, [])
2021-02-27 16:33:54 +01:00
2021-03-23 23:16:01 +01:00
useEffect(() => {
dispatch(clearPushLoading())
}, [])
2021-02-27 16:33:54 +01:00
const isLoading = instancePush?.global.loading || instancePush?.decode.loading
const alerts = useMemo(() => {
return instancePush?.alerts
? (['follow', 'favourite', 'reblog', 'mention', 'poll'] as [
'follow',
'favourite',
'reblog',
'mention',
'poll'
]).map(alert => (
<MenuRow
key={alert}
2021-03-28 23:31:10 +02:00
title={t(`me.push.${alert}.heading`)}
2021-03-01 01:27:26 +01:00
switchDisabled={
!pushEnabled || !instancePush.global.value || isLoading
}
2021-02-27 16:33:54 +01:00
switchValue={instancePush?.alerts[alert].value}
switchOnValueChange={() =>
dispatch(
updateInstancePushAlert({
changed: alert,
alerts: {
2021-03-09 01:17:07 +01:00
...instancePush?.alerts,
2021-02-27 16:33:54 +01:00
[alert]: {
...instancePush?.alerts[alert],
value: !instancePush?.alerts[alert].value
}
}
})
)
}
/>
))
: null
2021-03-10 10:22:53 +01:00
}, [pushEnabled, instancePush?.global, instancePush?.alerts, isLoading])
2021-02-27 16:33:54 +01:00
return (
<ScrollView>
2021-03-01 01:27:26 +01:00
{pushEnabled === false ? (
<MenuContainer>
<Button
type='text'
content={
pushCanAskAgain
2021-03-28 23:31:10 +02:00
? t('me.push.enable.direct')
: t('me.push.enable.settings')
2021-03-01 01:27:26 +01:00
}
style={{
marginTop: StyleConstants.Spacing.Global.PagePadding,
marginHorizontal: StyleConstants.Spacing.Global.PagePadding * 2
}}
onPress={async () => {
if (pushCanAskAgain) {
const result = await Notifications.requestPermissionsAsync()
setPushEnabled(result.granted)
setPushCanAskAgain(result.canAskAgain)
} else {
2021-03-20 19:41:46 +01:00
Linking.openSettings()
2021-03-01 01:27:26 +01:00
}
}}
/>
</MenuContainer>
) : null}
2021-02-27 16:33:54 +01:00
<MenuContainer>
<MenuRow
2021-03-28 23:31:10 +02:00
title={t('me.push.global.heading')}
description={t('me.push.global.description')}
2021-02-27 16:33:54 +01:00
loading={instancePush?.global.loading}
2021-03-01 01:27:26 +01:00
switchDisabled={!pushEnabled || isLoading}
switchValue={
pushEnabled === false ? false : instancePush?.global.value
}
2021-02-27 16:33:54 +01:00
switchOnValueChange={() =>
dispatch(updateInstancePush(!instancePush?.global.value))
}
/>
</MenuContainer>
<MenuContainer>
<MenuRow
2021-03-28 23:31:10 +02:00
title={t('me.push.decode.heading')}
description={t('me.push.decode.description')}
2021-02-27 16:33:54 +01:00
loading={instancePush?.decode.loading}
2021-03-01 01:27:26 +01:00
switchDisabled={
!pushEnabled || !instancePush?.global.value || isLoading
}
2021-02-27 16:33:54 +01:00
switchValue={instancePush?.decode.value}
switchOnValueChange={() =>
dispatch(updateInstancePushDecode(!instancePush?.decode.value))
}
/>
<MenuRow
2021-03-28 23:31:10 +02:00
title={t('me.push.howitworks')}
2021-02-27 16:33:54 +01:00
iconBack='ExternalLink'
onPress={() =>
WebBrowser.openBrowserAsync('https://tooot.app/how-push-works')
}
/>
</MenuContainer>
<MenuContainer>{alerts}</MenuContainer>
</ScrollView>
)
}
export default ScreenMeSettingsPush