Skip to content

Commit

Permalink
Move manuals back to PDFs
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Oct 20, 2024
1 parent 41d60b9 commit 87dad27
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 11 deletions.
17 changes: 17 additions & 0 deletions lib/Frontend/go_router_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -207,6 +208,22 @@ class HtmlPageRoute extends GoRouteData {
);
}

@TypedGoRoute<PDFPageRoute>(
path: '/viewPDF',
name: 'viewPDF',
)
class PDFPageRoute extends GoRouteData {
const PDFPageRoute({required this.$extra});

final PDFInfo $extra;
static final GlobalKey<NavigatorState> $navigatorKey = rootNavigatorKey;

@override
Widget build(BuildContext context, GoRouterState state) => ViewPDF(
pdfInfo: $extra,
);
}

@TypedGoRoute<DirectGearControlRoute>(
path: '/joystick',
name: 'Direct Gear Control',
Expand Down
19 changes: 10 additions & 9 deletions lib/Frontend/pages/more.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -94,9 +95,9 @@ class _MoreState extends ConsumerState<More> {
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);
Expand All @@ -106,9 +107,9 @@ class _MoreState extends ConsumerState<More> {
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);
Expand All @@ -118,9 +119,9 @@ class _MoreState extends ConsumerState<More> {
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);
Expand Down
105 changes: 105 additions & 0 deletions lib/Frontend/pages/view_pdf.dart
Original file line number Diff line number Diff line change
@@ -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<ViewPDF> createState() => _ViewPDFState();
}

class _ViewPDFState extends State<ViewPDF> {
CancelToken cancelToken = CancelToken();
double progress = 0;

Uint8List? data;

@override
void dispose() {
super.dispose();
cancelToken.cancel();
}

@override
void initState() {
super.initState();
downloadPDF();
}

Future<void> downloadPDF() async {
final Response<List<int>> 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,
),
);
}
}),
));
}
}
12 changes: 10 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 87dad27

Please sign in to comment.