Skip to content

Commit

Permalink
🐛 Fix: favorite not working when empty
Browse files Browse the repository at this point in the history
  • Loading branch information
devaryakjha committed Oct 23, 2023
1 parent dc13439 commit bf43982
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
8 changes: 3 additions & 5 deletions lib/features/user-library/data/user_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ final class Favorite extends UserLibrary {

static const String boxKey = "favorite";

const Favorite.empty() : super.empty(UserLibraryType.favorite);
factory Favorite.empty() {
return const Favorite(mediaItems: []);
}
}

@HiveType(typeId: 20)
Expand All @@ -125,8 +127,6 @@ final class AlbumLibrary extends UserLibrary {
required super.mediaItems,
required super.images,
}) : super(type: UserLibraryType.album);

const AlbumLibrary.empty() : super.empty(UserLibraryType.album);
}

@HiveType(typeId: 21)
Expand All @@ -138,6 +138,4 @@ final class PlaylistLibrary extends UserLibrary {
required super.mediaItems,
required super.images,
}) : super(type: UserLibraryType.playlist);

const PlaylistLibrary.empty() : super.empty(UserLibraryType.playlist);
}
8 changes: 4 additions & 4 deletions lib/features/user-library/data/user_library_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class UserLibraryRepository {
Future<void> favoriteSong(Song song) async {
final favourites = _box.get(
Favorite.boxKey,
defaultValue: const Favorite.empty(),
) as UserLibrary;
defaultValue: Favorite.empty(),
)!;
final newFavourites = favourites.copyWith(
mediaItems: [...favourites.mediaItems, song],
);
Expand All @@ -47,8 +47,8 @@ class UserLibraryRepository {
Future<void> unfavoriteSong(Song song) async {
final favourites = _box.get(
Favorite.boxKey,
defaultValue: const Favorite.empty(),
) as UserLibrary;
defaultValue: Favorite.empty(),
)!;
final newFavourites = favourites.copyWith(
mediaItems: favourites.mediaItems.where((e) => e.id != song.id).toList(),
);
Expand Down

0 comments on commit bf43982

Please sign in to comment.