Skip to content

Commit

Permalink
fix(playbutton_card): play and add to queue needs 2 clicks work
Browse files Browse the repository at this point in the history
feat: add disk caching to liked tracks and categories query
  • Loading branch information
KRTirtho committed Mar 2, 2023
1 parent f5dc76a commit bdd7098
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/extensions/map.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extension CastDeepMaps on Map {
Map<K2, dynamic> castKeyDeep<K2>() {
return cast<K2, dynamic>().map((key, value) {
if (value is Map) {
return MapEntry(key, value.castKeyDeep<K2>());
} else if (value is List) {
return MapEntry(
key,
value.map((e) => e is Map ? e.castKeyDeep<K2>() : e).toList(),
);
}
return MapEntry(key, value);
});
}
}
61 changes: 61 additions & 0 deletions lib/extensions/page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'package:spotify/spotify.dart';

extension CursorPageJson<T> on CursorPage<T> {
static CursorPage<T> fromJson<T>(
Map<String, dynamic> json,
T Function(dynamic json) itemFromJson,
) {
final metadata = Paging.fromJson(json["metadata"]);
final paging = CursorPaging<T>();
paging.cursors = Cursor.fromJson(json["metadata"])..after = json["after"];
paging.href = metadata.href;
paging.itemsNative = paging.itemsNative;
paging.limit = metadata.limit;
paging.next = metadata.next;
return CursorPage<T>(
paging,
itemFromJson,
);
}

Map<String, dynamic> toJson() {
return {
"after": after,
"metadata": metadata.toJson(),
};
}
}

extension PagingToJson<T> on Paging<T> {
Map<String, dynamic> toJson() {
return {
"items": itemsNative,
"total": total,
"next": next,
"previous": previous,
"limit": limit,
"offset": offset,
"href": href,
};
}
}

extension PageJson<T> on Page<T> {
static Page<T> fromJson<T>(
Map<String, dynamic> json,
T Function(dynamic json) itemFromJson,
) {
return Page<T>(
Paging<T>.fromJson(
Map.castFrom<dynamic, dynamic, String, dynamic>(json["metadata"]),
),
itemFromJson,
);
}

Map<String, dynamic> toJson() {
return {
"metadata": metadata.toJson(),
};
}
}
11 changes: 11 additions & 0 deletions lib/services/queries/category.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:fl_query/fl_query.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/extensions/map.dart';
import 'package:spotube/extensions/page.dart';
import 'package:spotube/hooks/use_spotify_infinite_query.dart';

class CategoryQueries {
Expand All @@ -24,6 +26,15 @@ class CategoryQueries {
}
return lastPageData.nextOffset;
},
jsonConfig: JsonConfig<Page<Category>>(
toJson: (page) => page.toJson(),
fromJson: (json) => PageJson.fromJson<Category>(
json,
(json) {
return Category.fromJson((json as Map).castKeyDeep<String>());
},
),
),
ref: ref,
);
}
Expand Down
14 changes: 14 additions & 0 deletions lib/services/queries/playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:catcher/catcher.dart';
import 'package:fl_query/fl_query.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/extensions/map.dart';
import 'package:spotube/extensions/track.dart';
import 'package:spotube/hooks/use_spotify_infinite_query.dart';
import 'package:spotube/hooks/use_spotify_query.dart';

Expand Down Expand Up @@ -51,6 +53,18 @@ class PlaylistQueries {
return useSpotifyQuery<List<Track>, dynamic>(
"playlist-tracks/$playlistId",
(spotify) => tracksOf(playlistId, spotify),
jsonConfig: playlistId == "user-liked-tracks"
? JsonConfig(
toJson: (tracks) => <String, dynamic>{
'tracks': tracks.map((e) => e.toJson()).toList()
},
fromJson: (json) => (json['tracks'] as List)
.map((e) => Track.fromJson(
(e as Map).castKeyDeep<String>(),
))
.toList(),
)
: null,
ref: ref,
);
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ packages:
description:
path: "packages/fl_query"
ref: new-architecture
resolved-ref: cf2550a2909d0cb957324fe114acacb431a5f33a
resolved-ref: "64d661779a88c845e5280f3da99a2bd90bdc586b"
url: "https://github.com/KRTirtho/fl-query.git"
source: git
version: "0.3.1"
Expand All @@ -544,7 +544,7 @@ packages:
description:
path: "packages/fl_query_hooks"
ref: new-architecture
resolved-ref: cf2550a2909d0cb957324fe114acacb431a5f33a
resolved-ref: "64d661779a88c845e5280f3da99a2bd90bdc586b"
url: "https://github.com/KRTirtho/fl-query.git"
source: git
version: "0.3.1"
Expand Down

0 comments on commit bdd7098

Please sign in to comment.