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-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(
|
|
|
|
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,
|
2023-03-27 19:45:57 +02:00
|
|
|
children: (_recents.isEmpty)
|
|
|
|
? [
|
|
|
|
const Flexible(
|
2023-03-18 16:55:08 +01:00
|
|
|
child: Text(
|
|
|
|
"You haven't opened anything recently, swipe right and start searching."),
|
|
|
|
)
|
2023-03-27 19:45:57 +02:00
|
|
|
]
|
|
|
|
: _recents
|
|
|
|
..insert(
|
|
|
|
0,
|
|
|
|
const Text(
|
|
|
|
"Recent pages",
|
|
|
|
style: PageStyles.h1,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
..insert(
|
|
|
|
1,
|
|
|
|
const SizedBox(
|
|
|
|
height: 15,
|
|
|
|
),
|
|
|
|
),
|
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"]))
|
|
|
|
? 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),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-03-18 16:55:08 +01:00
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
}
|