Skip to content

Commit

Permalink
feat: Further migrations to go_router, add onclick to document previews
Browse files Browse the repository at this point in the history
  • Loading branch information
astubenbord committed Jul 31, 2023
1 parent f1398e6 commit f3e660e
Show file tree
Hide file tree
Showing 33 changed files with 862 additions and 839 deletions.
111 changes: 2 additions & 109 deletions lib/core/navigation/push_routes.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import 'dart:typed_data';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:go_router/go_router.dart';
import 'package:hive/hive.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/bloc/connectivity_cubit.dart';
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
import 'package:paperless_mobile/core/database/tables/global_settings.dart';
import 'package:paperless_mobile/core/database/tables/local_user_account.dart';
import 'package:paperless_mobile/core/database/tables/local_user_app_state.dart';
import 'package:paperless_mobile/core/notifier/document_changed_notifier.dart';
Expand All @@ -18,44 +11,14 @@ import 'package:paperless_mobile/core/repository/user_repository.dart';
import 'package:paperless_mobile/features/document_bulk_action/cubit/document_bulk_action_cubit.dart';
import 'package:paperless_mobile/features/document_bulk_action/view/widgets/fullscreen_bulk_edit_label_page.dart';
import 'package:paperless_mobile/features/document_bulk_action/view/widgets/fullscreen_bulk_edit_tags_widget.dart';
import 'package:paperless_mobile/features/document_search/cubit/document_search_cubit.dart';
import 'package:paperless_mobile/features/document_search/view/document_search_page.dart';
import 'package:paperless_mobile/features/document_upload/cubit/document_upload_cubit.dart';
import 'package:paperless_mobile/features/document_upload/view/document_upload_preparation_page.dart';
import 'package:paperless_mobile/features/home/view/model/api_version.dart';
import 'package:paperless_mobile/features/linked_documents/cubit/linked_documents_cubit.dart';
import 'package:paperless_mobile/features/linked_documents/view/linked_documents_page.dart';
import 'package:paperless_mobile/features/notifications/services/local_notification_service.dart';
import 'package:paperless_mobile/features/saved_view/cubit/saved_view_cubit.dart';
import 'package:paperless_mobile/features/saved_view/view/add_saved_view_page.dart';
import 'package:paperless_mobile/features/saved_view_details/cubit/saved_view_details_cubit.dart';
import 'package:paperless_mobile/features/saved_view_details/view/saved_view_details_page.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
import 'package:provider/provider.dart';

// These are convenience methods for nativating to views without having to pass providers around explicitly.
// Providers unfortunately have to be passed to the routes since they are children of the Navigator, not ancestors.

Future<void> pushDocumentSearchPage(BuildContext context) {
final currentUser = Hive.box<GlobalSettings>(HiveBoxes.globalSettings)
.getValue()!
.loggedInUserId;
final userRepo = context.read<UserRepository>();
return Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => BlocProvider(
create: (context) => DocumentSearchCubit(
context.read(),
context.read(),
Hive.box<LocalUserAppState>(HiveBoxes.localUserAppState)
.get(currentUser)!,
),
child: const DocumentSearchPage(),
),
),
);
}

Future<void> pushSavedViewDetailsRoute(
BuildContext context, {
required SavedView savedView,
Expand Down Expand Up @@ -84,7 +47,8 @@ Future<void> pushSavedViewDetailsRoute(
savedView: savedView,
),
child: SavedViewDetailsPage(
onDelete: context.read<SavedViewCubit>().remove),
onDelete: context.read<SavedViewCubit>().remove,
),
);
},
),
Expand All @@ -107,39 +71,6 @@ Future<SavedView?> pushAddSavedViewRoute(BuildContext context,
);
}

Future<void> pushLinkedDocumentsView(
BuildContext context, {
required DocumentFilter filter,
}) {
return Navigator.push(
context,
MaterialPageRoute(
builder: (_) => MultiProvider(
providers: [
Provider.value(value: context.read<ApiVersion>()),
Provider.value(value: context.read<LabelRepository>()),
Provider.value(value: context.read<DocumentChangedNotifier>()),
Provider.value(value: context.read<PaperlessDocumentsApi>()),
Provider.value(value: context.read<LocalNotificationService>()),
Provider.value(value: context.read<CacheManager>()),
Provider.value(value: context.read<ConnectivityCubit>()),
if (context.watch<LocalUserAccount>().hasMultiUserSupport)
Provider.value(value: context.read<UserRepository>()),
],
builder: (context, _) => BlocProvider(
create: (context) => LinkedDocumentsCubit(
filter,
context.read(),
context.read(),
context.read(),
),
child: const LinkedDocumentsPage(),
),
),
),
);
}

