Skip to content

Commit

Permalink
feat: improve sleep timer feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gokadzev committed Feb 18, 2025
1 parent c8146af commit dc9d884
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
65 changes: 54 additions & 11 deletions lib/screens/now_playing_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'package:musify/models/position_data.dart';
import 'package:musify/services/settings_manager.dart';
import 'package:musify/utilities/common_variables.dart';
import 'package:musify/utilities/flutter_bottom_sheet.dart';
import 'package:musify/utilities/flutter_toast.dart';
import 'package:musify/utilities/formatter.dart';
import 'package:musify/utilities/mediaitem.dart';
import 'package:musify/utilities/utils.dart';
Expand Down Expand Up @@ -526,8 +527,9 @@ class NowPlayingPage extends StatelessWidget {
showDialog(
context: context,
builder: (context) {
var hours = 0;
var minutes = 10;
final duration = sleepTimerNotifier.value ?? Duration.zero;
var hours = duration.inMinutes ~/ 60;
var minutes = duration.inMinutes % 60;
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
Expand All @@ -537,7 +539,6 @@ class NowPlayingPage extends StatelessWidget {
children: [
Text(context.l10n!.selectDuration),
const SizedBox(height: 16),

Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expand All @@ -548,15 +549,27 @@ class NowPlayingPage extends StatelessWidget {
icon: const Icon(Icons.remove),
onPressed: () {
if (hours > 0) {
setState(() => hours--);
setState(() {
hours--;
sleepTimerNotifier.value = Duration(
hours: hours,
minutes: minutes,
);
});
}
},
),
Text('$hours'),
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() => hours++);
setState(() {
hours++;
sleepTimerNotifier.value = Duration(
hours: hours,
minutes: minutes,
);
});
},
),
],
Expand All @@ -574,15 +587,27 @@ class NowPlayingPage extends StatelessWidget {
icon: const Icon(Icons.remove),
onPressed: () {
if (minutes > 0) {
setState(() => minutes--);
setState(() {
minutes--;
sleepTimerNotifier.value = Duration(
hours: hours,
minutes: minutes,
);
});
}
},
),
Text('$minutes'),
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() => minutes++);
setState(() {
minutes++;
sleepTimerNotifier.value = Duration(
hours: hours,
minutes: minutes,
);
});
},
),
],
Expand All @@ -601,6 +626,7 @@ class NowPlayingPage extends StatelessWidget {
final duration = Duration(hours: hours, minutes: minutes);
if (duration.inSeconds > 0) {
audioHandler.setSleepTimer(duration);
showToast(context, context.l10n!.addedSuccess);
}
Navigator.pop(context);
},
Expand Down Expand Up @@ -702,10 +728,27 @@ class NowPlayingPage extends StatelessWidget {
iconSize: iconSize,
onPressed: _lyricsController.flipcard,
),
IconButton.filledTonal(
icon: Icon(FluentIcons.timer_24_regular, color: _primaryColor),
iconSize: iconSize,
onPressed: () => _showSleepTimerDialog(context),
ValueListenableBuilder<Duration?>(
valueListenable: sleepTimerNotifier,
builder: (_, value, __) {
return IconButton.filledTonal(
icon: Icon(
value != null
? FluentIcons.timer_24_filled
: FluentIcons.timer_24_regular,
color: _primaryColor,
),
iconSize: iconSize,
onPressed: () {
if (value != null) {
audioHandler.cancelSleepTimer();
sleepTimerNotifier.value = null;
} else {
_showSleepTimerDialog(context);
}
},
);
},
),
ValueListenableBuilder<bool>(
valueListenable: songLikeStatus,
Expand Down
6 changes: 1 addition & 5 deletions lib/services/audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class MusifyAudioHandler extends BaseAudioHandler {
);

Timer? _sleepTimer;
bool sleepTimerExpired = false;

late StreamSubscription<PlaybackEvent> _playbackEventSubscription;
late StreamSubscription<Duration?> _durationSubscription;
Expand Down Expand Up @@ -80,7 +79,7 @@ class MusifyAudioHandler extends BaseAudioHandler {
try {
if (event.processingState == ProcessingState.completed &&
audioPlayer.playing &&
!sleepTimerExpired) {
_sleepTimer == null) {
skipToNext();
}
_updatePlaybackState();
Expand Down Expand Up @@ -396,11 +395,9 @@ class MusifyAudioHandler extends BaseAudioHandler {

Future<void> setSleepTimer(Duration duration) async {
_sleepTimer?.cancel();
sleepTimerExpired = false;
_sleepTimer = Timer(duration, () async {
await stop();
playNextSongAutomatically.value = false;
sleepTimerExpired = true;
_sleepTimer = null;
});
}
Expand All @@ -409,7 +406,6 @@ class MusifyAudioHandler extends BaseAudioHandler {
if (_sleepTimer != null) {
_sleepTimer!.cancel();
_sleepTimer = null;
sleepTimerExpired = false;
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/services/settings_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ final repeatNotifier = ValueNotifier<AudioServiceRepeatMode>(
AudioServiceRepeatMode.none,
);

var sleepTimerNotifier = ValueNotifier<Duration?>(null);

// Server-Notifiers

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

0 comments on commit dc9d884

Please sign in to comment.