Skip to content

Commit

Permalink
fix: hour not showing for tracks longer than 60 minutes (#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Sep 11, 2023
1 parent d12ea48 commit de335f4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 34 deletions.
19 changes: 3 additions & 16 deletions lib/components/player/player_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import 'package:palette_generator/palette_generator.dart';
import 'package:spotube/collections/spotube_icons.dart';
import 'package:spotube/collections/intents.dart';
import 'package:spotube/extensions/context.dart';
import 'package:spotube/extensions/duration.dart';
import 'package:spotube/hooks/use_progress.dart';
import 'package:spotube/models/logger.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
import 'package:spotube/services/audio_player/audio_player.dart';
import 'package:spotube/services/audio_player/loop_mode.dart';
import 'package:spotube/utils/primitive_utils.dart';

class PlayerControls extends HookConsumerWidget {
final PaletteGenerator? palette;
Expand Down Expand Up @@ -113,19 +113,6 @@ class PlayerControls extends HookConsumerWidget {
:progressStatic
) = useProgress(ref);

final totalMinutes = PrimitiveUtils.zeroPadNumStr(
duration.inMinutes.remainder(60),
);
final totalSeconds = PrimitiveUtils.zeroPadNumStr(
duration.inSeconds.remainder(60),
);
final currentMinutes = PrimitiveUtils.zeroPadNumStr(
position.inMinutes.remainder(60),
);
final currentSeconds = PrimitiveUtils.zeroPadNumStr(
position.inSeconds.remainder(60),
);

final progress = useState<num>(
useMemoized(() => progressStatic, []),
);
Expand Down Expand Up @@ -173,8 +160,8 @@ class PlayerControls extends HookConsumerWidget {
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("$currentMinutes:$currentSeconds"),
Text("$totalMinutes:$totalSeconds"),
Text(position.toHumanReadableString()),
Text(duration.toHumanReadableString()),
],
),
),
Expand Down
5 changes: 2 additions & 3 deletions lib/components/player/sibling_tracks_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:spotube/collections/spotube_icons.dart';
import 'package:spotube/components/shared/image/universal_image.dart';
import 'package:spotube/extensions/constrains.dart';
import 'package:spotube/extensions/context.dart';
import 'package:spotube/extensions/duration.dart';
import 'package:spotube/hooks/use_debounce.dart';
import 'package:spotube/models/matched_track.dart';
import 'package:spotube/models/spotube_track.dart';
Expand Down Expand Up @@ -99,9 +100,7 @@ class SiblingTracksSheet extends HookConsumerWidget {
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
trailing: Text(
PrimitiveUtils.toReadableDuration(video.duration),
),
trailing: Text(video.duration.toHumanReadableString()),
subtitle: Text(video.channelName),
enabled: playlist.isFetching != true,
selected: playlist.isFetching != true &&
Expand Down
2 changes: 1 addition & 1 deletion lib/components/shared/track_table/track_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class TrackTile extends HookConsumerWidget {
const SizedBox(width: 8),
Text(
Duration(milliseconds: track.durationMs ?? 0)
.toHumanReadableString(),
.toHumanReadableString(padZero: false),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
Expand Down
17 changes: 14 additions & 3 deletions lib/extensions/duration.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import 'package:duration/locale.dart';
import 'package:spotube/utils/primitive_utils.dart';
import 'package:duration/duration.dart';

extension DurationToHumanReadableString on Duration {
String toHumanReadableString() =>
"${inMinutes.remainder(60)}:${PrimitiveUtils.zeroPadNumStr(inSeconds.remainder(60))}";
String toHumanReadableString({padZero = true}) {
final mm = inMinutes
.remainder(60)
.toString()
.padLeft(2, !padZero && inHours == 0 ? '' : "0");
final ss = inSeconds.remainder(60).toString().padLeft(2, "0");

if (inHours > 0) {
final hh = inHours.toString().padLeft(2, !padZero ? '' : "0");
return "$hh:$mm:$ss";
}

return "$mm:$ss";
}

String format({
DurationTersity tersity = DurationTersity.second,
Expand Down
11 changes: 0 additions & 11 deletions lib/utils/primitive_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ abstract class PrimitiveUtils {
}
}

static String zeroPadNumStr(int input) {
return input < 10 ? "0$input" : input.toString();
}

static String toReadableDuration(Duration duration) {
final hours = duration.inHours;
final minutes = duration.inMinutes % 60;
final seconds = duration.inSeconds % 60;
return "${hours > 0 ? "${zeroPadNumStr(hours)}:" : ""}${zeroPadNumStr(minutes)}:${zeroPadNumStr(seconds)}";
}

static Future<T> raceMultiple<T>(
Future<T> Function() inner, {
Duration timeout = const Duration(milliseconds: 2500),
Expand Down

0 comments on commit de335f4

Please sign in to comment.