Move language preferences to new persistence + context (#1837)

This commit is contained in:
Paul Frazee 2023-11-08 09:38:28 -08:00 committed by GitHub
parent e75b2d508b
commit 5843e212c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 233 additions and 190 deletions

View file

@ -9,11 +9,18 @@ import {deviceLocales} from 'platform/detection'
import {LANGUAGES, LANGUAGES_MAP_CODE2} from '../../../../locale/languages'
import {LanguageToggle} from './LanguageToggle'
import {ConfirmLanguagesButton} from './ConfirmLanguagesButton'
import {
useLanguagePrefs,
useSetLanguagePrefs,
toggleContentLanguage,
} from '#/state/preferences/languages'
export const snapPoints = ['100%']
export function Component({}: {}) {
const store = useStores()
const langPrefs = useLanguagePrefs()
const setLangPrefs = useSetLanguagePrefs()
const pal = usePalette('default')
const {isMobile} = useWebMediaQueries()
const onPressDone = React.useCallback(() => {
@ -29,23 +36,23 @@ export function Component({}: {}) {
// sort so that device & selected languages are on top, then alphabetically
langs.sort((a, b) => {
const hasA =
store.preferences.hasContentLanguage(a.code2) ||
langPrefs.contentLanguages.includes(a.code2) ||
deviceLocales.includes(a.code2)
const hasB =
store.preferences.hasContentLanguage(b.code2) ||
langPrefs.contentLanguages.includes(b.code2) ||
deviceLocales.includes(b.code2)
if (hasA === hasB) return a.name.localeCompare(b.name)
if (hasA) return -1
return 1
})
return langs
}, [store])
}, [langPrefs])
const onPress = React.useCallback(
(code2: string) => {
store.preferences.toggleContentLanguage(code2)
toggleContentLanguage(langPrefs, setLangPrefs, code2)
},
[store],
[langPrefs, setLangPrefs],
)
return (

View file

@ -3,7 +3,7 @@ import {StyleSheet} from 'react-native'
import {usePalette} from 'lib/hooks/usePalette'
import {observer} from 'mobx-react-lite'
import {ToggleButton} from 'view/com/util/forms/ToggleButton'
import {useStores} from 'state/index'
import {useLanguagePrefs, toPostLanguages} from '#/state/preferences/languages'
export const LanguageToggle = observer(function LanguageToggleImpl({
code2,
@ -17,17 +17,17 @@ export const LanguageToggle = observer(function LanguageToggleImpl({
langType: 'contentLanguages' | 'postLanguages'
}) {
const pal = usePalette('default')
const store = useStores()
const langPrefs = useLanguagePrefs()
const isSelected = store.preferences[langType].includes(code2)
const values =
langType === 'contentLanguages'
? langPrefs.contentLanguages
: toPostLanguages(langPrefs.postLanguage)
const isSelected = values.includes(code2)
// enforce a max of 3 selections for post languages
let isDisabled = false
if (
langType === 'postLanguages' &&
store.preferences[langType].length >= 3 &&
!isSelected
) {
if (langType === 'postLanguages' && values.length >= 3 && !isSelected) {
isDisabled = true
}

View file

@ -10,11 +10,19 @@ import {deviceLocales} from 'platform/detection'
import {LANGUAGES, LANGUAGES_MAP_CODE2} from '../../../../locale/languages'
import {ConfirmLanguagesButton} from './ConfirmLanguagesButton'
import {ToggleButton} from 'view/com/util/forms/ToggleButton'
import {
useLanguagePrefs,
useSetLanguagePrefs,
hasPostLanguage,
togglePostLanguage,
} from '#/state/preferences/languages'
export const snapPoints = ['100%']
export const Component = observer(function PostLanguagesSettingsImpl() {
const store = useStores()
const langPrefs = useLanguagePrefs()
const setLangPrefs = useSetLanguagePrefs()
const pal = usePalette('default')
const {isMobile} = useWebMediaQueries()
const onPressDone = React.useCallback(() => {
@ -30,23 +38,23 @@ export const Component = observer(function PostLanguagesSettingsImpl() {
// sort so that device & selected languages are on top, then alphabetically
langs.sort((a, b) => {
const hasA =
store.preferences.hasPostLanguage(a.code2) ||
hasPostLanguage(langPrefs.postLanguage, a.code2) ||
deviceLocales.includes(a.code2)
const hasB =
store.preferences.hasPostLanguage(b.code2) ||
hasPostLanguage(langPrefs.postLanguage, b.code2) ||
deviceLocales.includes(b.code2)
if (hasA === hasB) return a.name.localeCompare(b.name)
if (hasA) return -1
return 1
})
return langs
}, [store])
}, [langPrefs])
const onPress = React.useCallback(
(code2: string) => {
store.preferences.togglePostLanguage(code2)
togglePostLanguage(langPrefs, setLangPrefs, code2)
},
[store],
[langPrefs, setLangPrefs],
)
return (
@ -70,14 +78,11 @@ export const Component = observer(function PostLanguagesSettingsImpl() {
</Text>
<ScrollView style={styles.scrollContainer}>
{languages.map(lang => {
const isSelected = store.preferences.hasPostLanguage(lang.code2)
const isSelected = hasPostLanguage(langPrefs.postLanguage, lang.code2)
// enforce a max of 3 selections for post languages
let isDisabled = false
if (
store.preferences.postLanguage.split(',').length >= 3 &&
!isSelected
) {
if (langPrefs.postLanguage.split(',').length >= 3 && !isSelected) {
isDisabled = true
}