Skip to content

Commit

Permalink
fix: sorting by date crashes app (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Aug 26, 2023
1 parent fb36003 commit 48e90a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/provider/download_manager_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DownloadManagerProvider extends ChangeNotifier {
album: track.album?.name,
albumArtist: track.artists?.map((a) => a.name).join(", "),
year: track.album?.releaseDate != null
? int.tryParse(track.album!.releaseDate!) ?? 1969
? int.tryParse(track.album!.releaseDate!.split("-").first) ?? 1969
: 1969,
trackNumber: track.trackNumber,
discNumber: track.discNumber,
Expand Down
23 changes: 19 additions & 4 deletions lib/utils/service_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ abstract class ServiceUtils {
router.push(location, extra: extra);
}

static DateTime parseSpotifyAlbumDate(AlbumSimple? album) {
if (album == null || album.releaseDate == null) {
return DateTime.parse("1975-01-01");
}

switch (album.releaseDatePrecision ?? DatePrecision.year) {
case DatePrecision.day:
return DateTime.parse(album.releaseDate!);
case DatePrecision.month:
return DateTime.parse("${album.releaseDate}-01");
case DatePrecision.year:
return DateTime.parse("${album.releaseDate}-01-01");
}
}

static List<T> sortTracks<T extends Track>(List<T> tracks, SortBy sortBy) {
if (sortBy == SortBy.none) return tracks;
return List<T>.from(tracks)
Expand All @@ -286,12 +301,12 @@ abstract class ServiceUtils {
case SortBy.ascending:
return a.name?.compareTo(b.name ?? "") ?? 0;
case SortBy.oldest:
final aDate = DateTime.parse(a.album?.releaseDate ?? "2069-01-01");
final bDate = DateTime.parse(b.album?.releaseDate ?? "2069-01-01");
final aDate = parseSpotifyAlbumDate(a.album);
final bDate = parseSpotifyAlbumDate(b.album);
return aDate.compareTo(bDate);
case SortBy.newest:
final aDate = DateTime.parse(a.album?.releaseDate ?? "2069-01-01");
final bDate = DateTime.parse(b.album?.releaseDate ?? "2069-01-01");
final aDate = parseSpotifyAlbumDate(a.album);
final bDate = parseSpotifyAlbumDate(b.album);
return bDate.compareTo(aDate);
case SortBy.descending:
return b.name?.compareTo(a.name ?? "") ?? 0;
Expand Down
24 changes: 2 additions & 22 deletions lib/utils/type_conversion_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import 'package:path/path.dart';
import 'package:spotube/collections/assets.gen.dart';
import 'package:spotube/components/shared/links/anchor_button.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/models/matched_track.dart';
import 'package:spotube/models/spotube_track.dart';
import 'package:spotube/services/youtube/youtube.dart';
import 'package:spotube/utils/primitive_utils.dart';
import 'package:spotube/utils/service_utils.dart';

Expand Down Expand Up @@ -122,29 +119,12 @@ abstract class TypeConversionUtils {
return track;
}

static SpotubeTrack localTrack_X_Track(
static Track localTrack_X_Track(
File file, {
Metadata? metadata,
String? art,
}) {
final track = SpotubeTrack(
YoutubeVideoInfo(
searchMode: SearchMode.youtube,
id: "dQw4w9WgXcQ",
title: basenameWithoutExtension(file.path),
duration: Duration(milliseconds: metadata?.durationMs?.toInt() ?? 0),
dislikes: 0,
likes: 0,
thumbnailUrl: art ?? "",
views: 0,
channelName: metadata?.albumArtist ?? "Spotube",
channelId: metadata?.albumArtist ?? "Spotube",
publishedAt:
metadata?.year != null ? DateTime(metadata!.year!) : DateTime(2003),
),
file.path,
[],
);
final track = Track();
track.album = Album()
..name = metadata?.album ?? "Spotube"
..images = [if (art != null) Image()..url = art]
Expand Down

0 comments on commit 48e90a4

Please sign in to comment.