refactor: create separate BLoCs for each screen with comprehensive tests
- 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
This commit is contained in:
93
bookshelf_flutter/lib/bloc/wishlist/wishlist_bloc.dart
Normal file
93
bookshelf_flutter/lib/bloc/wishlist/wishlist_bloc.dart
Normal file
@@ -0,0 +1,93 @@
|
||||
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,
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user