Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move require alt-text to new persistence + context #1839

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/state/models/ui/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export class PreferencesModel {
prioritizeFollowedUsers: true,
lab_treeViewEnabled: false, // experimental
}
requireAltTextEnabled: boolean = false

// used to help with transitions from device-stored to server-stored preferences
legacyPreferences: LegacyPreferences | undefined
Expand Down Expand Up @@ -111,7 +110,6 @@ export class PreferencesModel {
contentLabels: this.contentLabels,
savedFeeds: this.savedFeeds,
pinnedFeeds: this.pinnedFeeds,
requireAltTextEnabled: this.requireAltTextEnabled,
}
}

Expand Down Expand Up @@ -180,13 +178,6 @@ export class PreferencesModel {
) {
this.pinnedFeeds = v.pinnedFeeds
}
// check if requiring alt text is enabled in preferences, then hydrate
if (
hasProp(v, 'requireAltTextEnabled') &&
typeof v.requireAltTextEnabled === 'boolean'
) {
this.requireAltTextEnabled = v.requireAltTextEnabled
}
// grab legacy values
this.legacyPreferences = getLegacyPreferences(v)
}
Expand Down Expand Up @@ -608,10 +599,6 @@ export class PreferencesModel {
}
}

toggleRequireAltTextEnabled() {
this.requireAltTextEnabled = !this.requireAltTextEnabled
}

setPrimaryLanguage(lang: string) {
this.primaryLanguage = lang
}
Expand Down
2 changes: 1 addition & 1 deletion src/state/persisted/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as store from '#/state/persisted/store'
import BroadcastChannel from '#/state/persisted/broadcast'

export type {Schema} from '#/state/persisted/schema'
export {defaults as schema} from '#/state/persisted/schema'
export {defaults} from '#/state/persisted/schema'

const broadcast = new BroadcastChannel('BSKY_BROADCAST_CHANNEL')
const UPDATE_EVENT = 'BSKY_UPDATE'
Expand Down
48 changes: 48 additions & 0 deletions src/state/shell/alt-text-required.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be shell state or preferences?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally preferences - I just put it here for convenience since the other waiting PR adds preferences

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react'
import * as persisted from '#/state/persisted'

type StateContext = persisted.Schema['requireAltTextEnabled']
type SetContext = (v: persisted.Schema['requireAltTextEnabled']) => void

const stateContext = React.createContext<StateContext>(
persisted.defaults.requireAltTextEnabled,
)
const setContext = React.createContext<SetContext>(
(_: persisted.Schema['requireAltTextEnabled']) => {},
)

export function Provider({children}: React.PropsWithChildren<{}>) {
const [state, setState] = React.useState(
persisted.get('requireAltTextEnabled'),
)

const setStateWrapped = React.useCallback(
(requireAltTextEnabled: persisted.Schema['requireAltTextEnabled']) => {
setState(requireAltTextEnabled)
persisted.write('requireAltTextEnabled', requireAltTextEnabled)
},
[setState],
)

React.useEffect(() => {
return persisted.onUpdate(() => {
setState(persisted.get('requireAltTextEnabled'))
})
}, [setStateWrapped])

return (
<stateContext.Provider value={state}>
<setContext.Provider value={setStateWrapped}>
{children}
</setContext.Provider>
</stateContext.Provider>
)
}

export function useRequireAltTextEnabled() {
return React.useContext(stateContext)
}

export function useSetRequireAltTextEnabled() {
return React.useContext(setContext)
}
9 changes: 8 additions & 1 deletion src/state/shell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Provider as DrawerOpenProvider} from './drawer-open'
import {Provider as DrawerSwipableProvider} from './drawer-swipe-disabled'
import {Provider as MinimalModeProvider} from './minimal-mode'
import {Provider as ColorModeProvider} from './color-mode'
import {Provider as AltTextRequiredProvider} from './alt-text-required'

