- Fixed test file import paths to point to correct Bloc file locations - Fixed Bloc file import paths for models (../../../models/models.dart) - Added explicit type annotations to resolve null safety warnings - Fixed null safety issues in wishlist_bloc_test.dart - All 70 tests now passing
124 lines
4.1 KiB
Dart
124 lines
4.1 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import '../../../models/models.dart';
|
||
import 'library_event.dart';
|
||
import 'library_state.dart';
|
||
|
||
class LibraryBloc extends Bloc<LibraryEvent, LibraryState> {
|
||
LibraryBloc() : super(LibraryState.initial()) {
|
||
on<LoadBooks>(_onLoadBooks);
|
||
on<SearchBooks>(_onSearchBooks);
|
||
on<BookSelected>(_onBookSelected);
|
||
on<FilterByStatus>(_onFilterByStatus);
|
||
on<ClearFilters>(_onClearFilters);
|
||
}
|
||
|
||
static List<Book> get _initialBooks => [
|
||
createBook(
|
||
id: '1',
|
||
title: 'Великий Гэтсби',
|
||
author: 'Ф. Скотт Фицджеральд',
|
||
genre: 'Classic',
|
||
annotation:
|
||
'История о несбывшейся любви и трагедии американской мечты на фоне бурных двадцатых годов.',
|
||
coverUrl: 'https://picsum.photos/seed/gatsby/400/600',
|
||
pages: 208,
|
||
language: 'English',
|
||
publishedYear: 1925,
|
||
rating: 4.8,
|
||
status: BookStatus.reading,
|
||
progress: 45,
|
||
isFavorite: true,
|
||
),
|
||
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,
|
||
),
|
||
createBook(
|
||
id: '3',
|
||
title: 'Дюна',
|
||
author: 'Фрэнк Герберт',
|
||
genre: 'Sci-Fi',
|
||
annotation:
|
||
'Эпическая сага о борьбе за власть над самой важной планетой во Вселенной.',
|
||
coverUrl: 'https://picsum.photos/seed/dune/400/600',
|
||
pages: 896,
|
||
language: 'English',
|
||
publishedYear: 1965,
|
||
rating: 4.7,
|
||
status: BookStatus.reading,
|
||
progress: 12,
|
||
isFavorite: false,
|
||
),
|
||
createBook(
|
||
id: '4',
|
||
title: 'Хоббит',
|
||
author: 'Дж. Р. Р. Толкин',
|
||
genre: 'Fantasy',
|
||
annotation:
|
||
'Путешествие Бильбо Бэггинса туда и обратно в поисках сокровищ гномов.',
|
||
coverUrl: 'https://picsum.photos/seed/hobbit/400/600',
|
||
pages: 310,
|
||
language: 'English',
|
||
publishedYear: 1937,
|
||
rating: 4.9,
|
||
status: BookStatus.done,
|
||
isFavorite: false,
|
||
),
|
||
];
|
||
|
||
void _onLoadBooks(LoadBooks event, Emitter<LibraryState> emit) {
|
||
emit(state.copyWith(
|
||
books: _initialBooks,
|
||
filteredBooks: _initialBooks,
|
||
isLoading: false,
|
||
));
|
||
}
|
||
|
||
void _onSearchBooks(SearchBooks event, Emitter<LibraryState> emit) {
|
||
final query = event.query.toLowerCase();
|
||
final filtered = state.books.where((Book 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 _onBookSelected(BookSelected event, Emitter<LibraryState> emit) {
|
||
emit(state.copyWith(selectedBook: event.book));
|
||
}
|
||
|
||
void _onFilterByStatus(FilterByStatus event, Emitter<LibraryState> emit) {
|
||
final filtered = state.books.where((Book book) {
|
||
return book.status == event.status;
|
||
}).toList();
|
||
|
||
emit(state.copyWith(
|
||
statusFilter: event.status,
|
||
filteredBooks: filtered,
|
||
));
|
||
}
|
||
|
||
void _onClearFilters(ClearFilters event, Emitter<LibraryState> emit) {
|
||
emit(state.copyWith(
|
||
searchQuery: '',
|
||
statusFilter: null,
|
||
filteredBooks: state.books,
|
||
));
|
||
}
|
||
} |