- 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
110 lines
3.6 KiB
Dart
110 lines
3.6 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../models/models.dart';
|
|
import 'add_book_event.dart';
|
|
import 'add_book_state.dart';
|
|
|
|
class AddBookBloc extends Bloc<AddBookEvent, AddBookState> {
|
|
AddBookBloc() : super(AddBookState.initial()) {
|
|
on<InitializeForm>(_onInitializeForm);
|
|
on<UpdateFormField>(_onUpdateFormField);
|
|
on<ToggleFavorite>(_onToggleFavorite);
|
|
on<ClearForm>(_onClearForm);
|
|
on<SubmitBook>(_onSubmitBook);
|
|
}
|
|
|
|
void _onInitializeForm(InitializeForm event, Emitter<AddBookState> emit) {
|
|
emit(state.copyWith(
|
|
prefilledData: event.prefilledData,
|
|
isLoading: false,
|
|
));
|
|
|
|
if (event.prefilledData != null) {
|
|
final data = event.prefilledData!;
|
|
emit(state.copyWith(
|
|
title: data['title'] as String? ?? '',
|
|
author: data['author'] as String? ?? '',
|
|
genre: data['genre'] as String? ?? '',
|
|
annotation: data['annotation'] as String? ?? '',
|
|
coverUrl: data['coverUrl'] as String? ?? '',
|
|
pages: data['pages'] as int?,
|
|
language: data['language'] as String? ?? '',
|
|
publishedYear: data['publishedYear'] as int?,
|
|
rating: data['rating'] as double? ?? 0.0,
|
|
status: data['status'] as BookStatus? ?? BookStatus.wantToRead,
|
|
progress: data['progress'] as int? ?? 0,
|
|
isFavorite: data['isFavorite'] as bool? ?? false,
|
|
));
|
|
}
|
|
}
|
|
|
|
void _onUpdateFormField(UpdateFormField event, Emitter<AddBookState> emit) {
|
|
switch (event.field) {
|
|
case 'title':
|
|
emit(state.copyWith(title: event.value as String));
|
|
break;
|
|
case 'author':
|
|
emit(state.copyWith(author: event.value as String));
|
|
break;
|
|
case 'genre':
|
|
emit(state.copyWith(genre: event.value as String));
|
|
break;
|
|
case 'annotation':
|
|
emit(state.copyWith(annotation: event.value as String));
|
|
break;
|
|
case 'coverUrl':
|
|
emit(state.copyWith(coverUrl: event.value as String));
|
|
break;
|
|
case 'pages':
|
|
emit(state.copyWith(pages: event.value as int?));
|
|
break;
|
|
case 'language':
|
|
emit(state.copyWith(language: event.value as String));
|
|
break;
|
|
case 'publishedYear':
|
|
emit(state.copyWith(publishedYear: event.value as int?));
|
|
break;
|
|
case 'rating':
|
|
emit(state.copyWith(rating: event.value as double?));
|
|
break;
|
|
case 'status':
|
|
emit(state.copyWith(status: event.value as BookStatus));
|
|
break;
|
|
case 'progress':
|
|
emit(state.copyWith(progress: event.value as int?));
|
|
break;
|
|
}
|
|
}
|
|
|
|
void _onToggleFavorite(ToggleFavorite event, Emitter<AddBookState> emit) {
|
|
emit(state.copyWith(isFavorite: !state.isFavorite));
|
|
}
|
|
|
|
void _onClearForm(ClearForm event, Emitter<AddBookState> emit) {
|
|
emit(AddBookState.initial());
|
|
}
|
|
|
|
void _onSubmitBook(SubmitBook event, Emitter<AddBookState> emit) {
|
|
final book = createBook(
|
|
id: DateTime.now().millisecondsSinceEpoch.toString(),
|
|
title: state.title.isNotEmpty ? state.title : 'Unknown',
|
|
author: state.author.isNotEmpty ? state.author : 'Unknown',
|
|
genre: state.genre.isNotEmpty ? state.genre : 'Unknown',
|
|
annotation: state.annotation,
|
|
coverUrl: state.coverUrl.isNotEmpty
|
|
? state.coverUrl
|
|
: 'https://picsum.photos/seed/${DateTime.now().millisecondsSinceEpoch}/400/600',
|
|
pages: state.pages,
|
|
language: state.language,
|
|
publishedYear: state.publishedYear,
|
|
rating: state.rating,
|
|
status: state.status,
|
|
progress: state.progress,
|
|
isFavorite: state.isFavorite,
|
|
);
|
|
|
|
emit(state.copyWith(
|
|
submittedBook: book,
|
|
isSubmitted: true,
|
|
));
|
|
}
|
|
} |