Skip to content

Commit

Permalink
🐛 Fix: Favorite / Unfavorite not working
Browse files Browse the repository at this point in the history
  • Loading branch information
devaryakjha committed Oct 23, 2023
1 parent 9671f63 commit d3e4e5f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/features/user-library/cubit/user_library_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class UserLibraryLoaded extends UserLibraryState {
@override
List<Object> 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;

Expand Down
26 changes: 18 additions & 8 deletions lib/features/user-library/data/user_library.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -79,6 +80,23 @@ class UserLibrary extends Equatable {
mediaItems: mediaItems,
);
}

UserLibrary copyWith({
String? id,
String? title,
String? description,
List<Song>? mediaItems,
List<Image>? 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)
Expand All @@ -96,14 +114,6 @@ final class Favorite extends UserLibrary {
static const String boxKey = "favorite";

const Favorite.empty() : super.empty(UserLibraryType.favorite);

Favorite copyWith({
List<Song>? mediaItems,
}) {
return Favorite(
mediaItems: mediaItems ?? this.mediaItems,
);
}
}

@HiveType(typeId: 20)
Expand Down
4 changes: 2 additions & 2 deletions lib/features/user-library/data/user_library_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);
Expand All @@ -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(),
);
Expand Down

0 comments on commit d3e4e5f

Please sign in to comment.