From 83c0b49da962d9f3d40de9525f90f0b320e8f7b8 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Sun, 29 Oct 2023 20:19:03 +0600 Subject: [PATCH] fix: 0:00 media duration in queue after application restart #782 --- lib/extensions/track.dart | 41 ++++++++++--------- lib/models/local_track.dart | 20 +-------- lib/models/spotube_track.dart | 20 +-------- .../proxy_playlist/proxy_playlist.dart | 12 +++--- 4 files changed, 33 insertions(+), 60 deletions(-) diff --git a/lib/extensions/track.dart b/lib/extensions/track.dart index e17e851ec..51498b339 100644 --- a/lib/extensions/track.dart +++ b/lib/extensions/track.dart @@ -4,26 +4,29 @@ import 'package:spotube/extensions/artist_simple.dart'; extension TrackJson on Track { Map toJson() { + return TrackJson.trackToJson(this); + } + + static Map 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, }; } } diff --git a/lib/models/local_track.dart b/lib/models/local_track.dart index e297e9749..134cd327c 100644 --- a/lib/models/local_track.dart +++ b/lib/models/local_track.dart @@ -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; @@ -38,22 +37,7 @@ class LocalTrack extends Track { Map 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, }; } diff --git a/lib/models/spotube_track.dart b/lib/models/spotube_track.dart index a8b94ef5c..686410102 100644 --- a/lib/models/spotube_track.dart +++ b/lib/models/spotube_track.dart @@ -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'; @@ -264,22 +263,7 @@ class SpotubeTrack extends Track { Map 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, diff --git a/lib/provider/proxy_playlist/proxy_playlist.dart b/lib/provider/proxy_playlist/proxy_playlist.dart index c0563f21e..e5dfa7e8b 100644 --- a/lib/provider/proxy_playlist/proxy_playlist.dart +++ b/lib/provider/proxy_playlist/proxy_playlist.dart @@ -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 _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 toJson() {