export {useIsDrawerOpen, useSetDrawerOpen} from './drawer-open'
export {
Expand All @@ -11,13 +12,19 @@ export {
} from './drawer-swipe-disabled'
export {useMinimalShellMode, useSetMinimalShellMode} from './minimal-mode'
export {useColorMode, useSetColorMode} from './color-mode'
export {
useRequireAltTextEnabled,
useSetRequireAltTextEnabled,
} from './alt-text-required'

export function Provider({children}: React.PropsWithChildren<{}>) {
return (
<DrawerOpenProvider>
<DrawerSwipableProvider>
<MinimalModeProvider>
<ColorModeProvider>{children}</ColorModeProvider>
<ColorModeProvider>
<AltTextRequiredProvider>{children}</AltTextRequiredProvider>
</ColorModeProvider>
</MinimalModeProvider>
</DrawerSwipableProvider>
</DrawerOpenProvider>
Expand Down
14 changes: 6 additions & 8 deletions src/view/com/composer/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {LabelsBtn} from './labels/LabelsBtn'
import {SelectLangBtn} from './select-language/SelectLangBtn'
import {EmojiPickerButton} from './text-input/web/EmojiPicker.web'
import {insertMentionAt} from 'lib/strings/mention-manip'
import {useRequireAltTextEnabled} from '#/state/shell'

type Props = ComposerOpts
export const ComposePost = observer(function ComposePost({
Expand All @@ -61,6 +62,7 @@ export const ComposePost = observer(function ComposePost({
const pal = usePalette('default')
const {isDesktop, isMobile} = useWebMediaQueries()
const store = useStores()
const requireAltTextEnabled = useRequireAltTextEnabled()
const textInput = useRef<TextInputRef>(null)
const [isKeyboardVisible] = useIsKeyboardVisible({iosUseWillEvents: true})
const [isProcessing, setIsProcessing] = useState(false)
Expand Down Expand Up @@ -187,7 +189,7 @@ export const ComposePost = observer(function ComposePost({
if (isProcessing || graphemeLength > MAX_GRAPHEME_LENGTH) {
return
}
if (store.preferences.requireAltTextEnabled && gallery.needsAltText) {
if (requireAltTextEnabled && gallery.needsAltText) {
return
}

Expand Down Expand Up @@ -241,12 +243,8 @@ export const ComposePost = observer(function ComposePost({
const canPost = useMemo(
() =>
graphemeLength <= MAX_GRAPHEME_LENGTH &&
(!store.preferences.requireAltTextEnabled || !gallery.needsAltText),
[
graphemeLength,
store.preferences.requireAltTextEnabled,
gallery.needsAltText,
],
(!requireAltTextEnabled || !gallery.needsAltText),
[graphemeLength, requireAltTextEnabled, gallery.needsAltText],
)
const selectTextInputPlaceholder = replyTo ? 'Write your reply' : `What's up?`

Expand Down Expand Up @@ -314,7 +312,7 @@ export const ComposePost = observer(function ComposePost({
</>
)}
</View>
{store.preferences.requireAltTextEnabled && gallery.needsAltText && (
{requireAltTextEnabled && gallery.needsAltText && (
<View style={[styles.reminderLine, pal.viewLight]}>
<View style={styles.errorIcon}>
<FontAwesomeIcon
Expand Down
8 changes: 6 additions & 2 deletions src/view/screens/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import {
useSetMinimalShellMode,
useColorMode,
useSetColorMode,
useRequireAltTextEnabled,
useSetRequireAltTextEnabled,
} from '#/state/shell'

// TEMPORARY (APP-700)
Expand All @@ -66,6 +68,8 @@ export const SettingsScreen = withAuthRequired(
const pal = usePalette('default')
const store = useStores()
const setMinimalShellMode = useSetMinimalShellMode()
const requireAltTextEnabled = useRequireAltTextEnabled()
const setRequireAltTextEnabled = useSetRequireAltTextEnabled()
const navigation = useNavigation<NavigationProp>()
const {isMobile} = useWebMediaQueries()
const {screen, track} = useAnalytics()
Expand Down Expand Up @@ -372,8 +376,8 @@ export const SettingsScreen = withAuthRequired(
type="default-light"
label="Require alt text before posting"
labelType="lg"
isSelected={store.preferences.requireAltTextEnabled}
onPress={store.preferences.toggleRequireAltTextEnabled}
isSelected={requireAltTextEnabled}
onPress={() => setRequireAltTextEnabled(!requireAltTextEnabled)}
/>
</View>

Expand Down