voyagehandbook/lib/views/home.dart
2023-03-27 19:45:57 +02:00

96 lines
2.7 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';
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(
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(
mainAxisAlignment: MainAxisAlignment.center,
children: (_recents.isEmpty)
? [
const Flexible(
child: Text(
"You haven't opened anything recently, swipe right and start searching."),
)
]
: _recents
..insert(
0,
const Text(
"Recent pages",
style: PageStyles.h1,
),
)
..insert(
1,
const SizedBox(
height: 15,
),
),
),
),
),
);
}
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);
_recents = [];
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),
),
),
),
),
);
}
setState(() {});
}
}