Browse Source

等待整点对其剩余时间界面更新

heavyrain 3 months ago
parent
commit
0fd4eb2fc3
1 changed files with 48 additions and 2 deletions
  1. 48 2
      lib/pages/timer/timer_page.dart

+ 48 - 2
lib/pages/timer/timer_page.dart

@@ -30,6 +30,8 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
   // Waiting for alignment timer
   Timer? _alignmentTimer;
   DateTime? _alignmentTargetTime;
+  DateTime? _alignmentStartTime; // 记录开始等待的时间点
+  Timer? _alignmentUIUpdateTimer; // For refreshing UI during waiting
 
   // Total timer duration in seconds (for progress calculation)
   int _totalDurationSeconds = 0;
@@ -65,6 +67,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
     _audioManager.dispose();
     _backgroundTimerService.dispose();
     _alignmentTimer?.cancel();
+    _alignmentUIUpdateTimer?.cancel(); // Cancel UI update timer
     if (ScreenManager.isWakeLockEnabled) {
       ScreenManager.disableWakeLock();
     }
@@ -145,15 +148,41 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
       0
     );
   }
+
+  // 计算等待进度
+  double _calculateWaitingProgress() {
+    if (_alignmentTargetTime == null) return 0.0;
+    
+    final now = DateTime.now();
+    final targetTime = _alignmentTargetTime!;
+    final startTime = _alignmentStartTime ?? now;
+    
+    // 如果还没开始等待或已经过了目标时间
+    if (now.isAfter(targetTime)) return 1.0;
+    if (now.isBefore(startTime)) return 0.0;
+    
+    // 计算总等待时间和已等待时间
+    final totalWaitDuration = targetTime.difference(startTime).inMilliseconds;
+    final elapsedWaitDuration = now.difference(startTime).inMilliseconds;
+    
+    // 计算进度比例
+    if (totalWaitDuration <= 0) return 1.0;
+    final progress = elapsedWaitDuration / totalWaitDuration;
+    
+    // 确保进度在0.0到1.0之间
+    return progress.clamp(0.0, 1.0);
+  }
   
   /// Wait until the next 10-minute mark and then start the timer
   void _startTimerWithAlignment(int totalSeconds) {
     // 取消任何现有的对齐定时器
     _alignmentTimer?.cancel();
+    _alignmentUIUpdateTimer?.cancel();
     
     // 计算下一个整10分钟的时间点
     final alignmentTime = _calculateNextAlignmentTime();
     _alignmentTargetTime = alignmentTime;
+    _alignmentStartTime = DateTime.now(); // 记录开始等待的时间
     
     // 设置状态为等待
     setState(() {
@@ -171,9 +200,20 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
         _remainingSeconds = totalSeconds;
         _totalDurationSeconds = totalSeconds;
         _alignmentTargetTime = null;
+        _alignmentStartTime = null;
       });
       
       _backgroundTimerService.startTimer(totalSeconds);
+      _alignmentUIUpdateTimer?.cancel(); // Cancel UI update timer when alignment is done
+    });
+    
+    // 创建UI刷新定时器,每秒更新一次
+    _alignmentUIUpdateTimer = Timer.periodic(Duration(seconds: 1), (timer) {
+      if (mounted) {
+        setState(() {
+          // 仅用于刷新UI
+        });
+      }
     });
   }
 
@@ -236,7 +276,9 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
     if (timerState == TimerState.waiting) {
       // 如果在等待对齐状态,取消等待定时器
       _alignmentTimer?.cancel();
+      _alignmentUIUpdateTimer?.cancel(); // Cancel UI update timer
       _alignmentTargetTime = null;
+      _alignmentStartTime = null;
     } else {
       _backgroundTimerService.pauseTimer();
     }
@@ -252,7 +294,9 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
     _audioManager.stopSound();
     _audioManager.stopVibration();
     _alignmentTimer?.cancel();
+    _alignmentUIUpdateTimer?.cancel(); // Cancel UI update timer
     _alignmentTargetTime = null;
+    _alignmentStartTime = null;
     
     setState(() {
       timerState = TimerState.prepare;
@@ -298,6 +342,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
     // 如果差异为负,说明已经过了目标时间
     if (difference.isNegative) return "00:00";
     
+    // 计算分钟和秒数
     final minutes = difference.inMinutes;
     final seconds = difference.inSeconds % 60;
     
@@ -535,6 +580,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
   // 构建等待整点对齐的视图
   Widget _buildWaitingForAlignmentView() {
     final nextAlignmentTime = _alignmentTargetTime;
+    final waitingProgress = _calculateWaitingProgress();
     
     return Scaffold(
       backgroundColor: Colors.white,
@@ -559,12 +605,12 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
                     child: Stack(
                       alignment: Alignment.center,
                       children: [
-                        // 倒计时进度(未开始,显示脉动效果)
+                        // 等待进度指示器
                         SizedBox(
                           width: 300,
                           height: 300,
                           child: CircularProgressIndicator(
-                            value: null, // 显示不确定进度
+                            value: waitingProgress, // 显示确定的等待进度
                             strokeWidth: 5,
                             backgroundColor: Colors.grey.withOpacity(0.1),
                             color: Colors.orange,