tooot/src/screens/Me/Settings.tsx

247 lines
7.8 KiB
TypeScript
Raw Normal View History

2021-01-07 19:13:09 +01:00
import Button from '@components/Button'
2021-01-16 14:16:58 +01:00
import haptics from '@components/haptics'
2020-12-13 14:04:25 +01:00
import { MenuContainer, MenuRow } from '@components/Menu'
2021-01-13 01:03:46 +01:00
import { useActionSheet } from '@expo/react-native-action-sheet'
2021-01-07 19:13:09 +01:00
import { useNavigation } from '@react-navigation/native'
import { persistor } from '@root/store'
import {
getLocalActiveIndex,
getLocalInstances,
getRemoteUrl
2021-01-16 14:16:58 +01:00
} from '@utils/slices/instancesSlice'
import {
2020-12-29 16:19:04 +01:00
changeAnalytics,
2020-12-20 18:41:28 +01:00
changeBrowser,
changeLanguage,
2020-11-29 18:08:31 +01:00
changeTheme,
2020-12-29 16:19:04 +01:00
getSettingsAnalytics,
2020-12-20 18:41:28 +01:00
getSettingsBrowser,
2020-11-29 18:08:31 +01:00
getSettingsLanguage,
getSettingsTheme
2020-12-13 14:04:25 +01:00
} from '@utils/slices/settingsSlice'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2021-01-16 14:16:58 +01:00
import Constants from 'expo-constants'
2020-12-29 16:19:04 +01:00
import prettyBytes from 'pretty-bytes'
2021-01-07 19:13:09 +01:00
import React, { useEffect, useState } from 'react'
2020-12-29 16:19:04 +01:00
import { useTranslation } from 'react-i18next'
2021-01-13 01:03:46 +01:00
import { StyleSheet, Text } from 'react-native'
2020-12-29 16:19:04 +01:00
import { CacheManager } from 'react-native-expo-image-cache'
2021-01-13 01:03:46 +01:00
import { ScrollView } from 'react-native-gesture-handler'
2020-12-29 16:19:04 +01:00
import { useDispatch, useSelector } from 'react-redux'
2020-11-28 17:07:30 +01:00
2021-01-07 19:13:09 +01:00
const DevDebug: React.FC = () => {
2021-01-13 01:03:46 +01:00
const { showActionSheetWithOptions } = useActionSheet()
2021-01-07 19:13:09 +01:00
const localActiveIndex = useSelector(getLocalActiveIndex)
const localInstances = useSelector(getLocalInstances)
return (
<MenuContainer>
<MenuRow
title={'Local active index'}
content={typeof localActiveIndex + ' - ' + localActiveIndex}
onPress={() => {}}
/>
<MenuRow
title={'Saved local instances'}
content={localInstances.length.toString()}
iconBack='ChevronRight'
onPress={() =>
2021-01-13 01:03:46 +01:00
showActionSheetWithOptions(
2021-01-07 19:13:09 +01:00
{
options: localInstances
.map(instance => {
return instance.url + ': ' + instance.account.id
})
.concat(['Cancel']),
cancelButtonIndex: localInstances.length
},
buttonIndex => {}
)
}
/>
<Button
type='text'
content={'Purge secure storage'}
style={{
marginHorizontal: StyleConstants.Spacing.Global.PagePadding * 2,
marginBottom: StyleConstants.Spacing.Global.PagePadding * 2
}}
destructive
onPress={() => persistor.purge()}
/>
</MenuContainer>
)
}
2020-11-28 17:07:30 +01:00
const ScreenMeSettings: React.FC = () => {
2021-01-13 01:03:46 +01:00
const { showActionSheetWithOptions } = useActionSheet()
2021-01-07 19:13:09 +01:00
const navigation = useNavigation()
2020-11-29 18:08:31 +01:00
const { t, i18n } = useTranslation('meSettings')
2020-12-01 00:44:28 +01:00
const { setTheme, theme } = useTheme()
2020-11-29 18:08:31 +01:00
const settingsLanguage = useSelector(getSettingsLanguage)
const settingsTheme = useSelector(getSettingsTheme)
2020-12-20 18:41:28 +01:00
const settingsBrowser = useSelector(getSettingsBrowser)
2021-01-07 19:13:09 +01:00
const settingsRemote = useSelector(getRemoteUrl)
2020-12-29 16:19:04 +01:00
const settingsAnalytics = useSelector(getSettingsAnalytics)
const dispatch = useDispatch()
const [cacheSize, setCacheSize] = useState<number>()
useEffect(() => {
2020-12-27 00:55:22 +01:00
CacheManager.getCacheSize().then(size => setCacheSize(size))
}, [])
return (
2021-01-13 01:03:46 +01:00
<ScrollView>
2020-12-18 23:58:53 +01:00
<MenuContainer>
2020-12-03 01:28:56 +01:00
<MenuRow
2020-12-01 00:44:28 +01:00
title={t('content.language.heading')}
content={t(`content.language.options.${settingsLanguage}`)}
iconBack='ChevronRight'
2021-01-17 22:37:05 +01:00
onPress={() => {
const availableLanguages = Object.keys(
i18n.services.resourceStore.data
)
2021-01-13 01:03:46 +01:00
showActionSheetWithOptions(
2020-12-01 00:44:28 +01:00
{
2021-01-13 01:03:46 +01:00
title: t('content.language.heading'),
2020-12-01 00:44:28 +01:00
options: [
2021-01-17 22:37:05 +01:00
...availableLanguages.map(language =>
t(`content.language.options.${language}`)
),
2020-12-01 00:44:28 +01:00
t('content.language.options.cancel')
],
2021-01-17 22:37:05 +01:00
cancelButtonIndex: i18n.languages.length
2020-12-01 00:44:28 +01:00
},
buttonIndex => {
2021-01-17 22:37:05 +01:00
haptics('Success')
dispatch(changeLanguage(availableLanguages[buttonIndex]))
i18n.changeLanguage(availableLanguages[buttonIndex])
}
2020-12-01 00:44:28 +01:00
)
2021-01-17 22:37:05 +01:00
}}
2020-12-01 00:44:28 +01:00
/>
2020-12-03 01:28:56 +01:00
<MenuRow
2020-12-01 00:44:28 +01:00
title={t('content.theme.heading')}
content={t(`content.theme.options.${settingsTheme}`)}
iconBack='ChevronRight'
2020-12-01 00:44:28 +01:00
onPress={() =>
2021-01-13 01:03:46 +01:00
showActionSheetWithOptions(
2020-12-01 00:44:28 +01:00
{
2021-01-13 01:03:46 +01:00
title: t('content.theme.heading'),
2020-12-01 00:44:28 +01:00
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:
2020-12-30 14:33:33 +01:00
haptics('Success')
2020-12-01 00:44:28 +01:00
dispatch(changeTheme('auto'))
break
case 1:
2020-12-30 14:33:33 +01:00
haptics('Success')
2020-12-01 00:44:28 +01:00
dispatch(changeTheme('light'))
setTheme('light')
break
case 2:
2020-12-30 14:33:33 +01:00
haptics('Success')
2020-12-01 00:44:28 +01:00
dispatch(changeTheme('dark'))
setTheme('dark')
break
}
2020-11-29 18:08:31 +01:00
}
2020-12-01 00:44:28 +01:00
)
}
/>
2020-12-20 18:41:28 +01:00
<MenuRow
title={t('content.browser.heading')}
content={t(`content.browser.options.${settingsBrowser}`)}
iconBack='ChevronRight'
2020-12-20 18:41:28 +01:00
onPress={() =>
2021-01-13 01:03:46 +01:00
showActionSheetWithOptions(
2020-12-20 18:41:28 +01:00
{
2021-01-13 01:03:46 +01:00
title: t('content.browser.heading'),
2020-12-20 18:41:28 +01:00
options: [
t('content.browser.options.internal'),
t('content.browser.options.external'),
t('content.browser.options.cancel')
],
cancelButtonIndex: 2
},
buttonIndex => {
switch (buttonIndex) {
case 0:
2020-12-30 14:33:33 +01:00
haptics('Success')
2020-12-20 18:41:28 +01:00
dispatch(changeBrowser('internal'))
break
case 1:
2020-12-30 14:33:33 +01:00
haptics('Success')
2020-12-20 18:41:28 +01:00
dispatch(changeBrowser('external'))
break
}
}
)
}
/>
2020-12-01 00:44:28 +01:00
</MenuContainer>
<MenuContainer>
2021-01-07 19:13:09 +01:00
<MenuRow
title={t('content.remote.heading')}
description={t('content.remote.description')}
content={settingsRemote}
iconBack='ChevronRight'
onPress={() => navigation.navigate('Screen-Me-Settings-UpdateRemote')}
/>
<MenuRow
title={t('content.cache.heading')}
2021-01-07 19:13:09 +01:00
content={
cacheSize ? prettyBytes(cacheSize) : t('content.cache.empty')
}
iconBack='ChevronRight'
onPress={async () => {
await CacheManager.clearCache()
2020-12-30 14:33:33 +01:00
haptics('Success')
setCacheSize(0)
}}
/>
2021-01-07 19:13:09 +01:00
</MenuContainer>
<MenuContainer>
2020-12-29 16:19:04 +01:00
<MenuRow
title={t('content.analytics.heading')}
description={t('content.analytics.description')}
switchValue={settingsAnalytics}
switchOnValueChange={() =>
dispatch(changeAnalytics(!settingsAnalytics))
}
/>
2020-12-03 01:28:56 +01:00
<MenuRow
2020-12-01 00:44:28 +01:00
title={t('content.copyrights.heading')}
iconBack='ChevronRight'
2020-12-03 01:28:56 +01:00
/>
2020-12-01 00:44:28 +01:00
<Text style={[styles.version, { color: theme.secondary }]}>
2021-01-17 22:37:05 +01:00
{t('content.version', { version: Constants.manifest.version })}
2020-12-01 00:44:28 +01:00
</Text>
</MenuContainer>
2021-01-07 19:13:09 +01:00
2021-01-16 14:16:58 +01:00
{__DEV__ || Constants.manifest.releaseChannel === 'testing' ? (
<DevDebug />
) : null}
2021-01-13 01:03:46 +01:00
</ScrollView>
)
2020-11-28 17:07:30 +01:00
}
2020-12-01 00:44:28 +01:00
const styles = StyleSheet.create({
version: {
textAlign: 'center',
...StyleConstants.FontStyle.S,
2020-12-01 00:44:28 +01:00
marginTop: StyleConstants.Spacing.M
}
})
2020-11-28 17:07:30 +01:00
export default ScreenMeSettings