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,13 +1,13 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:json_annotation/json_annotation.dart';
import 'dart:developer';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/features/documents/bloc/documents_state.dart';
class DocumentsCubit extends Cubit<DocumentsState> {
class DocumentsCubit extends HydratedCubit<DocumentsState> {
final PaperlessDocumentsApi _api;
DocumentsCubit(this._api) : super(DocumentsState.initial);
DocumentsCubit(this._api) : super(const DocumentsState());
Future<void> bulkRemove(List<DocumentModel> documents) async {
await _api.bulkAction(
@@ -40,42 +40,85 @@ class DocumentsCubit extends Cubit<DocumentsState> {
}
Future<void> load() async {
final result = await _api.find(state.filter);
emit(DocumentsState(
isLoaded: true,
value: [...state.value, result],
filter: state.filter,
));
emit(state.copyWith(isLoading: true));
try {
final result = await _api.find(state.filter);
emit(state.copyWith(
isLoading: false,
hasLoaded: true,
value: [...state.value, result],
));
} catch (err) {
emit(state.copyWith(isLoading: false));
rethrow;
}
}
Future<void> reload() async {
if (state.currentPageNumber >= 5) {
return _bulkReloadDocuments();
emit(state.copyWith(isLoading: true));
try {
if (state.currentPageNumber >= 5) {
return _bulkReloadDocuments();
}
var newPages = <PagedSearchResult<DocumentModel>>[];
for (final page in state.value) {
final result =
await _api.find(state.filter.copyWith(page: page.pageKey));
newPages.add(result);
}
emit(DocumentsState(
hasLoaded: true,
value: newPages,
filter: state.filter,
isLoading: false,
));
} catch (err) {
emit(state.copyWith(isLoading: false));
rethrow;
}
var newPages = <PagedSearchResult>[];
for (final page in state.value) {
final result = await _api.find(state.filter.copyWith(page: page.pageKey));
newPages.add(result);
}
emit(DocumentsState(isLoaded: true, value: newPages, filter: state.filter));
}
Future<void> _bulkReloadDocuments() async {
final result = await _api
.find(state.filter.copyWith(page: 1, pageSize: state.documents.length));
emit(DocumentsState(isLoaded: true, value: [result], filter: state.filter));
emit(state.copyWith(isLoading: true));
try {
final result = await _api.find(
state.filter.copyWith(
page: 1,
pageSize: state.documents.length,
),
);
emit(DocumentsState(
hasLoaded: true,
value: [result],
filter: state.filter,
isLoading: false,
));
} catch (err) {
emit(state.copyWith(isLoading: false));
rethrow;
}
}
Future<void> loadMore() async {
if (state.isLastPageLoaded) {
return;
}
emit(state.copyWith(isLoading: true));
final newFilter = state.filter.copyWith(page: state.filter.page + 1);
final result = await _api.find(newFilter);
emit(
DocumentsState(
isLoaded: true, value: [...state.value, result], filter: newFilter),
);
try {
final result = await _api.find(newFilter);
emit(
DocumentsState(
hasLoaded: true,
value: [...state.value, result],
filter: newFilter,
isLoading: false,
),
);
} catch (err) {
emit(state.copyWith(isLoading: false));
rethrow;
}
}
///
@@ -84,8 +127,21 @@ class DocumentsCubit extends Cubit<DocumentsState> {
Future<void> updateFilter({
final DocumentFilter filter = DocumentFilter.initial,
}) async {
final result = await _api.find(filter.copyWith(page: 1));
emit(DocumentsState(filter: filter, value: [result], isLoaded: true));
try {
emit(state.copyWith(isLoading: true));
final result = await _api.find(filter.copyWith(page: 1));
emit(
DocumentsState(
filter: filter,
value: [result],
hasLoaded: true,
isLoading: false,
),
);
} catch (err) {
emit(state.copyWith(isLoading: false));
rethrow;
}
}
Future<void> resetFilter() {
@@ -125,6 +181,17 @@ class DocumentsCubit extends Cubit<DocumentsState> {
}
void reset() {
emit(DocumentsState.initial);
emit(const DocumentsState());
}
@override
DocumentsState? fromJson(Map<String, dynamic> json) {
log(json['filter'].toString());
return DocumentsState.fromJson(json);
}
@override
Map<String, dynamic>? toJson(DocumentsState state) {
return state.toJson();
}
}