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,
|
||||
);
|
||||
}
|
||||
}
|
||||
47
books_flutter/lib/bloc/book/book_bloc.dart
Normal file
47
books_flutter/lib/bloc/book/book_bloc.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../constants/constants.dart';
|
||||
import 'book_event.dart';
|
||||
import 'book_state.dart';
|
||||
|
||||
class BookBloc extends Bloc<BookEvent, BookState> {
|
||||
BookBloc() : super(const BookState(books: initialBooks)) {
|
||||
on<AddBook>((event, emit) {
|
||||
emit(BookState(books: [...state.books, event.book]));
|
||||
});
|
||||
|
||||
on<UpdateBook>((event, emit) {
|
||||
final updated = state.books.map((b) {
|
||||
return b.id == event.book.id ? event.book : b;
|
||||
}).toList();
|
||||
emit(BookState(books: updated));
|
||||
});
|
||||
|
||||
on<DeleteBook>((event, emit) {
|
||||
emit(
|
||||
BookState(books: state.books.where((b) => b.id != event.id).toList()),
|
||||
);
|
||||
});
|
||||
|
||||
on<ToggleFavorite>((event, emit) {
|
||||
final updated = state.books.map((b) {
|
||||
if (b.id != event.id) return b;
|
||||
return (
|
||||
id: b.id,
|
||||
title: b.title,
|
||||
author: b.author,
|
||||
genre: b.genre,
|
||||
annotation: b.annotation,
|
||||
coverUrl: b.coverUrl,
|
||||
pages: b.pages,
|
||||
language: b.language,
|
||||
publishedYear: b.publishedYear,
|
||||
rating: b.rating,
|
||||
status: b.status,
|
||||
progress: b.progress,
|
||||
isFavorite: !b.isFavorite,
|
||||
);
|
||||
}).toList();
|
||||
emit(BookState(books: updated));
|
||||
});
|
||||
}
|
||||
}
|
||||
23
books_flutter/lib/bloc/book/book_event.dart
Normal file
23
books_flutter/lib/bloc/book/book_event.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import '../../models/models.dart';
|
||||
|
||||
sealed class BookEvent {}
|
||||
|
||||
class AddBook extends BookEvent {
|
||||
final Book book;
|
||||
AddBook(this.book);
|
||||
}
|
||||
|
||||
class UpdateBook extends BookEvent {
|
||||
final Book book;
|
||||
UpdateBook(this.book);
|
||||
}
|
||||
|
||||
class DeleteBook extends BookEvent {
|
||||
final String id;
|
||||
DeleteBook(this.id);
|
||||
}
|
||||
|
||||
class ToggleFavorite extends BookEvent {
|
||||
final String id;
|
||||
ToggleFavorite(this.id);
|
||||
}
|
||||
6
books_flutter/lib/bloc/book/book_state.dart
Normal file
6
books_flutter/lib/bloc/book/book_state.dart
Normal file
@@ -0,0 +1,6 @@
|
||||
import '../../models/models.dart';
|
||||
|
||||
class BookState {
|
||||
final List<Book> books;
|
||||
const BookState({required this.books});
|
||||
}
|
||||
21
books_flutter/lib/bloc/library/library_bloc.dart
Normal file
21
books_flutter/lib/bloc/library/library_bloc.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'library_event.dart';
|
||||
import 'library_state.dart';
|
||||
|
||||
class LibraryBloc extends Bloc<LibraryEvent, LibraryState> {
|
||||
LibraryBloc() : super(const LibraryState()) {
|
||||
on<UpdateSearchQuery>(_onUpdateSearchQuery);
|
||||
on<ChangeTab>(_onChangeTab);
|
||||
}
|
||||
|
||||
void _onUpdateSearchQuery(
|
||||
UpdateSearchQuery event,
|
||||
Emitter<LibraryState> emit,
|
||||
) {
|
||||
emit(state.copyWith(searchQuery: event.query));
|
||||
}
|
||||
|
||||
void _onChangeTab(ChangeTab event, Emitter<LibraryState> emit) {
|
||||
emit(state.copyWith(tabIndex: event.tabIndex));
|
||||
}
|
||||
}
|
||||
11
books_flutter/lib/bloc/library/library_event.dart
Normal file
11
books_flutter/lib/bloc/library/library_event.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
sealed class LibraryEvent {}
|
||||
|
||||
class UpdateSearchQuery extends LibraryEvent {
|
||||
final String query;
|
||||
UpdateSearchQuery(this.query);
|
||||
}
|
||||
|
||||
class ChangeTab extends LibraryEvent {
|
||||
final int tabIndex;
|
||||
ChangeTab(this.tabIndex);
|
||||
}
|
||||
13
books_flutter/lib/bloc/library/library_state.dart
Normal file
13
books_flutter/lib/bloc/library/library_state.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
class LibraryState {
|
||||
final String searchQuery;
|
||||
final int tabIndex;
|
||||
|
||||
const LibraryState({this.searchQuery = '', this.tabIndex = 0});
|
||||
|
||||
LibraryState copyWith({String? searchQuery, int? tabIndex}) {
|
||||
return LibraryState(
|
||||
searchQuery: searchQuery ?? this.searchQuery,
|
||||
tabIndex: tabIndex ?? this.tabIndex,
|
||||
);
|
||||
}
|
||||
}
|
||||
126
books_flutter/lib/bloc/scanner/scanner_bloc.dart
Normal file
126
books_flutter/lib/bloc/scanner/scanner_bloc.dart
Normal file
@@ -0,0 +1,126 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../../services/camera_service.dart';
|
||||
import '../../services/openai_service.dart';
|
||||
import 'scanner_event.dart';
|
||||
import 'scanner_state.dart';
|
||||
|
||||
class ScannerBloc extends Bloc<ScannerEvent, ScannerState> {
|
||||
final CameraService cameraService;
|
||||
|
||||
ScannerBloc({required this.cameraService}) : super(const ScannerState()) {
|
||||
on<InitializeCamera>(_onInitializeCamera);
|
||||
on<CaptureAndAnalyze>(_onCaptureAndAnalyze);
|
||||
on<SwitchCamera>(_onSwitchCamera);
|
||||
on<DismissError>(_onDismissError);
|
||||
}
|
||||
|
||||
Future<void> _onInitializeCamera(
|
||||
InitializeCamera event,
|
||||
Emitter<ScannerState> emit,
|
||||
) async {
|
||||
try {
|
||||
final initialized = await cameraService.initializeCamera();
|
||||
emit(
|
||||
state.copyWith(
|
||||
isInitialized: initialized,
|
||||
hasPermissionError: !initialized,
|
||||
errorMessage: initialized ? null : 'Нет доступа к камере',
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
hasPermissionError: true,
|
||||
errorMessage: 'Ошибка инициализации камеры: $e',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onCaptureAndAnalyze(
|
||||
CaptureAndAnalyze event,
|
||||
Emitter<ScannerState> emit,
|
||||
) async {
|
||||
if (cameraService.controller == null) return;
|
||||
|
||||
emit(state.copyWith(isCapturing: true));
|
||||
|
||||
try {
|
||||
// Capture image
|
||||
final imagePath = await cameraService.captureImage();
|
||||
if (imagePath == null) {
|
||||
throw Exception('Не удалось сделать снимок');
|
||||
}
|
||||
|
||||
emit(state.copyWith(isAnalyzing: true, isCapturing: false));
|
||||
|
||||
Book? book;
|
||||
|
||||
// Try OpenAI first if available
|
||||
if (event.openaiApiKey != null && event.openaiApiKey!.isNotEmpty) {
|
||||
print('Using OpenAI service for analysis');
|
||||
final openaiService = OpenAIService(
|
||||
apiKey: event.openaiApiKey!,
|
||||
baseUrl: event.openaiBaseUrl,
|
||||
);
|
||||
book = await openaiService.analyzeBookCover(imagePath);
|
||||
}
|
||||
|
||||
// Fall back to Gemini if OpenAI failed or is not configured
|
||||
// if (book == null) {
|
||||
// if (event.geminiApiKey == null || event.geminiApiKey!.isEmpty) {
|
||||
// throw Exception('API ключ не настроен (ни OpenAI, ни Gemini)');
|
||||
// }
|
||||
// print('Using Gemini service for analysis');
|
||||
// final geminiService = GeminiService(apiKey: event.geminiApiKey!);
|
||||
// book = await geminiService.analyzeBookCover(imagePath);
|
||||
// }
|
||||
|
||||
if (book == null) {
|
||||
throw Exception('Не удалось распознать книгу');
|
||||
}
|
||||
|
||||
// Clean up temporary image
|
||||
try {
|
||||
await File(imagePath).delete();
|
||||
} catch (e) {
|
||||
print('Error deleting temporary file: $e');
|
||||
}
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
analyzedBook: book,
|
||||
isAnalyzing: false,
|
||||
isCapturing: false,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
errorMessage: e.toString(),
|
||||
isCapturing: false,
|
||||
isAnalyzing: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onSwitchCamera(
|
||||
SwitchCamera event,
|
||||
Emitter<ScannerState> emit,
|
||||
) async {
|
||||
await cameraService.switchCamera();
|
||||
}
|
||||
|
||||
void _onDismissError(DismissError event, Emitter<ScannerState> emit) {
|
||||
emit(state.copyWith(clearError: true));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
cameraService.dispose();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
21
books_flutter/lib/bloc/scanner/scanner_event.dart
Normal file
21
books_flutter/lib/bloc/scanner/scanner_event.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:books_flutter/config/api_config.dart';
|
||||
|
||||
sealed class ScannerEvent {}
|
||||
|
||||
class InitializeCamera extends ScannerEvent {}
|
||||
|
||||
class CaptureAndAnalyze extends ScannerEvent {
|
||||
final String? openaiApiKey;
|
||||
final String openaiBaseUrl;
|
||||
final String? geminiApiKey;
|
||||
|
||||
CaptureAndAnalyze({
|
||||
this.openaiApiKey,
|
||||
this.openaiBaseUrl = ApiConfig.openaiBaseUrl,
|
||||
this.geminiApiKey,
|
||||
});
|
||||
}
|
||||
|
||||
class SwitchCamera extends ScannerEvent {}
|
||||
|
||||
class DismissError extends ScannerEvent {}
|
||||
39
books_flutter/lib/bloc/scanner/scanner_state.dart
Normal file
39
books_flutter/lib/bloc/scanner/scanner_state.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import '../../models/models.dart';
|
||||
|
||||
class ScannerState {
|
||||
final bool isInitialized;
|
||||
final bool isCapturing;
|
||||
final bool isAnalyzing;
|
||||
final bool hasPermissionError;
|
||||
final String? errorMessage;
|
||||
final Book? analyzedBook;
|
||||
|
||||
const ScannerState({
|
||||
this.isInitialized = false,
|
||||
this.isCapturing = false,
|
||||
this.isAnalyzing = false,
|
||||
this.hasPermissionError = false,
|
||||
this.errorMessage,
|
||||
this.analyzedBook,
|
||||
});
|
||||
|
||||
ScannerState copyWith({
|
||||
bool? isInitialized,
|
||||
bool? isCapturing,
|
||||
bool? isAnalyzing,
|
||||
bool? hasPermissionError,
|
||||
String? errorMessage,
|
||||
Book? analyzedBook,
|
||||
bool clearError = false,
|
||||
bool clearBook = false,
|
||||
}) {
|
||||
return ScannerState(
|
||||
isInitialized: isInitialized ?? this.isInitialized,
|
||||
isCapturing: isCapturing ?? this.isCapturing,
|
||||
isAnalyzing: isAnalyzing ?? this.isAnalyzing,
|
||||
hasPermissionError: hasPermissionError ?? this.hasPermissionError,
|
||||
errorMessage: clearError ? null : (errorMessage ?? this.errorMessage),
|
||||
analyzedBook: clearBook ? null : (analyzedBook ?? this.analyzedBook),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user