Skip to content

Commit

Permalink
Allows Page with multiple types
Browse files Browse the repository at this point in the history
  • Loading branch information
hayribakici committed Nov 30, 2023
1 parent c1679f1 commit 2117d1f
Show file tree
Hide file tree
Showing 5 changed files with 548 additions and 99 deletions.
53 changes: 35 additions & 18 deletions lib/src/endpoints/endpoint_paging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ abstract class EndpointPaging extends EndpointBase {
[String? pageKey, ParserFunction<Object>? pageContainerParser]) =>
CursorPages(_api, path, pageItemParser, pageKey, pageContainerParser);

MultiPage _getMultiPage<T>(
String path, Map<String, ParserFunction<T>> itemMappers) =>
MultiPage(_api, path, itemMappers);

BundledPages _getBundledPages<T>(
String path, Map<String, ParserFunction<T>> pageItemParsers,
[String? pageKey, ParserFunction<Object>? pageContainerParser]) =>
Expand All @@ -36,10 +40,18 @@ abstract class BasePage<T> {
_container = pageContainer;
}

// BasePage.fromParserMap(this._paging, Map<String, ParserFunction<T> parserMap, [Object? pageContainer]) {
// _items = _paging.itemsNative!.map((e) => parserMap[e.])
// _container = pageContainer;
// }
BasePage.fromParserMap(
this._paging, Map<String, ParserFunction<dynamic>> parserMap,
[Object? pageContainer]) {
_items = _paging.itemsNative!.map((item) {
var type = item["track"]["type"] as String;
if (parserMap.containsKey(type)) {
return parserMap[type]!(item);
}
throw TypeError();
});
_container = pageContainer;
}

/// The offset-based paging object is a container for a set of objects. It
/// contains a key called items (whose value is an array of the requested
Expand Down Expand Up @@ -72,6 +84,11 @@ class Page<T> extends BasePage<T> {
[Object? pageContainer])
: super(paging, pageItemParser, pageContainer);

Page.fromParserMap(
Paging<T> paging, Map<String, ParserFunction<dynamic>> parserMap,
[Object? pageContainer])
: super.fromParserMap(paging, parserMap);

@override
bool get isLast {
var paging = _paging as Paging<T>;
Expand Down Expand Up @@ -290,24 +307,24 @@ class CursorPages<T> extends SinglePages<T, CursorPage<T>>
}
}

// class MultiPage extends SinglePages<dynamic, Page<dynamic>>
// with OffsetStrategy<Page<dynamic>> {
// final Map<String, ParserFunction<dynamic>> _pageMappers;
class MultiPage extends SinglePages<dynamic, Page<dynamic>>
with OffsetStrategy<Page<dynamic>> {
final Map<String, ParserFunction<dynamic>> _pageMappers;

// MultiPage(SpotifyApiBase api, String path, this._pageMappers)
// : super(api, path, null, null);
MultiPage(SpotifyApiBase api, String path, this._pageMappers)
: super(api, path, null, null);

// @override
// Future<Page<dynamic>> getPage(int limit, [int offset = 0]) async {
// var pathDelimiter = _path.contains('?') ? '&' : '?';
// var newPath = '$_path${pathDelimiter}limit=$limit&offset=$offset';
@override
Future<Page<dynamic>> getPage(int limit, [int offset = 0]) async {
var pathDelimiter = _path.contains('?') ? '&' : '?';
var newPath = '$_path${pathDelimiter}limit=$limit&offset=$offset';

// var jsonString = await _api._get(newPath);
// var map = Paging.fromJson(json.decode(jsonString));
var jsonString = await _api._get(newPath);
var paging = Paging.fromJson(json.decode(jsonString));

// for (var key in _pageMappers.keys) {}
// }
// }
return Page.fromParserMap(paging, _pageMappers);
}
}

/// Page that allows multiple types (artist, track, episode etc.) together
class BundledPages extends _Pages with OffsetStrategy<List<Page<dynamic>>> {
Expand Down
22 changes: 11 additions & 11 deletions lib/src/endpoints/playlists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class Playlists extends EndpointPaging {
(json) => Track.fromJson(json['track']));
}

// Pages<List<T>> getItemsByPlaylistId<T>(String playlistId,
// {Iterable<PlaylistFilter> filters = PlaylistFilter.values}) {
// final additionalTypes = filters.map((filter) => filter._key).join(',');
// final query = _buildQuery({'additional_types': additionalTypes});
// return _getPages('v1/playlists/$playlistId/tracks$query', {
// 'track': (json) => Track.fromJson(json),
// 'episode': (json) => EpisodeFull.fromJson(json)
// });
// }
MultiPage getItemsByPlaylistId<T>(String playlistId,
{Iterable<PlaylistFilter> filters = PlaylistFilter.values}) {
final additionalTypes = filters.map((filter) => filter._key).join(',');
final query = _buildQuery({'additional_types': additionalTypes});
return _getMultiPage('v1/playlists/$playlistId/tracks?$query', {
'track': (json) => Track.fromJson(json),
'episode': (json) => EpisodeFull.fromJson(json)
});
}

/// [userId] - the Spotify user ID
///
Expand Down Expand Up @@ -335,8 +335,8 @@ class Playlists extends EndpointPaging {
}

enum PlaylistFilter {
track(key: 'album'),
episode(key: 'artist');
track(key: 'track'),
episode(key: 'episode');

const PlaylistFilter({required String key}) : _key = key;

Expand Down
2 changes: 1 addition & 1 deletion test/data/v1/episodes/5Xt5DXGzch68nYYamXrNxZ.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
],
"is_externally_hosted": true,
"languages": [
"string"
"en"
],
"media_type": "string",
"name": "The No-Show",
Expand Down
Loading

0 comments on commit 2117d1f

Please sign in to comment.