voyagehandbook/lib/views/home.dart
2023-04-06 22:46:40 +02:00

118 lines
3.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:voyagehandbook/util/drawer.dart';
import 'package:voyagehandbook/util/storage.dart';
import 'package:voyagehandbook/util/styles.dart';
import 'package:voyagehandbook/views/pageview.dart';
import 'package:voyagehandbook/views/search.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
/*
Voyage Handbook - The open-source WikiVoyage reader
Copyright (C) 2023 Matyáš Caras
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as published by
the Free Software Foundation.
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/>.
*/
class HomeView extends StatefulWidget {
const HomeView({super.key});
@override
State<HomeView> createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
var _recents = <Widget>[];
@override
void initState() {
super.initState();
load();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (_) => const SearchView(),
),
),
child: const Icon(Icons.search),
),
appBar: AppBar(title: Text(AppLocalizations.of(context)!.home)),
drawer: genDrawer(1, context),
body: Center(
child: SizedBox(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width * 0.9,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: (_recents.isEmpty)
? [
Flexible(
child: Text(AppLocalizations.of(context)!.noRecents),
)
]
: _recents),
),
),
);
}
void load() async {
var recent = await StorageAccess.recent;
if (recent.isEmpty) return;
recent.sort((a, b) => DateTime.fromMillisecondsSinceEpoch(a["date"])
.isAfter(DateTime.fromMillisecondsSinceEpoch(b["date"]))
? -1
: DateTime.fromMillisecondsSinceEpoch(a["date"]).isAtSameMomentAs(
DateTime.fromMillisecondsSinceEpoch(b["date"]))
? 0
: 1);
if (!mounted) return;
_recents = [
Text(
AppLocalizations.of(context)!.recentPages,
style: PageStyles.h1,
),
const SizedBox(
height: 15,
),
];
for (var r in recent) {
if (!mounted) return;
_recents.add(
SizedBox(
width: MediaQuery.of(context).size.width * 0.9,
height: 50,
child: InkWell(
onTap: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (c) => ArticleView(pageKey: r["key"], name: r["name"]),
),
)..then((_) => load()),
child: Align(
alignment: Alignment.center,
child: Text(
r["name"],
style:
const TextStyle(fontWeight: FontWeight.w500, fontSize: 18),
),
),
),
),
);
}
setState(() {});
}
}