| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- import { Component, CUSTOM_ELEMENTS_SCHEMA, OnInit } from '@angular/core';
- import { CommonModule } from '@angular/common';
- import { FormsModule } from '@angular/forms';
- import { IonicModule } from '@ionic/angular';
- import { Router } from '@angular/router';
- import { addIcons } from 'ionicons';
- import {
- schoolOutline,
- briefcaseOutline,
- moonOutline,
- barbellOutline,
- bookOutline,
- codeSlashOutline,
- leafOutline,
- musicalNotesOutline,
- languageOutline,
- pencilOutline,
- trashOutline,
- addOutline,
- arrowBackOutline,
- pauseOutline,
- playOutline,
- stopOutline
- } from 'ionicons/icons';
- import { AlertController } from '@ionic/angular';
- import { FocusDataService } from '../services/focus-data.service';
- interface TimerRecord {
- id: number;
- type: string;
- startTime: string;
- endTime: string;
- duration: number;
- parseId?: string;
- }
- @Component({
- selector: 'app-tab2',
- templateUrl: 'tab2.page.html',
- styleUrls: ['tab2.page.scss'],
- standalone: true,
- schemas: [CUSTOM_ELEMENTS_SCHEMA],
- imports: [IonicModule, CommonModule, FormsModule]
- })
- export class Tab2Page implements OnInit {
- activityTypes = [
- { id: 'study', name: '学习', icon: 'school-outline' },
- { id: 'work', name: '工作', icon: 'briefcase-outline' },
- { id: 'sleep', name: '睡眠', icon: 'moon-outline' },
- { id: 'sport', name: '运动', icon: 'barbell-outline' },
- { id: 'reading', name: '阅读', icon: 'book-outline' },
- { id: 'coding', name: '编程', icon: 'code-slash-outline' },
- { id: 'meditation', name: '冥想', icon: 'leaf-outline' },
- { id: 'music', name: '音乐', icon: 'musical-notes-outline' },
- { id: 'language', name: '语言', icon: 'language-outline' },
- { id: 'writing', name: '写作', icon: 'pencil-outline' }
- ];
- selectedType: string = '';
- isTimerRunning: boolean = false;
- timerStartTime: Date | null = null;
- currentTime: string = '00:00:00';
- records: TimerRecord[] = [];
- timer: any;
- countdownMinutes: number = 25;
- constructor(
- private router: Router,
- private alertController: AlertController,
- private focusDataService: FocusDataService
- ) {
- addIcons({
- schoolOutline,
- briefcaseOutline,
- moonOutline,
- barbellOutline,
- bookOutline,
- codeSlashOutline,
- leafOutline,
- musicalNotesOutline,
- languageOutline,
- pencilOutline,
- trashOutline,
- addOutline,
- arrowBackOutline,
- pauseOutline,
- playOutline,
- stopOutline
- });
- }
- ngOnInit() {
- this.loadRecords();
- }
- 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('加载记录失败');
- }
- }
- selectType(typeId: string) {
- this.selectedType = typeId;
- }
- startTimer() {
- if (!this.selectedType) {
- return;
- }
-
- this.isTimerRunning = true;
- this.timerStartTime = new Date();
-
- this.timer = setInterval(() => {
- const now = new Date();
- const diff = now.getTime() - this.timerStartTime!.getTime();
-
- const hours = Math.floor(diff / 3600000);
- const minutes = Math.floor((diff % 3600000) / 60000);
- const seconds = Math.floor((diff % 60000) / 1000);
-
- this.currentTime = `${this.padNumber(hours)}:${this.padNumber(minutes)}:${this.padNumber(seconds)}`;
- }, 1000);
- }
- async stopTimer() {
- if (this.timer) {
- clearInterval(this.timer);
- const endTime = new Date();
- const duration = Math.floor((endTime.getTime() - this.timerStartTime!.getTime()) / 60000);
-
- 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;
- this.currentTime = '00:00:00';
- this.selectedType = '';
- }
- }
- startCountdown() {
- if (!this.selectedType) {
- return;
- }
-
- const selectedActivity = this.activityTypes.find(t => t.id === this.selectedType);
- if (selectedActivity) {
- this.router.navigate(['/countdown'], {
- queryParams: {
- type: selectedActivity.icon,
- name: selectedActivity.name,
- duration: this.countdownMinutes,
- category: selectedActivity.id
- }
- });
- }
- }
- private padNumber(num: number): string {
- return num.toString().padStart(2, '0');
- }
- getTypeName(typeId: string): string {
- const type = this.activityTypes.find(t => t.id === typeId);
- return type ? type.name : '';
- }
- getTypeIcon(typeId: string): string {
- const type = this.activityTypes.find(t => t.id === typeId);
- return type ? type.icon : '';
- }
- formatDuration(minutes: number): string {
- const hours = Math.floor(minutes / 60);
- const mins = minutes % 60;
- return hours > 0 ? `${hours}小时${mins}分钟` : `${mins}分钟`;
- }
- async deleteRecord(index: number) {
- const record = this.records[index];
- if (!record.parseId) {
- return;
- }
- const alert = await this.alertController.create({
- header: '确认删除',
- message: '你确定要删除这条记录吗?',
- buttons: [
- {
- text: '取消',
- role: 'cancel',
- },
- {
- text: '删除',
- role: 'destructive',
- handler: async () => {
- try {
- await this.focusDataService.deleteFocusRecord(record.parseId!);
- this.records.splice(index, 1);
- } catch (error) {
- console.error('删除记录失败:', error);
- this.showErrorAlert('删除记录失败');
- }
- }
- }
- ]
- });
- await alert.present();
- }
- private async showErrorAlert(message: string) {
- const alert = await this.alertController.create({
- header: '错误',
- message,
- buttons: ['确定']
- });
- await alert.present();
- }
- }
|