|
|
@@ -1,4 +1,4 @@
|
|
|
-import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
|
|
|
+import { Component, CUSTOM_ELEMENTS_SCHEMA, OnInit } from '@angular/core';
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
import { FormsModule } from '@angular/forms';
|
|
|
import { IonicModule } from '@ionic/angular';
|
|
|
@@ -16,9 +16,14 @@ import {
|
|
|
languageOutline,
|
|
|
pencilOutline,
|
|
|
trashOutline,
|
|
|
- addOutline
|
|
|
+ addOutline,
|
|
|
+ arrowBackOutline,
|
|
|
+ pauseOutline,
|
|
|
+ playOutline,
|
|
|
+ stopOutline
|
|
|
} from 'ionicons/icons';
|
|
|
import { AlertController } from '@ionic/angular';
|
|
|
+import { FocusDataService } from '../services/focus-data.service';
|
|
|
|
|
|
interface TimerRecord {
|
|
|
id: number;
|
|
|
@@ -26,6 +31,7 @@ interface TimerRecord {
|
|
|
startTime: string;
|
|
|
endTime: string;
|
|
|
duration: number;
|
|
|
+ parseId?: string;
|
|
|
}
|
|
|
|
|
|
@Component({
|
|
|
@@ -36,7 +42,7 @@ interface TimerRecord {
|
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA],
|
|
|
imports: [IonicModule, CommonModule, FormsModule]
|
|
|
})
|
|
|
-export class Tab2Page {
|
|
|
+export class Tab2Page implements OnInit {
|
|
|
activityTypes = [
|
|
|
{ id: 'study', name: '学习', icon: 'school-outline' },
|
|
|
{ id: 'work', name: '工作', icon: 'briefcase-outline' },
|
|
|
@@ -56,31 +62,51 @@ export class Tab2Page {
|
|
|
currentTime: string = '00:00:00';
|
|
|
records: TimerRecord[] = [];
|
|
|
timer: any;
|
|
|
- countdownMinutes: number = 25; // 默认25分钟
|
|
|
+ countdownMinutes: number = 25;
|
|
|
|
|
|
constructor(
|
|
|
private router: Router,
|
|
|
- private alertController: AlertController
|
|
|
+ private alertController: AlertController,
|
|
|
+ private focusDataService: FocusDataService
|
|
|
) {
|
|
|
- // 注册图标
|
|
|
addIcons({
|
|
|
- 'school-outline': schoolOutline,
|
|
|
- 'briefcase-outline': briefcaseOutline,
|
|
|
- 'moon-outline': moonOutline,
|
|
|
- 'barbell-outline': barbellOutline,
|
|
|
- 'book-outline': bookOutline,
|
|
|
- 'code-slash-outline': codeSlashOutline,
|
|
|
- 'leaf-outline': leafOutline,
|
|
|
- 'musical-notes-outline': musicalNotesOutline,
|
|
|
- 'language-outline': languageOutline,
|
|
|
- 'pencil-outline': pencilOutline,
|
|
|
- 'trash-outline': trashOutline,
|
|
|
- 'add-outline': addOutline
|
|
|
+ schoolOutline,
|
|
|
+ briefcaseOutline,
|
|
|
+ moonOutline,
|
|
|
+ barbellOutline,
|
|
|
+ bookOutline,
|
|
|
+ codeSlashOutline,
|
|
|
+ leafOutline,
|
|
|
+ musicalNotesOutline,
|
|
|
+ languageOutline,
|
|
|
+ pencilOutline,
|
|
|
+ trashOutline,
|
|
|
+ addOutline,
|
|
|
+ arrowBackOutline,
|
|
|
+ pauseOutline,
|
|
|
+ playOutline,
|
|
|
+ stopOutline
|
|
|
});
|
|
|
+ }
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ this.loadRecords();
|
|
|
+ }
|
|
|
|
|
|
- const savedRecords = localStorage.getItem('timerRecords');
|
|
|
- if (savedRecords) {
|
|
|
- this.records = JSON.parse(savedRecords);
|
|
|
+ async loadRecords() {
|
|
|
+ try {
|
|
|
+ const records = await this.focusDataService.getFocusRecords();
|
|
|
+ this.records = records.map(record => ({
|
|
|
+ id: Date.now(),
|
|
|
+ type: record.category,
|
|
|
+ startTime: record.date.toISOString(),
|
|
|
+ endTime: record.date.toISOString(),
|
|
|
+ duration: record.duration,
|
|
|
+ parseId: record.id
|
|
|
+ }));
|
|
|
+ } catch (error) {
|
|
|
+ console.error('加载记录失败:', error);
|
|
|
+ this.showErrorAlert('加载记录失败');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -108,22 +134,34 @@ export class Tab2Page {
|
|
|
}, 1000);
|
|
|
}
|
|
|
|
|
|
- stopTimer() {
|
|
|
+ async stopTimer() {
|
|
|
if (this.timer) {
|
|
|
clearInterval(this.timer);
|
|
|
const endTime = new Date();
|
|
|
- const duration = Math.floor((endTime.getTime() - this.timerStartTime!.getTime()) / 60000); // 转换为分钟
|
|
|
+ const duration = Math.floor((endTime.getTime() - this.timerStartTime!.getTime()) / 60000);
|
|
|
|
|
|
- const record: TimerRecord = {
|
|
|
- id: Date.now(),
|
|
|
- type: this.selectedType,
|
|
|
- startTime: this.timerStartTime!.toISOString(),
|
|
|
- endTime: endTime.toISOString(),
|
|
|
- duration: duration
|
|
|
- };
|
|
|
-
|
|
|
- this.records.unshift(record);
|
|
|
- this.saveRecords();
|
|
|
+ try {
|
|
|
+ const savedRecord = await this.focusDataService.addFocusRecord({
|
|
|
+ duration: duration,
|
|
|
+ category: this.selectedType,
|
|
|
+ startTime: this.timerStartTime!,
|
|
|
+ endTime: endTime
|
|
|
+ });
|
|
|
+
|
|
|
+ const record: TimerRecord = {
|
|
|
+ id: Date.now(),
|
|
|
+ type: this.selectedType,
|
|
|
+ startTime: this.timerStartTime!.toISOString(),
|
|
|
+ endTime: endTime.toISOString(),
|
|
|
+ duration: duration,
|
|
|
+ parseId: savedRecord.id
|
|
|
+ };
|
|
|
+
|
|
|
+ this.records.unshift(record);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('保存专注记录失败:', error);
|
|
|
+ this.showErrorAlert('保存记录失败');
|
|
|
+ }
|
|
|
|
|
|
this.isTimerRunning = false;
|
|
|
this.timerStartTime = null;
|
|
|
@@ -139,17 +177,12 @@ export class Tab2Page {
|
|
|
|
|
|
const selectedActivity = this.activityTypes.find(t => t.id === this.selectedType);
|
|
|
if (selectedActivity) {
|
|
|
- console.log('Navigating to countdown with params:', {
|
|
|
- type: selectedActivity.icon,
|
|
|
- name: selectedActivity.name,
|
|
|
- duration: this.countdownMinutes
|
|
|
- });
|
|
|
-
|
|
|
- this.router.navigate(['/tabs/countdown'], {
|
|
|
+ this.router.navigate(['/countdown'], {
|
|
|
queryParams: {
|
|
|
type: selectedActivity.icon,
|
|
|
name: selectedActivity.name,
|
|
|
- duration: this.countdownMinutes
|
|
|
+ duration: this.countdownMinutes,
|
|
|
+ category: selectedActivity.id
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@@ -159,10 +192,6 @@ export class Tab2Page {
|
|
|
return num.toString().padStart(2, '0');
|
|
|
}
|
|
|
|
|
|
- private saveRecords() {
|
|
|
- localStorage.setItem('timerRecords', JSON.stringify(this.records));
|
|
|
- }
|
|
|
-
|
|
|
getTypeName(typeId: string): string {
|
|
|
const type = this.activityTypes.find(t => t.id === typeId);
|
|
|
return type ? type.name : '';
|
|
|
@@ -180,7 +209,11 @@ export class Tab2Page {
|
|
|
}
|
|
|
|
|
|
async deleteRecord(index: number) {
|
|
|
- // 创建一个警告对话框
|
|
|
+ const record = this.records[index];
|
|
|
+ if (!record.parseId) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
const alert = await this.alertController.create({
|
|
|
header: '确认删除',
|
|
|
message: '你确定要删除这条记录吗?',
|
|
|
@@ -192,9 +225,14 @@ export class Tab2Page {
|
|
|
{
|
|
|
text: '删除',
|
|
|
role: 'destructive',
|
|
|
- handler: () => {
|
|
|
- // 从数组中删除指定索引的记录
|
|
|
- this.records.splice(index, 1);
|
|
|
+ handler: async () => {
|
|
|
+ try {
|
|
|
+ await this.focusDataService.deleteFocusRecord(record.parseId!);
|
|
|
+ this.records.splice(index, 1);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('删除记录失败:', error);
|
|
|
+ this.showErrorAlert('删除记录失败');
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
]
|
|
|
@@ -202,4 +240,13 @@ export class Tab2Page {
|
|
|
|
|
|
await alert.present();
|
|
|
}
|
|
|
+
|
|
|
+ private async showErrorAlert(message: string) {
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '错误',
|
|
|
+ message,
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+ await alert.present();
|
|
|
+ }
|
|
|
}
|