tooot/src/screens/Tabs/Me/Settings/App.tsx

232 lines
8.1 KiB
TypeScript
Raw Normal View History

2021-01-27 00:35:34 +01:00
import analytics from '@components/analytics'
import haptics from '@components/haptics'
import { MenuContainer, MenuRow } from '@components/Menu'
import { useActionSheet } from '@expo/react-native-action-sheet'
2021-02-20 19:12:44 +01:00
import { useNavigation } from '@react-navigation/native'
2021-03-28 23:31:10 +02:00
import { LOCALES } from '@root/i18n/locales'
2021-03-02 01:17:06 +01:00
import androidDefaults from '@utils/slices/instances/push/androidDefaults'
2021-05-09 21:59:03 +02:00
import { getInstances } from '@utils/slices/instancesSlice'
2021-01-27 00:35:34 +01:00
import {
changeBrowser,
changeLanguage,
changeTheme,
getSettingsTheme,
2021-03-10 10:22:53 +01:00
getSettingsBrowser,
getSettingsFontsize
2021-01-27 00:35:34 +01:00
} from '@utils/slices/settingsSlice'
import { useTheme } from '@utils/styles/ThemeManager'
2021-03-02 01:17:06 +01:00
import * as Notifications from 'expo-notifications'
2021-01-28 01:14:00 +01:00
import React from 'react'
2021-01-27 00:35:34 +01:00
import { useTranslation } from 'react-i18next'
2021-03-02 01:17:06 +01:00
import { Platform } from 'react-native'
2021-01-27 00:35:34 +01:00
import { useDispatch, useSelector } from 'react-redux'
2021-05-09 21:59:03 +02:00
import { mapFontsizeToName } from '../SettingsFontsize'
2021-01-27 00:35:34 +01:00
const SettingsApp: React.FC = () => {
2021-02-20 19:12:44 +01:00
const navigation = useNavigation()
2021-01-27 00:35:34 +01:00
const dispatch = useDispatch()
const { showActionSheetWithOptions } = useActionSheet()
const { setTheme } = useTheme()
2021-03-28 23:31:10 +02:00
const { t, i18n } = useTranslation('screenTabs')
2021-01-27 00:35:34 +01:00
2021-03-02 01:17:06 +01:00
const instances = useSelector(getInstances, () => true)
2021-03-10 10:22:53 +01:00
const settingsFontsize = useSelector(getSettingsFontsize)
2021-01-27 00:35:34 +01:00
const settingsTheme = useSelector(getSettingsTheme)
const settingsBrowser = useSelector(getSettingsBrowser)
2021-02-08 23:47:20 +01:00
2021-01-27 00:35:34 +01:00
return (
<MenuContainer>
2021-05-09 21:59:03 +02:00
<MenuRow
title={t('me.settings.fontsize.heading')}
content={t(
`me.settings.fontsize.content.${mapFontsizeToName(settingsFontsize)}`
)}
iconBack='ChevronRight'
onPress={() => {
navigation.navigate('Tab-Me-Settings-Fontsize')
}}
/>
2021-01-27 00:35:34 +01:00
<MenuRow
2021-03-28 23:31:10 +02:00
title={t('me.settings.language.heading')}
// @ts-ignore
content={LOCALES[i18n.language]}
2021-01-27 00:35:34 +01:00
iconBack='ChevronRight'
onPress={() => {
2021-03-28 23:31:10 +02:00
const options = Object.keys(LOCALES)
// @ts-ignore
.map(locale => LOCALES[locale])
.concat(t('me.settings.language.options.cancel'))
2021-01-27 00:35:34 +01:00
showActionSheetWithOptions(
{
2021-03-28 23:31:10 +02:00
title: t('me.settings.language.heading'),
2021-01-27 00:35:34 +01:00
options,
cancelButtonIndex: options.length - 1
},
buttonIndex => {
2021-01-31 03:09:35 +01:00
if (buttonIndex < options.length - 1) {
2021-01-27 00:35:34 +01:00
analytics('settings_language_press', {
current: i18n.language,
2021-03-28 23:31:10 +02:00
new: options[buttonIndex]
2021-01-27 00:35:34 +01:00
})
haptics('Success')
2021-03-02 01:17:06 +01:00
2021-01-27 00:35:34 +01:00
// @ts-ignore
2021-03-28 23:31:10 +02:00
dispatch(changeLanguage(Object.keys(LOCALES)[buttonIndex]))
i18n.changeLanguage(Object.keys(LOCALES)[buttonIndex])
2021-03-02 01:17:06 +01:00
// Update Android notification channel language
if (Platform.OS === 'android') {
instances.forEach(instance => {
const accountFull = `@${instance.account.acct}@${instance.uri}`
if (instance.push.decode.value === false) {
Notifications.setNotificationChannelAsync(
`${accountFull}_default`,
{
groupId: accountFull,
2021-03-28 23:31:10 +02:00
name: t('me.push.default.heading'),
2021-03-02 01:17:06 +01:00
...androidDefaults
}
)
} else {
Notifications.setNotificationChannelAsync(
`${accountFull}_follow`,
{
groupId: accountFull,
2021-03-28 23:31:10 +02:00
name: t('me.push.follow.heading'),
2021-03-02 01:17:06 +01:00
...androidDefaults
}
)
Notifications.setNotificationChannelAsync(
`${accountFull}_favourite`,
{
groupId: accountFull,
2021-03-28 23:31:10 +02:00
name: t('me.push.favourite.heading'),
2021-03-02 01:17:06 +01:00
...androidDefaults
}
)
Notifications.setNotificationChannelAsync(
`${accountFull}_reblog`,
{
groupId: accountFull,
2021-03-28 23:31:10 +02:00
name: t('me.push.reblog.heading'),
2021-03-02 01:17:06 +01:00
...androidDefaults
}
)
Notifications.setNotificationChannelAsync(
`${accountFull}_mention`,
{
groupId: accountFull,
2021-03-28 23:31:10 +02:00
name: t('me.push.mention.heading'),
2021-03-02 01:17:06 +01:00
...androidDefaults
}
)
Notifications.setNotificationChannelAsync(
`${accountFull}_poll`,
{
groupId: accountFull,
2021-03-28 23:31:10 +02:00
name: t('me.push.poll.heading'),
2021-03-02 01:17:06 +01:00
...androidDefaults
}
)
}
})
}
2021-01-27 00:35:34 +01:00
}
}
)
}}
/>
<MenuRow
2021-03-28 23:31:10 +02:00
title={t('me.settings.theme.heading')}
content={t(`me.settings.theme.options.${settingsTheme}`)}
2021-01-27 00:35:34 +01:00
iconBack='ChevronRight'
onPress={() =>
showActionSheetWithOptions(
{
2021-03-28 23:31:10 +02:00
title: t('me.settings.theme.heading'),
2021-01-27 00:35:34 +01:00
options: [
2021-03-28 23:31:10 +02:00
t('me.settings.theme.options.auto'),
t('me.settings.theme.options.light'),
t('me.settings.theme.options.dark'),
t('me.settings.theme.options.cancel')
2021-01-27 00:35:34 +01:00
],
cancelButtonIndex: 3
},
buttonIndex => {
switch (buttonIndex) {
case 0:
analytics('settings_appearance_press', {
current: settingsTheme,
new: 'auto'
})
haptics('Success')
dispatch(changeTheme('auto'))
break
case 1:
analytics('settings_appearance_press', {
current: settingsTheme,
new: 'light'
})
haptics('Success')
dispatch(changeTheme('light'))
setTheme('light')
break
case 2:
analytics('settings_appearance_press', {
current: settingsTheme,
new: 'dark'
})
haptics('Success')
dispatch(changeTheme('dark'))
setTheme('dark')
break
}
}
)
}
/>
<MenuRow
2021-03-28 23:31:10 +02:00
title={t('me.settings.browser.heading')}
content={t(`me.settings.browser.options.${settingsBrowser}`)}
2021-01-27 00:35:34 +01:00
iconBack='ChevronRight'
onPress={() =>
showActionSheetWithOptions(
{
2021-03-28 23:31:10 +02:00
title: t('me.settings.browser.heading'),
2021-01-27 00:35:34 +01:00
options: [
2021-03-28 23:31:10 +02:00
t('me.settings.browser.options.internal'),
t('me.settings.browser.options.external'),
t('me.settings.browser.options.cancel')
2021-01-27 00:35:34 +01:00
],
cancelButtonIndex: 2
},
buttonIndex => {
switch (buttonIndex) {
case 0:
analytics('settings_browser_press', {
current: settingsBrowser,
new: 'internal'
})
haptics('Success')
dispatch(changeBrowser('internal'))
break
case 1:
analytics('settings_browser_press', {
current: settingsBrowser,
new: 'external'
})
haptics('Success')
dispatch(changeBrowser('external'))
break
}
}
)
}
/>
</MenuContainer>
)
}
export default SettingsApp