From 87dad2715b807e8ed1ec8cf38665feed70f2aa41 Mon Sep 17 00:00:00 2001 From: Codel1417 Date: Sun, 20 Oct 2024 12:43:01 -0400 Subject: [PATCH] Move manuals back to PDFs --- lib/Frontend/go_router_config.dart | 17 +++++ lib/Frontend/pages/more.dart | 19 +++--- lib/Frontend/pages/view_pdf.dart | 105 +++++++++++++++++++++++++++++ pubspec.lock | 12 +++- pubspec.yaml | 1 + 5 files changed, 143 insertions(+), 11 deletions(-) create mode 100644 lib/Frontend/pages/view_pdf.dart diff --git a/lib/Frontend/go_router_config.dart b/lib/Frontend/go_router_config.dart index a0930b45..8407169c 100644 --- a/lib/Frontend/go_router_config.dart +++ b/lib/Frontend/go_router_config.dart @@ -5,6 +5,7 @@ import 'package:go_router/go_router.dart'; import 'package:logarte/logarte.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:tail_app/Frontend/pages/developer/bulk_ota.dart'; +import 'package:tail_app/Frontend/pages/view_pdf.dart'; import '../Backend/Definitions/Action/base_action.dart'; import '../Backend/Definitions/Device/device_definition.dart'; @@ -207,6 +208,22 @@ class HtmlPageRoute extends GoRouteData { ); } +@TypedGoRoute( + path: '/viewPDF', + name: 'viewPDF', +) +class PDFPageRoute extends GoRouteData { + const PDFPageRoute({required this.$extra}); + + final PDFInfo $extra; + static final GlobalKey $navigatorKey = rootNavigatorKey; + + @override + Widget build(BuildContext context, GoRouterState state) => ViewPDF( + pdfInfo: $extra, + ); +} + @TypedGoRoute( path: '/joystick', name: 'Direct Gear Control', diff --git a/lib/Frontend/pages/more.dart b/lib/Frontend/pages/more.dart index 35040102..98a461b9 100644 --- a/lib/Frontend/pages/more.dart +++ b/lib/Frontend/pages/more.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:package_info_plus/package_info_plus.dart'; +import 'package:tail_app/Frontend/pages/view_pdf.dart'; import 'package:url_launcher/url_launcher.dart'; import '../../Backend/logging_wrappers.dart'; @@ -94,9 +95,9 @@ class _MoreState extends ConsumerState { title: Text(moreManualMiTailTitle()), subtitle: Text(moreManualSubTitle()), onTap: () async { - HtmlPageRoute( - $extra: HtmlPageInfo( - url: "https://docs.thetailcompany.com/doku.php?id=en:man:mitail&do=export_xhtmlbody", + PDFPageRoute( + $extra: PDFInfo( + url: "https://thetailcompany.com/mitail.pdf", title: moreManualMiTailTitle(), ), ).push(context); @@ -106,9 +107,9 @@ class _MoreState extends ConsumerState { title: Text(moreManualEargearTitle()), subtitle: Text(moreManualSubTitle()), onTap: () async { - HtmlPageRoute( - $extra: HtmlPageInfo( - url: "https://docs.thetailcompany.com/doku.php?id=en:man:eg2&do=export_xhtmlbody", + PDFPageRoute( + $extra: PDFInfo( + url: "https://thetailcompany.com/eargear.pdf", title: moreManualEargearTitle(), ), ).push(context); @@ -118,9 +119,9 @@ class _MoreState extends ConsumerState { title: Text(moreManualFlutterWingsTitle()), subtitle: Text(moreManualSubTitle()), onTap: () async { - HtmlPageRoute( - $extra: HtmlPageInfo( - url: "https://docs.thetailcompany.com/doku.php?id=en:man:flutterwings&do=export_xhtmlbody", + PDFPageRoute( + $extra: PDFInfo( + url: "https://thetailcompany.com/flutterwings.pdf", title: moreManualFlutterWingsTitle(), ), ).push(context); diff --git a/lib/Frontend/pages/view_pdf.dart b/lib/Frontend/pages/view_pdf.dart new file mode 100644 index 00000000..e8df8615 --- /dev/null +++ b/lib/Frontend/pages/view_pdf.dart @@ -0,0 +1,105 @@ +import 'dart:typed_data'; + +import 'package:dio/dio.dart'; +import 'package:flutter/material.dart'; +import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:pdfrx/pdfrx.dart'; +import 'package:tail_app/Frontend/utils.dart'; +import 'package:tail_app/constants.dart'; + +part 'view_pdf.freezed.dart'; + +@freezed +class PDFInfo with _$PDFInfo { + const factory PDFInfo({ + required String url, + required String title, + }) = _PDFInfo; +} + +class ViewPDF extends StatefulWidget { + final PDFInfo pdfInfo; + + const ViewPDF({required this.pdfInfo, super.key}); + + @override + State createState() => _ViewPDFState(); +} + +class _ViewPDFState extends State { + CancelToken cancelToken = CancelToken(); + double progress = 0; + + Uint8List? data; + + @override + void dispose() { + super.dispose(); + cancelToken.cancel(); + } + + @override + void initState() { + super.initState(); + downloadPDF(); + } + + Future downloadPDF() async { + final Response> rs = await (await initDio()).get( + widget.pdfInfo.url, + cancelToken: cancelToken, + options: Options( + contentType: 'application/pdf', + responseType: ResponseType.bytes, + ), + onReceiveProgress: (current, total) { + setState( + () { + progress = current / total; + }, + ); + }, + ); + if (rs.statusCode! < 400) { + if (context.mounted) { + progress = 0; + setState(() { + data = Uint8List.fromList(rs.data!); + }); + } + } else { + setState( + () { + progress = 0; + }, + ); + } + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(widget.pdfInfo.title), + ), + body: AnimatedSwitcher( + duration: animationTransitionDuration, + child: Builder( + key: ValueKey(data != null), + builder: (context) { + if (data != null) { + return PdfViewer.data( + data!, + sourceName: widget.pdfInfo.title, + ); + } else { + return Center( + child: CircularProgressIndicator( + value: progress, + ), + ); + } + }), + )); + } +} diff --git a/pubspec.lock b/pubspec.lock index aee6450c..07e56497 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1256,6 +1256,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.3.0" + pdfrx: + dependency: "direct main" + description: + name: pdfrx + sha256: "586ba63bf1c638de3986a220edf43941375ee2fe19de93a6c2706578605de060" + url: "https://pub.dev" + source: hosted + version: "1.0.84" pedometer: dependency: "direct main" description: @@ -1956,10 +1964,10 @@ packages: dependency: transitive description: name: web - sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb + sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062 url: "https://pub.dev" source: hosted - version: "1.1.0" + version: "1.0.0" web_socket: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 593560a3..c9c175fa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -79,6 +79,7 @@ dependencies: flutter_widget_from_html_core: ^0.15.2 visibility_detector: ^0.4.0+2 # used on the tail blog widgets lottie: ^3.1.2 + pdfrx: ^1.0.84 # Dio HTTP dio: ^5.7.0