feat(workout): 新增锻炼结束监听和个性化通知功能
实现了iOS HealthKit锻炼数据实时监听,当用户完成锻炼时自动发送个性化鼓励通知。包括锻炼类型筛选、时间范围控制、用户偏好设置等完整功能,并提供了测试工具和详细文档。
This commit is contained in:
198
utils/workoutTestHelper.ts
Normal file
198
utils/workoutTestHelper.ts
Normal file
@@ -0,0 +1,198 @@
|
||||
import { workoutMonitorService } from '@/services/workoutMonitor';
|
||||
import { WorkoutData } from '@/utils/health';
|
||||
|
||||
/**
|
||||
* 锻炼测试工具
|
||||
* 用于开发和测试锻炼监听功能
|
||||
*/
|
||||
export class WorkoutTestHelper {
|
||||
/**
|
||||
* 模拟一个锻炼完成事件
|
||||
*/
|
||||
static async simulateWorkoutCompletion(): Promise<void> {
|
||||
console.log('=== 开始模拟锻炼完成事件 ===');
|
||||
|
||||
try {
|
||||
// 手动触发锻炼检查
|
||||
await workoutMonitorService.manualCheck();
|
||||
console.log('✅ 锻炼检查已手动触发');
|
||||
} catch (error) {
|
||||
console.error('❌ 模拟锻炼完成事件失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取锻炼监听服务状态
|
||||
*/
|
||||
static getWorkoutMonitorStatus(): any {
|
||||
return workoutMonitorService.getStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建测试用的模拟锻炼数据
|
||||
*/
|
||||
static createMockWorkout(type: string = 'running'): WorkoutData {
|
||||
const now = new Date();
|
||||
const duration = 30 * 60; // 30分钟
|
||||
const startTime = new Date(now.getTime() - duration * 1000);
|
||||
|
||||
const workoutTypes: Record<string, number> = {
|
||||
running: 37,
|
||||
cycling: 13,
|
||||
swimming: 46,
|
||||
yoga: 57,
|
||||
functionalstrengthtraining: 20,
|
||||
traditionalstrengthtraining: 50,
|
||||
highintensityintervaltraining: 63,
|
||||
walking: 52,
|
||||
};
|
||||
|
||||
return {
|
||||
id: `mock-workout-${Date.now()}`,
|
||||
startDate: startTime.toISOString(),
|
||||
endDate: now.toISOString(),
|
||||
duration: duration,
|
||||
workoutActivityType: workoutTypes[type] || 37,
|
||||
workoutActivityTypeString: type,
|
||||
totalEnergyBurned: Math.round(Math.random() * 300 + 100), // 100-400千卡
|
||||
totalDistance: type === 'running' || type === 'cycling' ? Math.round(Math.random() * 10000 + 1000) : undefined,
|
||||
averageHeartRate: Math.round(Math.random() * 50 + 120), // 120-170次/分
|
||||
source: {
|
||||
name: 'Test App',
|
||||
bundleIdentifier: 'com.test.app'
|
||||
},
|
||||
metadata: {
|
||||
HKAverageMETs: Math.random() * 10 + 5 // 5-15 METs
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试不同类型的锻炼通知
|
||||
*/
|
||||
static async testWorkoutNotifications(): Promise<void> {
|
||||
console.log('=== 开始测试不同类型锻炼通知 ===');
|
||||
|
||||
const workoutTypes = ['running', 'cycling', 'swimming', 'yoga', 'functionalstrengthtraining', 'highintensityintervaltraining'];
|
||||
|
||||
for (const type of workoutTypes) {
|
||||
console.log(`--- 测试 ${type} 锻炼通知 ---`);
|
||||
|
||||
try {
|
||||
// 这里需要导入通知服务来直接测试
|
||||
const { analyzeWorkoutAndSendNotification } = await import('@/services/workoutNotificationService');
|
||||
const mockWorkout = this.createMockWorkout(type);
|
||||
|
||||
await analyzeWorkoutAndSendNotification(mockWorkout);
|
||||
|
||||
console.log(`✅ ${type} 锻炼通知测试成功`);
|
||||
|
||||
// 等待一段时间再测试下一个
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
} catch (error) {
|
||||
console.error(`❌ ${type} 锻炼通知测试失败:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('=== 锻炼通知测试完成 ===');
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试偏好设置功能
|
||||
*/
|
||||
static async testPreferences(): Promise<void> {
|
||||
console.log('=== 开始测试偏好设置功能 ===');
|
||||
|
||||
try {
|
||||
const {
|
||||
getWorkoutNotificationPreferences,
|
||||
saveWorkoutNotificationPreferences,
|
||||
isNotificationTimeAllowed,
|
||||
isWorkoutTypeEnabled
|
||||
} = await import('@/utils/workoutPreferences');
|
||||
|
||||
// 获取当前设置
|
||||
const currentPrefs = await getWorkoutNotificationPreferences();
|
||||
console.log('当前偏好设置:', currentPrefs);
|
||||
|
||||
// 测试时间检查
|
||||
const timeAllowed = await isNotificationTimeAllowed();
|
||||
console.log('当前时间是否允许通知:', timeAllowed);
|
||||
|
||||
// 测试锻炼类型检查
|
||||
const runningEnabled = await isWorkoutTypeEnabled('running');
|
||||
console.log('跑步通知是否启用:', runningEnabled);
|
||||
|
||||
// 临时修改设置
|
||||
await saveWorkoutNotificationPreferences({
|
||||
enabled: true,
|
||||
startTimeHour: 9,
|
||||
endTimeHour: 21,
|
||||
enabledWorkoutTypes: ['running', 'cycling']
|
||||
});
|
||||
|
||||
console.log('临时设置已保存');
|
||||
|
||||
// 恢复原始设置
|
||||
await saveWorkoutNotificationPreferences(currentPrefs);
|
||||
console.log('原始设置已恢复');
|
||||
|
||||
console.log('✅ 偏好设置功能测试完成');
|
||||
} catch (error) {
|
||||
console.error('❌ 偏好设置功能测试失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行完整的测试套件
|
||||
*/
|
||||
static async runFullTestSuite(): Promise<void> {
|
||||
console.log('🧪 开始运行锻炼监听功能完整测试套件');
|
||||
|
||||
try {
|
||||
// 1. 检查服务状态
|
||||
console.log('\n1. 检查服务状态...');
|
||||
const status = this.getWorkoutMonitorStatus();
|
||||
console.log('服务状态:', status);
|
||||
|
||||
// 2. 测试偏好设置
|
||||
console.log('\n2. 测试偏好设置...');
|
||||
await this.testPreferences();
|
||||
|
||||
// 3. 测试通知功能
|
||||
console.log('\n3. 测试通知功能...');
|
||||
await this.testWorkoutNotifications();
|
||||
|
||||
// 4. 测试手动触发
|
||||
console.log('\n4. 测试手动触发...');
|
||||
await this.simulateWorkoutCompletion();
|
||||
|
||||
console.log('\n🎉 完整测试套件运行完成!');
|
||||
} catch (error) {
|
||||
console.error('\n❌ 测试套件运行失败:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开发者调试函数
|
||||
* 可以在开发者控制台中调用
|
||||
*/
|
||||
declare global {
|
||||
interface Window {
|
||||
testWorkoutNotifications: () => Promise<void>;
|
||||
testWorkoutPreferences: () => Promise<void>;
|
||||
simulateWorkoutCompletion: () => Promise<void>;
|
||||
runWorkoutTestSuite: () => Promise<void>;
|
||||
}
|
||||
}
|
||||
|
||||
// 在开发环境中暴露调试函数
|
||||
if (__DEV__) {
|
||||
// 这些函数可以在开发者控制台中调用
|
||||
// 例如: window.testWorkoutNotifications()
|
||||
|
||||
// 注意:这些函数需要在实际运行环境中绑定
|
||||
// 可以在应用的初始化代码中添加:
|
||||
// window.testWorkoutNotifications = WorkoutTestHelper.testWorkoutNotifications;
|
||||
}
|
||||
Reference in New Issue
Block a user