2022-01-19 02:10:05 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
import '../components/common/loading.dart';
|
|
|
|
import '../components/common/opaque_box.dart';
|
|
|
|
import '../components/history/date_divider.dart';
|
2023-02-24 09:55:55 +01:00
|
|
|
import '../components/history/history_entry_tile.dart';
|
2022-06-05 02:41:11 +02:00
|
|
|
import '../models/history/history_entry.dart';
|
2022-01-25 20:44:00 +01:00
|
|
|
import '../services/datetime.dart';
|
2022-01-19 02:10:05 +01:00
|
|
|
|
|
|
|
class HistoryView extends StatelessWidget {
|
|
|
|
const HistoryView({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-06-05 02:41:11 +02:00
|
|
|
// TODO: Use infinite scroll pagination
|
|
|
|
return FutureBuilder<List<HistoryEntry>>(
|
|
|
|
future: HistoryEntry.fromDB,
|
2022-01-19 02:10:05 +01:00
|
|
|
builder: (context, snapshot) {
|
2022-06-05 02:41:11 +02:00
|
|
|
// TODO: provide proper error handling
|
|
|
|
if (snapshot.hasError) return ErrorWidget(snapshot.error!);
|
2022-01-19 02:10:05 +01:00
|
|
|
if (!snapshot.hasData) return const LoadingScreen();
|
|
|
|
|
2022-06-05 02:41:11 +02:00
|
|
|
final Map<int, HistoryEntry> data = snapshot.data!.asMap();
|
2022-01-19 02:10:05 +01:00
|
|
|
if (data.isEmpty)
|
|
|
|
return const Center(
|
|
|
|
child: Text('The history is empty.\nTry searching for something!'),
|
|
|
|
);
|
|
|
|
|
|
|
|
return OpaqueBox(
|
|
|
|
child: ListView.separated(
|
|
|
|
itemCount: data.length + 1,
|
|
|
|
itemBuilder: historyEntryWithData(data),
|
|
|
|
separatorBuilder:
|
|
|
|
historyEntrySeparatorWithData(data.values.toList()),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget Function(BuildContext, int) historyEntrySeparatorWithData(
|
2022-06-05 02:41:11 +02:00
|
|
|
List<HistoryEntry> data,
|
2022-01-19 02:10:05 +01:00
|
|
|
) =>
|
|
|
|
(context, index) {
|
2022-06-05 02:41:11 +02:00
|
|
|
final HistoryEntry search = data[index];
|
|
|
|
final DateTime searchDate = search.lastTimestamp;
|
2022-01-19 02:10:05 +01:00
|
|
|
|
2023-02-24 09:55:55 +01:00
|
|
|
if (index == 0 ||
|
|
|
|
!dateIsEqual(data[index - 1].lastTimestamp, searchDate))
|
2022-01-25 20:44:00 +01:00
|
|
|
return TextDivider(text: formatDate(roundToDay(searchDate)));
|
2022-01-19 02:10:05 +01:00
|
|
|
|
2023-02-24 09:55:55 +01:00
|
|
|
return const Divider(
|
|
|
|
height: 0,
|
|
|
|
indent: 10,
|
|
|
|
endIndent: 10,
|
|
|
|
);
|
2022-01-19 02:10:05 +01:00
|
|
|
};
|
2022-01-25 20:44:00 +01:00
|
|
|
|
|
|
|
Widget Function(BuildContext, int) historyEntryWithData(
|
2022-06-05 02:41:11 +02:00
|
|
|
Map<int, HistoryEntry> data,
|
2022-01-25 20:44:00 +01:00
|
|
|
) =>
|
|
|
|
(context, index) => (index == 0)
|
|
|
|
? const SizedBox.shrink()
|
2023-02-24 09:55:55 +01:00
|
|
|
: HistoryEntryTile(
|
2022-06-05 02:41:11 +02:00
|
|
|
entry: data.values.toList()[index - 1],
|
2022-01-25 20:44:00 +01:00
|
|
|
objectKey: data.keys.toList()[index - 1],
|
|
|
|
onDelete: () => build(context),
|
|
|
|
);
|
2022-01-19 02:10:05 +01:00
|
|
|
}
|