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 0f53a6a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
53 changes: 42 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,19 @@ class NowPlayingPage extends StatelessWidget {
icon: const Icon(Icons.remove),
onPressed: () {
if (hours > 0) {
setState(() => hours--);
setState(() {
hours--;
});
}
},
),
Text('$hours'),
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() => hours++);
setState(() {
hours++;
});
},
),
],
Expand All @@ -574,15 +579,19 @@ class NowPlayingPage extends StatelessWidget {
icon: const Icon(Icons.remove),
onPressed: () {
if (minutes > 0) {
setState(() => minutes--);
setState(() {
minutes--;
});
}
},
),
Text('$minutes'),
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() => minutes++);
setState(() {
minutes++;
});
},
),
],
Expand All @@ -601,6 +610,11 @@ class NowPlayingPage extends StatelessWidget {
final duration = Duration(hours: hours, minutes: minutes);
if (duration.inSeconds > 0) {
audioHandler.setSleepTimer(duration);
sleepTimerNotifier.value = Duration(
hours: hours,
minutes: minutes,
);
showToast(context, context.l10n!.addedSuccess);
}
Navigator.pop(context);
},
Expand Down Expand Up @@ -702,10 +716,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
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 0f53a6a

Please sign in to comment.