Future<void> pushBulkEditCorrespondentRoute(
BuildContext context, {
required List<DocumentModel> selection,
Expand Down Expand Up @@ -306,44 +237,6 @@ Future<void> pushBulkEditDocumentTypeRoute(BuildContext context,
);
}

Future<DocumentUploadResult?> pushDocumentUploadPreparationPage(
BuildContext context, {
required Uint8List bytes,
String? filename,
String? fileExtension,
String? title,
}) {
final labelRepo = context.read<LabelRepository>();
final docsApi = context.read<PaperlessDocumentsApi>();
final connectivity = context.read<Connectivity>();
final apiVersion = context.read<ApiVersion>();
return Navigator.of(context).push<DocumentUploadResult>(
MaterialPageRoute(
builder: (_) => MultiProvider(
providers: [
Provider.value(value: labelRepo),
Provider.value(value: docsApi),
Provider.value(value: connectivity),
Provider.value(value: apiVersion)
],
builder: (_, child) => BlocProvider(
create: (_) => DocumentUploadCubit(
context.read(),
context.read(),
context.read(),
),
child: DocumentUploadPreparationPage(
fileBytes: bytes,
fileExtension: fileExtension,
filename: filename,
title: title,
),
),
),
),
);
}

List<Provider> _getRequiredBulkEditProviders(BuildContext context) {
return [
Provider.value(value: context.read<PaperlessDocumentsApi>()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/widgets/form_fields/fullscreen_selection_form.dart';
import 'package:paperless_mobile/extensions/dart_extensions.dart';
Expand Down Expand Up @@ -86,6 +87,7 @@ class _FullscreenBulkEditLabelPageState<T extends Label>
selectionCount: _labels.length,
floatingActionButton: !hideFab
? FloatingActionButton.extended(
heroTag: "fab_fullscreen_bulk_edit_label",
onPressed: _onSubmit,
label: Text(S.of(context)!.apply),
icon: const Icon(Icons.done),
Expand Down Expand Up @@ -122,7 +124,7 @@ class _FullscreenBulkEditLabelPageState<T extends Label>

void _onSubmit() async {
if (_selection == null) {
Navigator.pop(context);
context.pop();
} else {
bool shouldPerformAction;
if (_selection!.label == null) {
Expand All @@ -148,7 +150,7 @@ class _FullscreenBulkEditLabelPageState<T extends Label>
}
if (shouldPerformAction) {
widget.onSubmit(_selection!.label);
Navigator.pop(context);
context.pop();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/widgets/form_fields/fullscreen_selection_form.dart';
import 'package:paperless_mobile/extensions/dart_extensions.dart';
Expand Down Expand Up @@ -74,6 +75,7 @@ class _FullscreenBulkEditTagsWidgetState
controller: _controller,
floatingActionButton: _addTags.isNotEmpty || _removeTags.isNotEmpty
? FloatingActionButton.extended(
heroTag: "fab_fullscreen_bulk_edit_tags",
label: Text(S.of(context)!.apply),
icon: const Icon(Icons.done),
onPressed: _submit,
Expand Down Expand Up @@ -173,7 +175,7 @@ class _FullscreenBulkEditTagsWidgetState
removeTagIds: _removeTags,
addTagIds: _addTags,
);
Navigator.pop(context);
context.pop();
}
}
}
Expand Down
94 changes: 49 additions & 45 deletions lib/features/document_details/view/pages/document_details_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:open_filex/open_filex.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/bloc/connectivity_cubit.dart';
Expand Down Expand Up @@ -92,37 +93,43 @@ class _DocumentDetailsPageState extends State<DocumentDetailsPage> {
DocumentDetailsState>(
builder: (context, state) {
return Positioned.fill(
child: DocumentPreview(
document: state.document,
fit: BoxFit.cover,
child: GestureDetector(
onTap: () {
DocumentPreviewRoute($extra: state.document)
.push(context);
},
child: DocumentPreview(
document: state.document,
fit: BoxFit.cover,
),
),
);
},
),
Positioned.fill(
top: 0,
child: DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Theme.of(context)
.colorScheme
.background
.withOpacity(0.8),
Theme.of(context)
.colorScheme
.background
.withOpacity(0.5),
Colors.transparent,
Colors.transparent,
Colors.transparent,
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
),
),
// Positioned.fill(
// top: -kToolbarHeight,
// child: DecoratedBox(
// decoration: BoxDecoration(
// gradient: LinearGradient(
// colors: [
// Theme.of(context)
// .colorScheme
// .background
// .withOpacity(0.8),
// Theme.of(context)
// .colorScheme
// .background
// .withOpacity(0.5),
// Colors.transparent,
// Colors.transparent,
// Colors.transparent,
// ],
// begin: Alignment.topCenter,
// end: Alignment.bottomCenter,
// ),
// ),
// ),
// ),
],
),
),
Expand Down Expand Up @@ -302,6 +309,7 @@ class _DocumentDetailsPageState extends State<DocumentDetailsPage> {
preferBelow: false,
verticalOffset: 40,
child: FloatingActionButton(
heroTag: "fab_document_details",
child: const Icon(Icons.edit),
onPressed: () => EditDocumentRoute(state.document).push(context),
),
Expand Down Expand Up @@ -333,13 +341,13 @@ class _DocumentDetailsPageState extends State<DocumentDetailsPage> {
document: state.document,
enabled: isConnected,
),
//TODO: Enable again, need new pdf viewer package...
IconButton(
tooltip: S.of(context)!.previewTooltip,
icon: const Icon(Icons.visibility),
onPressed:
(isConnected) ? () => _onOpen(state.document) : null,
).paddedOnly(right: 4.0),
// //TODO: Enable again, need new pdf viewer package...
// IconButton(
// tooltip: S.of(context)!.previewTooltip,
// icon: const Icon(Icons.visibility),
// onPressed:
// (isConnected) ? () => _onOpen(state.document) : null,
// ).paddedOnly(right: 4.0),
IconButton(
tooltip: S.of(context)!.openInSystemViewer,
icon: const Icon(Icons.open_in_new),
Expand Down Expand Up @@ -391,21 +399,17 @@ class _DocumentDetailsPageState extends State<DocumentDetailsPage> {
} on PaperlessApiException catch (error, stackTrace) {
showErrorMessage(context, error, stackTrace);
} finally {
// Document deleted => go back to primary route
Navigator.popUntil(context, (route) => route.isFirst);
do {
context.pop();
} while (context.canPop());
}
}
}

Future<void> _onOpen(DocumentModel document) async {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => DocumentView(
documentBytes:
context.read<PaperlessDocumentsApi>().download(document),
title: document.title,
),
),
);
DocumentPreviewRoute(
$extra: document,
title: document.title,
).push(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/database/tables/local_user_account.dart';
import 'package:paperless_mobile/extensions/flutter_extensions.dart';
import 'package:paperless_mobile/features/document_details/cubit/document_details_cubit.dart';
import 'package:paperless_mobile/features/document_details/view/widgets/archive_serial_number_field.dart';
Expand All @@ -25,6 +26,7 @@ class DocumentMetaDataWidget extends StatefulWidget {
class _DocumentMetaDataWidgetState extends State<DocumentMetaDataWidget> {
@override
Widget build(BuildContext context) {
final currentUser = context.watch<LocalUserAccount>().paperlessUser;
return BlocBuilder<DocumentDetailsCubit, DocumentDetailsState>(
builder: (context, state) {
if (state.metaData == null) {
Expand All @@ -37,9 +39,10 @@ class _DocumentMetaDataWidgetState extends State<DocumentMetaDataWidget> {
return SliverList(
delegate: SliverChildListDelegate(
[
ArchiveSerialNumberField(
document: widget.document,
).paddedOnly(bottom: widget.itemSpacing),
if (currentUser.canEditDocuments)
ArchiveSerialNumberField(
document: widget.document,
).paddedOnly(bottom: widget.itemSpacing),
DetailsItem.text(
DateFormat().format(widget.document.modified),
context: context,
Expand Down
Loading

0 comments on commit f3e660e

Please sign in to comment.