All files / hooks useThemeAwareColorScheme.ts

85.71% Statements 6/7
50% Branches 3/6
100% Functions 1/1
85.71% Lines 6/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                            308x 308x     308x 5x       303x       303x    
/**
 * 主题感知的颜色方案 Hook
 *
 * 根据用户设置返回当前应该使用的主题模式:
 * - 'light': 强制使用亮色主题
 * - 'dark': 强制使用暗色主题
 * - 'system': 跟随系统主题
 */
 
import { useColorScheme as useRNColorScheme } from 'react-native';
 
import { useThemeStore } from '@/src/store/themeStore';
 
export function useThemeAwareColorScheme() {
  const { themeMode, _hasHydrated } = useThemeStore();
  const systemColorScheme = useRNColorScheme();
 
  // 在水化完成前,默认使用亮色主题
  if (!_hasHydrated) {
    return 'light';
  }
 
  // 如果用户选择跟随系统,返回系统主题;否则返回用户选择的主题
  Iif (themeMode === 'system') {
    return systemColorScheme ?? 'light';
  }
 
  return themeMode;
}