Skip to content

Commit c972b13

Browse files
committed
fix(mobile): keep Device language after other preference saves
Picking Device language wrote voiceInputLocale: undefined, which JSON drops. The preferences atom merges the initial load under confirmed saves, so the next unrelated save brought the old locale back. Store null for the device language, matching terminalFontSize and codeFontSize.
1 parent c78c6a2 commit c972b13

5 files changed

Lines changed: 91 additions & 9 deletions

File tree

‎apps/mobile/src/features/settings/SettingsDictationLanguageRouteScreen.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/
99
import { SettingsChoiceRow } from "./components/SettingsChoiceRow";
1010
import { SettingsSection } from "./components/SettingsSection";
1111

12-
const OPTIONS = [{ id: undefined, label: DEVICE_DICTATION_LANGUAGE_LABEL }, ...DICTATION_LANGUAGES];
12+
const OPTIONS = [{ id: null, label: DEVICE_DICTATION_LANGUAGE_LABEL }, ...DICTATION_LANGUAGES];
1313

1414
export function SettingsDictationLanguageRouteScreen() {
1515
const insets = useSafeAreaInsets();
1616
const preferencesResult = useAtomValue(mobilePreferencesAtom);
1717
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
1818
const preferencesReady = AsyncResult.isSuccess(preferencesResult) && !preferencesResult.waiting;
1919
const selectedId = AsyncResult.isSuccess(preferencesResult)
20-
? preferencesResult.value.voiceInputLocale
21-
: null;
20+
? (preferencesResult.value.voiceInputLocale ?? null)
21+
: undefined;
2222

2323
return (
2424
<View collapsable={false} className="flex-1 bg-sheet">

‎apps/mobile/src/features/voice-input/VoiceInputProvider.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ function useVoiceInputRuntime() {
156156
getTranscriber: () => {
157157
const preferences = registry.get(mobilePreferencesAtom);
158158
return getLocalVoiceTranscriber(
159-
AsyncResult.isSuccess(preferences) ? preferences.value.voiceInputLocale : undefined,
159+
AsyncResult.isSuccess(preferences)
160+
? (preferences.value.voiceInputLocale ?? undefined)
161+
: undefined,
160162
);
161163
},
162164
requestPermission: async () => {

‎apps/mobile/src/lib/dictationLanguages.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function isDictationLanguage(value: unknown): value is string {
3030
return DICTATION_LANGUAGES.some((language) => language.id === value);
3131
}
3232

33-
export function dictationLanguageLabel(id: string | undefined): string {
33+
export function dictationLanguageLabel(id: string | null | undefined): string {
3434
return (
3535
DICTATION_LANGUAGES.find((language) => language.id === id)?.label ??
3636
DEVICE_DICTATION_LANGUAGE_LABEL

‎apps/mobile/src/persistence/mobile-preferences.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ export interface Preferences {
7373
readonly composerEnterBehavior?: ComposerEnterBehavior;
7474
readonly followUpBehavior?: FollowUpBehavior;
7575
readonly microphones?: ReadonlyArray<MicrophoneEntry>;
76-
/** An id from `DICTATION_LANGUAGES`. Writing undefined returns to the device language. */
77-
readonly voiceInputLocale?: string | undefined;
76+
/** An id from `DICTATION_LANGUAGES`, or null for the device language. */
77+
readonly voiceInputLocale?: string | null;
7878
readonly projectGroupingEnabled?: boolean;
7979
readonly projectGroupingMode?: SidebarProjectGroupingMode;
8080
readonly planModeEnabled?: boolean;
@@ -148,7 +148,7 @@ function sanitizePreferences(parsed: Preferences): Preferences {
148148
composerEnterBehavior?: ComposerEnterBehavior;
149149
followUpBehavior?: FollowUpBehavior;
150150
microphones?: ReadonlyArray<MicrophoneEntry>;
151-
voiceInputLocale?: string;
151+
voiceInputLocale?: string | null;
152152
projectGroupingEnabled?: boolean;
153153
projectGroupingMode?: SidebarProjectGroupingMode;
154154
planModeEnabled?: boolean;
@@ -244,7 +244,7 @@ function sanitizePreferences(parsed: Preferences): Preferences {
244244
: [];
245245
});
246246
}
247-
if (isDictationLanguage(parsed.voiceInputLocale)) {
247+
if (isDictationLanguage(parsed.voiceInputLocale) || parsed.voiceInputLocale === null) {
248248
preferences.voiceInputLocale = parsed.voiceInputLocale;
249249
}
250250
if (typeof parsed.projectGroupingEnabled === "boolean") {

‎apps/mobile/src/state/preferences.test.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ vi.mock("../lib/runtime", async () => {
2727
});
2828

2929
import { createMobilePreferencesState } from "./preferences";
30+
import * as MobileDatabase from "../persistence/mobile-database";
3031
import * as MobilePreferences from "../persistence/mobile-preferences";
32+
import * as MobileSecureStorage from "../persistence/mobile-secure-storage";
3133

3234
function deferred<A>() {
3335
let resolve!: (value: A) => void;
@@ -60,6 +62,53 @@ function makePreferencesState(
6062
);
6163
}
6264

65+
function makePreferencesStore(initialPayload: MobilePreferences.Preferences) {
66+
const row = { payload: JSON.stringify(initialPayload), updatedAt: 1 };
67+
const unused = Effect.die("unused in preferences tests");
68+
return MobilePreferences.make().pipe(
69+
Effect.provideService(
70+
MobileDatabase.MobileDatabase,
71+
MobileDatabase.MobileDatabase.of({
72+
loadCache: () => unused,
73+
listCache: () => unused,
74+
saveCache: () => unused,
75+
removeCache: () => unused,
76+
clearCacheKind: () => unused,
77+
clearEnvironmentCache: () => unused,
78+
clearAllCaches: unused,
79+
inspectCaches: unused,
80+
loadPreferencesJson: Effect.sync(() => Option.some({ ...row })),
81+
savePreferencesJson: (payload, updatedAt) =>
82+
Effect.sync(() => {
83+
row.payload = payload;
84+
row.updatedAt = updatedAt;
85+
}),
86+
}),
87+
),
88+
Effect.provideService(
89+
MobileSecureStorage.MobileSecureStorage,
90+
MobileSecureStorage.MobileSecureStorage.of({
91+
getItem: () => Effect.succeed(null),
92+
setItem: () => Effect.void,
93+
removeItem: () => Effect.void,
94+
}),
95+
),
96+
Effect.map((store) => ({ store, storedPayload: () => JSON.parse(row.payload) as unknown })),
97+
);
98+
}
99+
100+
describe("mobile preferences store", () => {
101+
it.effect("keeps a null dictation language and drops an unknown one", () =>
102+
Effect.gen(function* () {
103+
const cleared = yield* makePreferencesStore({ voiceInputLocale: null });
104+
expect(yield* cleared.store.load).toEqual({ voiceInputLocale: null });
105+
106+
const unknown = yield* makePreferencesStore({ voiceInputLocale: "xx-XX" });
107+
expect(yield* unknown.store.load).toEqual({});
108+
}),
109+
);
110+
});
111+
63112
describe("mobile preferences state", () => {
64113
it.effect("shares one preference load across consumers", () =>
65114
Effect.gen(function* () {
@@ -355,4 +404,35 @@ describe("mobile preferences state", () => {
355404
registry.dispose();
356405
}),
357406
);
407+
408+
it.effect("keeps the device dictation language after another preference saves", () =>
409+
Effect.gen(function* () {
410+
const { store, storedPayload } = yield* makePreferencesStore({ voiceInputLocale: "pt-BR" });
411+
const state = createMobilePreferencesState(
412+
Atom.runtime(Layer.succeed(MobilePreferences.MobilePreferencesStore, store)),
413+
);
414+
const registry = AtomRegistry.make();
415+
const unmountPreferences = registry.mount(state.preferencesAtom);
416+
const unmountUpdate = registry.mount(state.updatePreferencesAtom);
417+
const save = (patch: Partial<MobilePreferences.Preferences>) => {
418+
registry.set(state.updatePreferencesAtom, patch);
419+
return AtomRegistry.getResult(registry, state.updatePreferencesAtom, {
420+
suspendOnWaiting: true,
421+
});
422+
};
423+
424+
yield* AtomRegistry.getResult(registry, state.preferencesAtom, { suspendOnWaiting: true });
425+
yield* save({ voiceInputLocale: null });
426+
yield* save({ baseFontSize: 18 });
427+
428+
expect(
429+
yield* AtomRegistry.getResult(registry, state.preferencesAtom, { suspendOnWaiting: true }),
430+
).toEqual({ voiceInputLocale: null, baseFontSize: 18 });
431+
expect(storedPayload()).toEqual({ voiceInputLocale: null, baseFontSize: 18 });
432+
433+
unmountUpdate();
434+
unmountPreferences();
435+
registry.dispose();
436+
}),
437+
);
358438
});

0 commit comments

Comments
 (0)