Skip to content

Commit dc9d884

Browse files
committed
feat: improve sleep timer feature
1 parent c8146af commit dc9d884

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

lib/screens/now_playing_page.dart

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import 'package:musify/models/position_data.dart';
3030
import 'package:musify/services/settings_manager.dart';
3131
import 'package:musify/utilities/common_variables.dart';
3232
import 'package:musify/utilities/flutter_bottom_sheet.dart';
33+
import 'package:musify/utilities/flutter_toast.dart';
3334
import 'package:musify/utilities/formatter.dart';
3435
import 'package:musify/utilities/mediaitem.dart';
3536
import 'package:musify/utilities/utils.dart';
@@ -526,8 +527,9 @@ class NowPlayingPage extends StatelessWidget {
526527
showDialog(
527528
context: context,
528529
builder: (context) {
529-
var hours = 0;
530-
var minutes = 10;
530+
final duration = sleepTimerNotifier.value ?? Duration.zero;
531+
var hours = duration.inMinutes ~/ 60;
532+
var minutes = duration.inMinutes % 60;
531533
return StatefulBuilder(
532534
builder: (context, setState) {
533535
return AlertDialog(
@@ -537,7 +539,6 @@ class NowPlayingPage extends StatelessWidget {
537539
children: [
538540
Text(context.l10n!.selectDuration),
539541
const SizedBox(height: 16),
540-
541542
Row(
542543
mainAxisAlignment: MainAxisAlignment.spaceBetween,
543544
children: [
@@ -548,15 +549,27 @@ class NowPlayingPage extends StatelessWidget {
548549
icon: const Icon(Icons.remove),
549550
onPressed: () {
550551
if (hours > 0) {
551-
setState(() => hours--);
552+
setState(() {
553+
hours--;
554+
sleepTimerNotifier.value = Duration(
555+
hours: hours,
556+
minutes: minutes,
557+
);
558+
});
552559
}
553560
},
554561
),
555562
Text('$hours'),
556563
IconButton(
557564
icon: const Icon(Icons.add),
558565
onPressed: () {
559-
setState(() => hours++);
566+
setState(() {
567+
hours++;
568+
sleepTimerNotifier.value = Duration(
569+
hours: hours,
570+
minutes: minutes,
571+
);
572+
});
560573
},
561574
),
562575
],
@@ -574,15 +587,27 @@ class NowPlayingPage extends StatelessWidget {
574587
icon: const Icon(Icons.remove),
575588
onPressed: () {
576589
if (minutes > 0) {
577-
setState(() => minutes--);
590+
setState(() {
591+
minutes--;
592+
sleepTimerNotifier.value = Duration(
593+
hours: hours,
594+
minutes: minutes,
595+
);
596+
});
578597
}
579598
},
580599
),
581600
Text('$minutes'),
582601
IconButton(
583602
icon: const Icon(Icons.add),
584603
onPressed: () {
585-
setState(() => minutes++);
604+
setState(() {
605+
minutes++;
606+
sleepTimerNotifier.value = Duration(
607+
hours: hours,
608+
minutes: minutes,
609+
);
610+
});
586611
},
587612
),
588613
],
@@ -601,6 +626,7 @@ class NowPlayingPage extends StatelessWidget {
601626
final duration = Duration(hours: hours, minutes: minutes);
602627
if (duration.inSeconds > 0) {
603628
audioHandler.setSleepTimer(duration);
629+
showToast(context, context.l10n!.addedSuccess);
604630
}
605631
Navigator.pop(context);
606632
},
@@ -702,10 +728,27 @@ class NowPlayingPage extends StatelessWidget {
702728
iconSize: iconSize,
703729
onPressed: _lyricsController.flipcard,
704730
),
705-
IconButton.filledTonal(
706-
icon: Icon(FluentIcons.timer_24_regular, color: _primaryColor),
707-
iconSize: iconSize,
708-
onPressed: () => _showSleepTimerDialog(context),
731+
ValueListenableBuilder<Duration?>(
732+
valueListenable: sleepTimerNotifier,
733+
builder: (_, value, __) {
734+
return IconButton.filledTonal(
735+
icon: Icon(
736+
value != null
737+
? FluentIcons.timer_24_filled
738+
: FluentIcons.timer_24_regular,
739+
color: _primaryColor,
740+
),
741+
iconSize: iconSize,
742+
onPressed: () {
743+
if (value != null) {
744+
audioHandler.cancelSleepTimer();
745+
sleepTimerNotifier.value = null;
746+
} else {
747+
_showSleepTimerDialog(context);
748+
}
749+
},
750+
);
751+
},
709752
),
710753
ValueListenableBuilder<bool>(
711754
valueListenable: songLikeStatus,

lib/services/audio_service.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class MusifyAudioHandler extends BaseAudioHandler {
5252
);
5353

5454
Timer? _sleepTimer;
55-
bool sleepTimerExpired = false;
5655

5756
late StreamSubscription<PlaybackEvent> _playbackEventSubscription;
5857
late StreamSubscription<Duration?> _durationSubscription;
@@ -80,7 +79,7 @@ class MusifyAudioHandler extends BaseAudioHandler {
8079
try {
8180
if (event.processingState == ProcessingState.completed &&
8281
audioPlayer.playing &&
83-
!sleepTimerExpired) {
82+
_sleepTimer == null) {
8483
skipToNext();
8584
}
8685
_updatePlaybackState();
@@ -396,11 +395,9 @@ class MusifyAudioHandler extends BaseAudioHandler {
396395

397396
Future<void> setSleepTimer(Duration duration) async {
398397
_sleepTimer?.cancel();
399-
sleepTimerExpired = false;
400398
_sleepTimer = Timer(duration, () async {
401399
await stop();
402400
playNextSongAutomatically.value = false;
403-
sleepTimerExpired = true;
404401
_sleepTimer = null;
405402
});
406403
}
@@ -409,7 +406,6 @@ class MusifyAudioHandler extends BaseAudioHandler {
409406
if (_sleepTimer != null) {
410407
_sleepTimer!.cancel();
411408
_sleepTimer = null;
412-
sleepTimerExpired = false;
413409
}
414410
}
415411

lib/services/settings_manager.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ final repeatNotifier = ValueNotifier<AudioServiceRepeatMode>(
8282
AudioServiceRepeatMode.none,
8383
);
8484

85+
var sleepTimerNotifier = ValueNotifier<Duration?>(null);
86+
8587
// Server-Notifiers
8688

8789
final announcementURL = ValueNotifier<String?>(null);

0 commit comments

Comments
 (0)