heavyrain 4 months ago
parent
commit
86531acc88
3 changed files with 108 additions and 64 deletions
  1. 61 35
      lib/pages/timer_page.dart
  2. 45 28
      lib/pages/timer_settings_page.dart
  3. 2 1
      lib/utils/audio_manager.dart

+ 61 - 35
lib/pages/timer_page.dart

@@ -24,6 +24,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
 
   // Settings
   late TimerSettings _settings;
+  bool _settingsLoaded = false;
   final AudioManager _audioManager = AudioManager();
 
   // Wheel controllers
@@ -64,7 +65,9 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
 
   Future<void> _loadSettings() async {
     _settings = await TimerSettings.loadSettings();
-    setState(() {});
+    setState(() {
+      _settingsLoaded = true;
+    });
   }
 
   void _startTimer() {
@@ -122,7 +125,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
 
     // If loop is enabled, restart the timer
     if (_settings.loop) {
-      Future.delayed(Duration(seconds: 2), () {
+      Future.delayed(Duration(seconds: 5), () {
         _audioManager.stopSound();
         _audioManager.stopVibration();
         _startTimer();
@@ -139,6 +142,10 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
 
   @override
   Widget build(BuildContext context) {
+    if (!_settingsLoaded) {
+      return Center(child: CircularProgressIndicator());
+    }
+    
     if (_isRunning || _isCompleted) {
       return _buildCountdownView();
     } else {
@@ -319,9 +326,13 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
               mainAxisAlignment: MainAxisAlignment.spaceAround,
               children: [
                 _buildCircleButton(
-                  Icons.stop,
-                  Colors.red,
-                  () => _resetTimer(),
+                  Icons.phone_android,
+                  Colors.grey[600]!,
+                  () {
+                    ScreenManager.toggleWakeLock();
+                    setState(() {});
+                  },
+                  isActive: ScreenManager.isWakeLockEnabled,
                 ),
                 _isRunning
                     ? _buildCircleButton(
@@ -335,21 +346,9 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
                         () => _startTimer(),
                       ),
                 _buildCircleButton(
-                  Icons.settings,
-                  Colors.grey[600]!,
-                  () async {
-                    final result = await Navigator.push(
-                      context,
-                      MaterialPageRoute(
-                          builder: (context) =>
-                              TimerSettingsPage(settings: _settings)),
-                    );
-                    if (result != null) {
-                      setState(() {
-                        _settings = result;
-                      });
-                    }
-                  },
+                  Icons.stop,
+                  Colors.red,
+                  () => _resetTimer(),
                 ),
               ],
             ),
@@ -392,25 +391,52 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
     return Column(
       children: [
         Expanded(
-          child: ListWheelScrollView(
-            controller: controller,
-            physics: FixedExtentScrollPhysics(),
-            diameterRatio: 1.5,
-            itemExtent: 50,
-            children: items.map((value) {
-              return Center(
-                child: Text(
-                  value.toString().padLeft(2, '0'),
-                  style: TextStyle(
-                    fontSize: 30,
-                    color: Colors.black,
+          child: Container(
+            decoration: BoxDecoration(
+              border: Border(
+                top: BorderSide(color: Colors.grey.withOpacity(0.3), width: 1),
+                bottom:
+                    BorderSide(color: Colors.grey.withOpacity(0.3), width: 1),
+              ),
+            ),
+            child: Stack(
+              children: [
+                // Center highlight
+                Positioned.fill(
+                  child: Center(
+                    child: Container(
+                      height: 50,
+                      decoration: BoxDecoration(
+                        color: Colors.blue.withOpacity(0.1),
+                        borderRadius: BorderRadius.circular(8),
+                      ),
+                    ),
                   ),
                 ),
-              );
-            }).toList(),
-            onSelectedItemChanged: onChanged,
+                ListWheelScrollView(
+                  controller: controller,
+                  physics: FixedExtentScrollPhysics(),
+                  diameterRatio: 1.5,
+                  itemExtent: 50,
+                  children: items.map((value) {
+                    return Center(
+                      child: Text(
+                        value.toString().padLeft(2, '0'),
+                        style: TextStyle(
+                          fontSize: 30,
+                          color: Colors.black,
+                          fontWeight: FontWeight.w500,
+                        ),
+                      ),
+                    );
+                  }).toList(),
+                  onSelectedItemChanged: onChanged,
+                ),
+              ],
+            ),
           ),
         ),
+        SizedBox(height: 8),
         Text(
           unit,
           style: TextStyle(

+ 45 - 28
lib/pages/timer_settings_page.dart

@@ -26,31 +26,53 @@ class _TimerSettingsPageState extends State<TimerSettingsPage> {
     _vibrate = widget.settings.vibrate;
     _loop = widget.settings.loop;
   }
+  
+  // Save settings
+  Future<TimerSettings> _saveSettings() async {
+    final updatedSettings = TimerSettings(
+      sound: _sound,
+      volume: _volume,
+      vibrate: _vibrate,
+      loop: _loop,
+    );
+    await updatedSettings.saveSettings();
+    return updatedSettings;
+  }
 
   @override
   Widget build(BuildContext context) {
-    return Scaffold(
-      backgroundColor: Colors.white,
-      appBar: AppBar(
+    return WillPopScope(
+      onWillPop: () async {
+        final updatedSettings = await _saveSettings();
+        Navigator.pop(context, updatedSettings);
+        return false;
+      },
+      child: Scaffold(
         backgroundColor: Colors.white,
-        elevation: 0,
-        leading: IconButton(
-          icon: Icon(Icons.close, color: Colors.black),
-          onPressed: () => Navigator.pop(context),
+        appBar: AppBar(
+          backgroundColor: Colors.white,
+          elevation: 0,
+          leading: IconButton(
+            icon: Icon(Icons.close, color: Colors.black),
+            onPressed: () async {
+              final updatedSettings = await _saveSettings();
+              Navigator.pop(context, updatedSettings);
+            },
+          ),
+          title: Text(
+            'Settings',
+            style: TextStyle(color: Colors.black, fontSize: 24),
+          ),
+          centerTitle: true,
         ),
-        title: Text(
-          'Settings',
-          style: TextStyle(color: Colors.black, fontSize: 24),
+        body: ListView(
+          children: [
+            _buildSoundSetting(),
+            _buildVolumeSetting(),
+            _buildVibrateSetting(),
+            _buildLoopSetting(),
+          ],
         ),
-        centerTitle: true,
-      ),
-      body: ListView(
-        children: [
-          _buildSoundSetting(),
-          _buildVolumeSetting(),
-          _buildVibrateSetting(),
-          _buildLoopSetting(),
-        ],
       ),
     );
   }
@@ -114,6 +136,7 @@ class _TimerSettingsPageState extends State<TimerSettingsPage> {
             setState(() {
               _volume = value;
             });
+            _saveSettings();
           },
         ),
         Divider(),
@@ -141,6 +164,7 @@ class _TimerSettingsPageState extends State<TimerSettingsPage> {
                   setState(() {
                     _vibrate = value;
                   });
+                  _saveSettings();
                 },
               ),
             ],
@@ -171,6 +195,7 @@ class _TimerSettingsPageState extends State<TimerSettingsPage> {
                   setState(() {
                     _loop = value;
                   });
+                  _saveSettings();
                 },
               ),
             ],
@@ -194,6 +219,7 @@ class _TimerSettingsPageState extends State<TimerSettingsPage> {
                 setState(() {
                   _sound = sound;
                 });
+                _saveSettings();
                 Navigator.pop(context);
               },
               trailing: _sound == sound
@@ -208,15 +234,6 @@ class _TimerSettingsPageState extends State<TimerSettingsPage> {
 
   @override
   void dispose() {
-    // Save settings before exiting
-    final updatedSettings = TimerSettings(
-      sound: _sound,
-      volume: _volume,
-      vibrate: _vibrate,
-      loop: _loop,
-    );
-    updatedSettings.saveSettings();
-    Navigator.pop(context, updatedSettings);
     super.dispose();
   }
 }

+ 2 - 1
lib/utils/audio_manager.dart

@@ -48,7 +48,8 @@ class AudioManager {
 
   Future<void> triggerVibration() async {
     if (await Vibration.hasVibrator() ?? false) {
-      Vibration.vibrate(pattern: [500, 1000, 500, 1000], repeat: 1);
+      // Vibrate continuously for 5 seconds
+      Vibration.vibrate(duration: 5000);
     }
   }