refactor(init): 优化应用初始化流程,将权限请求延迟到引导完成后
- 将服务初始化拆分为基础服务和权限相关服务两个阶段 - 基础服务(用户数据、HealthKit初始化、快捷动作等)在应用启动时立即执行 - 权限相关服务(通知、HealthKit权限请求)仅在用户完成引导流程后才执行 - 在Redux store中添加onboardingCompleted状态管理 - 引导页面完成时通过Redux更新状态而非直接操作AsyncStorage - 启动页面从预加载数据中读取引导完成状态,避免重复读取存储 - 使用ref防止权限服务重复初始化
This commit is contained in:
@@ -22,10 +22,8 @@ import { SafeAreaView } from 'react-native-safe-area-context';
|
||||
|
||||
import { palette } from '@/constants/Colors';
|
||||
import { ROUTES } from '@/constants/Routes';
|
||||
import { STORAGE_KEYS } from '@/services/api';
|
||||
import AsyncStorage from '@/utils/kvStore';
|
||||
|
||||
const ONBOARDING_COMPLETED_KEY = STORAGE_KEYS.onboardingCompleted;
|
||||
import { useAppDispatch } from '@/hooks/redux';
|
||||
import { setOnboardingCompleted } from '@/store/userSlice';
|
||||
|
||||
type OnboardingSlide = {
|
||||
key: string;
|
||||
@@ -63,6 +61,7 @@ const SLIDES: OnboardingSlide[] = [
|
||||
|
||||
export default function OnboardingScreen() {
|
||||
const router = useRouter();
|
||||
const dispatch = useAppDispatch();
|
||||
const { width } = useWindowDimensions();
|
||||
const [currentIndex, setCurrentIndex] = useState(0);
|
||||
const listRef = useRef<FlatList<OnboardingSlide>>(null);
|
||||
@@ -92,9 +91,10 @@ export default function OnboardingScreen() {
|
||||
);
|
||||
|
||||
const completeOnboarding = useCallback(async () => {
|
||||
await AsyncStorage.setItem(ONBOARDING_COMPLETED_KEY, 'true');
|
||||
// 通过 Redux 更新 onboarding 状态(会自动保存到 AsyncStorage)
|
||||
await dispatch(setOnboardingCompleted());
|
||||
router.replace(ROUTES.TAB_STATISTICS);
|
||||
}, [router]);
|
||||
}, [dispatch, router]);
|
||||
|
||||
const handleSkip = useCallback(() => {
|
||||
completeOnboarding();
|
||||
|
||||
Reference in New Issue
Block a user