diff --git a/lib/components/PlayerScreen/progress_slider.dart b/lib/components/PlayerScreen/progress_slider.dart index 55875f75..b7fede51 100644 --- a/lib/components/PlayerScreen/progress_slider.dart +++ b/lib/components/PlayerScreen/progress_slider.dart @@ -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'; @@ -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 ), diff --git a/lib/components/print_duration.dart b/lib/components/print_duration.dart index 7140c5cf..9e6ffe9b 100644 --- a/lib/components/print_duration.dart +++ b/lib/components/print_duration.dart @@ -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"; } @@ -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; }