import 'package:json_annotation/json_annotation.dart'; import 'package:paperless_api/paperless_api.dart'; import 'package:paperless_mobile/features/paged_document_view/model/documents_paged_state.dart'; class DocumentsState extends DocumentsPagedState { final int? selectedSavedViewId; @JsonKey(ignore: true) final List selection; const DocumentsState({ this.selection = const [], this.selectedSavedViewId, super.value = const [], super.filter = const DocumentFilter(), super.hasLoaded = false, super.isLoading = false, }); List get selectedIds => selection.map((e) => e.id).toList(); DocumentsState copyWith({ bool? hasLoaded, bool? isLoading, List>? value, DocumentFilter? filter, List? selection, int? Function()? selectedSavedViewId, }) { return DocumentsState( hasLoaded: hasLoaded ?? this.hasLoaded, isLoading: isLoading ?? this.isLoading, value: value ?? this.value, filter: filter ?? this.filter, selection: selection ?? this.selection, selectedSavedViewId: selectedSavedViewId != null ? selectedSavedViewId.call() : this.selectedSavedViewId, ); } @override List get props => [ hasLoaded, filter, value, selection, isLoading, selectedSavedViewId, ]; Map toJson() { final json = { 'hasLoaded': hasLoaded, 'isLoading': isLoading, 'filter': filter.toJson(), 'selectedSavedViewId': selectedSavedViewId, 'value': value.map((e) => e.toJson(DocumentModelJsonConverter())).toList(), }; return json; } factory DocumentsState.fromJson(Map json) { return DocumentsState( hasLoaded: json['hasLoaded'], isLoading: json['isLoading'], selectedSavedViewId: json['selectedSavedViewId'], value: (json['value'] as List) .map((e) => PagedSearchResult.fromJsonT(e, DocumentModelJsonConverter())) .toList(), filter: DocumentFilter.fromJson(json['filter']), ); } @override DocumentsState copyWithPaged({ bool? hasLoaded, bool? isLoading, List>? value, DocumentFilter? filter, }) { return copyWith( filter: filter, hasLoaded: hasLoaded, isLoading: isLoading, value: value, ); } }