mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 16:05:18 +00:00
fix(playbutton_card): play and add to queue needs 2 clicks work
feat: add disk caching to liked tracks and categories query
This commit is contained in:
parent
f5dc76a98f
commit
bdd70984e6
15
lib/extensions/map.dart
Normal file
15
lib/extensions/map.dart
Normal file
@ -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
lib/extensions/page.dart
Normal file
61
lib/extensions/page.dart
Normal file
@ -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(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
import 'package:fl_query/fl_query.dart';
|
import 'package:fl_query/fl_query.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
import 'package:spotify/spotify.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';
|
import 'package:spotube/hooks/use_spotify_infinite_query.dart';
|
||||||
|
|
||||||
class CategoryQueries {
|
class CategoryQueries {
|
||||||
@ -24,6 +26,15 @@ class CategoryQueries {
|
|||||||
}
|
}
|
||||||
return lastPageData.nextOffset;
|
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,
|
ref: ref,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ import 'package:catcher/catcher.dart';
|
|||||||
import 'package:fl_query/fl_query.dart';
|
import 'package:fl_query/fl_query.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
import 'package:spotify/spotify.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_infinite_query.dart';
|
||||||
import 'package:spotube/hooks/use_spotify_query.dart';
|
import 'package:spotube/hooks/use_spotify_query.dart';
|
||||||
|
|
||||||
@ -51,6 +53,18 @@ class PlaylistQueries {
|
|||||||
return useSpotifyQuery<List<Track>, dynamic>(
|
return useSpotifyQuery<List<Track>, dynamic>(
|
||||||
"playlist-tracks/$playlistId",
|
"playlist-tracks/$playlistId",
|
||||||
(spotify) => tracksOf(playlistId, spotify),
|
(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,
|
ref: ref,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -535,7 +535,7 @@ packages:
|
|||||||
description:
|
description:
|
||||||
path: "packages/fl_query"
|
path: "packages/fl_query"
|
||||||
ref: new-architecture
|
ref: new-architecture
|
||||||
resolved-ref: cf2550a2909d0cb957324fe114acacb431a5f33a
|
resolved-ref: "64d661779a88c845e5280f3da99a2bd90bdc586b"
|
||||||
url: "https://github.com/KRTirtho/fl-query.git"
|
url: "https://github.com/KRTirtho/fl-query.git"
|
||||||
source: git
|
source: git
|
||||||
version: "0.3.1"
|
version: "0.3.1"
|
||||||
@ -544,7 +544,7 @@ packages:
|
|||||||
description:
|
description:
|
||||||
path: "packages/fl_query_hooks"
|
path: "packages/fl_query_hooks"
|
||||||
ref: new-architecture
|
ref: new-architecture
|
||||||
resolved-ref: cf2550a2909d0cb957324fe114acacb431a5f33a
|
resolved-ref: "64d661779a88c845e5280f3da99a2bd90bdc586b"
|
||||||
url: "https://github.com/KRTirtho/fl-query.git"
|
url: "https://github.com/KRTirtho/fl-query.git"
|
||||||
source: git
|
source: git
|
||||||
version: "0.3.1"
|
version: "0.3.1"
|
||||||
|
Loading…
Reference in New Issue
Block a user