Pre-web image changes refactor (#560)

* Pre-web image changes refactor

* Remove unneeded async behavior
zio/stable
Ollie H 2023-05-02 14:27:00 -07:00 committed by GitHub
parent 74fbb47979
commit bd80db619b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 35 deletions

View File

@ -1,20 +1,12 @@
import {RootStoreModel} from 'state/index' import {RootStoreModel} from 'state/index'
import {ImageModel} from 'state/models/media/image'
export async function openAltTextModal( export async function openAltTextModal(
store: RootStoreModel, store: RootStoreModel,
prevAltText: string, image: ImageModel,
): Promise<string> { ) {
return new Promise((resolve, reject) => {
store.shell.openModal({ store.shell.openModal({
name: 'alt-text-image', name: 'alt-text-image',
prevAltText, image,
onAltTextSet: (altText?: string) => {
if (altText) {
resolve(altText)
} else {
reject(new Error('Canceled'))
}
},
})
}) })
} }

View File

@ -5,7 +5,6 @@ import {makeAutoObservable, runInAction} from 'mobx'
import {openCropper} from 'lib/media/picker' import {openCropper} from 'lib/media/picker'
import {POST_IMG_MAX} from 'lib/constants' import {POST_IMG_MAX} from 'lib/constants'
import {scaleDownDimensions} from 'lib/media/util' import {scaleDownDimensions} from 'lib/media/util'
import {openAltTextModal} from 'lib/media/alt-text'
// TODO: EXIF embed // TODO: EXIF embed
// Cases to consider: ExternalEmbed // Cases to consider: ExternalEmbed
@ -43,16 +42,8 @@ export class ImageModel implements RNImage {
this.scaledHeight = height this.scaledHeight = height
} }
async setAltText() { async setAltText(altText: string) {
try {
const altText = await openAltTextModal(this.rootStore, this.altText)
runInAction(() => {
this.altText = altText this.altText = altText
})
} catch (err) {
this.rootStore.log.error('Failed to set alt text', err)
}
} }
async crop() { async crop() {

View File

@ -4,6 +4,7 @@ import {makeAutoObservable} from 'mobx'
import {ProfileModel} from '../content/profile' import {ProfileModel} from '../content/profile'
import {isObj, hasProp} from 'lib/type-guards' import {isObj, hasProp} from 'lib/type-guards'
import {Image as RNImage} from 'react-native-image-crop-picker' import {Image as RNImage} from 'react-native-image-crop-picker'
import {ImageModel} from '../media/image'
export interface ConfirmModal { export interface ConfirmModal {
name: 'confirm' name: 'confirm'
@ -43,8 +44,7 @@ export interface CropImageModal {
export interface AltTextImageModal { export interface AltTextImageModal {
name: 'alt-text-image' name: 'alt-text-image'
prevAltText: string image: ImageModel
onAltTextSet: (altText?: string) => void
} }
export interface AltTextImageReadModal { export interface AltTextImageReadModal {

View File

@ -9,12 +9,15 @@ import {ImageModel} from 'state/models/media/image'
import {Image} from 'expo-image' import {Image} from 'expo-image'
import {Text} from 'view/com/util/text/Text' import {Text} from 'view/com/util/text/Text'
import {isDesktopWeb} from 'platform/detection' import {isDesktopWeb} from 'platform/detection'
import {openAltTextModal} from 'lib/media/alt-text'
import {useStores} from 'state/index'
interface Props { interface Props {
gallery: GalleryModel gallery: GalleryModel
} }
export const Gallery = observer(function ({gallery}: Props) { export const Gallery = observer(function ({gallery}: Props) {
const store = useStores()
const getImageStyle = useCallback(() => { const getImageStyle = useCallback(() => {
let side: number let side: number
@ -34,9 +37,9 @@ export const Gallery = observer(function ({gallery}: Props) {
const handleAddImageAltText = useCallback( const handleAddImageAltText = useCallback(
(image: ImageModel) => { (image: ImageModel) => {
Keyboard.dismiss() Keyboard.dismiss()
gallery.setAltText(image) openAltTextModal(store, image)
}, },
[gallery], [store],
) )
const handleRemovePhoto = useCallback( const handleRemovePhoto = useCallback(
(image: ImageModel) => { (image: ImageModel) => {

View File

@ -11,24 +11,25 @@ import {TouchableOpacity} from 'react-native-gesture-handler'
import LinearGradient from 'react-native-linear-gradient' import LinearGradient from 'react-native-linear-gradient'
import {useStores} from 'state/index' import {useStores} from 'state/index'
import {isDesktopWeb} from 'platform/detection' import {isDesktopWeb} from 'platform/detection'
import {ImageModel} from 'state/models/media/image'
export const snapPoints = ['80%'] export const snapPoints = ['80%']
interface Props { interface Props {
prevAltText: string image: ImageModel
onAltTextSet: (altText?: string | undefined) => void
} }
export function Component({prevAltText, onAltTextSet}: Props) { export function Component({image}: Props) {
const pal = usePalette('default') const pal = usePalette('default')
const store = useStores() const store = useStores()
const theme = useTheme() const theme = useTheme()
const [altText, setAltText] = useState(prevAltText) const [altText, setAltText] = useState(image.altText)
const onPressSave = useCallback(() => { const onPressSave = useCallback(() => {
onAltTextSet(altText) setAltText(altText)
image.setAltText(altText)
store.shell.closeModal() store.shell.closeModal()
}, [store, altText, onAltTextSet]) }, [store, image, altText])
const onPressCancel = () => { const onPressCancel = () => {
store.shell.closeModal() store.shell.closeModal()