151 lines
4.3 KiB
Dart
151 lines
4.3 KiB
Dart
import 'package:diameter/components/dialogs.dart';
|
|
import 'package:diameter/config.dart';
|
|
import 'package:diameter/models/log_entry.dart';
|
|
import 'package:diameter/models/log_event.dart';
|
|
import 'package:diameter/utils/date_time_utils.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class ActiveLogEventListScreen extends StatefulWidget {
|
|
static const String routeName = '/active-log-events';
|
|
|
|
final int endLogEntryId;
|
|
final Function()? onSetEndTime;
|
|
|
|
const ActiveLogEventListScreen(
|
|
{Key? key, this.endLogEntryId = 0, this.onSetEndTime})
|
|
: super(key: key);
|
|
|
|
@override
|
|
_ActiveLogEventListScreenState createState() =>
|
|
_ActiveLogEventListScreenState();
|
|
}
|
|
|
|
class _ActiveLogEventListScreenState extends State<ActiveLogEventListScreen> {
|
|
List<LogEvent> _activeLogEvents = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
reload();
|
|
}
|
|
|
|
void reload({String? message}) {
|
|
setState(() {
|
|
_activeLogEvents = LogEvent.getAllOngoing();
|
|
});
|
|
|
|
setState(() {
|
|
if (message != null) {
|
|
var snackBar = SnackBar(
|
|
content: Text(message),
|
|
duration: const Duration(seconds: 2),
|
|
);
|
|
ScaffoldMessenger.of(context)
|
|
..removeCurrentSnackBar()
|
|
..showSnackBar(snackBar);
|
|
}
|
|
});
|
|
}
|
|
|
|
void onStop(LogEvent event) async {
|
|
event.endTime = DateTime.now();
|
|
event.endLogEntry.target =
|
|
LogEntry.get(widget.endLogEntryId) ?? LogEntry(time: DateTime.now());
|
|
LogEvent.put(event);
|
|
reload();
|
|
if (widget.onSetEndTime != null) {
|
|
widget.onSetEndTime!();
|
|
}
|
|
}
|
|
|
|
void handleStopAction(LogEvent event) async {
|
|
if (showConfirmationDialogOnStopEvent) {
|
|
Dialogs.showConfirmationDialog(
|
|
context: context,
|
|
onConfirm: () => onStop(event),
|
|
message: 'Are you sure you want to end this Event?',
|
|
);
|
|
} else {
|
|
onStop(event);
|
|
}
|
|
}
|
|
|
|
void onDelete(LogEvent event) {
|
|
LogEvent.remove(event.id);
|
|
reload(message: 'Event deleted');
|
|
}
|
|
|
|
void handleDeleteAction(LogEvent event) async {
|
|
if (showConfirmationDialogOnDelete) {
|
|
Dialogs.showConfirmationDialog(
|
|
context: context,
|
|
onConfirm: () => onDelete(event),
|
|
message: 'Are you sure you want to delete this Event?',
|
|
);
|
|
} else {
|
|
onDelete(event);
|
|
}
|
|
}
|
|
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return SingleChildScrollView(
|
|
padding: const EdgeInsets.only(top: 10.0),
|
|
child: Column(
|
|
children: [
|
|
AppBar(
|
|
title: const Text('Active Events'),
|
|
primary: false,
|
|
automaticallyImplyLeading: false,
|
|
actions: [
|
|
IconButton(icon: const Icon(Icons.refresh), onPressed: reload),
|
|
],
|
|
),
|
|
_activeLogEvents.isNotEmpty ?
|
|
ListView.builder(
|
|
shrinkWrap: true,
|
|
itemCount: _activeLogEvents.length,
|
|
itemBuilder: (context, index) {
|
|
final event = _activeLogEvents[index];
|
|
return ListTile(
|
|
title: Row(
|
|
mainAxisSize: MainAxisSize.max,
|
|
children: [
|
|
Expanded(
|
|
child: Text(event.eventType.target?.value ?? ''),
|
|
),
|
|
],
|
|
),
|
|
subtitle: Text(
|
|
'${DateTimeUtils.displayDateTime(event.time)}${event.hasEndTime ? ' - ${DateTimeUtils.displayDateTime(event.endTime)}' : ''}'),
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.delete,
|
|
color: Colors.blue,
|
|
),
|
|
onPressed: () => handleStopAction(event),
|
|
),
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.delete,
|
|
color: Colors.blue,
|
|
),
|
|
onPressed: () => handleDeleteAction(event),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
) : const Center(
|
|
child: Text('There are no currently ongoing events!'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|