refactor(init): 优化应用初始化流程,将权限请求延迟到引导完成后
- 将服务初始化拆分为基础服务和权限相关服务两个阶段 - 基础服务(用户数据、HealthKit初始化、快捷动作等)在应用启动时立即执行 - 权限相关服务(通知、HealthKit权限请求)仅在用户完成引导流程后才执行 - 在Redux store中添加onboardingCompleted状态管理 - 引导页面完成时通过Redux更新状态而非直接操作AsyncStorage - 启动页面从预加载数据中读取引导完成状态,避免重复读取存储 - 使用ref防止权限服务重复初始化
This commit is contained in:
@@ -10,15 +10,17 @@ let preloadedUserData: {
|
||||
token: string | null;
|
||||
profile: UserProfile;
|
||||
privacyAgreed: boolean;
|
||||
onboardingCompleted: boolean;
|
||||
} | null = null;
|
||||
|
||||
// 预加载用户数据的函数
|
||||
export async function preloadUserData() {
|
||||
try {
|
||||
const [profileStr, privacyAgreedStr, token] = await Promise.all([
|
||||
const [profileStr, privacyAgreedStr, token, onboardingCompletedStr] = await Promise.all([
|
||||
AsyncStorage.getItem(STORAGE_KEYS.userProfile),
|
||||
AsyncStorage.getItem(STORAGE_KEYS.privacyAgreed),
|
||||
AsyncStorage.getItem(STORAGE_KEYS.authToken),
|
||||
AsyncStorage.getItem(STORAGE_KEYS.onboardingCompleted),
|
||||
]);
|
||||
|
||||
let profile: UserProfile = {
|
||||
@@ -35,20 +37,24 @@ export async function preloadUserData() {
|
||||
}
|
||||
|
||||
const privacyAgreed = privacyAgreedStr === 'true';
|
||||
const onboardingCompleted = onboardingCompletedStr === 'true';
|
||||
|
||||
// 如果有 token,需要设置到 API 客户端
|
||||
if (token) {
|
||||
await setAuthToken(token);
|
||||
}
|
||||
|
||||
preloadedUserData = { token, profile, privacyAgreed };
|
||||
preloadedUserData = { token, profile, privacyAgreed, onboardingCompleted };
|
||||
return preloadedUserData;
|
||||
} catch (error) {
|
||||
console.error('预加载用户数据失败:', error);
|
||||
preloadedUserData = {
|
||||
token: null, profile: {
|
||||
token: null,
|
||||
profile: {
|
||||
memberNumber: 0
|
||||
}, privacyAgreed: false
|
||||
},
|
||||
privacyAgreed: false,
|
||||
onboardingCompleted: false
|
||||
};
|
||||
return preloadedUserData;
|
||||
}
|
||||
@@ -56,7 +62,7 @@ export async function preloadUserData() {
|
||||
|
||||
// 获取预加载的用户数据
|
||||
function getPreloadedUserData() {
|
||||
return preloadedUserData || { token: null, profile: {}, privacyAgreed: false };
|
||||
return preloadedUserData || { token: null, profile: {}, privacyAgreed: false, onboardingCompleted: false };
|
||||
}
|
||||
|
||||
export type Gender = 'male' | 'female' | '';
|
||||
@@ -108,6 +114,7 @@ export type UserState = {
|
||||
error: string | null;
|
||||
weightHistory: WeightHistoryItem[];
|
||||
activityHistory: ActivityHistoryItem[];
|
||||
onboardingCompleted: boolean; // 是否完成引导流程
|
||||
};
|
||||
|
||||
export const DEFAULT_MEMBER_NAME = '朋友';
|
||||
@@ -128,6 +135,7 @@ const getInitialState = (): UserState => {
|
||||
error: null,
|
||||
weightHistory: [],
|
||||
activityHistory: [],
|
||||
onboardingCompleted: preloaded.onboardingCompleted, // 引导完成状态
|
||||
};
|
||||
};
|
||||
|
||||
@@ -207,6 +215,12 @@ export const setPrivacyAgreed = createAsyncThunk('user/setPrivacyAgreed', async
|
||||
return true;
|
||||
});
|
||||
|
||||
// 设置 onboarding 完成状态
|
||||
export const setOnboardingCompleted = createAsyncThunk('user/setOnboardingCompleted', async () => {
|
||||
await AsyncStorage.setItem(STORAGE_KEYS.onboardingCompleted, 'true');
|
||||
return true;
|
||||
});
|
||||
|
||||
export const logout = createAsyncThunk('user/logout', async () => {
|
||||
await Promise.all([
|
||||
AsyncStorage.removeItem(STORAGE_KEYS.authToken),
|
||||
@@ -364,6 +378,9 @@ const userSlice = createSlice({
|
||||
})
|
||||
.addCase(setPrivacyAgreed.fulfilled, (state) => {
|
||||
})
|
||||
.addCase(setOnboardingCompleted.fulfilled, (state) => {
|
||||
state.onboardingCompleted = true;
|
||||
})
|
||||
.addCase(fetchWeightHistory.fulfilled, (state, action) => {
|
||||
state.weightHistory = action.payload;
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user