Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 显示模式偏好读回后再释放启动遮罩,保存失败时提示

kirozeng1ea6d0358ae3
diff --git a/apps/mobile/app/_layout.tsx b/apps/mobile/app/_layout.tsx
index c02bd02704..0a9db516c8 100644
--- a/apps/mobile/app/_layout.tsx
+++ b/apps/mobile/app/_layout.tsx
@@ -86,6 +86,8 @@ import { IncomingShareBridge } from '@/session/IncomingShareBridge';
import { HomeEntryProvider, useHomeEntrySplashRelease } from '@/session/HomeEntryProvider';
import { RemoteDesktopHost } from '@/remote-desktop/RemoteDesktopHost';
+const holdSplash = () => undefined;
+
function NavigationGate() {
const windowGeometry = useAdaptiveWindow();
// Establish chrome before push starts, rather than revealing a hidden bar after mount.
@@ -94,7 +96,7 @@ function NavigationGate() {
const auth = useAuth();
const router = useRouter();
const segments = useSegments();
- const { mode, colors } = useTheme();
+ const { mode, colors, preferenceReady } = useTheme();
const { releaseSplash, splashActive } = useStartupSplash();
// iOS 状态栏样式走 react-native-screens 的 VC-based 通道(Info.plist 已翻
// UIViewControllerBasedStatusBarAppearance=YES):iOS 27 起 UIKit 不再接受
@@ -118,7 +120,8 @@ function NavigationGate() {
// 登录与本机首页偏好就绪、默认入口重定向完成后再释放常驻 splash。
// 深链不经过 index;同样在这里释放,避免先露出任务再跳回伙伴。
- useHomeEntrySplashRelease(releaseSplash);
+ // 显示模式偏好读回前不释放,避免已选浅色 / 深色的用户先看到系统外观再切换。
+ useHomeEntrySplashRelease(preferenceReady ? releaseSplash : holdSplash);
// 启动链走完 = 本次热更 reload(如果有)确实落地:清掉 reload 闸门记录。
// 只在目标 update 已成为当前运行版本时才清,判定在 markStartupOtaLaunchSuccess 内。
diff --git a/apps/mobile/app/settings.tsx b/apps/mobile/app/settings.tsx
index 51cde98e3e..db1c32e929 100644
--- a/apps/mobile/app/settings.tsx
+++ b/apps/mobile/app/settings.tsx
@@ -326,9 +326,12 @@ export default function SettingsScreen() {
const selectAppearance = useCallback((next: string) => {
const nextPreference = THEME_PREFERENCES.find((option) => option === next);
if (!nextPreference) return;
- setThemePreference(nextPreference);
setAppearancePickerOpen(false);
- }, [setThemePreference]);
+ // 本次会话已切换;只有本机存储写失败时提示下次启动会回到原设置。
+ setThemePreference(nextPreference).catch(() => {
+ Alert.alert(t('settings.appearance.modeLabel'), t('settings.appearance.saveFailed'));
+ });
+ }, [setThemePreference, t]);
useEffect(() => {
if (!auth.isAuthenticated || !auth.deviceId) {
diff --git a/apps/mobile/src/__tests__/themePreferenceStore.test.ts b/apps/mobile/src/__tests__/themePreferenceStore.test.ts
index fdcd58933b..3ad959ffae 100644
--- a/apps/mobile/src/__tests__/themePreferenceStore.test.ts
+++ b/apps/mobile/src/__tests__/themePreferenceStore.test.ts
@@ -49,6 +49,20 @@ describe('themePreferenceStore', () => {
await expect(readThemePreference()).resolves.toBe('system');
});
+ it('读失败按跟随系统处理,不阻塞启动', async () => {
+ const storage = (await import('@react-native-async-storage/async-storage')).default;
+ vi.mocked(storage.getItem).mockRejectedValueOnce(new Error('disk'));
+ await expect(readThemePreference()).resolves.toBe('system');
+ });
+
+ it('写失败向上抛出,调用方才能提示「未保存」', async () => {
+ const storage = (await import('@react-native-async-storage/async-storage')).default;
+ vi.mocked(storage.setItem).mockRejectedValueOnce(new Error('disk full'));
+ await expect(saveThemePreference('dark')).rejects.toThrow('disk full');
+ vi.mocked(storage.removeItem).mockRejectedValueOnce(new Error('disk full'));
+ await expect(saveThemePreference('system')).rejects.toThrow('disk full');
+ });
+
it('有效模式 = override ?? 系统外观', () => {
expect(resolveThemeMode('system', 'dark')).toBe('dark');
expect(resolveThemeMode('system', 'light')).toBe('light');
diff --git a/apps/mobile/src/i18n/locales/en/settings.json b/apps/mobile/src/i18n/locales/en/settings.json
index a81ce35dfa..24d5185ea9 100644
--- a/apps/mobile/src/i18n/locales/en/settings.json
+++ b/apps/mobile/src/i18n/locales/en/settings.json
@@ -30,7 +30,8 @@
"system": "Auto (follow system)",
"light": "Light",
"dark": "Dark"
- }
+ },
+ "saveFailed": "Failed to save appearance mode. The previous setting will return after restart."
},
"language": {
"title": "Language",
diff --git a/apps/mobile/src/i18n/locales/ja/settings.json b/apps/mobile/src/i18n/locales/ja/settings.json
index e979f36f81..f8202402af 100644
--- a/apps/mobile/src/i18n/locales/ja/settings.json
+++ b/apps/mobile/src/i18n/locales/ja/settings.json
@@ -30,7 +30,8 @@
"system": "自動 (システムに従う)",
"light": "ライト",
"dark": "ダーク"
- }
+ },
+ "saveFailed": "表示モードを保存できませんでした。再起動すると以前の設定に戻ります。"
},
"language": {
"title": "言語",
diff --git a/apps/mobile/src/i18n/locales/ko/settings.json b/apps/mobile/src/i18n/locales/ko/settings.json
index 8726f48e65..a0131f0022 100644
--- a/apps/mobile/src/i18n/locales/ko/settings.json
+++ b/apps/mobile/src/i18n/locales/ko/settings.json
@@ -30,7 +30,8 @@
"system": "자동 (시스템 따르기)",
"light": "라이트",
"dark": "다크"
- }
+ },
+ "saveFailed": "표시 모드를 저장하지 못했습니다. 다시 열면 이전 설정으로 돌아갑니다."
},
"language": {
"title": "언어",
diff --git a/apps/mobile/src/i18n/locales/zh-CN/settings.json b/apps/mobile/src/i18n/locales/zh-CN/settings.json
index f7ab105374..1ee7a24b1b 100644
--- a/apps/mobile/src/i18n/locales/zh-CN/settings.json
+++ b/apps/mobile/src/i18n/locales/zh-CN/settings.json
@@ -30,7 +30,8 @@
"system": "跟随系统",
"light": "浅色",
"dark": "深色"
- }
+ },
+ "saveFailed": "显示模式没有保存成功,重新打开后会恢复之前的设置"
},
"language": {
"title": "语言",
diff --git a/apps/mobile/src/i18n/locales/zh-TW/settings.json b/apps/mobile/src/i18n/locales/zh-TW/settings.json
index 72376c0b1a..58f9b4dfb8 100644
--- a/apps/mobile/src/i18n/locales/zh-TW/settings.json
+++ b/apps/mobile/src/i18n/locales/zh-TW/settings.json
@@ -30,7 +30,8 @@
"system": "跟隨系統",
"light": "淺色",
"dark": "深色"
- }
+ },
+ "saveFailed": "顯示模式沒有儲存成功,重新開啟後會恢復之前的設定"
},
"language": {
"title": "語言",
diff --git a/apps/mobile/src/theme/ThemeProvider.tsx b/apps/mobile/src/theme/ThemeProvider.tsx
index f84fd0ac33..795d2790ea 100644
--- a/apps/mobile/src/theme/ThemeProvider.tsx
+++ b/apps/mobile/src/theme/ThemeProvider.tsx
@@ -13,15 +13,21 @@ interface ThemeValue {
colors: ThemeColors;
/** 用户的显示模式偏好(含 'system');子树覆盖(ThemeOverrideProvider)不改变它。 */
preference: ThemePreference;
- /** 设置显示模式偏好 —— 持久化 override、同步原生外观。 */
- setPreference: (next: ThemePreference) => void;
+ /** 已保存的偏好是否已读回;启动遮罩等它就绪后再释放,避免先按系统外观闪一下。 */
+ preferenceReady: boolean;
+ /**
+ * 设置显示模式偏好:立即切换并同步原生外观,返回的 Promise 在持久化失败时 reject,
+ * 调用方据此提示「未保存」(本次会话仍按新选择显示)。
+ */
+ setPreference: (next: ThemePreference) => Promise<void>;
}
const ThemeContext = createContext<ThemeValue>({
mode: 'light',
colors: palettes.light,
preference: 'system',
- setPreference: () => undefined,
+ preferenceReady: true,
+ setPreference: async () => undefined,
});
/**
@@ -35,21 +41,25 @@ function applyNativeScheme(preference: ThemePreference): void {
/**
* 按显示模式偏好(默认跟随系统)向下提供当前主题色板。挂在 SafeAreaProvider 内、其它业务
- * Provider 之上。偏好读出是异步的:先按系统外观渲染首帧,挂载后读出 override 再切换;
- * 启动期有 StartupSplashOverlay 顶着,不产生可见闪变。
+ * Provider 之上。偏好读出是异步的:读回前按系统外观渲染,StartupSplashOverlay 以
+ * `preferenceReady` 为释放条件之一,保证遮罩淡出时已是用户选择的模式。
*/
export function ThemeProvider({ children }: { children: ReactNode }) {
const systemScheme = useColorScheme();
const [preference, setPreferenceState] = useState<ThemePreference>('system');
+ const [preferenceReady, setPreferenceReady] = useState(false);
// 用户已手动选择过时置位:挂载期的异步读回不得覆盖更晚的手动选择。
const userChoseRef = useRef(false);
useEffect(() => {
let cancelled = false;
void readThemePreference().then((stored) => {
- if (cancelled || userChoseRef.current || stored === 'system') return;
- applyNativeScheme(stored);
- setPreferenceState(stored);
+ if (cancelled) return;
+ if (!userChoseRef.current && stored !== 'system') {
+ applyNativeScheme(stored);
+ setPreferenceState(stored);
+ }
+ setPreferenceReady(true);
});
return () => {
cancelled = true;
@@ -60,14 +70,13 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
userChoseRef.current = true;
applyNativeScheme(next);
setPreferenceState(next);
- // 持久化失败不影响当前会话切换('system' = 删除 override)。
- void saveThemePreference(next);
+ return saveThemePreference(next);
}, []);
const mode = resolveThemeMode(preference, systemScheme);
const value = useMemo<ThemeValue>(
- () => ({ mode, colors: palettes[mode], preference, setPreference }),
- [mode, preference, setPreference],
+ () => ({ mode, colors: palettes[mode], preference, preferenceReady, setPreference }),
+ [mode, preference, preferenceReady, setPreference],
);
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
}
diff --git a/apps/mobile/src/theme/themePreferenceStore.ts b/apps/mobile/src/theme/themePreferenceStore.ts
index e4407a274f..e4ff2e2319 100644
--- a/apps/mobile/src/theme/themePreferenceStore.ts
+++ b/apps/mobile/src/theme/themePreferenceStore.ts
@@ -29,17 +29,19 @@ export function resolveThemeMode(
return systemScheme === 'dark' ? 'dark' : 'light';
}
+/** 读失败按「跟随系统」处理:启动不能因为本机存储异常卡住。 */
export async function readThemePreference(): Promise<ThemePreference> {
const raw = await AsyncStorage.getItem(STORAGE_KEY).catch(() => null);
return isThemeOverride(raw) ? raw : 'system';
}
+/** 写失败向上抛出:调用方需告知用户这次选择没有保存,不能把未落盘的选择当成已保存。 */
export async function saveThemePreference(preference: ThemePreference): Promise<void> {
if (preference === 'system') {
- await AsyncStorage.removeItem(STORAGE_KEY).catch(() => undefined);
+ await AsyncStorage.removeItem(STORAGE_KEY);
return;
}
- await AsyncStorage.setItem(STORAGE_KEY, preference).catch(() => undefined);
+ await AsyncStorage.setItem(STORAGE_KEY, preference);
}
export const __testing = {