Files
AcWeb/ac_web/lib/Widgets/LatestReportColumn.dart
T
2026-08-31 15:33:06 +02:00

147 lines
3.8 KiB
Dart

import 'package:ac_web/Api/AcApi.dart';
import 'package:ac_web/Api/Dtos/EReportType.dart';
import 'package:ac_web/Api/Dtos/ReportDto.dart';
import 'package:ac_web/Pages/ReportDetails.dart';
import 'package:ac_web/Widgets/LatestReportEntry.dart';
import 'package:ac_web/Widgets/LatestReportHeader.dart';
import 'package:flutter/material.dart';
class LatestReportColumn extends StatefulWidget{
const LatestReportColumn({super.key});
@override
State<LatestReportColumn> createState() => _LatestReportColumnState();
}
class _LatestReportColumnState extends State<LatestReportColumn>{
late Future<List<ReportDto>> mReportsFuture;
@override
void initState() {
super.initState();
mReportsFuture = AcApi.instance.reports.getReports();
}
@override
Widget build(BuildContext context) {
return FutureBuilder<List<ReportDto>>(
future: mReportsFuture,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(child: Text('Could not load reports: ${snapshot.error}'));
}
// The list of reports fetched from the ReportsController.
final List<ReportDto> reports = snapshot.data ?? <ReportDto>[];
return _buildList(reports);
},
);
}
Widget _buildList(List<ReportDto> reports) {
List<Widget> widgets = <Widget>[];
// Title Entry
widgets.add(LatestReportHeader(text: "Latest Reports", bigHeader: true,));
DateTime? lastDay;
// Iterate through the reports and create Report entries accordingly
for(int i = 0; i < reports.length; i++){
// Add a day header above the first entry of every day
final DateTime day = _dayOf(reports[i].timestamp);
if(lastDay == null || day != lastDay){
widgets.add(LatestReportHeader(text: _dayLabel(day)));
lastDay = day;
}
final ReportDto report = reports[i];
IconData icon;
switch(reports[i].type){
case EReportType.log:
icon = Icons.receipt_long;
case EReportType.codeReview:
icon = Icons.code;
case EReportType.projectPlan:
icon = Icons.assignment;
case EReportType.research:
icon = Icons.science;
case EReportType.newspaper:
icon = Icons.newspaper;
case EReportType.unknown:
icon = Icons.question_mark;
}
widgets.add(LatestReportEntry(
text: reports[i].title,
icon: icon,
onTap: () => _openReport(report),
));
}
return ListView(
padding: EdgeInsetsGeometry.all(8),
children: widgets,
);
}
/// Opens a new page that shows all details of the given report.
void _openReport(ReportDto report) {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (context) => ReportDetails(report: report),
),
);
}
/// Strips the time part, so that two timestamps of the same day are equal.
DateTime _dayOf(DateTime timestamp) {
final DateTime local = timestamp.toLocal();
return DateTime(local.year, local.month, local.day);
}
/// "Today", "Yesterday" or e.g. "15. August" for all other days.
String _dayLabel(DateTime day) {
final DateTime today = _dayOf(DateTime.now());
final DateTime yesterday = DateTime(today.year, today.month, today.day - 1);
if (day == today) {
return "Today";
}
if (day == yesterday) {
return "Yesterday";
}
return "${day.day}. ${_monthNames[day.month - 1]}";
}
static const List<String> _monthNames = <String>[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
}