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

262 lines
9.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-01-27 00:35:34 +01:00
import i18n from '@root/i18n/i18n'
2021-03-02 01:17:06 +01:00
import androidDefaults from '@utils/slices/instances/push/androidDefaults'
2021-02-27 16:33:54 +01:00
import {
getInstanceActive,
2021-03-02 01:17:06 +01:00
getInstancePush,
getInstances
2021-02-27 16:33:54 +01:00
} from '@utils/slices/instancesSlice'
2021-01-27 00:35:34 +01:00
import {
changeBrowser,
changeLanguage,
changeTheme,
getSettingsLanguage,
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-03-10 10:22:53 +01:00
import { mapFontsizeToName } from '../Fontsize'
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()
const { t } = useTranslation('meSettings')
2021-03-02 01:17:06 +01:00
const instances = useSelector(getInstances, () => true)
2021-02-20 19:12:44 +01:00
const instanceActive = useSelector(getInstanceActive)
2021-03-10 10:22:53 +01:00
const settingsFontsize = useSelector(getSettingsFontsize)
2021-01-27 00:35:34 +01:00
const settingsLanguage = useSelector(getSettingsLanguage)
const settingsTheme = useSelector(getSettingsTheme)
const settingsBrowser = useSelector(getSettingsBrowser)
2021-02-27 16:33:54 +01:00
const instancePush = useSelector(
getInstancePush,
(prev, next) => prev?.global.value === next?.global.value
)
2021-02-08 23:47:20 +01:00
2021-01-27 00:35:34 +01:00
return (
<MenuContainer>
2021-02-20 19:12:44 +01:00
{instanceActive !== -1 ? (
2021-03-10 10:22:53 +01:00
<>
<MenuRow
title={t('content.push.heading')}
content={
instancePush?.global.value
? t('content.push.content.enabled')
: t('content.push.content.disabled')
}
iconBack='ChevronRight'
onPress={() => {
navigation.navigate('Tab-Me-Settings-Push')
}}
/>
<MenuRow
title={t('content.fontsize.heading')}
content={t(
`content.fontsize.content.${mapFontsizeToName(settingsFontsize)}`
)}
iconBack='ChevronRight'
onPress={() => {
navigation.navigate('Tab-Me-Settings-Fontsize')
}}
/>
</>
2021-02-20 19:12:44 +01:00
) : null}
2021-01-27 00:35:34 +01:00
<MenuRow
title={t('content.language.heading')}
content={t(`content.language.options.${settingsLanguage}`)}
iconBack='ChevronRight'
onPress={() => {
const availableLanguages = Object.keys(
i18n.services.resourceStore.data
)
const options = availableLanguages
2021-01-31 03:09:35 +01:00
.map(language => {
return t(`content.language.options.${language}`)
})
2021-01-27 00:35:34 +01:00
.concat(t('content.language.options.cancel'))
showActionSheetWithOptions(
{
title: t('content.language.heading'),
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,
new: availableLanguages[buttonIndex]
})
haptics('Success')
2021-03-02 01:17:06 +01:00
2021-01-27 00:35:34 +01:00
// @ts-ignore
dispatch(changeLanguage(availableLanguages[buttonIndex]))
i18n.changeLanguage(availableLanguages[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,
name: t('meSettingsPush:content.default.heading'),
...androidDefaults
}
)
} else {
Notifications.setNotificationChannelAsync(
`${accountFull}_follow`,
{
groupId: accountFull,
name: t('meSettingsPush:content.follow.heading'),
...androidDefaults
}
)
Notifications.setNotificationChannelAsync(
`${accountFull}_favourite`,
{
groupId: accountFull,
name: t('meSettingsPush:content.favourite.heading'),
...androidDefaults
}
)
Notifications.setNotificationChannelAsync(
`${accountFull}_reblog`,
{
groupId: accountFull,
name: t('meSettingsPush:content.reblog.heading'),
...androidDefaults
}
)
Notifications.setNotificationChannelAsync(
`${accountFull}_mention`,
{
groupId: accountFull,
name: t('meSettingsPush:content.mention.heading'),
...androidDefaults
}
)
Notifications.setNotificationChannelAsync(
`${accountFull}_poll`,
{
groupId: accountFull,
name: t('meSettingsPush:content.poll.heading'),
...androidDefaults
}
)
}
})
}
2021-01-27 00:35:34 +01:00
}
}
)
}}
/>
<MenuRow
title={t('content.theme.heading')}
content={t(`content.theme.options.${settingsTheme}`)}
iconBack='ChevronRight'
onPress={() =>
showActionSheetWithOptions(
{
title: t('content.theme.heading'),
options: [
t('content.theme.options.auto'),
t('content.theme.options.light'),
t('content.theme.options.dark'),
t('content.theme.options.cancel')
],
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
title={t('content.browser.heading')}
content={t(`content.browser.options.${settingsBrowser}`)}
iconBack='ChevronRight'
onPress={() =>
showActionSheetWithOptions(
{
title: t('content.browser.heading'),
options: [
t('content.browser.options.internal'),
t('content.browser.options.external'),
t('content.browser.options.cancel')
],
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