voyagehandbook/lib/views/home.dart

119 lines
3.6 KiB
Dart
Raw Normal View History

2023-03-18 16:55:08 +01:00
import 'package:flutter/material.dart';
import 'package:voyagehandbook/util/drawer.dart';
import 'package:voyagehandbook/util/storage.dart';
2023-03-27 19:45:57 +02:00
import 'package:voyagehandbook/util/styles.dart';
import 'package:voyagehandbook/views/pageview.dart';
2023-03-28 18:08:26 +02:00
import 'package:voyagehandbook/views/search.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
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-18 16:55:08 +01:00
class HomeView extends StatefulWidget {
const HomeView({super.key});
@override
State<HomeView> createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
2023-03-27 19:45:57 +02:00
var _recents = <Widget>[];
2023-03-18 16:55:08 +01:00
@override
void initState() {
super.initState();
load();
}
@override
Widget build(BuildContext context) {
return Scaffold(
2023-03-28 18:08:26 +02:00
floatingActionButton: FloatingActionButton(
onPressed: () => Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (_) => const SearchView(),
),
),
child: const Icon(Icons.search),
),
appBar: AppBar(title: Text(AppLocalizations.of(context)!.home)),
2023-03-18 16:55:08 +01:00
drawer: genDrawer(1, context),
body: Center(
child: SizedBox(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width * 0.9,
child: Column(
2023-03-27 19:50:13 +02:00
mainAxisAlignment: MainAxisAlignment.center,
children: (_recents.isEmpty)
? [
Flexible(
child: Text(AppLocalizations.of(context)!.noRecents),
2023-03-27 19:50:13 +02:00
)
]
: _recents),
2023-03-18 16:55:08 +01:00
),
),
);
}
void load() async {
2023-03-27 19:45:57 +02:00
var recent = await StorageAccess.recent;
if (recent.isEmpty) return;
recent.sort((a, b) => DateTime.fromMillisecondsSinceEpoch(a["date"])
.isAfter(DateTime.fromMillisecondsSinceEpoch(b["date"]))
2023-03-27 19:50:13 +02:00
? -1
2023-03-27 19:45:57 +02:00
: DateTime.fromMillisecondsSinceEpoch(a["date"]).isAtSameMomentAs(
DateTime.fromMillisecondsSinceEpoch(b["date"]))
? 0
2023-03-27 19:50:13 +02:00
: 1);
if (!mounted) return;
2023-03-27 19:50:13 +02:00
_recents = [
Text(
AppLocalizations.of(context)!.recentPages,
2023-03-27 19:50:13 +02:00
style: PageStyles.h1,
),
const SizedBox(
height: 15,
),
];
2023-03-27 19:45:57 +02:00
for (var r in recent) {
if (!mounted) return;
_recents.add(
SizedBox(
width: MediaQuery.of(context).size.width * 0.9,
height: 50,
child: InkWell(
2023-04-03 17:07:27 +02:00
onTap: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (c) => ArticleView(pageKey: r["key"], name: r["name"]),
),
)..then((_) => load()),
2023-03-27 19:45:57 +02:00
child: Align(
alignment: Alignment.center,
child: Text(
r["name"],
style:
const TextStyle(fontWeight: FontWeight.w500, fontSize: 18),
),
),
),
),
);
}
2023-03-18 16:55:08 +01:00
setState(() {});
}
}