Fixed bugs, added serialization for documents state

This commit is contained in:
Anton Stubenbord
2023-01-06 18:22:43 +01:00
parent 738ef99bc5
commit 23bcb355b1
76 changed files with 1333 additions and 595 deletions

View File

@@ -1,30 +1,27 @@
import 'dart:developer';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/paperless_api.dart';
@JsonSerializable()
class DocumentsState extends Equatable {
final bool isLoaded;
final bool isLoading;
final bool hasLoaded;
final DocumentFilter filter;
final List<PagedSearchResult> value;
final List<PagedSearchResult<DocumentModel>> value;
@JsonKey(ignore: true)
final List<DocumentModel> selection;
const DocumentsState({
required this.isLoaded,
required this.value,
required this.filter,
this.hasLoaded = false,
this.isLoading = false,
this.value = const [],
this.filter = const DocumentFilter(),
this.selection = const [],
});
static const DocumentsState initial = DocumentsState(
isLoaded: false,
value: [],
filter: DocumentFilter.initial,
selection: [],
);
int get currentPageNumber {
return filter.page;
}
@@ -41,7 +38,7 @@ class DocumentsState extends Equatable {
}
bool get isLastPageLoaded {
if (!isLoaded) {
if (!hasLoaded) {
return false;
}
if (value.isNotEmpty) {
@@ -51,7 +48,7 @@ class DocumentsState extends Equatable {
}
int inferPageCount({required int pageSize}) {
if (!isLoaded) {
if (!hasLoaded) {
return 100000;
}
if (value.isEmpty) {
@@ -67,13 +64,15 @@ class DocumentsState extends Equatable {
DocumentsState copyWith({
bool overwrite = false,
bool? isLoaded,
List<PagedSearchResult>? value,
bool? hasLoaded,
bool? isLoading,
List<PagedSearchResult<DocumentModel>>? value,
DocumentFilter? filter,
List<DocumentModel>? selection,
}) {
return DocumentsState(
isLoaded: isLoaded ?? this.isLoaded,
hasLoaded: hasLoaded ?? this.hasLoaded,
isLoading: isLoading ?? this.isLoading,
value: value ?? this.value,
filter: filter ?? this.filter,
selection: selection ?? this.selection,
@@ -81,5 +80,28 @@ class DocumentsState extends Equatable {
}
@override
List<Object?> get props => [isLoaded, filter, value, selection];
List<Object?> get props => [hasLoaded, filter, value, selection, isLoading];
Map<String, dynamic> toJson() {
final json = {
'hasLoaded': hasLoaded,
'isLoading': isLoading,
'filter': filter.toJson(),
'value':
value.map((e) => e.toJson(DocumentModelJsonConverter())).toList(),
};
return json;
}
factory DocumentsState.fromJson(Map<String, dynamic> json) {
return DocumentsState(
hasLoaded: json['hasLoaded'],
isLoading: json['isLoading'],
value: (json['value'] as List<dynamic>)
.map((e) =>
PagedSearchResult.fromJson(e, DocumentModelJsonConverter()))
.toList(),
filter: DocumentFilter.fromJson(json['filter']),
);
}
}