refactor: clean up

This commit is contained in:
Patryk Hegenberg 2025-04-09 22:17:20 +02:00
parent 18e07dcb6d
commit ef130fa83c
2 changed files with 178 additions and 31 deletions

View file

@ -76,6 +76,7 @@ class _ReportScreenState extends State<ReportScreen> {
Tag? _selectedTag;
ReportData? _reportData;
bool _isLoadingReport = false;
TimeEntry? _editingEntry;
String _formatDuration(Duration duration) {
String twoDigits(int n) => n.toString().padLeft(2, "0");
@ -666,6 +667,13 @@ class _ReportScreenState extends State<ReportScreen> {
icon: Icons.delete,
label: 'Löschen',
),
SlidableAction(
onPressed:
(context) => _showEditEntryDialog(context, entry),
backgroundColor: Colors.blue,
icon: Icons.edit,
label: 'Bearbeiten',
),
],
),
child: ListTile(
@ -691,29 +699,169 @@ class _ReportScreenState extends State<ReportScreen> {
);
}
// void _showPlatformFeedbackDialog(
// BuildContext context,
// String title,
// String message,
// ) {
// showPlatformDialog(
// context: context, // Still need context to initiate
// useRootNavigator:
// true, // This might help find a stable navigator ancestor
// builder:
// (dialogContext) => PlatformAlertDialog(
// title: PlatformText(title),
// content: PlatformText(message),
// actions: <Widget>[
// PlatformDialogAction(
// child: PlatformText('OK'),
// onPressed:
// () => Navigator.pop(
// dialogContext,
// ), // Pop using dialog's context
// ),
// ],
// ),
// );
// }
Future<void> _showEditEntryDialog(
BuildContext context,
TimeEntry entry,
) async {
DateTime? startTime = entry.startDateTime;
DateTime? endTime = entry.endDateTime;
await showPlatformDialog(
context: context,
builder: (BuildContext context) {
return PlatformAlertDialog(
title: Text('Eintrag bearbeiten'),
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
PlatformText('Startzeit:'),
PlatformElevatedButton(
child: Text(
DateFormat('yyyy-MM-dd HH:mm').format(startTime!),
),
onPressed: () async {
DateTime? pickedDate = await showPlatformDatePicker(
context: context,
initialDate: startTime!,
firstDate: DateTime(2000),
lastDate: DateTime(2101),
);
if (pickedDate != null) {
TimeOfDay? pickedTime = await showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(startTime!),
);
if (pickedTime != null) {
setState(() {
startTime = DateTime(
pickedDate.year,
pickedDate.month,
pickedDate.day,
pickedTime.hour,
pickedTime.minute,
);
});
}
}
},
),
PlatformText('Endzeit:'),
PlatformElevatedButton(
child: Text(
endTime != null
? DateFormat('yyyy-MM-dd HH:mm').format(endTime!)
: 'Keine Endzeit',
),
onPressed: () async {
DateTime? pickedDate = await showPlatformDatePicker(
context: context,
initialDate: endTime ?? DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2101),
);
if (pickedDate != null) {
TimeOfDay? pickedTime = await showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(
endTime ?? DateTime.now(),
),
);
if (pickedTime != null) {
setState(() {
endTime = DateTime(
pickedDate.year,
pickedDate.month,
pickedDate.day,
pickedTime.hour,
pickedTime.minute,
);
});
}
}
},
),
],
);
},
),
actions: [
PlatformDialogAction(
child: PlatformText('Abbrechen'),
onPressed: () {
Navigator.of(context).pop();
},
),
PlatformDialogAction(
child: PlatformText('Speichern'),
onPressed: () async {
// Hier die Logik zum Speichern der bearbeiteten Daten
Navigator.of(context).pop();
_updateTimeEntry(entry, startTime, endTime);
},
),
],
);
},
);
}
Future<void> _updateTimeEntry(
TimeEntry entry,
DateTime? startTime,
DateTime? endTime,
) async {
final timeService = Provider.of<TimeTrackingService>(
context,
listen: false,
);
if (startTime == null || endTime == null) {
log("Start- oder Endzeit darf nicht null sein.");
return;
}
bool success = await timeService.flUpdateTimeEntry(
entryId: entry.id,
tagId: entry.tagId,
startTime: startTime,
endTime: endTime,
);
if (success) {
_generateReport();
} else {
_showPlatformFeedbackDialog(
context,
'Error',
'Fehler beim Aktualisieren des TimeEntry.',
);
}
}
void _showPlatformFeedbackDialog(
BuildContext context,
String title,
String message,
) {
showPlatformDialog(
context: context,
useRootNavigator: true,
builder:
(dialogContext) => PlatformAlertDialog(
title: PlatformText(title),
content: PlatformText(message),
actions: <Widget>[
PlatformDialogAction(
child: PlatformText('OK'),
onPressed:
() => Navigator.pop(
dialogContext,
), // Pop using dialog's context
),
],
),
);
}
}