openai service
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../models/models.dart';
|
||||
import '../constants/constants.dart';
|
||||
|
||||
// Events
|
||||
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);
|
||||
}
|
||||
|
||||
// State
|
||||
class BookState {
|
||||
final List<Book> books;
|
||||
const BookState({required this.books});
|
||||
}
|
||||
|
||||
// Bloc
|
||||
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));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../models/models.dart';
|
||||
|
||||
// Events
|
||||
sealed class NavigationEvent {}
|
||||
|
||||
class NavigateTo extends NavigationEvent {
|
||||
final AppScreen screen;
|
||||
final Book? selectedBook;
|
||||
final Book? prefilledData;
|
||||
NavigateTo(this.screen, {this.selectedBook, this.prefilledData});
|
||||
}
|
||||
|
||||
// State
|
||||
class NavigationState {
|
||||
final AppScreen screen;
|
||||
final Book? selectedBook;
|
||||
final Book? prefilledData;
|
||||
const NavigationState({
|
||||
this.screen = AppScreen.library,
|
||||
this.selectedBook,
|
||||
this.prefilledData,
|
||||
});
|
||||
|
||||
NavigationState copyWith({
|
||||
AppScreen? screen,
|
||||
Book? Function()? selectedBook,
|
||||
Book? Function()? prefilledData,
|
||||
}) {
|
||||
return NavigationState(
|
||||
screen: screen ?? this.screen,
|
||||
selectedBook: selectedBook != null ? selectedBook() : this.selectedBook,
|
||||
prefilledData: prefilledData != null
|
||||
? prefilledData()
|
||||
: this.prefilledData,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Bloc
|
||||
class NavigationBloc extends Bloc<NavigationEvent, NavigationState> {
|
||||
NavigationBloc() : super(const NavigationState()) {
|
||||
on<NavigateTo>((event, emit) {
|
||||
emit(
|
||||
NavigationState(
|
||||
screen: event.screen,
|
||||
selectedBook: event.selectedBook ?? state.selectedBook,
|
||||
prefilledData: event.prefilledData,
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user