Skip to content

Commit

Permalink
Merge pull request #351 from astubenbord/bugfix/preview-not-working
Browse files Browse the repository at this point in the history
fix: Switch to pdfrx pdf rendering library
  • Loading branch information
astubenbord authored Jan 7, 2024
2 parents 27c4f7c + 30f1e64 commit 18d8d34
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 289 deletions.
2 changes: 1 addition & 1 deletion build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ targets:
generate_for:
- lib/**.dart
- test/**.dart
- integration_test/**.dart
- integration_test/src/mocks/**.dart
1 change: 1 addition & 0 deletions devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extensions:
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ class FormBuilderColorPickerField extends FormBuilderField<Color> {
onSaved: onSaved,
enabled: enabled,
onReset: onReset,
decoration: decoration,
focusNode: focusNode,
builder: (FormFieldState<Color?> field) {
final state = field as FormBuilderColorPickerFieldState;
return TextField(
style: style,
decoration: state.decoration.copyWith(
decoration: decoration.copyWith(
suffixIcon: colorPreviewBuilder != null
? colorPreviewBuilder(field.value)
: LayoutBuilder(
Expand Down
5 changes: 3 additions & 2 deletions lib/features/document_edit/view/document_edit_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import 'package:paperless_mobile/helpers/message_helpers.dart';
import 'package:paperless_mobile/routing/routes/labels_route.dart';
import 'package:paperless_mobile/routing/routes/shells/authenticated_route.dart';

typedef ItemBuilder<T> = Widget Function(BuildContext context, T itemData);
class DocumentEditPage extends StatefulWidget {
const DocumentEditPage({
Key? key,
}) : super(key: key);
super.key
});

@override
State<DocumentEditPage> createState() => _DocumentEditPageState();
Expand Down
116 changes: 82 additions & 34 deletions lib/features/documents/view/pages/document_view.dart
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:paperless_mobile/core/extensions/flutter_extensions.dart';
import 'package:pdfx/pdfx.dart';
import 'package:pdfrx/pdfrx.dart';

class DocumentView extends StatefulWidget {
final Future<Uint8List> documentBytes;
final String? title;
final bool showAppBar;
final bool showControls;
const DocumentView({
Key? key,
super.key,
required this.documentBytes,
this.showAppBar = true,
this.showControls = true,
this.title,
}) : super(key: key);
});

@override
State<DocumentView> createState() => _DocumentViewState();
}

class _DocumentViewState extends State<DocumentView> {
late final PdfController _controller;
final PdfViewerController _controller = PdfViewerController();
int _currentPage = 1;
int? _totalPages;

@override
void initState() {
super.initState();
final document =
widget.documentBytes.then((value) => PdfDocument.openData(value));
_controller = PdfController(document: document);
_controller.addListener(() {
if (mounted) {
if (_controller.pageNumber != null) {
setState(() {
_currentPage = _controller.pageNumber!;
});
}
setState(() {
_totalPages = _controller.pages.length;
});
}
});
}

@override
Expand All @@ -45,8 +57,7 @@ class _DocumentViewState extends State<DocumentView> {
? 0.milliseconds
: 100.milliseconds;
final canGoToNextPage = _totalPages != null && _currentPage < _totalPages!;
final canGoToPreviousPage =
_controller.pagesCount != null && _currentPage > 1;
final canGoToPreviousPage = _totalPages != null && _currentPage > 1;
return Scaffold(
appBar: widget.showAppBar
? AppBar(
Expand All @@ -63,9 +74,8 @@ class _DocumentViewState extends State<DocumentView> {
IconButton.filled(
onPressed: canGoToPreviousPage
? () async {
await _controller.previousPage(
duration: pageTransitionDuration,
curve: Curves.easeOut,
await _controller.goToPage(
pageNumber: _currentPage - 1,
);
}
: null,
Expand All @@ -75,9 +85,8 @@ class _DocumentViewState extends State<DocumentView> {
IconButton.filled(
onPressed: canGoToNextPage
? () async {
await _controller.nextPage(
duration: pageTransitionDuration,
curve: Curves.easeOut,
await _controller.goToPage(
pageNumber: _currentPage + 1,
);
}
: null,
Expand All @@ -86,14 +95,13 @@ class _DocumentViewState extends State<DocumentView> {
],
),
),
PdfPageNumber(
controller: _controller,
builder: (context, loadingState, page, pagesCount) {
if (loadingState != PdfLoadingState.success) {
Builder(
builder: (context) {
if (_totalPages == null) {
return const Text("-/-");
}
return Text(
"$page/$pagesCount",
"$_currentPage/$_totalPages",
style: Theme.of(context).textTheme.titleMedium,
).padded();
},
Expand All @@ -102,21 +110,61 @@ class _DocumentViewState extends State<DocumentView> {
),
)
: null,
body: PdfView(
controller: _controller,
onDocumentLoaded: (document) {
if (mounted) {
setState(() {
_totalPages = document.pagesCount;
});
}
},
onPageChanged: (page) {
if (mounted) {
setState(() {
_currentPage = page;
});
body: FutureBuilder<Uint8List>(
future: widget.documentBytes,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}

return PdfViewer.data(
snapshot.data!,
controller: _controller,
anchor: PdfPageAnchor.all,
displayParams: PdfViewerParams(
backgroundColor: Theme.of(context).colorScheme.background,
margin: 0,
layoutPages: (pages, params) {
final height =
pages.fold(0.0, (prev, page) => max(prev, page.height)) +
params.margin * 2;
final pageLayouts = <Rect>[];
double x = params.margin;
for (var page in pages) {
pageLayouts.add(
Rect.fromLTWH(
x,
(height - page.height) / 2, // center vertically
page.width,
page.height,
),
);
x += page.width + params.margin;
}
return PdfPageLayout(
pageLayouts: pageLayouts,
documentSize: Size(x, height),
);
},
),
// controller: _controller,
// onDocumentLoaded: (document) {
// if (mounted) {
// setState(() {
// _totalPages = document.pagesCount;
// });
// }
// },
// onPageChanged: (page) {
// if (mounted) {
// setState(() {
// _currentPage = page;
// });
// }
// },
);
},
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ class DocumentListItem extends DocumentItem {
children: [
Row(
children: [
AbsorbPointer(
absorbing: isSelectionActive,
child: CorrespondentWidget(
isClickable: isLabelClickable,
correspondent:
labelRepository.correspondents[document.correspondent],
onSelected: onCorrespondentSelected,
Flexible(
child: AbsorbPointer(
absorbing: isSelectionActive,
child: CorrespondentWidget(
isClickable: isLabelClickable,
correspondent:
labelRepository.correspondents[document.correspondent],
onSelected: onCorrespondentSelected,
),
),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class CorrespondentWidget extends StatelessWidget {
final TextStyle? textStyle;

const CorrespondentWidget({
Key? key,
super.key,
required this.correspondent,
this.textColor,
this.isClickable = true,
this.textStyle,
this.onSelected,
}) : super(key: key);
});

@override
Widget build(BuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class LocalNotificationService {
await _plugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.requestPermission();
?.requestNotificationsPermission();
}

Future<void> notifyFileDownload({
Expand All @@ -56,7 +56,7 @@ class LocalNotificationService {
"File download complete.",
NotificationDetails(
android: AndroidNotificationDetails(
NotificationChannel.fileDownload.id + "_${filePath.hashCode}",
"${NotificationChannel.fileDownload.id}_${filePath.hashCode}",
NotificationChannel.fileDownload.name,
importance: Importance.max,
priority: Priority.high,
Expand Down Expand Up @@ -92,7 +92,7 @@ class LocalNotificationService {
: tr.notificationDownloadingDocument,
NotificationDetails(
android: AndroidNotificationDetails(
NotificationChannel.documentDownload.id + "_${document.id}",
"${NotificationChannel.documentDownload.id}_${document.id}",
NotificationChannel.documentDownload.name,
progress: ((progress ?? 0) * 100).toInt(),
maxProgress: 100,
Expand Down Expand Up @@ -146,7 +146,7 @@ class LocalNotificationService {
: tr.notificationDownloadingDocument,
NotificationDetails(
android: AndroidNotificationDetails(
NotificationChannel.documentDownload.id + "_$filename",
"${NotificationChannel.documentDownload.id}_$filename",
NotificationChannel.documentDownload.name,
ongoing: !finished,
indeterminate: true,
Expand Down
5 changes: 2 additions & 3 deletions packages/mock_server/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: mock_server
description: A new Flutter package project.
version: 0.0.1
homepage:

environment:
sdk: '>=3.0.0 <4.0.0'
Expand All @@ -13,12 +12,12 @@ dependencies:
logging: ^1.1.1
flutter:
sdk: flutter
http: ^0.13.5
http: ^1.1.2

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter_lints: ^2.0.3

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ class RelativeDateRangeQuery extends DateRangeQuery {
DateTime get dateTime {
switch (unit) {
case DateRangeUnit.day:
return Jiffy().subtract(days: offset).dateTime;
return Jiffy.now().subtract(days: offset).dateTime;
case DateRangeUnit.week:
return Jiffy().subtract(weeks: offset).dateTime;
return Jiffy.now().subtract(weeks: offset).dateTime;
case DateRangeUnit.month:
return Jiffy().subtract(months: offset).dateTime;
return Jiffy.now().subtract(months: offset).dateTime;
case DateRangeUnit.year:
return Jiffy().subtract(years: offset).dateTime;
return Jiffy.now().subtract(years: offset).dateTime;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/paperless_api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ dependencies:
equatable: ^2.0.5
http: ^1.0.0
json_annotation: ^4.8.1
intl: ^0.18.1
intl: ^0.19.0
dio: ^5.0.0
collection: ^1.17.0
jiffy: ^5.0.0
jiffy: ^6.2.2
freezed_annotation: ^2.4.1
hive: ^2.2.3
mockito: ^5.4.4
Expand Down
Loading

0 comments on commit 18d8d34

Please sign in to comment.