2023-04-07 13:02:40 +02:00
|
|
|
import 'package:dio/dio.dart';
|
2023-03-24 23:48:55 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:voyagehandbook/api/wikimedia.dart';
|
|
|
|
import 'package:voyagehandbook/util/drawer.dart';
|
|
|
|
import 'package:voyagehandbook/util/render.dart';
|
2023-03-27 19:45:57 +02:00
|
|
|
import 'package:voyagehandbook/util/storage.dart';
|
2023-03-28 18:05:05 +02:00
|
|
|
import 'package:voyagehandbook/util/styles.dart';
|
2023-04-06 22:46:40 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-03-24 23:48:55 +01:00
|
|
|
|
2023-03-28 18:08:26 +02:00
|
|
|
/*
|
|
|
|
Voyage Handbook - The open-source WikiVoyage reader
|
|
|
|
Copyright (C) 2023 Matyáš Caras
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
2023-03-28 20:10:46 +02:00
|
|
|
it under the terms of the GNU General Public License version 3 as published by
|
|
|
|
the Free Software Foundation.
|
2023-03-28 18:08:26 +02:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-03-24 23:48:55 +01:00
|
|
|
/// Renders a single WikiVoyage article
|
|
|
|
class ArticleView extends StatefulWidget {
|
2023-03-27 19:45:57 +02:00
|
|
|
const ArticleView({super.key, required this.pageKey, required this.name});
|
2023-03-24 23:48:55 +01:00
|
|
|
final String pageKey;
|
2023-03-27 19:45:57 +02:00
|
|
|
final String name;
|
2023-03-24 23:48:55 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<ArticleView> createState() => _ArticleViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ArticleViewState extends State<ArticleView> {
|
|
|
|
var _content = <Widget>[];
|
|
|
|
@override
|
2023-03-27 19:45:57 +02:00
|
|
|
didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
|
2023-03-24 23:48:55 +01:00
|
|
|
loadPage();
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:45:57 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
addToRecents();
|
|
|
|
}
|
|
|
|
|
2023-03-24 23:48:55 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
actions: (kDebugMode)
|
|
|
|
? [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () => loadPage(),
|
|
|
|
icon: const Icon(Icons.restart_alt))
|
|
|
|
]
|
|
|
|
: null),
|
|
|
|
drawer: genDrawer(0, context),
|
|
|
|
body: Center(
|
|
|
|
child: SizedBox(
|
|
|
|
height: MediaQuery.of(context).size.height,
|
|
|
|
width: MediaQuery.of(context).size.width * 0.9,
|
|
|
|
child: SingleChildScrollView(
|
2023-03-27 19:45:57 +02:00
|
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
2023-03-24 23:48:55 +01:00
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: _content,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadPage() async {
|
2023-04-07 13:02:40 +02:00
|
|
|
if (!mounted) return;
|
2023-04-03 17:07:27 +02:00
|
|
|
var renderer = PageRenderer(
|
|
|
|
Theme.of(context).colorScheme,
|
|
|
|
MediaQuery.of(context).size.height,
|
|
|
|
MediaQuery.of(context).size.width,
|
2023-04-06 22:46:40 +02:00
|
|
|
context,
|
|
|
|
AppLocalizations.of(context)!);
|
2023-04-07 13:02:40 +02:00
|
|
|
|
|
|
|
try {
|
2023-03-28 18:05:05 +02:00
|
|
|
_content = [
|
|
|
|
SizedBox(
|
|
|
|
width: MediaQuery.of(context).size.width * 0.9,
|
|
|
|
height: MediaQuery.of(context).size.height,
|
2023-04-07 13:02:40 +02:00
|
|
|
child: await renderer
|
2023-03-28 20:03:21 +02:00
|
|
|
.renderFromPageHTML(await WikiApi.getRawPage(widget.pageKey)),
|
2023-03-28 18:05:05 +02:00
|
|
|
)
|
|
|
|
];
|
2023-04-07 13:02:40 +02:00
|
|
|
} catch (e) {
|
|
|
|
if (e.toString().contains("Failed host lookup")) {
|
|
|
|
// user is offline
|
|
|
|
var offline = await StorageAccess.getOfflinePage(widget.pageKey);
|
|
|
|
if (offline == null) {
|
|
|
|
// Not downloaded, show error
|
|
|
|
if (!mounted) return;
|
|
|
|
_content = [
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.renderError,
|
|
|
|
style: PageStyles.h1,
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 10,
|
|
|
|
),
|
|
|
|
Text(AppLocalizations.of(context)!.offlineError)
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
// Render offline version
|
|
|
|
if (!mounted) return;
|
|
|
|
_content = [
|
|
|
|
SizedBox(
|
|
|
|
width: MediaQuery.of(context).size.width * 0.9,
|
|
|
|
height: MediaQuery.of(context).size.height,
|
|
|
|
child: await renderer.renderFromPageHTML(offline),
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-03 19:09:51 +02:00
|
|
|
_content = [
|
2023-04-06 22:46:40 +02:00
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.renderError,
|
2023-04-03 19:09:51 +02:00
|
|
|
style: PageStyles.h1,
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 10,
|
|
|
|
),
|
|
|
|
Text(e.toString())
|
|
|
|
];
|
|
|
|
}
|
2023-03-28 18:05:05 +02:00
|
|
|
}
|
|
|
|
|
2023-03-24 23:48:55 +01:00
|
|
|
setState(() {});
|
|
|
|
}
|
2023-03-27 19:45:57 +02:00
|
|
|
|
|
|
|
void addToRecents() {
|
|
|
|
StorageAccess.addToRecents(widget.name, widget.pageKey);
|
2023-04-03 17:07:27 +02:00
|
|
|
if (kDebugMode) {
|
|
|
|
print("Added ${widget.name} to recent");
|
|
|
|
}
|
2023-03-27 19:45:57 +02:00
|
|
|
}
|
2023-03-24 23:48:55 +01:00
|
|
|
}
|