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:
124
bookshelf_flutter/lib/bloc/library/library_bloc.dart
Normal file
124
bookshelf_flutter/lib/bloc/library/library_bloc.dart
Normal file
@@ -0,0 +1,124 @@
|
||||
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) {
|
||||
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) {
|
||||
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,
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user