voyagehandbook/lib/views/home.dart

116 lines
3.5 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';
/*
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),
),
2023-03-18 16:55:08 +01:00
appBar: AppBar(title: const Text("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(
2023-03-27 19:50:13 +02:00
mainAxisAlignment: MainAxisAlignment.center,
children: (_recents.isEmpty)
? [
const Flexible(
child: Text(
"You haven't opened anything recently, swipe right and start searching."),
)
]
: _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);
_recents = [
const Text(
"Recent pages",
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(
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (c) =>
ArticleView(pageKey: r["key"], name: r["name"]))),
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(() {});
}
}