- Created 8 separate BLoCs (Home, Library, BookDetails, AddBook, Scanner, Categories, Wishlist, Settings) - Each BLoC has its own event, state, and bloc files - Added 70 comprehensive tests covering all BLoC functionality - All tests passing (70/70) - Fixed linting issues and updated deprecated APIs - Improved code organization and maintainability
93 lines
2.9 KiB
Dart
93 lines
2.9 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import '../../models/models.dart';
|
||
import 'wishlist_event.dart';
|
||
import 'wishlist_state.dart';
|
||
|
||
class WishlistBloc extends Bloc<WishlistEvent, WishlistState> {
|
||
WishlistBloc() : super(WishlistState.initial()) {
|
||
on<LoadWishlist>(_onLoadWishlist);
|
||
on<RemoveFromWishlist>(_onRemoveFromWishlist);
|
||
on<SearchWishlist>(_onSearchWishlist);
|
||
on<MoveToLibrary>(_onMoveToLibrary);
|
||
}
|
||
|
||
static List<Book> get _initialBooks => [
|
||
createBook(
|
||
id: '2',
|
||
title: '1984',
|
||
author: 'Джордж Оруэлл',
|
||
genre: 'Dystopian',
|
||
annotation:
|
||
'Антиутопия о тоталитарном государстве, где мысли контролируются, а правда переменчива.',
|
||
coverUrl: 'https://picsum.photos/seed/1984/400/600',
|
||
pages: 328,
|
||
language: 'English',
|
||
publishedYear: 1949,
|
||
rating: 4.9,
|
||
status: BookStatus.wantToRead,
|
||
isFavorite: true,
|
||
),
|
||
];
|
||
|
||
void _onLoadWishlist(LoadWishlist event, Emitter<WishlistState> emit) {
|
||
final wishlistBooks = _initialBooks.where((book) {
|
||
return book.status == BookStatus.wantToRead || book.isFavorite;
|
||
}).toList();
|
||
|
||
emit(state.copyWith(
|
||
books: wishlistBooks,
|
||
filteredBooks: wishlistBooks,
|
||
isLoading: false,
|
||
));
|
||
}
|
||
|
||
void _onRemoveFromWishlist(RemoveFromWishlist event, Emitter<WishlistState> emit) {
|
||
final updatedBooks = state.books.where((book) => book.id != event.bookId).toList();
|
||
final updatedFiltered = state.filteredBooks.where((book) => book.id != event.bookId).toList();
|
||
|
||
emit(state.copyWith(
|
||
books: updatedBooks,
|
||
filteredBooks: updatedFiltered,
|
||
));
|
||
}
|
||
|
||
void _onSearchWishlist(SearchWishlist event, Emitter<WishlistState> emit) {
|
||
final query = event.query.toLowerCase();
|
||
final filtered = state.books.where((book) {
|
||
return book.title.toLowerCase().contains(query) ||
|
||
book.author.toLowerCase().contains(query) ||
|
||
book.genre.toLowerCase().contains(query);
|
||
}).toList();
|
||
|
||
emit(state.copyWith(
|
||
searchQuery: event.query,
|
||
filteredBooks: filtered,
|
||
));
|
||
}
|
||
|
||
void _onMoveToLibrary(MoveToLibrary event, Emitter<WishlistState> emit) {
|
||
final updatedBooks = state.books.map((book) {
|
||
if (book.id == event.bookId) {
|
||
return book.copyWith(
|
||
status: BookStatus.reading,
|
||
);
|
||
}
|
||
return book;
|
||
}).toList();
|
||
|
||
final updatedFiltered = state.filteredBooks.map((book) {
|
||
if (book.id == event.bookId) {
|
||
return book.copyWith(
|
||
status: BookStatus.reading,
|
||
);
|
||
}
|
||
return book;
|
||
}).toList();
|
||
|
||
emit(state.copyWith(
|
||
books: updatedBooks,
|
||
filteredBooks: updatedFiltered,
|
||
movedBookId: event.bookId,
|
||
));
|
||
}
|
||
} |