187 lines
6.9 KiB
Dart
187 lines
6.9 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/models/log_event_type.dart';
|
|
import 'package:diameter/screens/log/log_event_detail.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:diameter/components/progress_indicator.dart';
|
|
|
|
class ActiveLogEventListScreen extends StatefulWidget {
|
|
static const String routeName = '/active-log-events';
|
|
|
|
final LogEntry? endLogEntry;
|
|
final Function()? onSetEndTime;
|
|
|
|
const ActiveLogEventListScreen(
|
|
{Key? key, this.endLogEntry, this.onSetEndTime})
|
|
: super(key: key);
|
|
|
|
@override
|
|
_ActiveLogEventListScreenState createState() =>
|
|
_ActiveLogEventListScreenState();
|
|
}
|
|
|
|
class _ActiveLogEventListScreenState extends State<ActiveLogEventListScreen> {
|
|
late Future<List<LogEvent>> _activeLogEvents;
|
|
late Future<List<LogEventType>> _logEventTypes;
|
|
|
|
void refresh({String? message}) {
|
|
setState(() {
|
|
_logEventTypes = LogEventType.fetchAll();
|
|
});
|
|
|
|
setState(() {
|
|
_activeLogEvents = LogEvent.fetchAllForLogEntry(widget.endLogEntry!);
|
|
});
|
|
|
|
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 {
|
|
// TODO: create new entry if no existing entry is given
|
|
await LogEvent.update(
|
|
event.objectId!,
|
|
endTime: DateTime.now(),
|
|
endLogEntry: widget.endLogEntry!.objectId!,
|
|
);
|
|
refresh();
|
|
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) {
|
|
event.delete().then((_) => refresh(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
|
|
void initState() {
|
|
super.initState();
|
|
refresh();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
bool isNew = widget.endLogEntry == null;
|
|
return isNew
|
|
? Container()
|
|
: Container(
|
|
padding: const EdgeInsets.only(top: 10.0),
|
|
child: Column(
|
|
children: [
|
|
// TODO: make action button instead of appbar
|
|
AppBar(
|
|
title: const Text('Active Events'),
|
|
primary: false,
|
|
automaticallyImplyLeading: false,
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.add),
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => LogEventDetailScreen(
|
|
logEntry: widget.endLogEntry!,
|
|
),
|
|
),
|
|
).then((message) => refresh(message: message));
|
|
},
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.refresh), onPressed: refresh),
|
|
],
|
|
),
|
|
FutureBuilder<List<LogEvent>>(
|
|
future: _activeLogEvents,
|
|
builder: (context, snapshot) {
|
|
return ViewWithProgressIndicator(
|
|
snapshot: snapshot,
|
|
child: snapshot.data == null || snapshot.data!.isEmpty
|
|
? Container()
|
|
: ListBody(
|
|
children: [
|
|
// TODO: fix problems that this futurebuilder in futurebuilder creates
|
|
FutureBuilder<List<LogEventType>>(
|
|
future: _logEventTypes,
|
|
builder: (context, types) {
|
|
// return DataTable(
|
|
// columnSpacing: 10.0,
|
|
// showCheckboxColumn: false,
|
|
// rows: snapshot.data != null
|
|
// ? snapshot.data!.map((event) {
|
|
// return DataRow(
|
|
// cells: event.asDataTableCells(
|
|
// [
|
|
// IconButton(
|
|
// icon: const Icon(
|
|
// Icons.stop),
|
|
// iconSize: 16.0,
|
|
// onPressed: () =>
|
|
// handleStopAction(
|
|
// event),
|
|
// ),
|
|
// IconButton(
|
|
// icon: const Icon(
|
|
// Icons.delete),
|
|
// iconSize: 16.0,
|
|
// onPressed: () =>
|
|
// handleDeleteAction(
|
|
// event),
|
|
// ),
|
|
// ],
|
|
// types: types.data,
|
|
// ),
|
|
// );
|
|
// }).toList()
|
|
// : [],
|
|
// columns: LogEvent.asDataTableColumns(),
|
|
// );
|
|
return Container();
|
|
})
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|