|
|
@@ -0,0 +1,111 @@
|
|
|
+import { Component, OnInit, OnDestroy } from '@angular/core';
|
|
|
+import { CommonModule } from '@angular/common';
|
|
|
+import { IonicModule } from '@ionic/angular';
|
|
|
+import { FormsModule } from '@angular/forms';
|
|
|
+import { Router } from '@angular/router';
|
|
|
+import { addIcons } from 'ionicons';
|
|
|
+import { arrowBack, timer, play, pause, stop } from 'ionicons/icons';
|
|
|
+
|
|
|
+@Component({
|
|
|
+ selector: 'app-countdown',
|
|
|
+ templateUrl: './countdown.page.html',
|
|
|
+ styleUrls: ['./countdown.page.scss'],
|
|
|
+ standalone: true,
|
|
|
+ imports: [IonicModule, CommonModule, FormsModule]
|
|
|
+})
|
|
|
+export class CountdownPage implements OnInit, OnDestroy {
|
|
|
+ public remainingTime: string = '00:00';
|
|
|
+ public activityType: string = 'timer';
|
|
|
+ public activityName: string = '专注';
|
|
|
+ public progress: number = 1;
|
|
|
+ public isRunning: boolean = false;
|
|
|
+
|
|
|
+ private duration: number = 0;
|
|
|
+ private timer: any;
|
|
|
+ private endTime: number = 0;
|
|
|
+ private pausedTimeLeft: number = 0;
|
|
|
+
|
|
|
+ constructor(private router: Router) {
|
|
|
+ addIcons({
|
|
|
+ 'arrow-back': arrowBack,
|
|
|
+ 'timer': timer,
|
|
|
+ 'play': play,
|
|
|
+ 'pause': pause,
|
|
|
+ 'stop': stop
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ this.setDuration(5);
|
|
|
+ this.startTimer();
|
|
|
+ }
|
|
|
+
|
|
|
+ ngOnDestroy() {
|
|
|
+ if (this.timer) {
|
|
|
+ clearInterval(this.timer);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public togglePause() {
|
|
|
+ this.isRunning = !this.isRunning;
|
|
|
+ if (this.isRunning) {
|
|
|
+ // 继续计时
|
|
|
+ this.endTime = Date.now() + this.pausedTimeLeft;
|
|
|
+ this.startTimer();
|
|
|
+ } else {
|
|
|
+ // 暂停计时
|
|
|
+ if (this.timer) {
|
|
|
+ clearInterval(this.timer);
|
|
|
+ this.pausedTimeLeft = this.endTime - Date.now();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public stop() {
|
|
|
+ if (this.timer) {
|
|
|
+ clearInterval(this.timer);
|
|
|
+ }
|
|
|
+ this.isRunning = false;
|
|
|
+ this.setDuration(5); // 重置为5分钟
|
|
|
+ }
|
|
|
+
|
|
|
+ public setDuration(minutes: number) {
|
|
|
+ if (minutes < 5) {
|
|
|
+ alert('倒计时的最小时长为5分钟。');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.duration = minutes * 60;
|
|
|
+ this.remainingTime = this.formatTime(this.duration);
|
|
|
+ }
|
|
|
+
|
|
|
+ public exit() {
|
|
|
+ if (this.timer) {
|
|
|
+ clearInterval(this.timer);
|
|
|
+ }
|
|
|
+ this.router.navigate(['/']);
|
|
|
+ }
|
|
|
+
|
|
|
+ private startTimer() {
|
|
|
+ this.endTime = Date.now() + (this.duration * 1000);
|
|
|
+ this.isRunning = true;
|
|
|
+
|
|
|
+ this.timer = setInterval(() => {
|
|
|
+ const now = Date.now();
|
|
|
+ const timeLeft = Math.max(0, this.endTime - now);
|
|
|
+
|
|
|
+ if (timeLeft === 0) {
|
|
|
+ clearInterval(this.timer);
|
|
|
+ this.isRunning = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.remainingTime = this.formatTime(Math.floor(timeLeft / 1000));
|
|
|
+ this.progress = timeLeft / (this.duration * 1000);
|
|
|
+ }, 100);
|
|
|
+ }
|
|
|
+
|
|
|
+ private formatTime(seconds: number): string {
|
|
|
+ const minutes = Math.floor(seconds / 60);
|
|
|
+ const secs = Math.floor(seconds % 60);
|
|
|
+ return `${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
|
|
|
+ }
|
|
|
+}
|