Calling API in LatestReportColumn.dart
and improving UI
This commit is contained in:
@@ -1,49 +1,136 @@
|
||||
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/Widgets/LatestReportEntry.dart';
|
||||
import 'package:ac_web/Widgets/LatestReportHeader.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LatestReportColumn extends StatelessWidget{
|
||||
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,));
|
||||
|
||||
widgets.add(
|
||||
LatestReportHeader(text: "Today")
|
||||
);
|
||||
DateTime? lastDay;
|
||||
|
||||
widgets.add(LatestReportEntry(
|
||||
text: "Entry 0",
|
||||
icon: Icons.code,
|
||||
onTap: () => {widgets.add(
|
||||
LatestReportHeader(text: "WOOOD")
|
||||
)},
|
||||
));
|
||||
widgets.add(LatestReportEntry(
|
||||
text: "Entry 1",
|
||||
icon: Icons.newspaper,
|
||||
));
|
||||
// Iterate through the reports and create Report entries accordingly
|
||||
for(int i = 0; i < reports.length; i++){
|
||||
|
||||
widgets.add(
|
||||
LatestReportHeader(text: "Yesterday")
|
||||
);
|
||||
widgets.add(LatestReportEntry(
|
||||
text: "Entry -1",
|
||||
icon: Icons.code,
|
||||
));
|
||||
// Add a day header above the first entry of every day
|
||||
final DateTime day = _dayOf(reports[i].timestamp);
|
||||
|
||||
widgets.add(
|
||||
LatestReportHeader(text: "15. August")
|
||||
);
|
||||
if(lastDay == null || day != lastDay){
|
||||
widgets.add(LatestReportHeader(text: _dayLabel(day)));
|
||||
lastDay = day;
|
||||
}
|
||||
|
||||
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: () => {widgets.add(
|
||||
LatestReportHeader(text: "WOOOD")
|
||||
)},
|
||||
));
|
||||
}
|
||||
|
||||
return ListView(
|
||||
padding: EdgeInsetsGeometry.all(8),
|
||||
children: widgets,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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",
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user