fix(mobile): measure the screen on the first chat text read, not at import

This commit is contained in:
Kelvin Amoaba
2026-11-16 19:00:47 +00:00
parent a00aba6b17
commit e44a49aa13
3 changed files with 31 additions and 10 deletions
@@ -28,8 +28,14 @@ vi.mock('react-native-gesture-handler', () => {
runOnJS: () => GestureChain
onStart: () => GestureChain
onUpdate: () => GestureChain
onFinalize: () => GestureChain
}
const chain: GestureChain = {
runOnJS: () => chain,
onStart: () => chain,
onUpdate: () => chain,
onFinalize: () => chain
}
const chain: GestureChain = { runOnJS: () => chain, onStart: () => chain, onUpdate: () => chain }
return {
Gesture: { Simultaneous: () => ({}), Native: () => ({}), Pinch: () => chain },
GestureDetector: 'GestureDetector',
@@ -77,10 +77,15 @@ function parseSettings(raw: string | null): NativeChatTextSettings | null {
type Snapshot = { settings: NativeChatTextSettings; stored: boolean }
let snapshot: Snapshot = { settings: defaultNativeChatTextSettings(), stored: false }
// The phone default until the first storage read, which measures the screen; importing never does.
let snapshot: Snapshot = {
settings: { size: BASE_TEXT_SIZE, font: 'system', lineSpacing: 'default' },
stored: false
}
// A change made while a read is in flight wins over that older read.
let changeCount = 0
let writes: Promise<void> = Promise.resolve()
let watchingScreen = false
const listeners = new Set<() => void>()
function adopt(settings: NativeChatTextSettings | null): void {
@@ -101,12 +106,26 @@ function adopt(settings: NativeChatTextSettings | null): void {
}
}
// A foldable's default follows the screen it is on, until a size is saved.
function watchScreen(): void {
if (watchingScreen) {
return
}
watchingScreen = true
Dimensions.addEventListener('change', () => {
if (!snapshot.stored) {
adopt(null)
}
})
}
/** Rereads storage; the page calls it when the app republishes storage after a Settings change. */
export function reloadNativeChatTextSettings(): Promise<void> {
const readAt = changeCount
return Promise.resolve()
.then(() => AsyncStorage.getItem(NATIVE_CHAT_TEXT_KEY))
.then((raw) => {
watchScreen()
if (changeCount === readAt) {
adopt(parseSettings(raw))
}
@@ -127,12 +146,6 @@ let hydrated = false
const hydration = reloadNativeChatTextSettings().then(() => {
hydrated = true
})
// A foldable's default follows the screen it is on, until a size is saved.
Dimensions.addEventListener('change', () => {
if (!snapshot.stored) {
adopt(null)
}
})
function persist(settings: NativeChatTextSettings | null): Promise<void> {
changeCount += 1
@@ -1,4 +1,4 @@
import { useMemo, useRef, useState, type ComponentProps, type ReactNode } from 'react'
import { useEffect, useMemo, useRef, useState, type ComponentProps, type ReactNode } from 'react'
import { Gesture, GestureDetector } from 'react-native-gesture-handler'
import type { LegacyComposedGesture } from 'react-native-gesture-handler'
import {
@@ -17,7 +17,9 @@ export function useMobileNativeChatPinchGesture(): {
const { size } = useNativeChatTextSettingsFollowingShell()
const [pinched, setPinched] = useState<number | null>(null)
const saved = useRef(size)
saved.current = size
useEffect(() => {
saved.current = size
}, [size])
const live = useRef<number | null>(null)
const pinchBase = useRef(size)
// Run callbacks on JS to access React state safely; simultaneous native scrolling keeps active lists from stealing pinch recognition.