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

171 lines
5.5 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-02-20 19:12:44 +01:00
import { getInstanceActive } from '@utils/slices/instancesSlice'
2021-01-27 00:35:34 +01:00
import {
changeBrowser,
changeLanguage,
changeTheme,
getSettingsLanguage,
getSettingsTheme,
getSettingsBrowser
} from '@utils/slices/settingsSlice'
import { useTheme } from '@utils/styles/ThemeManager'
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'
import { useDispatch, useSelector } from 'react-redux'
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-02-20 19:12:44 +01:00
const instanceActive = useSelector(getInstanceActive)
2021-01-27 00:35:34 +01:00
const settingsLanguage = useSelector(getSettingsLanguage)
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-02-20 19:12:44 +01:00
{instanceActive !== -1 ? (
<MenuRow
title={t('content.notification.heading')}
iconBack='ChevronRight'
onPress={() => {
navigation.navigate('Tab-Me-Settings-Notification')
}}
/>
) : 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')
// @ts-ignore
dispatch(changeLanguage(availableLanguages[buttonIndex]))
i18n.changeLanguage(availableLanguages[buttonIndex])
}
}
)
}}
/>
<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