countdown.page.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { Component, OnInit, OnDestroy } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { FormsModule } from '@angular/forms';
  4. import { IonicModule, NavController } from '@ionic/angular';
  5. import { ActivatedRoute } from '@angular/router';
  6. import { addIcons } from 'ionicons';
  7. import {
  8. schoolOutline,
  9. briefcaseOutline,
  10. moonOutline,
  11. barbellOutline,
  12. bookOutline,
  13. codeSlashOutline,
  14. leafOutline,
  15. musicalNotesOutline,
  16. languageOutline,
  17. pencilOutline,
  18. pauseOutline,
  19. playOutline,
  20. stopOutline,
  21. exitOutline,
  22. trashOutline
  23. } from 'ionicons/icons';
  24. @Component({
  25. selector: 'app-countdown',
  26. templateUrl: './countdown.page.html',
  27. styleUrls: ['./countdown.page.scss'],
  28. standalone: true,
  29. imports: [IonicModule, CommonModule, FormsModule]
  30. })
  31. export class CountdownPage implements OnInit, OnDestroy {
  32. remainingTime: string = '';
  33. activityType: string = '';
  34. activityName: string = '';
  35. duration: number = 0;
  36. timer: any;
  37. isRunning: boolean = false;
  38. progress: number = 1;
  39. remainingSeconds: number = 0;
  40. alertAudio: HTMLAudioElement;
  41. constructor(private route: ActivatedRoute, private navCtrl: NavController) {
  42. addIcons({
  43. 'school-outline': schoolOutline,
  44. 'briefcase-outline': briefcaseOutline,
  45. 'moon-outline': moonOutline,
  46. 'barbell-outline': barbellOutline,
  47. 'book-outline': bookOutline,
  48. 'code-slash-outline': codeSlashOutline,
  49. 'leaf-outline': leafOutline,
  50. 'musical-notes-outline': musicalNotesOutline,
  51. 'language-outline': languageOutline,
  52. 'pencil-outline': pencilOutline,
  53. 'pause-outline': pauseOutline,
  54. 'play-outline': playOutline,
  55. 'stop-outline': stopOutline,
  56. 'exit-outline': exitOutline,
  57. 'trash-outline': trashOutline
  58. });
  59. // 初始化音频
  60. this.alertAudio = new Audio('assets/alert.mp3');
  61. }
  62. ngOnInit() {
  63. this.route.queryParams.subscribe(params => {
  64. this.activityType = params['type'];
  65. this.activityName = params['name'];
  66. this.duration = parseInt(params['duration']);
  67. this.remainingSeconds = this.duration * 60; // 转换为秒
  68. this.startCountdown();
  69. });
  70. }
  71. ngOnDestroy() {
  72. if (this.timer) {
  73. clearInterval(this.timer);
  74. }
  75. }
  76. startCountdown() {
  77. if (this.timer) {
  78. clearInterval(this.timer);
  79. }
  80. const startTime = this.remainingSeconds;
  81. this.timer = setInterval(() => {
  82. if (this.remainingSeconds > 0) {
  83. this.remainingSeconds--;
  84. this.progress = this.remainingSeconds / startTime;
  85. this.remainingTime = this.formatTime(this.remainingSeconds);
  86. } else {
  87. this.isRunning = false;
  88. clearInterval(this.timer);
  89. this.playAlertSound(); // 播放提示音
  90. }
  91. }, 1000);
  92. }
  93. formatTime(seconds: number): string {
  94. const hours = Math.floor(seconds / 3600);
  95. const minutes = Math.floor((seconds % 3600) / 60);
  96. const secs = seconds % 60;
  97. return `${this.padNumber(hours)}:${this.padNumber(minutes)}:${this.padNumber(secs)}`;
  98. }
  99. padNumber(num: number): string {
  100. return num.toString().padStart(2, '0');
  101. }
  102. togglePause() {
  103. console.log('Toggle Pause called');
  104. if (this.isRunning) {
  105. clearInterval(this.timer);
  106. console.log('Paused at:', this.remainingSeconds, 'seconds');
  107. } else {
  108. console.log('Resuming from:', this.remainingSeconds, 'seconds');
  109. this.startCountdown();
  110. }
  111. this.isRunning = !this.isRunning;
  112. }
  113. stop() {
  114. clearInterval(this.timer);
  115. this.isRunning = false;
  116. this.remainingSeconds = this.duration * 60;
  117. this.remainingTime = this.formatTime(this.remainingSeconds);
  118. this.progress = 1;
  119. }
  120. exit() {
  121. this.stop();
  122. this.navCtrl.back();
  123. }
  124. private playAlertSound() {
  125. this.alertAudio.play().catch(error => console.error('Error playing alert sound:', error));
  126. }
  127. }