Fix import paths and test issues
- 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
This commit is contained in:
124
bookshelf_flutter/lib/features/library/bloc/library_bloc.dart
Normal file
124
bookshelf_flutter/lib/features/library/bloc/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 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,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import '../../../models/models.dart';
|
||||
|
||||
abstract class LibraryEvent {
|
||||
const LibraryEvent();
|
||||
}
|
||||
|
||||
class LoadBooks extends LibraryEvent {
|
||||
const LoadBooks();
|
||||
}
|
||||
|
||||
class SearchBooks extends LibraryEvent {
|
||||
final String query;
|
||||
const SearchBooks(this.query);
|
||||
}
|
||||
|
||||
class BookSelected extends LibraryEvent {
|
||||
final Book book;
|
||||
const BookSelected(this.book);
|
||||
}
|
||||
|
||||
class FilterByStatus extends LibraryEvent {
|
||||
final BookStatus status;
|
||||
const FilterByStatus(this.status);
|
||||
}
|
||||
|
||||
class ClearFilters extends LibraryEvent {
|
||||
const ClearFilters();
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../../../models/models.dart';
|
||||
|
||||
class LibraryState extends Equatable {
|
||||
final List<Book> books;
|
||||
final List<Book> filteredBooks;
|
||||
final Book? selectedBook;
|
||||
final String searchQuery;
|
||||
final BookStatus? statusFilter;
|
||||
final bool isLoading;
|
||||
final String? errorMessage;
|
||||
|
||||
const LibraryState({
|
||||
this.books = const [],
|
||||
this.filteredBooks = const [],
|
||||
this.selectedBook,
|
||||
this.searchQuery = '',
|
||||
this.statusFilter,
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
factory LibraryState.initial() {
|
||||
return const LibraryState(
|
||||
isLoading: true,
|
||||
);
|
||||
}
|
||||
|
||||
LibraryState copyWith({
|
||||
List<Book>? books,
|
||||
List<Book>? filteredBooks,
|
||||
Book? selectedBook,
|
||||
String? searchQuery,
|
||||
BookStatus? statusFilter,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return LibraryState(
|
||||
books: books ?? this.books,
|
||||
filteredBooks: filteredBooks ?? this.filteredBooks,
|
||||
selectedBook: selectedBook,
|
||||
searchQuery: searchQuery ?? this.searchQuery,
|
||||
statusFilter: statusFilter ?? this.statusFilter,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
books,
|
||||
filteredBooks,
|
||||
selectedBook,
|
||||
searchQuery,
|
||||
statusFilter,
|
||||
isLoading,
|
||||
errorMessage,
|
||||
];
|
||||
}
|
||||
288
bookshelf_flutter/lib/features/library/library_screen.dart
Normal file
288
bookshelf_flutter/lib/features/library/library_screen.dart
Normal file
@@ -0,0 +1,288 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import '../../bloc/app_bloc.dart';
|
||||
import '../../bloc/app_event.dart';
|
||||
import '../../bloc/app_state.dart';
|
||||
import '../../../models/models.dart';
|
||||
|
||||
class LibraryScreen extends StatefulWidget {
|
||||
const LibraryScreen({super.key});
|
||||
|
||||
@override
|
||||
State<LibraryScreen> createState() => _LibraryScreenState();
|
||||
}
|
||||
|
||||
class _LibraryScreenState extends State<LibraryScreen> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: const Color(0xFF112116),
|
||||
body: Column(
|
||||
children: [
|
||||
_buildHeader(context),
|
||||
Expanded(child: _buildBookGrid(context)),
|
||||
],
|
||||
),
|
||||
floatingActionButton: Padding(
|
||||
padding: const EdgeInsets.only(bottom: 80),
|
||||
child: FloatingActionButton(
|
||||
onPressed: () {
|
||||
context.read<AppBloc>().add(const AddBookClicked());
|
||||
},
|
||||
backgroundColor: const Color(0xFF17CF54),
|
||||
elevation: 8,
|
||||
child: const Icon(Icons.add, size: 28),
|
||||
),
|
||||
),
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF112116).withValues(alpha: 0.95),
|
||||
border: Border(
|
||||
bottom: BorderSide(color: Colors.white.withValues(alpha: 0.05), width: 1),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const SizedBox(width: 40),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Книжная полка',
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
width: 40,
|
||||
child: Icon(
|
||||
Icons.notifications_outlined,
|
||||
color: Colors.white,
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
BlocBuilder<AppBloc, AppState>(
|
||||
buildWhen: (previous, current) =>
|
||||
previous.searchQuery != current.searchQuery,
|
||||
builder: (context, state) {
|
||||
return Container(
|
||||
height: 48,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF244730),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.white.withValues(alpha: 0.05)),
|
||||
),
|
||||
child: TextField(
|
||||
onChanged: (value) {
|
||||
context.read<AppBloc>().add(SearchChanged(value));
|
||||
},
|
||||
style: const TextStyle(color: Colors.white, fontSize: 16),
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Поиск по названию или автору...',
|
||||
hintStyle: const TextStyle(
|
||||
color: Color(0xFF93C8A5),
|
||||
),
|
||||
prefixIcon: const Icon(
|
||||
Icons.search,
|
||||
color: Color(0xFF93C8A5),
|
||||
size: 20,
|
||||
),
|
||||
border: InputBorder.none,
|
||||
contentPadding:
|
||||
const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBookGrid(BuildContext context) {
|
||||
return BlocBuilder<AppBloc, AppState>(
|
||||
builder: (context, state) {
|
||||
final filteredBooks = state.books.where((book) {
|
||||
final query = state.searchQuery.toLowerCase();
|
||||
return book.title.toLowerCase().contains(query) ||
|
||||
book.author.toLowerCase().contains(query);
|
||||
}).toList();
|
||||
|
||||
if (filteredBooks.isEmpty) {
|
||||
return Center(
|
||||
child: Text(
|
||||
'Книги не найдены',
|
||||
style: TextStyle(
|
||||
color: const Color(0xFF93C8A5),
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return GridView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
crossAxisSpacing: 16,
|
||||
mainAxisSpacing: 16,
|
||||
childAspectRatio: 0.67,
|
||||
),
|
||||
itemCount: filteredBooks.length,
|
||||
itemBuilder: (context, index) {
|
||||
final book = filteredBooks[index];
|
||||
return _buildBookCard(context, book);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBookCard(BuildContext context, Book book) {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
context.read<AppBloc>().add(BookClicked(book));
|
||||
},
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Stack(
|
||||
children: [
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
color: const Color(0xFF1A3222),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withValues(alpha: 0.3),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: book.coverUrl ??
|
||||
'https://picsum.photos/seed/placeholder/400/600',
|
||||
fit: BoxFit.cover,
|
||||
width: double.infinity,
|
||||
placeholder: (context, url) => const Center(
|
||||
child: CircularProgressIndicator(
|
||||
color: Color(0xFF17CF54),
|
||||
),
|
||||
),
|
||||
errorWidget: (context, url, error) => Container(
|
||||
color: const Color(0xFF1A3222),
|
||||
child: const Icon(
|
||||
Icons.book_outlined,
|
||||
size: 48,
|
||||
color: Color(0xFF93C8A5),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (book.status == BookStatus.reading && book.progress != null)
|
||||
Positioned(
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
child: Container(
|
||||
height: 4,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black.withValues(alpha: 0.5),
|
||||
),
|
||||
child: FractionallySizedBox(
|
||||
widthFactor: book.progress! / 100,
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Container(
|
||||
color: const Color(0xFF17CF54),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (book.status == BookStatus.done)
|
||||
Positioned(
|
||||
top: 8,
|
||||
left: 8,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8,
|
||||
vertical: 2,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF17CF54),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: const Text(
|
||||
'DONE',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (book.isFavorite)
|
||||
Positioned(
|
||||
top: 8,
|
||||
right: 8,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black.withValues(alpha: 0.6),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.favorite,
|
||||
size: 14,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
book.title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
book.author,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
color: Color(0xFF93C8A5),
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user