发布时间:2026/7/18 23:00:09
及时做APP开发实战(十五)-专注历史记录功能实现 即时做APP开发实战(十五) - 专注历史记录功能实现本文将介绍如何实现专注历史记录功能包括按日期分组显示和数据统计等。一、功能需求1.1 需求分析【图1历史记录弹窗界面】┌─────────────────────────────────────┐ │ 专注历史记录 关闭 │ ├─────────────────────────────────────┤ │ ┌─────────────────────────────┐ │ │ │ 今天 共 50 分钟 │ │ │ │ ┌─────────────────────┐ │ │ │ │ │ 09:30 25分钟 学习ArkTS │ │ │ │ │ │ 10:00 25分钟 开发 App │ │ │ │ │ └─────────────────────┘ │ │ │ └─────────────────────────────┘ │ │ │ │ ┌─────────────────────────────┐ │ │ │ 昨天 共 75 分钟 │ │ │ │ ┌─────────────────────┐ │ │ │ │ │ 14:00 25分钟 撰写博客 │ │ │ │ │ │ 15:00 25分钟 代码优化 │ │ │ │ │ │ 16:00 25分钟 功能测试 │ │ │ │ │ └─────────────────────────────┘ │ │ │ └─────────────────────────────┘ │ └─────────────────────────────────────┘1.2 功能点功能说明历史记录显示显示最近7天的专注记录按日期分组今日、昨日、具体日期统计总时长每天的总专注时长记录详情时间、时长、任务名称二、数据模型设计2.1 PomodoroRecord模型【图2历史记录详情界面】exportinterfacePomodoroRecord{id:number;// 记录IDtodoId:number;// 关联的待办IDduration:number;// 专注时长分钟completedAt:number;// 完成时间戳}exportfunctioncreatePomodoroRecord(todoId:number,duration:number):PomodoroRecord{return{id:Date.now(),todoId:todoId,duration:duration,completedAt:Date.now()};}2.2 数据存储结构┌─────────────────────────────────────┐ │ 数据存储结构 │ ├─────────────────────────────────────┤ │ Preferences 存储 │ │ { │ │ pomodoroRecords: [ │ │ { │ │ id: 1703001600000, │ │ todoId: 1, │ │ duration: 25, │ │ completedAt: 1703001600000 │ │ }, │ │ { │ │ id: 1703003100000, │ │ todoId: 2, │ │ duration: 25, │ │ completedAt: 1703003100000 │ │ } │ │ ] │ │ } │ └─────────────────────────────────────┘三、ViewModel 实现3.1 获取指定日期记录/** * 获取指定日期的番茄钟记录 */getRecordsByDate(date:Date):PomodoroRecord[]{conststartOfDaynewDate(date);startOfDay.setHours(0,0,0,0);constendOfDaynewDate(date);endOfDay.setHours(23,59,59,999);returnthis.records.filter(recordrecord.completedAtstartOfDay.getTime()record.completedAtendOfDay.getTime());}3.2 获取最近 N 天历史记录/** * 获取最近 N 天的历史记录按日期分组 */getRecentHistory(days:number7):Mapstring,PomodoroRecord[]{consthistorynewMapstring,PomodoroRecord[]();consttodaynewDate();for(leti0;idays;i){constdatenewDate(today);date.setDate(date.getDate()-i);constdateStrthis.formatDate(date);constrecordsthis.getRecordsByDate(date);if(records.length0){history.set(dateStr,records);}}returnhistory;}3.3 日期显示文本/** * 获取日期的显示文本 */getDateDisplayText(dateStr:string):string{consttodaynewDate();consttodayStrthis.formatDate(today);constyesterdaynewDate(today);yesterday.setDate(yesterday.getDate()-1);constyesterdayStrthis.formatDate(yesterday);if(dateStrtodayStr){return今天;}elseif(dateStryesterdayStr){return昨天;}else{returndateStr;}}四、UI 实现4.1 历史记录弹窗BuilderHistorySheet(){Column(){// 标题栏Row(){Text(专注历史).fontSize(20).fontWeight(FontWeight.Bold)Blank()Button(关闭).onClick((){this.showHistoryfalse;})}.width(100%).padding(20)Divider()// 历史记录列表if(this.pomodoroViewModel.getRecords().length0){this.EmptyState()}else{List({space:10}){ForEach(Array.from(this.pomodoroViewModel.getRecentHistory(7).entries()),(entry:[string,PomodoroRecord[]]){ListItem(){this.HistoryDateItem(entry[0],entry[1])}},(entry:[string,PomodoroRecord[]])entry[0])}.width(100%).layoutWeight(1).padding(15)}}.width(100%).height(100%)}4.2 日期分组项BuilderHistoryDateItem(dateStr:string,records:PomodoroRecord[]){Column(){// 日期标题Row(){Text(this.pomodoroViewModel.getDateDisplayText(dateStr)).fontSize(16).fontWeight(FontWeight.Bold)Blank()// 计算总时长Text(总计${this.getTotalMinutes(records)}分钟).fontSize(14).fontColor(this.themeColors.primary)}.width(100%).margin({bottom:10})// 记录列表Column({space:8}){ForEach(records,(record:PomodoroRecord){this.RecordItem(record)})}.width(100%)}.width(100%).padding(15).backgroundColor(#FFFFFF).borderRadius(12)}4.3 单条记录项BuilderRecordItem(record:PomodoroRecord){Row(){// 时间Text(this.formatRecordTime(record.completedAt)).fontSize(14).fontColor(#666666).width(60)// 时长Text(${record.duration}分钟).fontSize(14).fontColor(this.themeColors.primary).width(60)// 待办事项名称Text(this.getTodoName(record.todoId)).fontSize(14).fontColor(#333333).layoutWeight(1).maxLines(1).textOverflow({overflow:TextOverflow.Ellipsis})}.width(100%).padding(10).backgroundColor(#F5F5F5).borderRadius(8)}4.4 辅助方法// 将时间戳格式化为时间字符串privateformatRecordTime(timestamp:number):string{constdatenewDate(timestamp);consthoursdate.getHours().toString().padStart(2,0);constminutesdate.getMinutes().toString().padStart(2,0);return${hours}:${minutes};}// 获取待办事项名称privategetTodoName(todoId:number):string{consttodothis.todoList.find(itemitem.idtodoId);returntodo?todo.text:未知事项;}// 计算总时长privategetTotalMinutes(records:PomodoroRecord[]):number{returnrecords.reduce((sum,r)sumr.duration,0);}五、入口按钮5.1 顶部导航栏按钮添加BuilderTopBar(){Row(){Text( 番茄钟).fontSize(24).fontWeight(FontWeight.Bold).fontColor(#FFFFFF)Blank()// 历史记录按钮Button({type:ButtonType.Circle}){Text().fontSize(20)}.width(36).height(36).backgroundColor(rgba(255, 255, 255, 0.2)).onClick((){this.showHistorytrue;})Text(${this.pomodoroCount}/ 4).fontSize(16).fontColor(#FFFFFF).margin({left:10})}.width(100%).height(56).padding({left:20,right:20}).backgroundColor(this.themeColors.primary)}5.绑定模态弹窗弹窗build(){Column(){// 页面内容...}.bindSheet($$this.showHistory,this.HistorySheet(),{height:500,backgroundColor:Color.White,dragBar:true,onWillDismiss:(){this.showHistoryfalse;}})}六、数据流程图┌─────────────────────────────────────┐ │ 数据流程 │ ├─────────────────────────────────────│ 1. 用户完成一个番茄钟 │ │ │ ↓ │ │ 2. 调用completePomodoro() │ │ ↓ │ │ 3. 创建PomodoroRecord │ │ ↓ │ │ 4. 保存到Preferences │ │ ↓ │ 5. 用户点击历史记录按钮 │ │ │ ↓ │ │ 6. 调用getRecentHistory(7) │ │ ↓ │ │ 7. 按日期分组返回数据 │ │ ↓ │ 8. UI 渲染历史记录列表 │ │ └─────────────────────────────────────┘七、总结本文实现了专注历史记录功能其核心要点如下点数据存储使用Preferences持久化番茄钟记2.按日期分组通过时间戳进行过滤和分组分3.友好显示以“今天”、“昨天”及具体日期等形式呈现日期统计功能每天总专注时长

相关新闻

2026/7/18 22:55:08

AI 辅助 Rust 性能优化:让模型分析 criterion 基准测试报告

AI 辅助 Rust 性能优化:让模型分析 criterion 基准测试报告专栏: AI / AI学习 / Rust性能优化一、criterion 报告对人类的"不友好"与 AI 的机会 cargo bench 跑完之后,criterion 会在 target/criterion/ 下生成一堆 HTML 报告和 JSON 数据。数…

2026/7/18 22:55:08

AI Agent 入门(一):OpenClaw 与 Hermes(openclaw输出篇)

🦞 AI Agent 入门(一):OpenClaw 与 Hermes《AI Agent 对比系列》第一篇 本系列通过对比两个真实开源 AI Agent——OpenClaw(🦞)和 Hermes——带你深入理解 AI Agent 是什么、怎么工作、能做什么…

2026/7/19 0:40:19

CPU 缓存友好编程:从数据布局到访问模式的性能影响实测

CPU 缓存友好编程:从数据布局到访问模式的性能影响实测 一、同样的 O(n) 算法,为什么一个比另一个快 5 倍 一道在二维数组上做遍历的算法题,两个人都写出了 O(n*m) 的解法。逻辑一模一样,但一个的运行时间是 12ms,另一…

2026/7/19 0:40:19

分块思想在算法中的工程化:平方分割与莫队算法的实现要诀

分块思想在算法中的工程化:平方分割与莫队算法的实现要诀 一、区间查询问题,线段树不是唯一答案 线段树是处理区间查询的经典数据结构,单次查询 O(log n),功能强大。但它的实现代码量不小——建树、更新、查询,三个递归…

2026/7/19 0:40:19

AI 工具的用户反馈闭环:从隐性信号到模型优化

AI 工具的用户反馈闭环:从隐性信号到模型优化 一、用户反馈不只是「好评」和「差评」 独立产品的用户反馈,传统形式是评分(1-5星)或评论。对于 AI 工具,这些显式反馈(用户主动给出的评价)有价值,但其覆盖率通常不到 1%——绝大多数用户不会主动评价。如果只依赖显…

2026/7/19 0:40:19

AI 任务的优先级调度:不同用户、不同任务的资源分配

AI 任务的优先级调度:不同用户、不同任务的资源分配 一、当 AI 调用开始排队 产品在成长期,AI 调用量不再是「即来即处理」。在高并发时刻(如工作时间、产品推广期),AI API 的请求可能会出现排队——用户的请求发出了,但需要等待前面的请求处理完才能轮到。 如果所有…

2026/7/19 0:35:19

【AI问数】大模型选型与微调:AI问数的LLM落地实战

4 大选型维度 多模型 路由策略 99.97% 可用性 Spider 权威Benchmark AI问数对LLM的选型要求:中文理解强、SQL生成能力突出(Spider/Bird跑分)、上下文窗口足够(16K)、支持私有化部署。鲲溟KM AI采用多模型路由:主模型专用模型备用模型三层配置。 一…

2026/7/19 0:00:15

Unity与Python本地通信:基于Flask的跨语言数据交换实战

1. 项目概述:为什么我们需要一个本地通信服务器?在游戏开发、数字孪生、仿真训练等众多领域,Unity作为强大的实时3D内容创作平台,其核心逻辑通常由C#驱动。然而,当我们需要进行复杂的数据分析、机器学习推理、科学计算…

2026/7/19 0:00:15

Unity与Python本地通信:基于Flask的跨语言数据交换实战

1. 项目概述:为什么我们需要一个本地通信服务器?在游戏开发、数字孪生、仿真训练等众多领域,Unity作为强大的实时3D内容创作平台,其核心逻辑通常由C#驱动。然而,当我们需要进行复杂的数据分析、机器学习推理、科学计算…

2026/7/18 16:50:29

3个高效策略:快速掌握Axure中文界面配置

3个高效策略:快速掌握Axure中文界面配置 【免费下载链接】axure-cn Chinese language file for Axure RP. Axure RP 简体中文语言包。支持 Axure 11、10、9。不定期更新。 项目地址: https://gitcode.com/gh_mirrors/ax/axure-cn 还在为Axure RP的英文界面感…