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();
}
}

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']),
);
}
}

View File

@@ -35,6 +35,7 @@ class _DocumentsPageState extends State<DocumentsPage> {
final _pagingController = PagingController<int, DocumentModel>(
firstPageKey: 1,
);
final _refreshIndicatorKey = GlobalKey<RefreshIndicatorState>();
@override
void initState() {
@@ -78,8 +79,8 @@ class _DocumentsPageState extends State<DocumentsPage> {
builder: (context, state) {
final appliedFiltersCount = state.filter.appliedFiltersCount;
return Badge.count(
alignment: const AlignmentDirectional(44,
-4), //TODO: Wait for stable version of m3, then use AlignmentDirectional.topEnd
//TODO: Wait for stable version of m3, then use AlignmentDirectional.topEnd
alignment: const AlignmentDirectional(44, -4),
isLabelVisible: appliedFiltersCount > 0,
count: state.filter.appliedFiltersCount,
backgroundColor: Colors.red,
@@ -177,7 +178,7 @@ class _DocumentsPageState extends State<DocumentsPage> {
break;
}
if (state.isLoaded && state.documents.isEmpty) {
if (state.hasLoaded && state.documents.isEmpty) {
child = SliverToBoxAdapter(
child: DocumentsEmptyState(
state: state,
@@ -190,6 +191,7 @@ class _DocumentsPageState extends State<DocumentsPage> {
}
return RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _onRefresh,
notificationPredicate: (_) => isConnected,
child: CustomScrollView(
@@ -369,10 +371,10 @@ class _DocumentsPageState extends State<DocumentsPage> {
Future<void> _onRefresh() async {
try {
context.read<DocumentsCubit>().updateCurrentFilter(
await context.read<DocumentsCubit>().updateCurrentFilter(
(filter) => filter.copyWith(page: 1),
);
context.read<SavedViewCubit>().reload();
await context.read<SavedViewCubit>().reload();
} on PaperlessServerException catch (error, stackTrace) {
showErrorMessage(context, error, stackTrace);
}

View File

@@ -28,10 +28,13 @@ class TextQueryFormField extends StatelessWidget {
prefixIcon: const Icon(Icons.search_outlined),
labelText: _buildLabelText(context, field.value!.queryType),
suffixIcon: PopupMenuButton<QueryType>(
enabled: !onlyExtendedQueryAllowed,
color: onlyExtendedQueryAllowed
? Theme.of(context).disabledColor
icon: onlyExtendedQueryAllowed
? Icon(
Icons.more_vert,
color: Theme.of(context).disabledColor,
)
: null,
enabled: !onlyExtendedQueryAllowed,
itemBuilder: (context) => [
PopupMenuItem(
child: ListTile(
@@ -59,7 +62,6 @@ class TextQueryFormField extends StatelessWidget {
onSelected: (selection) {
field.didChange(field.value?.copyWith(queryType: selection));
},
child: const Icon(Icons.more_vert),
),
),
onChanged: (value) {

View File

@@ -1,3 +1,5 @@
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paperless_api/paperless_api.dart';
@@ -36,8 +38,15 @@ class _DocumentsPageAppBarState extends State<DocumentsPageAppBar> {
return BlocBuilder<DocumentsCubit, DocumentsState>(
builder: (context, documentsState) {
final hasSelection = documentsState.selection.isNotEmpty;
// final PreferredSize? loadingWidget = documentsState.isLoading
// ? const PreferredSize(
// child: LinearProgressIndicator(),
// preferredSize: Size.fromHeight(4.0),
// )
// : null;
if (hasSelection) {
return SliverAppBar(
// bottom: loadingWidget,
expandedHeight: kToolbarHeight + flexibleAreaHeight,
snap: true,
floating: true,
@@ -62,6 +71,7 @@ class _DocumentsPageAppBarState extends State<DocumentsPageAppBar> {
);
} else {
return SliverAppBar(
// bottom: loadingWidget,
expandedHeight: kToolbarHeight + flexibleAreaHeight,
snap: true,
floating: true,