48 lines
1.3 KiB
Dart
48 lines
1.3 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../constants/constants.dart';
|
|
import 'book_event.dart';
|
|
import 'book_state.dart';
|
|
|
|
class BookBloc extends Bloc<BookEvent, BookState> {
|
|
BookBloc() : super(const BookState(books: initialBooks)) {
|
|
on<AddBook>((event, emit) {
|
|
emit(BookState(books: [...state.books, event.book]));
|
|
});
|
|
|
|
on<UpdateBook>((event, emit) {
|
|
final updated = state.books.map((b) {
|
|
return b.id == event.book.id ? event.book : b;
|
|
}).toList();
|
|
emit(BookState(books: updated));
|
|
});
|
|
|
|
on<DeleteBook>((event, emit) {
|
|
emit(
|
|
BookState(books: state.books.where((b) => b.id != event.id).toList()),
|
|
);
|
|
});
|
|
|
|
on<ToggleFavorite>((event, emit) {
|
|
final updated = state.books.map((b) {
|
|
if (b.id != event.id) return b;
|
|
return (
|
|
id: b.id,
|
|
title: b.title,
|
|
author: b.author,
|
|
genre: b.genre,
|
|
annotation: b.annotation,
|
|
coverUrl: b.coverUrl,
|
|
pages: b.pages,
|
|
language: b.language,
|
|
publishedYear: b.publishedYear,
|
|
rating: b.rating,
|
|
status: b.status,
|
|
progress: b.progress,
|
|
isFavorite: !b.isFavorite,
|
|
);
|
|
}).toList();
|
|
emit(BookState(books: updated));
|
|
});
|
|
}
|
|
}
|