Skip to content

Commit

Permalink
show remaining time instead of duration on iOS/macOS
Browse files Browse the repository at this point in the history
- closes #664
  • Loading branch information
Chaphasilor committed Sep 9, 2024
1 parent 3edd482 commit defab64
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 14 additions & 4 deletions lib/components/PlayerScreen/progress_slider.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:audio_service/audio_service.dart';
import 'package:finamp/components/print_duration.dart';
import 'package:finamp/services/progress_state_stream.dart';
Expand Down Expand Up @@ -131,20 +133,28 @@ class _ProgressSliderDuration extends StatelessWidget {

@override
Widget build(BuildContext context) {
final showRemaining = Platform.isIOS || Platform.isMacOS;
final currentPosition = Duration(seconds: (position.inMilliseconds / 1000).round());
final roundedDuration = Duration(seconds: ((itemDuration?.inMilliseconds ?? 0) / 1000).round());
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
printDuration(
Duration(microseconds: position.inMicroseconds),
),
printDuration(currentPosition),
style: Theme.of(context).textTheme.bodySmall?.copyWith(
height: 0.5, // reduce line height
),
),
Text(
printDuration(itemDuration),
printDuration(
// display remaining time if on iOS or macOS
showRemaining ?
(roundedDuration - currentPosition)
: roundedDuration
,
isRemaining: showRemaining,
),
style: Theme.of(context).textTheme.bodySmall?.copyWith(
height: 0.5, // reduce line height
),
Expand Down
13 changes: 10 additions & 3 deletions lib/components/print_duration.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Flutter doesn't have a nice way of formatting durations for some reason so I stole this code from StackOverflow
String printDuration(Duration? duration) {
String printDuration(Duration? duration, {bool isRemaining = false}) {
if (duration == null) {
return "00:00";
}
Expand All @@ -8,10 +8,17 @@ String printDuration(Duration? duration) {
String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));

String durationString;
if (duration.inHours >= 1) {
String twoDigitHours = twoDigits(duration.inHours);
return "$twoDigitHours:$twoDigitMinutes:$twoDigitSeconds";
durationString = "$twoDigitHours:$twoDigitMinutes:$twoDigitSeconds";
}

return "$twoDigitMinutes:$twoDigitSeconds";
durationString = "$twoDigitMinutes:$twoDigitSeconds";

if (isRemaining) {
durationString = "-$durationString";
}

return durationString;
}

0 comments on commit defab64

Please sign in to comment.