From d3e4e5ffb89b8f47ee04465db62cde7003c15679 Mon Sep 17 00:00:00 2001 From: devaryakjha Date: Mon, 23 Oct 2023 21:30:47 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix:=20Favorite=20/=20Unfavorite?= =?UTF-8?q?=20not=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cubit/user_library_state.dart | 6 ++--- .../user-library/data/user_library.dart | 26 +++++++++++++------ .../data/user_library_repository.dart | 4 +-- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/features/user-library/cubit/user_library_state.dart b/lib/features/user-library/cubit/user_library_state.dart index 63381a9..467bc3e 100644 --- a/lib/features/user-library/cubit/user_library_state.dart +++ b/lib/features/user-library/cubit/user_library_state.dart @@ -28,10 +28,10 @@ class UserLibraryLoaded extends UserLibraryState { @override List get props => [library]; - Favorite get favorite => library.firstWhere( - (library) => library is Favorite, + UserLibrary get favorite => library.firstWhere( + (library) => library.id == Favorite.boxKey, orElse: () => const Favorite.empty(), - ) as Favorite; + ); bool get isEmpty => library.isEmpty; diff --git a/lib/features/user-library/data/user_library.dart b/lib/features/user-library/data/user_library.dart index a4d0721..70437f2 100644 --- a/lib/features/user-library/data/user_library.dart +++ b/lib/features/user-library/data/user_library.dart @@ -1,3 +1,4 @@ +// ignore_for_file: public_member_api_docs, sort_constructors_first import 'package:equatable/equatable.dart'; import 'package:hive/hive.dart'; import 'package:varanasi_mobile_app/models/image.dart'; @@ -79,6 +80,23 @@ class UserLibrary extends Equatable { mediaItems: mediaItems, ); } + + UserLibrary copyWith({ + String? id, + String? title, + String? description, + List? mediaItems, + List? images, + }) { + return UserLibrary( + type: type, + id: id ?? this.id, + title: title ?? this.title, + description: description ?? this.description, + mediaItems: mediaItems ?? this.mediaItems, + images: images ?? this.images, + ); + } } @HiveType(typeId: 19) @@ -96,14 +114,6 @@ final class Favorite extends UserLibrary { static const String boxKey = "favorite"; const Favorite.empty() : super.empty(UserLibraryType.favorite); - - Favorite copyWith({ - List? mediaItems, - }) { - return Favorite( - mediaItems: mediaItems ?? this.mediaItems, - ); - } } @HiveType(typeId: 20) diff --git a/lib/features/user-library/data/user_library_repository.dart b/lib/features/user-library/data/user_library_repository.dart index 00528d4..bff99d0 100644 --- a/lib/features/user-library/data/user_library_repository.dart +++ b/lib/features/user-library/data/user_library_repository.dart @@ -37,7 +37,7 @@ class UserLibraryRepository { final favourites = _box.get( Favorite.boxKey, defaultValue: const Favorite.empty(), - ) as Favorite; + ) as UserLibrary; final newFavourites = favourites.copyWith( mediaItems: [...favourites.mediaItems, song], ); @@ -48,7 +48,7 @@ class UserLibraryRepository { final favourites = _box.get( Favorite.boxKey, defaultValue: const Favorite.empty(), - ) as Favorite; + ) as UserLibrary; final newFavourites = favourites.copyWith( mediaItems: favourites.mediaItems.where((e) => e.id != song.id).toList(), );