open ai service
This commit is contained in:
97
books_flutter/lib/bloc/add_book/add_book_bloc.dart
Normal file
97
books_flutter/lib/bloc/add_book/add_book_bloc.dart
Normal file
@@ -0,0 +1,97 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
37
books_flutter/lib/bloc/add_book/add_book_event.dart
Normal file
37
books_flutter/lib/bloc/add_book/add_book_event.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import '../../models/models.dart';
|
||||
|
||||
sealed class AddBookEvent {}
|
||||
|
||||
class InitializeForm extends AddBookEvent {
|
||||
final Book? editBook;
|
||||
final Book? prefilledData;
|
||||
|
||||
InitializeForm({this.editBook, this.prefilledData});
|
||||
}
|
||||
|
||||
class UpdateTitle extends AddBookEvent {
|
||||
final String title;
|
||||
UpdateTitle(this.title);
|
||||
}
|
||||
|
||||
class UpdateAuthor extends AddBookEvent {
|
||||
final String author;
|
||||
UpdateAuthor(this.author);
|
||||
}
|
||||
|
||||
class UpdateAnnotation extends AddBookEvent {
|
||||
final String annotation;
|
||||
UpdateAnnotation(this.annotation);
|
||||
}
|
||||
|
||||
class UpdateGenre extends AddBookEvent {
|
||||
final String genre;
|
||||
UpdateGenre(this.genre);
|
||||
}
|
||||
|
||||
class ApplyScannedBook extends AddBookEvent {
|
||||
final Book scannedBook;
|
||||
ApplyScannedBook(this.scannedBook);
|
||||
}
|
||||
|
||||
class SaveBook extends AddBookEvent {}
|
||||
39
books_flutter/lib/bloc/add_book/add_book_state.dart
Normal file
39
books_flutter/lib/bloc/add_book/add_book_state.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import '../../models/models.dart';
|
||||
|
||||
class AddBookState {
|
||||
final String title;
|
||||
final String author;
|
||||
final String annotation;
|
||||
final String genre;
|
||||
final Book? editBook;
|
||||
final bool isSaved;
|
||||
|
||||
const AddBookState({
|
||||
this.title = '',
|
||||
this.author = '',
|
||||
this.annotation = '',
|
||||
this.genre = 'fiction',
|
||||
this.editBook,
|
||||
this.isSaved = false,
|
||||
});
|
||||
|
||||
bool get isEditing => editBook != null;
|
||||
|
||||
AddBookState copyWith({
|
||||
String? title,
|
||||
String? author,
|
||||
String? annotation,
|
||||
String? genre,
|
||||
Book? editBook,
|
||||
bool? isSaved,
|
||||
}) {
|
||||
return AddBookState(
|
||||
title: title ?? this.title,
|
||||
author: author ?? this.author,
|
||||
annotation: annotation ?? this.annotation,
|
||||
genre: genre ?? this.genre,
|
||||
editBook: editBook ?? this.editBook,
|
||||
isSaved: isSaved ?? this.isSaved,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user