import 'dart:developer'; import 'package:equatable/equatable.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:paperless_api/paperless_api.dart'; class DocumentsState extends Equatable { final bool isLoading; final bool hasLoaded; final DocumentFilter filter; final List> value; final int? selectedSavedViewId; @JsonKey(ignore: true) final List selection; const DocumentsState({ this.hasLoaded = false, this.isLoading = false, this.value = const [], this.filter = const DocumentFilter(), this.selection = const [], this.selectedSavedViewId, }); List get selectedIds => selection.map((e) => e.id).toList(); int get currentPageNumber { return filter.page; } int? get nextPageNumber { return isLastPageLoaded ? null : currentPageNumber + 1; } int get count { if (value.isEmpty) { return 0; } return value.first.count; } bool get isLastPageLoaded { if (!hasLoaded) { return false; } if (value.isNotEmpty) { return value.last.next == null; } return true; } int inferPageCount({required int pageSize}) { if (!hasLoaded) { return 100000; } if (value.isEmpty) { return 0; } return value.first.inferPageCount(pageSize: pageSize); } List get documents { return value.fold( [], (previousValue, element) => [...previousValue, ...element.results]); } DocumentsState copyWith({ bool overwrite = false, bool? hasLoaded, bool? isLoading, List>? value, DocumentFilter? filter, List? selection, int? 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 ?? 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']), ); } }