149 lines
4.0 KiB
Dart
149 lines
4.0 KiB
Dart
part of 'index.dart';
|
|
|
|
/// -----------------------------------------------
|
|
/// [DocumentPageBloc]
|
|
/// -----------------------------------------------
|
|
|
|
class DocumentPageBloc extends Bloc<DocumentPageEvent, DocumentPageState> {
|
|
final DocumentPageModel model;
|
|
static DocumentPageBloc? _singleton;
|
|
|
|
factory DocumentPageBloc(DocumentPageModel model) {
|
|
_singleton ??= DocumentPageBloc._internal(model);
|
|
return _singleton!;
|
|
}
|
|
|
|
DocumentPageBloc._internal(this.model) : super(DocumentPageState()) {
|
|
on<SelectDocumentEvent>(_selectDocument);
|
|
on<UnselectDocumentEvent>(_unselectDocument);
|
|
on<FilterCategoryEvent>(_filterCategoryEvent);
|
|
}
|
|
|
|
Future<void> _filterCategoryEvent(
|
|
FilterCategoryEvent event, Emitter<DocumentPageState> emit) async {
|
|
_selectCategory(event, emit);
|
|
state.isCategorySelected
|
|
? _unselectCategory(event, emit)
|
|
: _selectCategory(event, emit);
|
|
}
|
|
|
|
Future<void> _selectCategory(
|
|
FilterCategoryEvent event, Emitter<DocumentPageState> emit) async {
|
|
log('filterItems A: ${event.query}');
|
|
emit(state.copyWith(
|
|
isCategorySelected: true,
|
|
));
|
|
|
|
final listViewState = model.vihicleScreenManager.currentState!;
|
|
listViewState.widget.bodyItems = (await model.generateBodyItems(
|
|
1, 10, event.query)) as BodyItemsBuilder<Document>;
|
|
}
|
|
|
|
Future<void> _unselectCategory(
|
|
FilterCategoryEvent event, Emitter<DocumentPageState> emit) async {
|
|
emit(state.copyWith(
|
|
isCategorySelected: false,
|
|
));
|
|
|
|
final listViewState = model.vihicleScreenManager.currentState!;
|
|
listViewState.widget.bodyItems = (await model.generateBodyItems(
|
|
1, 10, null)) as BodyItemsBuilder<Document>;
|
|
}
|
|
|
|
Future<void> _selectDocument(
|
|
SelectDocumentEvent event, Emitter<DocumentPageState> emit) async {
|
|
print('-> select');
|
|
emit(
|
|
state.copyWith(
|
|
uri: await GetPDF().call(
|
|
event.document.id,
|
|
),
|
|
currentDocument: event.document,
|
|
isDocumentSelected: true,
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _unselectDocument(
|
|
UnselectDocumentEvent event, Emitter<DocumentPageState> emit) async {
|
|
emit(
|
|
state.copyWith(
|
|
currentDocument: null,
|
|
isDocumentSelected: false,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// -----------------------------------------------
|
|
/// [DocumentPageEvent]
|
|
/// -----------------------------------------------
|
|
|
|
abstract class DocumentPageEvent {}
|
|
|
|
class SelectDocumentEvent extends DocumentPageEvent {
|
|
final Document document;
|
|
SelectDocumentEvent(
|
|
this.document,
|
|
);
|
|
}
|
|
|
|
class UnselectDocumentEvent extends DocumentPageEvent {}
|
|
|
|
class FilterCategoryEvent extends DocumentPageEvent {
|
|
final Query query;
|
|
FilterCategoryEvent(this.query);
|
|
}
|
|
|
|
/// -----------------------------------------------
|
|
/// [DocumentPageState]
|
|
/// -----------------------------------------------
|
|
|
|
class DocumentPageState {
|
|
final bool isCategorySelected;
|
|
final bool isDocumentSelected;
|
|
final Document? currentDocument;
|
|
final Category? currentCategory;
|
|
final Uri? uri;
|
|
final int? count;
|
|
final dynamic page;
|
|
final Query? query;
|
|
|
|
const DocumentPageState({
|
|
this.query,
|
|
this.count,
|
|
this.page,
|
|
this.uri,
|
|
this.currentDocument,
|
|
this.isCategorySelected = false,
|
|
this.currentCategory,
|
|
this.isDocumentSelected = false,
|
|
});
|
|
|
|
DocumentPageState copyWith({
|
|
Uri? uri,
|
|
Query? query,
|
|
int? count,
|
|
dynamic page,
|
|
List<Document?>? documents,
|
|
Document? currentDocument,
|
|
bool? isDocumentSelected,
|
|
List<Category?>? categories,
|
|
Category? currentCategory,
|
|
bool? isCategorySelected,
|
|
}) {
|
|
return DocumentPageState(
|
|
uri: uri ?? this.uri,
|
|
query: query ?? this.query,
|
|
count: count ?? this.count,
|
|
page: page ?? this.page,
|
|
//
|
|
currentDocument: currentDocument ?? this.currentDocument,
|
|
isDocumentSelected: isDocumentSelected ?? this.isDocumentSelected,
|
|
//
|
|
currentCategory: currentCategory ?? this.currentCategory,
|
|
isCategorySelected: isCategorySelected ?? this.isCategorySelected,
|
|
);
|
|
}
|
|
}
|