Skip to content

Commit

Permalink
fix: 0:00 media duration in queue after application restart #782
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Oct 29, 2023
1 parent 4956bf3 commit 83c0b49
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 60 deletions.
41 changes: 22 additions & 19 deletions lib/extensions/track.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@ import 'package:spotube/extensions/artist_simple.dart';

extension TrackJson on Track {
Map<String, dynamic> toJson() {
return TrackJson.trackToJson(this);
}

static Map<String, dynamic> trackToJson(Track track) {
return {
"album": album?.toJson(),
"artists": artists?.map((artist) => artist.toJson()).toList(),
"availableMarkets": availableMarkets?.map((e) => e.name).toList(),
"discNumber": discNumber,
"duration": duration.toString(),
"durationMs": durationMs,
"explicit": explicit,
// "externalIds": externalIds,
// "externalUrls": externalUrls,
"href": href,
"id": id,
"isPlayable": isPlayable,
// "linkedFrom": linkedFrom,
"name": name,
"popularity": popularity,
"previewUrl": previewUrl,
"trackNumber": trackNumber,
"type": type,
"uri": uri,
"album": track.album?.toJson(),
"artists": track.artists?.map((artist) => artist.toJson()).toList(),
"available_markets": track.availableMarkets?.map((e) => e.name).toList(),
"disc_number": track.discNumber,
"duration_ms": track.durationMs,
"explicit": track.explicit,
// "external_ids"track.: externalIds,
// "external_urls"track.: externalUrls,
"href": track.href,
"id": track.id,
"is_playable": track.isPlayable,
// "linked_from"track.: linkedFrom,
"name": track.name,
"popularity": track.popularity,
"preview_rrl": track.previewUrl,
"track_number": track.trackNumber,
"type": track.type,
"uri": track.uri,
};
}
}
20 changes: 2 additions & 18 deletions lib/models/local_track.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:spotify/spotify.dart';
import 'package:spotube/extensions/album_simple.dart';
import 'package:spotube/extensions/artist_simple.dart';
import 'package:spotube/extensions/track.dart';

class LocalTrack extends Track {
final String path;
Expand Down Expand Up @@ -38,22 +37,7 @@ class LocalTrack extends Track {

Map<String, dynamic> toJson() {
return {
"album": album?.toJson(),
"artists": artists?.map((artist) => artist.toJson()).toList(),
"availableMarkets": availableMarkets?.map((m) => m.name),
"discNumber": discNumber,
"duration": duration.toString(),
"durationMs": durationMs,
"explicit": explicit,
"href": href,
"id": id,
"isPlayable": isPlayable,
"name": name,
"popularity": popularity,
"previewUrl": previewUrl,
"trackNumber": trackNumber,
"type": type,
"uri": uri,
...TrackJson.trackToJson(this),
'path': path,
};
}
Expand Down
20 changes: 2 additions & 18 deletions lib/models/spotube_track.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'dart:async';

import 'package:spotify/spotify.dart';
import 'package:spotube/extensions/album_simple.dart';
import 'package:spotube/extensions/artist_simple.dart';
import 'package:spotube/extensions/track.dart';
import 'package:spotube/models/matched_track.dart';
import 'package:spotube/provider/user_preferences_provider.dart';
import 'package:spotube/services/youtube/youtube.dart';
Expand Down Expand Up @@ -264,22 +263,7 @@ class SpotubeTrack extends Track {
Map<String, dynamic> toJson() {
return {
// super values
"album": album?.toJson(),
"artists": artists?.map((artist) => artist.toJson()).toList(),
"availableMarkets": availableMarkets?.map((m) => m.name),
"discNumber": discNumber,
"duration": duration.toString(),
"durationMs": durationMs,
"explicit": explicit,
"href": href,
"id": id,
"isPlayable": isPlayable,
"name": name,
"popularity": popularity,
"previewUrl": previewUrl,
"trackNumber": trackNumber,
"type": type,
"uri": uri,
...TrackJson.trackToJson(this),
// this values
"ytTrack": ytTrack.toJson(),
"ytUri": ytUri,
Expand Down
12 changes: 7 additions & 5 deletions lib/provider/proxy_playlist/proxy_playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ class ProxyPlaylist {
}
}

/// To make sure proper instance method is used for JSON serialization
/// Otherwise default super.toJson() is used
static Map<String, dynamic> _makeAppropriateTrackJson(Track track) {
if (track is LocalTrack) {
return track.toJson();
} else {
return track.toJson();
}
return switch (track.runtimeType) {
LocalTrack => track.toJson(),
SpotubeTrack => track.toJson(),
_ => track.toJson(),
};
}

Map<String, dynamic> toJson() {
Expand Down

0 comments on commit 83c0b49

Please sign in to comment.