Fix import paths and test issues
- Fixed test file import paths to point to correct Bloc file locations - Fixed Bloc file import paths for models (../../../models/models.dart) - Added explicit type annotations to resolve null safety warnings - Fixed null safety issues in wishlist_bloc_test.dart - All 70 tests now passing
This commit is contained in:
110
bookshelf_flutter/lib/features/add_book/bloc/add_book_bloc.dart
Normal file
110
bookshelf_flutter/lib/features/add_book/bloc/add_book_bloc.dart
Normal file
@@ -0,0 +1,110 @@
|
||||
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,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
abstract class AddBookEvent {
|
||||
const AddBookEvent();
|
||||
}
|
||||
|
||||
class InitializeForm extends AddBookEvent {
|
||||
final Map<String, dynamic>? prefilledData;
|
||||
const InitializeForm(this.prefilledData);
|
||||
}
|
||||
|
||||
class UpdateFormField extends AddBookEvent {
|
||||
final String field;
|
||||
final dynamic value;
|
||||
const UpdateFormField(this.field, this.value);
|
||||
}
|
||||
|
||||
class ToggleFavorite extends AddBookEvent {
|
||||
const ToggleFavorite();
|
||||
}
|
||||
|
||||
class ClearForm extends AddBookEvent {
|
||||
const ClearForm();
|
||||
}
|
||||
|
||||
class SubmitBook extends AddBookEvent {
|
||||
const SubmitBook();
|
||||
}
|
||||
109
bookshelf_flutter/lib/features/add_book/bloc/add_book_state.dart
Normal file
109
bookshelf_flutter/lib/features/add_book/bloc/add_book_state.dart
Normal file
@@ -0,0 +1,109 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../../../models/models.dart';
|
||||
|
||||
class AddBookState extends Equatable {
|
||||
final String title;
|
||||
final String author;
|
||||
final String genre;
|
||||
final String annotation;
|
||||
final String coverUrl;
|
||||
final int? pages;
|
||||
final String language;
|
||||
final int? publishedYear;
|
||||
final double? rating;
|
||||
final BookStatus status;
|
||||
final int? progress;
|
||||
final bool isFavorite;
|
||||
final Map<String, dynamic>? prefilledData;
|
||||
final Book? submittedBook;
|
||||
final bool isSubmitted;
|
||||
final bool isLoading;
|
||||
final String? errorMessage;
|
||||
|
||||
const AddBookState({
|
||||
this.title = '',
|
||||
this.author = '',
|
||||
this.genre = '',
|
||||
this.annotation = '',
|
||||
this.coverUrl = '',
|
||||
this.pages,
|
||||
this.language = '',
|
||||
this.publishedYear,
|
||||
this.rating,
|
||||
this.status = BookStatus.wantToRead,
|
||||
this.progress,
|
||||
this.isFavorite = false,
|
||||
this.prefilledData,
|
||||
this.submittedBook,
|
||||
this.isSubmitted = false,
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
factory AddBookState.initial() {
|
||||
return const AddBookState(
|
||||
isLoading: false,
|
||||
);
|
||||
}
|
||||
|
||||
AddBookState copyWith({
|
||||
String? title,
|
||||
String? author,
|
||||
String? genre,
|
||||
String? annotation,
|
||||
String? coverUrl,
|
||||
int? pages,
|
||||
String? language,
|
||||
int? publishedYear,
|
||||
double? rating,
|
||||
BookStatus? status,
|
||||
int? progress,
|
||||
bool? isFavorite,
|
||||
Map<String, dynamic>? prefilledData,
|
||||
Book? submittedBook,
|
||||
bool? isSubmitted,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return AddBookState(
|
||||
title: title ?? this.title,
|
||||
author: author ?? this.author,
|
||||
genre: genre ?? this.genre,
|
||||
annotation: annotation ?? this.annotation,
|
||||
coverUrl: coverUrl ?? this.coverUrl,
|
||||
pages: pages ?? this.pages,
|
||||
language: language ?? this.language,
|
||||
publishedYear: publishedYear ?? this.publishedYear,
|
||||
rating: rating ?? this.rating,
|
||||
status: status ?? this.status,
|
||||
progress: progress ?? this.progress,
|
||||
isFavorite: isFavorite ?? this.isFavorite,
|
||||
prefilledData: prefilledData ?? this.prefilledData,
|
||||
submittedBook: submittedBook ?? this.submittedBook,
|
||||
isSubmitted: isSubmitted ?? this.isSubmitted,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
title,
|
||||
author,
|
||||
genre,
|
||||
annotation,
|
||||
coverUrl,
|
||||
pages,
|
||||
language,
|
||||
publishedYear,
|
||||
rating,
|
||||
status,
|
||||
progress,
|
||||
isFavorite,
|
||||
prefilledData,
|
||||
submittedBook,
|
||||
isSubmitted,
|
||||
isLoading,
|
||||
errorMessage,
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user