mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-12-08 16:27:31 +00:00
84 lines
2.1 KiB
Dart
84 lines
2.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:spotify/spotify.dart';
|
|
import 'package:spotube/provider/history/state.dart';
|
|
import 'package:spotube/utils/persisted_state_notifier.dart';
|
|
|
|
class PlaybackHistoryState {
|
|
final List<PlaybackHistoryBase> items;
|
|
const PlaybackHistoryState({this.items = const []});
|
|
|
|
factory PlaybackHistoryState.fromJson(Map<String, dynamic> json) {
|
|
return PlaybackHistoryState(
|
|
items:
|
|
json["items"]?.map((json) => PlaybackHistoryBase.fromJson(json)));
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
"items": items.map((s) => s.toJson()).toList(),
|
|
};
|
|
}
|
|
|
|
PlaybackHistoryState copyWith({
|
|
List<PlaybackHistoryBase>? items,
|
|
}) {
|
|
return PlaybackHistoryState(items: items ?? this.items);
|
|
}
|
|
}
|
|
|
|
class PlaybackHistoryNotifier
|
|
extends PersistedStateNotifier<PlaybackHistoryState> {
|
|
PlaybackHistoryNotifier()
|
|
: super(const PlaybackHistoryState(), "playback_history");
|
|
|
|
@override
|
|
FutureOr<PlaybackHistoryState> fromJson(Map<String, dynamic> json) =>
|
|
PlaybackHistoryState.fromJson(json);
|
|
|
|
@override
|
|
Map<String, dynamic> toJson() {
|
|
return state.toJson();
|
|
}
|
|
|
|
void addPlaylists(List<PlaylistSimple> playlists) {
|
|
state = state.copyWith(
|
|
items: [
|
|
...state.items,
|
|
for (final playlist in playlists)
|
|
PlaybackHistoryPlaylist(date: DateTime.now(), playlist: playlist),
|
|
],
|
|
);
|
|
}
|
|
|
|
void addAlbums(List<AlbumSimple> albums) {
|
|
state = state.copyWith(
|
|
items: [
|
|
...state.items,
|
|
for (final album in albums)
|
|
PlaybackHistoryAlbum(date: DateTime.now(), album: album),
|
|
],
|
|
);
|
|
}
|
|
|
|
void addTracks(List<TrackSimple> tracks) {
|
|
state = state.copyWith(
|
|
items: [
|
|
...state.items,
|
|
for (final track in tracks)
|
|
PlaybackHistoryTrack(date: DateTime.now(), track: track),
|
|
],
|
|
);
|
|
}
|
|
|
|
void clear() {
|
|
state = state.copyWith(items: []);
|
|
}
|
|
}
|
|
|
|
final playbackHistoryProvider =
|
|
StateNotifierProvider<PlaybackHistoryNotifier, PlaybackHistoryState>(
|
|
(ref) => PlaybackHistoryNotifier(),
|
|
);
|