Finished inbox, fixed reverse sort order, bloc refactorings

This commit is contained in:
Anton Stubenbord
2022-11-30 01:24:28 +01:00
parent 50190f035e
commit b1db2a1209
27 changed files with 1144 additions and 632 deletions

View File

@@ -17,6 +17,7 @@ class ApplicationIntroSlideshow extends StatefulWidget {
_ApplicationIntroSlideshowState();
}
//TODO: INTL ALL
class _ApplicationIntroSlideshowState extends State<ApplicationIntroSlideshow> {
AssetImage secureImage = AssetImages.secureDocuments.image;
AssetImage successImage = AssetImages.success.image;

View File

@@ -1,6 +1,5 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:injectable/injectable.dart';
import 'package:paperless_mobile/features/documents/model/document.model.dart';
import 'package:paperless_mobile/features/documents/repository/document_repository.dart';

View File

@@ -58,7 +58,6 @@ class _DocumentDetailsPageState extends State<DocumentDetailsPage> {
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
print("Returning document...");
Navigator.of(context)
.pop(BlocProvider.of<DocumentDetailsCubit>(context).state.document);
return Future.value(false);

View File

@@ -79,8 +79,10 @@ class DocumentsCubit extends Cubit<DocumentsState> {
}
final newFilter = state.filter.copyWith(page: state.filter.page + 1);
final result = await documentRepository.find(newFilter);
emit(DocumentsState(
isLoaded: true, value: [...state.value, result], filter: newFilter));
emit(
DocumentsState(
isLoaded: true, value: [...state.value, result], filter: newFilter),
);
}
///

View File

@@ -5,18 +5,18 @@ import 'package:paperless_mobile/core/type/types.dart';
class DocumentModel extends Equatable {
static const idKey = 'id';
static const titleKey = "title";
static const contentKey = "content";
static const archivedFileNameKey = "archived_file_name";
static const asnKey = "archive_serial_number";
static const createdKey = "created";
static const modifiedKey = "modified";
static const addedKey = "added";
static const correspondentKey = "correspondent";
static const titleKey = 'title';
static const contentKey = 'content';
static const archivedFileNameKey = 'archived_file_name';
static const asnKey = 'archive_serial_number';
static const createdKey = 'created';
static const modifiedKey = 'modified';
static const addedKey = 'added';
static const correspondentKey = 'correspondent';
static const originalFileNameKey = 'original_file_name';
static const documentTypeKey = "document_type";
static const tagsKey = "tags";
static const storagePathKey = "storage_path";
static const documentTypeKey = 'document_type';
static const tagsKey = 'tags';
static const storagePathKey = 'storage_path';
final int id;
final String title;
@@ -121,7 +121,7 @@ class DocumentModel extends Equatable {
List<Object?> get props => [
id,
title,
content,
content.hashCode,
tags,
documentType,
storagePath,

View File

@@ -18,6 +18,7 @@ class OnlyNotAssignedTagsQuery extends TagsQuery {
class AnyAssignedTagsQuery extends TagsQuery {
final Iterable<int> tagIds;
const AnyAssignedTagsQuery({
this.tagIds = const [],
});

View File

@@ -37,26 +37,21 @@ class DocumentsPage extends StatefulWidget {
}
class _DocumentsPageState extends State<DocumentsPage> {
final PagingController<int, DocumentModel> _pagingController =
PagingController<int, DocumentModel>(
final _pagingController = PagingController<int, DocumentModel>(
firstPageKey: 1,
);
final PanelController _filterPanelController = PanelController();
final _filterPanelController = PanelController();
@override
void initState() {
super.initState();
_initDocuments();
_pagingController.addPageRequestListener(_loadNewPage);
}
Future<void> _initDocuments() async {
try {
BlocProvider.of<DocumentsCubit>(context).load();
} on ErrorMessage catch (error, stackTrace) {
showErrorMessage(context, error, stackTrace);
}
_pagingController.addPageRequestListener(_loadNewPage);
}
@override
@@ -120,7 +115,10 @@ class _DocumentsPageState extends State<DocumentsPage> {
return Scaffold(
drawer: BlocProvider.value(
value: BlocProvider.of<AuthenticationCubit>(context),
child: const InfoDrawer(),
child: InfoDrawer(
afterInboxClosed: () =>
BlocProvider.of<DocumentsCubit>(context).reload(),
),
),
resizeToAvoidBottomInset: true,
body: SlidingUpPanel(

View File

@@ -1,33 +1,30 @@
import 'package:badges/badges.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paperless_mobile/core/model/paperless_statistics_state.dart';
import 'package:paperless_mobile/features/inbox/bloc/inbox_cubit.dart';
import 'package:paperless_mobile/features/labels/bloc/global_state_bloc_provider.dart';
import 'package:paperless_mobile/core/bloc/paperless_server_information_cubit.dart';
import 'package:paperless_mobile/core/model/error_message.dart';
import 'package:paperless_mobile/core/model/paperless_server_information.dart';
import 'package:paperless_mobile/core/model/paperless_statistics.dart';
import 'package:paperless_mobile/core/service/paperless_statistics_service.dart';
import 'package:paperless_mobile/features/documents/repository/document_repository.dart';
import 'package:paperless_mobile/features/inbox/view/pages/inbox_page.dart';
import 'package:paperless_mobile/features/settings/bloc/application_settings_cubit.dart';
import 'package:paperless_mobile/di_initializer.dart';
import 'package:paperless_mobile/extensions/flutter_extensions.dart';
import 'package:paperless_mobile/features/documents/bloc/documents_cubit.dart';
import 'package:paperless_mobile/features/inbox/bloc/inbox_cubit.dart';
import 'package:paperless_mobile/features/inbox/view/pages/inbox_page.dart';
import 'package:paperless_mobile/features/labels/bloc/global_state_bloc_provider.dart';
import 'package:paperless_mobile/features/labels/correspondent/bloc/correspondents_cubit.dart';
import 'package:paperless_mobile/features/labels/document_type/bloc/document_type_cubit.dart';
import 'package:paperless_mobile/features/documents/bloc/documents_cubit.dart';
import 'package:paperless_mobile/features/settings/view/settings_page.dart';
import 'package:paperless_mobile/features/labels/tags/bloc/tags_cubit.dart';
import 'package:paperless_mobile/features/login/bloc/authentication_cubit.dart';
import 'package:paperless_mobile/features/scan/bloc/document_scanner_cubit.dart';
import 'package:paperless_mobile/features/labels/tags/bloc/tags_cubit.dart';
import 'package:paperless_mobile/features/settings/bloc/application_settings_cubit.dart';
import 'package:paperless_mobile/features/settings/view/settings_page.dart';
import 'package:paperless_mobile/generated/l10n.dart';
import 'package:paperless_mobile/util.dart';
import 'package:url_launcher/link.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:paperless_mobile/extensions/flutter_extensions.dart';
class InfoDrawer extends StatelessWidget {
const InfoDrawer({Key? key}) : super(key: key);
final VoidCallback? afterInboxClosed;
const InfoDrawer({Key? key, this.afterInboxClosed}) : super(key: key);
@override
Widget build(BuildContext context) {
return ClipRRect(
@@ -52,7 +49,7 @@ class InfoDrawer extends StatelessWidget {
Row(
children: [
Image.asset(
"assets/logos/paperless_logo_white.png",
'assets/logos/paperless_logo_white.png',
height: 32,
width: 32,
color: Theme.of(context).colorScheme.onPrimaryContainer,
@@ -124,7 +121,7 @@ class InfoDrawer extends StatelessWidget {
leading: const Icon(Icons.inbox),
onTap: () => _onOpenInbox(context),
),
Divider(),
const Divider(),
ListTile(
leading: const Icon(Icons.settings),
title: Text(
@@ -145,27 +142,27 @@ class InfoDrawer extends StatelessWidget {
title: Text(S.of(context).appDrawerReportBugLabel),
onTap: () {
launchUrlString(
"https://github.com/astubenbord/paperless-mobile/issues/new");
'https://github.com/astubenbord/paperless-mobile/issues/new');
},
),
const Divider(),
AboutListTile(
icon: const Icon(Icons.info),
applicationIcon: const ImageIcon(
AssetImage("assets/logos/paperless_logo_green.png")),
applicationName: "Paperless Mobile",
AssetImage('assets/logos/paperless_logo_green.png')),
applicationName: 'Paperless Mobile',
applicationVersion:
kPackageInfo.version + "+" + kPackageInfo.buildNumber,
kPackageInfo.version + '+' + kPackageInfo.buildNumber,
aboutBoxChildren: [
Text(
'${S.of(context).aboutDialogDevelopedByText} Anton Stubenbord'),
Link(
uri: Uri.parse(
"https://github.com/astubenbord/paperless-mobile"),
'https://github.com/astubenbord/paperless-mobile'),
builder: (context, followLink) => GestureDetector(
onTap: followLink,
child: Text(
"https://github.com/astubenbord/paperless-mobile",
'https://github.com/astubenbord/paperless-mobile',
style: TextStyle(
color: Theme.of(context).colorScheme.tertiary),
),
@@ -173,7 +170,7 @@ class InfoDrawer extends StatelessWidget {
),
const SizedBox(height: 16),
Text(
"Credits",
'Credits',
style: Theme.of(context).textTheme.titleMedium,
),
_buildOnboardingImageCredits(),
@@ -204,36 +201,38 @@ class InfoDrawer extends StatelessWidget {
);
}
Future<dynamic> _onOpenInbox(BuildContext context) {
return Navigator.of(context).push(
Future<void> _onOpenInbox(BuildContext context) async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => GlobalStateBlocProvider(
additionalProviders: [
BlocProvider<InboxCubit>.value(
value: getIt<InboxCubit>()..initialize(),
value: getIt<InboxCubit>()..loadInbox(),
),
],
child: const InboxPage(),
),
maintainState: false,
),
);
afterInboxClosed?.call();
}
Link _buildOnboardingImageCredits() {
return Link(
uri: Uri.parse(
"https://www.freepik.com/free-vector/business-team-working-cogwheel-mechanism-together_8270974.htm#query=setting&position=4&from_view=author"),
'https://www.freepik.com/free-vector/business-team-working-cogwheel-mechanism-together_8270974.htm#query=setting&position=4&from_view=author'),
builder: (context, followLink) => Wrap(
children: [
const Text("Onboarding images by "),
const Text('Onboarding images by '),
GestureDetector(
onTap: followLink,
child: Text(
"pch.vector",
'pch.vector',
style: TextStyle(color: Theme.of(context).colorScheme.tertiary),
),
),
const Text(" on Freepik.")
const Text(' on Freepik.')
],
),
);

View File

@@ -21,9 +21,18 @@ class InboxCubit extends Cubit<InboxState> {
///
/// Fetches inbox tag ids and loads the inbox items (documents).
///
Future<void> initialize() async {
Future<void> loadInbox() async {
final inboxTags = await _labelRepository.getTags().then(
(value) => value.where((t) => t.isInboxTag ?? false).map((t) => t.id!));
(tags) => tags.where((t) => t.isInboxTag ?? false).map((t) => t.id!),
);
if (inboxTags.isEmpty) {
// no inbox tags = no inbox items.
return emit(const InboxState(
isLoaded: true,
inboxItems: [],
inboxTags: [],
));
}
final inboxDocuments = await _documentRepository
.find(DocumentFilter(
tags: AnyAssignedTagsQuery(tagIds: inboxTags),
@@ -38,33 +47,11 @@ class InboxCubit extends Cubit<InboxState> {
emit(newState);
}
Future<void> reloadInbox() async {
if (!state.isLoaded) {
throw "State has not yet loaded. Ensure the state is loaded when calling this method!";
}
final inboxDocuments = await _documentRepository
.find(DocumentFilter(
tags: AnyAssignedTagsQuery(tagIds: state.inboxTags),
sortField: SortField.added,
))
.then((psr) => psr.results);
emit(
InboxState(
isLoaded: true,
inboxItems: inboxDocuments,
inboxTags: state.inboxTags,
),
);
}
///
/// Updates the document with all inbox tags removed and removes the document
/// from the currently loaded inbox documents.
///
Future<Iterable<int>> remove(DocumentModel document) async {
if (!state.isLoaded) {
throw "State has not loaded yet. Ensure the state is loaded when calling this method!";
}
final tagsToRemove =
document.tags.toSet().intersection(state.inboxTags.toSet());

View File

@@ -1,15 +1,19 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
import 'package:paperless_mobile/core/model/error_message.dart';
import 'package:paperless_mobile/core/widgets/documents_list_loading_widget.dart';
import 'package:paperless_mobile/extensions/flutter_extensions.dart';
import 'package:paperless_mobile/features/documents/model/document.model.dart';
import 'package:paperless_mobile/features/inbox/bloc/inbox_cubit.dart';
import 'package:paperless_mobile/features/inbox/bloc/state/inbox_state.dart';
import 'package:paperless_mobile/features/inbox/view/widgets/document_inbox_item.dart';
import 'package:paperless_mobile/features/inbox/view/widgets/inbox_item.dart';
import 'package:paperless_mobile/features/inbox/view/widgets/inbox_empty_widget.dart';
import 'package:paperless_mobile/generated/l10n.dart';
import 'package:paperless_mobile/util.dart';
import 'package:collection/collection.dart';
import 'package:paperless_mobile/extensions/dart_extensions.dart';
class InboxPage extends StatefulWidget {
const InboxPage({super.key});
@@ -30,7 +34,6 @@ class _InboxPageState extends State<InboxPage> {
@override
Widget build(BuildContext context) {
//TODO: Group by date (today, yseterday, etc.)
return Scaffold(
appBar: AppBar(
title: Text(S.of(context).bottomNavInboxPageLabel),
@@ -39,7 +42,7 @@ class _InboxPageState extends State<InboxPage> {
onPressed: () => Navigator.pop(context),
),
bottom: PreferredSize(
preferredSize: Size.fromHeight(14),
preferredSize: const Size.fromHeight(14),
child: BlocBuilder<InboxCubit, InboxState>(
builder: (context, state) {
return Align(
@@ -49,7 +52,7 @@ class _InboxPageState extends State<InboxPage> {
child: ColoredBox(
color: Theme.of(context).colorScheme.secondaryContainer,
child: Text(
'${state.inboxItems.length} unseen',
'${state.inboxItems.length} ${S.of(context).inboxPageUnseenText}',
textAlign: TextAlign.start,
style: Theme.of(context).textTheme.caption,
).padded(const EdgeInsets.symmetric(horizontal: 4.0)),
@@ -62,8 +65,11 @@ class _InboxPageState extends State<InboxPage> {
),
floatingActionButton: BlocBuilder<InboxCubit, InboxState>(
builder: (context, state) {
if (!state.isLoaded || state.inboxItems.isEmpty) {
return const SizedBox.shrink();
}
return FloatingActionButton.extended(
label: Text("Mark all as seen"),
label: Text(S.of(context).inboxPageMarkAllAsSeenLabel),
icon: const Icon(Icons.done_all),
onPressed: state.isLoaded && state.inboxItems.isNotEmpty
? () => _onMarkAllAsSeen(
@@ -81,51 +87,68 @@ class _InboxPageState extends State<InboxPage> {
}
if (state.inboxItems.isEmpty) {
return RefreshIndicator(
key: _emptyStateRefreshIndicatorKey,
onRefresh: () =>
BlocProvider.of<InboxCubit>(context).reloadInbox(),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('You do not have unseen documents.'),
TextButton(
onPressed: () =>
_emptyStateRefreshIndicatorKey.currentState?.show(),
child: Text('Refresh'),
),
],
),
),
return InboxEmptyWidget(
emptyStateRefreshIndicatorKey: _emptyStateRefreshIndicatorKey,
);
}
// Build a list of slivers alternating between SliverToBoxAdapter
// (group header) and a SliverList (inbox items).
final List<Widget> slivers = _groupByDate(state.inboxItems)
.entries
.map(
(entry) => [
SliverToBoxAdapter(
child: Align(
alignment: Alignment.centerLeft,
child: ClipRRect(
borderRadius: BorderRadius.circular(32.0),
child: Text(
entry.key,
style: Theme.of(context).textTheme.caption,
textAlign: TextAlign.center,
).padded(),
),
).padded(const EdgeInsets.only(top: 8.0)),
),
SliverList(
delegate: SliverChildBuilderDelegate(
childCount: entry.value.length,
(context, index) => _buildListItem(
context,
entry.value[index],
),
),
),
],
)
.flattened
.toList();
return RefreshIndicator(
onRefresh: () => BlocProvider.of<InboxCubit>(context).reloadInbox(),
onRefresh: () => BlocProvider.of<InboxCubit>(context).loadInbox(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Hint: Swipe left to mark a document as seen. This will remove all inbox tags from the document.', //TODO: INTL
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.caption,
).padded(
const EdgeInsets.only(
top: 4.0,
left: 8.0,
right: 8.0,
bottom: 8.0,
),
),
Expanded(
child: ListView.builder(
itemCount: state.inboxItems.length,
itemBuilder: (context, index) {
final doc = state.inboxItems.elementAt(index);
return _buildListItem(context, doc);
},
child: CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: Text(
S.of(context).inboxPageUsageHintText,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.caption,
).padded(
const EdgeInsets.only(
top: 8.0,
left: 8.0,
right: 8.0,
bottom: 8.0,
),
),
),
...slivers
],
),
),
],
@@ -147,7 +170,7 @@ class _InboxPageState extends State<InboxPage> {
color: Theme.of(context).colorScheme.primary,
).padded(),
Text(
'Mark as seen', //TODO: INTL
S.of(context).inboxPageMarkAsSeenText,
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
@@ -156,7 +179,7 @@ class _InboxPageState extends State<InboxPage> {
).padded(),
confirmDismiss: (_) => _onItemDismissed(doc),
key: UniqueKey(),
child: DocumentInboxItem(document: doc),
child: InboxItem(document: doc),
);
}
@@ -167,9 +190,11 @@ class _InboxPageState extends State<InboxPage> {
final isActionConfirmed = await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Confirm action'),
title: Text(S
.of(context)
.inboxPageMarkAllAsSeenConfirmationDialogTitleText),
content: Text(
'Are you sure you want to mark all documents as seen? This will perform a bulk edit operation removing all inbox tags from the documents.\nThis action is not reversible! Are you sure you want to continue?',
S.of(context).inboxPageMarkAllAsSeenConfirmationDialogText,
),
actions: [
TextButton(
@@ -178,7 +203,10 @@ class _InboxPageState extends State<InboxPage> {
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text(S.of(context).genericActionOkLabel),
child: Text(
S.of(context).genericActionOkLabel,
style: TextStyle(color: Theme.of(context).colorScheme.error),
),
),
],
),
@@ -195,9 +223,9 @@ class _InboxPageState extends State<InboxPage> {
await BlocProvider.of<InboxCubit>(context).remove(doc);
showSnackBar(
context,
'Document removed from inbox.', //TODO: INTL
S.of(context).inboxPageDocumentRemovedMessageText,
action: SnackBarAction(
label: 'UNDO', //TODO: INTL
label: S.of(context).inboxPageUndoRemoveText,
onPressed: () => _onUndoMarkAsSeen(doc, removedTags),
),
);
@@ -225,4 +253,21 @@ class _InboxPageState extends State<InboxPage> {
showErrorMessage(context, error, stackTrace);
}
}
Map<String, List<DocumentModel>> _groupByDate(
Iterable<DocumentModel> documents,
) {
return groupBy<DocumentModel, String>(
documents,
(doc) {
if (doc.added.isToday) {
return S.of(context).inboxPageTodayText;
}
if (doc.added.isYesterday) {
return S.of(context).inboxPageYesterdayText;
}
return DateFormat.yMMMMd().format(doc.added);
},
);
}
}

View File

@@ -0,0 +1,36 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paperless_mobile/features/inbox/bloc/inbox_cubit.dart';
class InboxEmptyWidget extends StatelessWidget {
const InboxEmptyWidget({
Key? key,
required GlobalKey<RefreshIndicatorState> emptyStateRefreshIndicatorKey,
}) : _emptyStateRefreshIndicatorKey = emptyStateRefreshIndicatorKey,
super(key: key);
final GlobalKey<RefreshIndicatorState> _emptyStateRefreshIndicatorKey;
@override
Widget build(BuildContext context) {
return RefreshIndicator(
key: _emptyStateRefreshIndicatorKey,
onRefresh: () => BlocProvider.of<InboxCubit>(context).loadInbox(),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('You do not have unseen documents.'),
TextButton(
onPressed: () =>
_emptyStateRefreshIndicatorKey.currentState?.show(),
child: Text('Refresh'),
),
],
),
),
);
}
}

View File

@@ -11,14 +11,16 @@ import 'package:paperless_mobile/features/documents/view/widgets/document_previe
import 'package:paperless_mobile/features/labels/bloc/global_state_bloc_provider.dart';
import 'package:paperless_mobile/features/labels/tags/view/widgets/tags_widget.dart';
class DocumentInboxItem extends StatelessWidget {
class InboxItem extends StatelessWidget {
static const _a4AspectRatio = 1 / 1.4142;
final DocumentModel document;
const DocumentInboxItem({
const InboxItem({
super.key,
required this.document,
});
static const _a4AspectRatio = 1 / 1.4142;
@override
Widget build(BuildContext context) {
return ListTile(

View File

@@ -50,7 +50,7 @@ class _EditLabelPageState<T extends Label> extends State<EditLabelPage<T>> {
],
),
floatingActionButton: FloatingActionButton.extended(
icon: const Icon(Icons.add),
icon: const Icon(Icons.update),
label: Text(S.of(context).genericActionUpdateLabel),
onPressed: _onSubmit,
),