98 lines
3.0 KiB
Dart
98 lines
3.0 KiB
Dart
import 'dart:math';
|
|
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> {
|
|
final void Function(Book book) onAddBook;
|
|
final void Function(Book book) onUpdateBook;
|
|
|
|
AddBookBloc({required this.onAddBook, required this.onUpdateBook})
|
|
: super(const AddBookState()) {
|
|
on<InitializeForm>(_onInitializeForm);
|
|
on<UpdateTitle>(_onUpdateTitle);
|
|
on<UpdateAuthor>(_onUpdateAuthor);
|
|
on<UpdateAnnotation>(_onUpdateAnnotation);
|
|
on<UpdateGenre>(_onUpdateGenre);
|
|
on<ApplyScannedBook>(_onApplyScannedBook);
|
|
on<SaveBook>(_onSaveBook);
|
|
}
|
|
|
|
void _onInitializeForm(InitializeForm event, Emitter<AddBookState> emit) {
|
|
final source = event.editBook ?? event.prefilledData;
|
|
if (source != null) {
|
|
emit(
|
|
AddBookState(
|
|
title: source.title,
|
|
author: source.author,
|
|
annotation: source.annotation,
|
|
genre: source.genre.isNotEmpty ? source.genre : 'fiction',
|
|
editBook: event.editBook,
|
|
),
|
|
);
|
|
} else if (event.editBook != null) {
|
|
emit(state.copyWith(editBook: event.editBook));
|
|
}
|
|
}
|
|
|
|
void _onUpdateTitle(UpdateTitle event, Emitter<AddBookState> emit) {
|
|
emit(state.copyWith(title: event.title));
|
|
}
|
|
|
|
void _onUpdateAuthor(UpdateAuthor event, Emitter<AddBookState> emit) {
|
|
emit(state.copyWith(author: event.author));
|
|
}
|
|
|
|
void _onUpdateAnnotation(UpdateAnnotation event, Emitter<AddBookState> emit) {
|
|
emit(state.copyWith(annotation: event.annotation));
|
|
}
|
|
|
|
void _onUpdateGenre(UpdateGenre event, Emitter<AddBookState> emit) {
|
|
emit(state.copyWith(genre: event.genre));
|
|
}
|
|
|
|
void _onApplyScannedBook(ApplyScannedBook event, Emitter<AddBookState> emit) {
|
|
final scanned = event.scannedBook;
|
|
emit(
|
|
state.copyWith(
|
|
title: scanned.title,
|
|
author: scanned.author,
|
|
annotation: scanned.annotation,
|
|
genre: scanned.genre.isNotEmpty ? scanned.genre : 'fiction',
|
|
),
|
|
);
|
|
}
|
|
|
|
void _onSaveBook(SaveBook event, Emitter<AddBookState> emit) {
|
|
final existing = state.editBook;
|
|
final isEditing = existing != null;
|
|
|
|
final Book book = (
|
|
id: isEditing ? existing.id : '${Random().nextInt(100000)}',
|
|
title: state.title,
|
|
author: state.author,
|
|
genre: state.genre,
|
|
annotation: state.annotation,
|
|
coverUrl: isEditing
|
|
? existing.coverUrl
|
|
: 'https://picsum.photos/seed/newbook/400/600',
|
|
pages: isEditing ? existing.pages : 0,
|
|
language: isEditing ? existing.language : 'Russian',
|
|
publishedYear: isEditing ? existing.publishedYear : DateTime.now().year,
|
|
rating: isEditing ? existing.rating : 5.0,
|
|
status: isEditing ? existing.status : 'want_to_read',
|
|
progress: isEditing ? existing.progress : null,
|
|
isFavorite: isEditing ? existing.isFavorite : false,
|
|
);
|
|
|
|
if (isEditing) {
|
|
onUpdateBook(book);
|
|
} else {
|
|
onAddBook(book);
|
|
}
|
|
|
|
emit(state.copyWith(isSaved: true));
|
|
}
|
|
}
|