feat: 更新应用配置和引入新依赖

- 修改 app.json,禁用平板支持以优化用户体验
- 在 package.json 和 package-lock.json 中新增 react-native-toast-message 依赖,支持消息提示功能
- 在多个组件中集成 Toast 组件,提升用户交互反馈
- 更新训练计划相关逻辑,优化状态管理和数据处理
- 调整样式以适应新功能的展示和交互
This commit is contained in:
2025-08-16 09:42:33 +08:00
parent 3312250f2d
commit 5a4d86ff7d
16 changed files with 192 additions and 255 deletions

View File

@@ -10,7 +10,7 @@
"newArchEnabled": true,
"jsEngine": "jsc",
"ios": {
"supportsTablet": true,
"supportsTablet": false,
"bundleIdentifier": "com.anonymous.digitalpilates",
"infoPlist": {
"ITSAppUsesNonExemptEncryption": false,

View File

@@ -160,11 +160,6 @@ export default function HomeScreen() {
React.useEffect(() => {
let canceled = false;
async function load() {
if (!isLoggedIn) {
console.log('fetchRecommendations not logged in');
setItems(getFallbackItems());
return;
}
try {
const cards = await fetchRecommendations();
@@ -295,7 +290,7 @@ export default function HomeScreen() {
<Pressable
style={[styles.featureCard, styles.featureCardPrimary]}
onPress={() => router.push('/ai-posture-assessment')}
onPress={() => pushIfAuthedElseLogin('/ai-posture-assessment')}
>
<View style={styles.featureIconWrapper}>
<Image

View File

@@ -12,6 +12,7 @@ import { store } from '@/store';
import { rehydrateUser, setPrivacyAgreed } from '@/store/userSlice';
import React from 'react';
import RNExitApp from 'react-native-exit-app';
import Toast from 'react-native-toast-message';
import { Provider } from 'react-redux';
@@ -56,6 +57,7 @@ function Bootstrapper({ children }: { children: React.ReactNode }) {
onAgree={handlePrivacyAgree}
onDisagree={handlePrivacyDisagree}
/>
<Toast />
</>
);
}
@@ -91,6 +93,7 @@ export default function RootLayout() {
<Stack.Screen name="+not-found" />
</Stack>
<StatusBar style="dark" />
<Toast />
</ThemeProvider>
</Bootstrapper>
</Provider>

View File

@@ -13,6 +13,7 @@ import { Colors } from '@/constants/Colors';
import { useAppDispatch } from '@/hooks/redux';
import { useColorScheme } from '@/hooks/useColorScheme';
import { login } from '@/store/userSlice';
import Toast from 'react-native-toast-message';
export default function LoginScreen() {
const router = useRouter();
@@ -111,6 +112,11 @@ export default function LoginScreen() {
throw new Error('未获取到 Apple 身份令牌');
}
await dispatch(login({ appleIdentityToken: identityToken })).unwrap();
Toast.show({
text1: '登录成功',
type: 'success',
});
// 登录成功后处理重定向
const to = searchParams?.redirectTo as string | undefined;
const paramsJson = searchParams?.redirectParams as string | undefined;

View File

@@ -31,7 +31,6 @@ export default function SplashScreen() {
// 如果出现错误,默认显示引导页面
// setTimeout(() => {
// router.replace('/onboarding');
// setIsLoading(false);
// }, 1000);
}
setIsLoading(false);

View File

@@ -29,7 +29,6 @@ import {
TouchableOpacity,
View,
} from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
type WeightUnit = 'kg' | 'lb';
type HeightUnit = 'cm' | 'ft';
@@ -50,7 +49,6 @@ const STORAGE_KEY = '@user_profile';
export default function EditProfileScreen() {
const colorScheme = useColorScheme();
const colors = Colors[colorScheme ?? 'light'];
const insets = useSafeAreaInsets();
const dispatch = useAppDispatch();
const accountProfile = useAppSelector((s) => (s as any)?.user?.profile as any);
const userId: string | undefined = useMemo(() => {

View File

@@ -1,8 +1,8 @@
import { Ionicons } from '@expo/vector-icons';
import MaskedView from '@react-native-masked-view/masked-view';
import { LinearGradient } from 'expo-linear-gradient';
import { useLocalSearchParams, useRouter } from 'expo-router';
import React, { useEffect, useMemo, useState } from 'react';
import { useFocusEffect, useLocalSearchParams, useRouter } from 'expo-router';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { Alert, FlatList, Modal, Pressable, SafeAreaView, ScrollView, StyleSheet, Switch, Text, TextInput, TouchableOpacity, View } from 'react-native';
import Animated, {
FadeInUp,
@@ -21,6 +21,7 @@ import { HeaderBar } from '@/components/ui/HeaderBar';
import { Colors, palette } from '@/constants/Colors';
import { useAppDispatch, useAppSelector } from '@/hooks/redux';
import { useColorScheme } from '@/hooks/useColorScheme';
import { TrainingPlan } from '@/services/trainingPlanApi';
import {
addExercise,
clearExercises,
@@ -29,7 +30,7 @@ import {
loadExercises,
toggleCompletion
} from '@/store/scheduleExerciseSlice';
import { activatePlan, clearError, deletePlan, loadPlans, type TrainingPlan } from '@/store/trainingPlanSlice';
import { activatePlan, clearError, deletePlan, loadPlans } from '@/store/trainingPlanSlice';
import { buildClassicalSession } from '@/utils/classicalSession';
// Tab 类型定义
@@ -248,13 +249,14 @@ export default function TrainingPlanScreen() {
const router = useRouter();
const dispatch = useAppDispatch();
const params = useLocalSearchParams<{ planId?: string; tab?: string }>();
const { plans, currentId, loading, error } = useAppSelector((s) => s.trainingPlan);
const { plans, loading, error } = useAppSelector((s) => s.trainingPlan);
const { exercises, error: scheduleError } = useAppSelector((s) => s.scheduleExercise);
console.log('plans', plans);
// Tab 状态管理 - 支持从URL参数设置初始tab
const initialTab: TabType = params.tab === 'schedule' ? 'schedule' : 'list';
const [activeTab, setActiveTab] = useState<TabType>(initialTab);
const [selectedPlanId, setSelectedPlanId] = useState<string | null>(params.planId || currentId || null);
const [selectedPlanId, setSelectedPlanId] = useState<string | null>(params.planId || null);
// 一键排课配置
const [genVisible, setGenVisible] = useState(false);
@@ -274,9 +276,21 @@ export default function TrainingPlanScreen() {
}
}, [selectedPlanId, dispatch]);
useEffect(() => {
dispatch(loadPlans());
}, [dispatch]);
// 每次页面获得焦点时,如果当前有选中的计划,重新加载其排课数据
useFocusEffect(
useCallback(() => {
if (selectedPlanId) {
dispatch(loadExercises(selectedPlanId));
}
}, [selectedPlanId, dispatch])
);
// 每次页面获得焦点时重新加载训练计划数据
useFocusEffect(
useCallback(() => {
dispatch(loadPlans());
}, [dispatch])
);
useEffect(() => {
if (error) {
@@ -314,7 +328,7 @@ export default function TrainingPlanScreen() {
const handleTabChange = (tab: TabType) => {
if (tab === 'schedule' && !selectedPlanId && plans.length > 0) {
// 如果没有选中计划但要切换到排课页面,自动选择当前激活的计划或第一个计划
const targetPlan = plans.find(p => p.id === currentId) || plans[0];
const targetPlan = plans.find(p => p.isActive) || plans[0];
setSelectedPlanId(targetPlan.id);
}
setActiveTab(tab);
@@ -426,7 +440,7 @@ export default function TrainingPlanScreen() {
key={p.id}
plan={p}
index={index}
isActive={p.id === currentId}
isActive={p.isActive}
onPress={() => handlePlanSelect(p)}
onDelete={() => dispatch(deletePlan(p.id))}
onActivate={() => handleActivate(p.id)}
@@ -1098,7 +1112,7 @@ const styles = StyleSheet.create({
// 主内容区域
mainContent: {
flex: 1,
paddingBottom: 100, // 为底部 tab 留出空间
paddingBottom: 60, // 为底部 tab 留出空间
},
// 排课页面样式

View File

@@ -8,6 +8,7 @@ import { ThemedView } from '@/components/ThemedView';
import { HeaderBar } from '@/components/ui/HeaderBar';
import { palette } from '@/constants/Colors';
import { useAppDispatch, useAppSelector } from '@/hooks/redux';
import { PlanGoal } from '@/services/trainingPlanApi';
import {
clearError,
loadPlans,
@@ -21,7 +22,6 @@ import {
setStartDateNextMonday,
setStartWeight,
toggleDayOfWeek,
type PlanGoal
} from '@/store/trainingPlanSlice';
const WEEK_DAYS = ['日', '一', '二', '三', '四', '五', '六'];

View File

@@ -1,3 +1,4 @@
import { useAuthGuard } from '@/hooks/useAuthGuard';
import {
BMI_CATEGORIES,
canCalculateBMI,
@@ -5,7 +6,6 @@ import {
type BMIResult
} from '@/utils/bmi';
import { Ionicons } from '@expo/vector-icons';
import { useRouter } from 'expo-router';
import React, { useState } from 'react';
import {
Modal,
@@ -16,6 +16,7 @@ import {
TouchableOpacity,
View,
} from 'react-native';
import Toast from 'react-native-toast-message';
interface BMICardProps {
weight?: number;
@@ -24,7 +25,7 @@ interface BMICardProps {
}
export function BMICard({ weight, height, style }: BMICardProps) {
const router = useRouter();
const { pushIfAuthedElseLogin } = useAuthGuard();
const [showInfoModal, setShowInfoModal] = useState(false);
const canCalculate = canCalculateBMI(weight, height);
@@ -41,7 +42,11 @@ export function BMICard({ weight, height, style }: BMICardProps) {
}
const handleGoToProfile = () => {
router.push('/profile/edit');
Toast.show({
text1: '请先登录',
type: 'info',
});
pushIfAuthedElseLogin('/profile/edit');
};
const handleShowInfoModal = () => {

View File

@@ -64,7 +64,7 @@ export function useAuthGuard() {
await dispatch(logoutAction()).unwrap();
// 跳转到登录页面
router.replace('/auth/login');
router.push('/auth/login');
} catch (error) {
console.error('退出登录失败:', error);
Alert.alert('错误', '退出登录失败,请稍后重试');
@@ -105,7 +105,7 @@ export function useAuthGuard() {
Alert.alert('账号已注销', '您的账号已成功注销', [
{
text: '确定',
onPress: () => router.replace('/auth/login'),
onPress: () => router.push('/auth/login'),
},
]);
} catch (error: any) {

View File

@@ -344,10 +344,14 @@
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_DEBUG";
PRODUCT_BUNDLE_IDENTIFIER = com.anonymous.digitalpilates;
PRODUCT_NAME = digitalpilates;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_OBJC_BRIDGING_HEADER = "digitalpilates/digitalpilates-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGETED_DEVICE_FAMILY = 1;
VERSIONING_SYSTEM = "apple-generic";
};
name = Debug;
@@ -377,9 +381,13 @@
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_RELEASE";
PRODUCT_BUNDLE_IDENTIFIER = com.anonymous.digitalpilates;
PRODUCT_NAME = digitalpilates;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_OBJC_BRIDGING_HEADER = "digitalpilates/digitalpilates-Bridging-Header.h";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGETED_DEVICE_FAMILY = 1;
VERSIONING_SYSTEM = "apple-generic";
};
name = Release;
@@ -439,10 +447,7 @@
LIBRARY_SEARCH_PATHS = "$(SDKROOT)/usr/lib/swift\"$(inherited)\"";
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
OTHER_LDFLAGS = "$(inherited) ";
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG";
@@ -497,10 +502,7 @@
);
LIBRARY_SEARCH_PATHS = "$(SDKROOT)/usr/lib/swift\"$(inherited)\"";
MTL_ENABLE_DEBUG_INFO = NO;
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
OTHER_LDFLAGS = "$(inherited) ";
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = false;

View File

@@ -1,91 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>普拉提助手</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>digitalpilates</string>
<string>com.anonymous.digitalpilates</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>2</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSMinimumSystemVersion</key>
<string>12.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
<key>NSCameraUsageDescription</key>
<string>应用需要使用相机以拍摄您的体态照片用于AI测评。</string>
<key>NSHealthShareUsageDescription</key>
<string>应用需要访问您的健康数据(步数与能量消耗)以展示运动统计。</string>
<key>NSHealthUpdateUsageDescription</key>
<string>Allow $(PRODUCT_NAME) to update health info</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>应用需要写入相册以保存拍摄的体态照片(可选)。</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>应用需要访问相册以选择您的体态照片用于AI测评。</string>
<key>NSUserActivityTypes</key>
<array>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
</array>
<key>UILaunchStoryboardName</key>
<string>SplashScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>arm64</string>
</array>
<key>UIRequiresFullScreen</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
<dict>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>普拉提助手</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>digitalpilates</string>
<string>com.anonymous.digitalpilates</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>2</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSMinimumSystemVersion</key>
<string>12.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
<key>NSCameraUsageDescription</key>
<string>应用需要使用相机以拍摄您的体态照片用于AI测评。</string>
<key>NSHealthShareUsageDescription</key>
<string>应用需要访问您的健康数据(步数与能量消耗)以展示运动统计。</string>
<key>NSHealthUpdateUsageDescription</key>
<string>Allow $(PRODUCT_NAME) to update health info</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>应用需要写入相册以保存拍摄的体态照片(可选)。</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>应用需要访问相册以选择您的体态照片用于AI测评。</string>
<key>NSUserActivityTypes</key>
<array>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
</array>
<key>UILaunchStoryboardName</key>
<string>SplashScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>arm64</string>
</array>
<key>UIRequiresFullScreen</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
</plist>

11
package-lock.json generated
View File

@@ -49,6 +49,7 @@
"react-native-safe-area-context": "5.4.0",
"react-native-screens": "~4.11.1",
"react-native-svg": "^15.12.1",
"react-native-toast-message": "^2.3.3",
"react-native-web": "~0.20.0",
"react-native-webview": "13.13.5",
"react-redux": "^9.2.0"
@@ -11227,6 +11228,16 @@
"react-native": "*"
}
},
"node_modules/react-native-toast-message": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/react-native-toast-message/-/react-native-toast-message-2.3.3.tgz",
"integrity": "sha512-4IIUHwUPvKHu4gjD0Vj2aGQzqPATiblL1ey8tOqsxOWRPGGu52iIbL8M/mCz4uyqecvPdIcMY38AfwRuUADfQQ==",
"license": "MIT",
"peerDependencies": {
"react": "*",
"react-native": "*"
}
},
"node_modules/react-native-uuid": {
"version": "2.0.3",
"resolved": "https://mirrors.tencent.com/npm/react-native-uuid/-/react-native-uuid-2.0.3.tgz",

View File

@@ -52,6 +52,7 @@
"react-native-safe-area-context": "5.4.0",
"react-native-screens": "~4.11.1",
"react-native-svg": "^15.12.1",
"react-native-toast-message": "^2.3.3",
"react-native-web": "~0.20.0",
"react-native-webview": "13.13.5",
"react-redux": "^9.2.0"

View File

@@ -11,56 +11,52 @@ export interface CreateTrainingPlanDto {
preferredTimeOfDay?: 'morning' | 'noon' | 'evening' | '';
}
export interface TrainingPlanResponse {
id: string;
userId: string;
name: string;
createdAt: string;
startDate: string;
mode: 'daysOfWeek' | 'sessionsPerWeek';
daysOfWeek: number[];
sessionsPerWeek: number;
goal: string;
startWeightKg: number | null;
preferredTimeOfDay: 'morning' | 'noon' | 'evening' | '';
updatedAt: string;
deleted: boolean;
}
export type PlanMode = 'daysOfWeek' | 'sessionsPerWeek';
export interface TrainingPlanSummary {
id: string;
createdAt: string;
startDate: string;
goal: string;
mode: 'daysOfWeek' | 'sessionsPerWeek';
daysOfWeek: number[];
sessionsPerWeek: number;
preferredTimeOfDay: 'morning' | 'noon' | 'evening' | '';
startWeightKg: number | null;
}
export type PlanGoal =
| 'postpartum_recovery' // 产后恢复
| 'fat_loss' // 减脂塑形
| 'posture_correction' // 体态矫正
| 'core_strength' // 核心力量
| 'flexibility' // 柔韧灵活
| 'rehab' // 康复保健
| 'stress_relief'; // 释压放松
export type TrainingPlan = {
id: string;
createdAt: string; // ISO
startDate: string; // ISO (当天或下周一)
mode: PlanMode;
daysOfWeek: number[]; // 0(日) - 6(六)
sessionsPerWeek: number; // 1..7
goal: PlanGoal | '';
startWeightKg?: number;
preferredTimeOfDay?: 'morning' | 'noon' | 'evening' | '';
name?: string;
isActive?: boolean;
};
export interface TrainingPlanListResponse {
list: TrainingPlanSummary[];
list: TrainingPlan[];
total: number;
page: number;
limit: number;
}
class TrainingPlanApi {
async create(dto: CreateTrainingPlanDto): Promise<TrainingPlanResponse> {
return api.post<TrainingPlanResponse>('/training-plans', dto);
async create(dto: CreateTrainingPlanDto): Promise<TrainingPlan> {
return api.post<TrainingPlan>('/training-plans', dto);
}
async list(page: number = 1, limit: number = 10): Promise<TrainingPlanListResponse> {
return api.get<TrainingPlanListResponse>(`/training-plans?page=${page}&limit=${limit}`);
}
async detail(id: string): Promise<TrainingPlanResponse> {
return api.get<TrainingPlanResponse>(`/training-plans/${id}`);
async detail(id: string): Promise<TrainingPlan> {
return api.get<TrainingPlan>(`/training-plans/${id}`);
}
async update(id: string, dto: CreateTrainingPlanDto): Promise<TrainingPlanResponse> {
return api.post<TrainingPlanResponse>(`/training-plans/${id}/update`, dto);
async update(id: string, dto: CreateTrainingPlanDto): Promise<TrainingPlan> {
return api.post<TrainingPlan>(`/training-plans/${id}/update`, dto);
}
async delete(id: string): Promise<{ success: boolean }> {
@@ -71,9 +67,9 @@ class TrainingPlanApi {
return api.post<{ success: boolean }>(`/training-plans/${id}/activate`);
}
async getActivePlan(): Promise<TrainingPlanResponse | null> {
async getActivePlan(): Promise<TrainingPlan | null> {
try {
return api.get<TrainingPlanResponse>('/training-plans/active');
return api.get<TrainingPlan>('/training-plans/active');
} catch (error) {
// 如果没有激活的计划返回null
return null;

View File

@@ -1,41 +1,17 @@
import { CreateTrainingPlanDto, trainingPlanApi, TrainingPlanSummary } from '@/services/trainingPlanApi';
import { CreateTrainingPlanDto, PlanGoal, PlanMode, TrainingPlan, trainingPlanApi } from '@/services/trainingPlanApi';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
export type PlanMode = 'daysOfWeek' | 'sessionsPerWeek';
export type PlanGoal =
| 'postpartum_recovery' // 产后恢复
| 'fat_loss' // 减脂塑形
| 'posture_correction' // 体态矫正
| 'core_strength' // 核心力量
| 'flexibility' // 柔韧灵活
| 'rehab' // 康复保健
| 'stress_relief'; // 释压放松
export type TrainingPlan = {
id: string;
createdAt: string; // ISO
startDate: string; // ISO (当天或下周一)
mode: PlanMode;
daysOfWeek: number[]; // 0(日) - 6(六)
sessionsPerWeek: number; // 1..7
goal: PlanGoal | '';
startWeightKg?: number;
preferredTimeOfDay?: 'morning' | 'noon' | 'evening' | '';
name?: string;
};
export type TrainingPlanState = {
plans: TrainingPlan[];
currentId?: string | null;
editingId?: string | null;
draft: Omit<TrainingPlan, 'id' | 'createdAt'>;
loading: boolean;
error: string | null;
};
const STORAGE_KEY_LEGACY_SINGLE = '@training_plan';
const STORAGE_KEY_LIST = '@training_plans';
function nextMondayISO(): string {
@@ -50,7 +26,6 @@ function nextMondayISO(): string {
const initialState: TrainingPlanState = {
plans: [],
currentId: null,
editingId: null,
loading: false,
error: null,
@@ -73,13 +48,9 @@ export const loadPlans = createAsyncThunk('trainingPlan/loadPlans', async (_, {
try {
// 尝试从服务器获取数据
const response = await trainingPlanApi.list(1, 100); // 获取所有计划
console.log('response', response);
const plans: TrainingPlanSummary[] = response.list;
const plans = response.list;
// 读取最后一次使用的 currentId从本地存储
const currentId = (await AsyncStorage.getItem(`${STORAGE_KEY_LIST}__currentId`)) || null;
return { plans, currentId } as { plans: TrainingPlan[]; currentId: string | null };
return { plans };
} catch (error: any) {
// 如果API调用失败回退到本地存储
console.warn('API调用失败使用本地存储:', error.message);
@@ -89,27 +60,13 @@ export const loadPlans = createAsyncThunk('trainingPlan/loadPlans', async (_, {
if (listStr) {
try {
const plans = JSON.parse(listStr) as TrainingPlan[];
const currentId = (await AsyncStorage.getItem(`${STORAGE_KEY_LIST}__currentId`)) || null;
return { plans, currentId } as { plans: TrainingPlan[]; currentId: string | null };
return { plans } as { plans: TrainingPlan[] };
} catch {
// 解析失败则视为无数据
}
}
// 旧版:单计划
const legacyStr = await AsyncStorage.getItem(STORAGE_KEY_LEGACY_SINGLE);
if (legacyStr) {
try {
const legacy = JSON.parse(legacyStr) as TrainingPlan;
const plans = [legacy];
const currentId = legacy.id;
return { plans, currentId } as { plans: TrainingPlan[]; currentId: string | null };
} catch {
// ignore
}
}
return { plans: [], currentId: null } as { plans: TrainingPlan[]; currentId: string | null };
return { plans: [] } as { plans: TrainingPlan[] };
}
});
@@ -134,28 +91,9 @@ export const saveDraftAsPlan = createAsyncThunk(
preferredTimeOfDay: draft.preferredTimeOfDay,
};
const response = await trainingPlanApi.create(createDto);
const newPlan = await trainingPlanApi.create(createDto);
const plan: TrainingPlan = {
id: response.id,
createdAt: response.createdAt,
startDate: response.startDate,
mode: response.mode,
daysOfWeek: response.daysOfWeek,
sessionsPerWeek: response.sessionsPerWeek,
goal: response.goal as PlanGoal,
startWeightKg: response.startWeightKg || undefined,
preferredTimeOfDay: response.preferredTimeOfDay,
name: response.name,
};
const nextPlans = [...(s.plans || []), plan];
// 同时保存到本地存储作为缓存
await AsyncStorage.setItem(STORAGE_KEY_LIST, JSON.stringify(nextPlans));
await AsyncStorage.setItem(`${STORAGE_KEY_LIST}__currentId`, plan.id);
return { plans: nextPlans, currentId: plan.id } as { plans: TrainingPlan[]; currentId: string };
return newPlan;
} catch (error: any) {
return rejectWithValue(error.message || '创建训练计划失败');
}
@@ -257,16 +195,11 @@ export const deletePlan = createAsyncThunk(
// 更新本地状态
const nextPlans = (s.plans || []).filter((p) => p.id !== planId);
let nextCurrentId = s.currentId || null;
if (nextCurrentId === planId) {
nextCurrentId = nextPlans.length > 0 ? nextPlans[nextPlans.length - 1].id : null;
}
// 同时更新本地存储
await AsyncStorage.setItem(STORAGE_KEY_LIST, JSON.stringify(nextPlans));
await AsyncStorage.setItem(`${STORAGE_KEY_LIST}__currentId`, nextCurrentId ?? '');
return { plans: nextPlans, currentId: nextCurrentId } as { plans: TrainingPlan[]; currentId: string | null };
return { plans: nextPlans } as { plans: TrainingPlan[] };
} catch (error: any) {
return rejectWithValue(error.message || '删除训练计划失败');
}
@@ -277,11 +210,6 @@ const trainingPlanSlice = createSlice({
name: 'trainingPlan',
initialState,
reducers: {
setCurrentPlan(state, action: PayloadAction<string | null | undefined>) {
state.currentId = action.payload ?? null;
// 保存到本地存储
AsyncStorage.setItem(`${STORAGE_KEY_LIST}__currentId`, action.payload ?? '');
},
setMode(state, action: PayloadAction<PlanMode>) {
state.draft.mode = action.payload;
},
@@ -333,13 +261,6 @@ const trainingPlanSlice = createSlice({
.addCase(loadPlans.fulfilled, (state, action) => {
state.loading = false;
state.plans = action.payload.plans;
state.currentId = action.payload.currentId;
// 若存在历史计划,初始化 draft 基于该计划(便于编辑)
const current = state.plans.find((p) => p.id === state.currentId) || state.plans[state.plans.length - 1];
if (current) {
const { id, createdAt, ...rest } = current;
state.draft = { ...rest };
}
})
.addCase(loadPlans.rejected, (state, action) => {
state.loading = false;
@@ -352,8 +273,6 @@ const trainingPlanSlice = createSlice({
})
.addCase(saveDraftAsPlan.fulfilled, (state, action) => {
state.loading = false;
state.plans = action.payload.plans;
state.currentId = action.payload.currentId;
})
.addCase(saveDraftAsPlan.rejected, (state, action) => {
state.loading = false;
@@ -393,9 +312,6 @@ const trainingPlanSlice = createSlice({
})
.addCase(activatePlan.fulfilled, (state, action) => {
state.loading = false;
state.currentId = action.payload.id;
// 保存到本地存储
AsyncStorage.setItem(`${STORAGE_KEY_LIST}__currentId`, action.payload.id);
})
.addCase(activatePlan.rejected, (state, action) => {
state.loading = false;
@@ -409,7 +325,6 @@ const trainingPlanSlice = createSlice({
.addCase(deletePlan.fulfilled, (state, action) => {
state.loading = false;
state.plans = action.payload.plans;
state.currentId = action.payload.currentId;
})
.addCase(deletePlan.rejected, (state, action) => {
state.loading = false;
@@ -419,7 +334,6 @@ const trainingPlanSlice = createSlice({
});
export const {
setCurrentPlan,
setMode,
toggleDayOfWeek,
setSessionsPerWeek,