spotify-dart icon indicating copy to clipboard operation
spotify-dart copied to clipboard

Can I sort data by popularity from search response?

Open vovaklh opened this issue 2 years ago • 3 comments

Now search response contains a List of pages. Each page contains tracks or albums, or something else. How I can combine tracks and albums on UI and sort data by popularity?

vovaklh avatar Feb 22 '23 16:02 vovaklh

In the search, the access of the popularity property can only be done throughArtist and Track. It is not possible with Album as the search uses a simple version (AlbumSimple) of the Album model, which does not contain the popularity property. I think an investigation is necessary here, if Album can bu used in the search handling.

What would also be interesting is to extract common properties like href of popularity into parent objects, so that sorting by popularity beyond the search result type (Album, Track etc.) can be possible. @rinukkusu What do you think about this?

@vovaklh, I would filter out the Album, Tracks etc. of the search results into a separate lists and sort each type by popularity.

hayribakici avatar Feb 27 '23 11:02 hayribakici

Yeah, we'd need to look into that Album issue. The Spotify API docs sadly don't really make clear when they're using the stripped down version of their entities and when not. Sometimes you can deduct it from the request/response examples, but they don't seem to be always accurate.

rinukkusu avatar Mar 09 '23 06:03 rinukkusu

Regarding this issue, I am suggesting this solution:

class Popularity {
  int? popularity;
}

so that Artist and Track can implement it with

class Track extends TrackSimple implements Popularity {
...
}

, so that it is possible to extract the types with a popularity property by implementing

  var search = await spotify.search.get('metallica').first(2);

  search.forEach((pages) {
    var pops = pages.items!.whereType<Popularity>().toList();
    pops.sort((a, b) => a.popularity?.compareTo(b.popularity ?? 0) ?? 0);
    ...
  }

@rinukkusu @vovaklh What do you think?

hayribakici avatar Apr 09 '23 23:04 hayribakici