- 在 AI 教练聊天界面中添加会话缓存功能,支持冷启动时恢复聊天记录 - 实现轻量防抖机制,确保会话变动时及时保存缓存 - 在打卡功能中集成按月加载打卡记录,提升用户体验 - 更新 Redux 状态管理,支持打卡记录的按月加载和缓存 - 新增打卡日历页面,允许用户查看每日打卡记录 - 优化样式以适应新功能的展示和交互
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
|
|
export type AiCoachChatMessage = {
|
|
id: string;
|
|
role: 'user' | 'assistant';
|
|
content: string;
|
|
};
|
|
|
|
export type AiCoachSessionCache = {
|
|
conversationId?: string;
|
|
messages: AiCoachChatMessage[];
|
|
updatedAt: number;
|
|
};
|
|
|
|
const STORAGE_KEY = '@ai_coach_session_v1';
|
|
|
|
export async function loadAiCoachSessionCache(): Promise<AiCoachSessionCache | null> {
|
|
try {
|
|
const s = await AsyncStorage.getItem(STORAGE_KEY);
|
|
if (!s) return null;
|
|
const obj = JSON.parse(s) as AiCoachSessionCache;
|
|
if (!obj || !Array.isArray(obj.messages)) return null;
|
|
return obj;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function saveAiCoachSessionCache(cache: AiCoachSessionCache): Promise<void> {
|
|
try {
|
|
const payload: AiCoachSessionCache = {
|
|
conversationId: cache.conversationId,
|
|
messages: cache.messages?.slice?.(-200) ?? [], // 限制最多缓存 200 条,避免无限增长
|
|
updatedAt: Date.now(),
|
|
};
|
|
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(payload));
|
|
} catch { }
|
|
}
|
|
|
|
export async function clearAiCoachSessionCache(): Promise<void> {
|
|
try {
|
|
await AsyncStorage.removeItem(STORAGE_KEY);
|
|
} catch { }
|
|
}
|
|
|
|
|