2021-10-22 23:08:09 +00:00
|
|
|
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/active_log_event_list.dart';
|
|
|
|
import 'package:diameter/screens/log/log_event_detail.dart';
|
2021-10-26 23:25:40 +00:00
|
|
|
import 'package:diameter/utils/date_time_utils.dart';
|
2021-10-22 23:08:09 +00:00
|
|
|
import 'package:flex_color_scheme/flex_color_scheme.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:diameter/components/progress_indicator.dart';
|
|
|
|
|
|
|
|
class LogEventListScreen extends StatefulWidget {
|
|
|
|
final LogEntry? logEntry;
|
|
|
|
|
|
|
|
const LogEventListScreen({Key? key, this.logEntry}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_LogEventListScreenState createState() => _LogEventListScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LogEventListScreenState extends State<LogEventListScreen> {
|
|
|
|
late Future<List<LogEventType>> _logEventTypes;
|
|
|
|
|
|
|
|
void refresh({String? message}) {
|
|
|
|
if (widget.logEntry != null) {
|
|
|
|
setState(() {
|
|
|
|
widget.logEntry!.events =
|
|
|
|
LogEvent.fetchAllForLogEntry(widget.logEntry!);
|
|
|
|
widget.logEntry!.endedEvents =
|
|
|
|
LogEvent.fetchAllEndedByEntry(widget.logEntry!);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setState(() {
|
|
|
|
if (message != null) {
|
|
|
|
var snackBar = SnackBar(
|
|
|
|
content: Text(message),
|
|
|
|
duration: const Duration(seconds: 2),
|
|
|
|
);
|
|
|
|
ScaffoldMessenger.of(context)
|
|
|
|
..removeCurrentSnackBar()
|
|
|
|
..showSnackBar(snackBar);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_logEventTypes = LogEventType.fetchAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleEditAction(LogEvent event) {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => LogEventDetailScreen(
|
|
|
|
endLogEntry: widget.logEntry!,
|
|
|
|
logEvent: event,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
).then((message) => refresh(message: message));
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDelete(LogEvent logEvent) {
|
|
|
|
logEvent.delete().then((_) => refresh(message: 'Event deleted'));
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleDeleteAction(LogEvent logEvent) async {
|
|
|
|
if (showConfirmationDialogOnDelete) {
|
|
|
|
Dialogs.showConfirmationDialog(
|
|
|
|
context: context,
|
|
|
|
onConfirm: () => onDelete(logEvent),
|
|
|
|
message: 'Are you sure you want to delete this Event?',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
onDelete(logEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return SingleChildScrollView(
|
|
|
|
padding: const EdgeInsets.only(top: 10.0),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
// TODO: add button for active events
|
|
|
|
FutureBuilder<List<LogEvent>>(
|
|
|
|
future: widget.logEntry!.events,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return ViewWithProgressIndicator(
|
|
|
|
snapshot: snapshot,
|
|
|
|
child: snapshot.data == null || snapshot.data!.isEmpty
|
|
|
|
? const Padding(
|
|
|
|
padding: EdgeInsets.all(10.0),
|
|
|
|
child: Text('No Events for this Log Entry'),
|
|
|
|
)
|
2021-10-26 23:25:40 +00:00
|
|
|
: FutureBuilder<List<LogEventType>>(
|
|
|
|
future: _logEventTypes,
|
|
|
|
builder: (context, types) {
|
|
|
|
return ListView.builder(
|
|
|
|
shrinkWrap: true,
|
|
|
|
itemCount: snapshot.data != null
|
|
|
|
? snapshot.data!.length
|
|
|
|
: 0,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final event = snapshot.data![index];
|
|
|
|
return ListTile(
|
|
|
|
onTap: () {
|
|
|
|
handleEditAction(event);
|
|
|
|
},
|
|
|
|
title: Row(
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Text(types.data
|
|
|
|
?.firstWhere((element) =>
|
|
|
|
element.objectId ==
|
|
|
|
event.eventType)
|
|
|
|
.value ??
|
|
|
|
'')),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
subtitle: Text(
|
|
|
|
'${DateTimeUtils.displayDateTime(event.time)}${event.hasEndTime ? ' - ${DateTimeUtils.displayDateTime(event.endTime, fallback: '(ongoing)')}' : ''}'),
|
|
|
|
trailing: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(
|
|
|
|
Icons.delete,
|
|
|
|
color: Colors.blue,
|
|
|
|
),
|
|
|
|
onPressed: () =>
|
|
|
|
handleDeleteAction(event),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
2021-10-22 23:08:09 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
FutureBuilder<List<LogEvent>>(
|
|
|
|
future: widget.logEntry!.endedEvents,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return ViewWithProgressIndicator(
|
|
|
|
snapshot: snapshot,
|
|
|
|
child: snapshot.data == null || snapshot.data!.isEmpty
|
|
|
|
? Container()
|
2021-10-26 23:25:40 +00:00
|
|
|
: FutureBuilder<List<LogEventType>>(
|
|
|
|
future: _logEventTypes,
|
|
|
|
builder: (context, types) {
|
|
|
|
return ListView.builder(
|
|
|
|
shrinkWrap: true,
|
|
|
|
itemCount: snapshot.data != null
|
|
|
|
? snapshot.data!.length
|
|
|
|
: 0,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final event = snapshot.data![index];
|
|
|
|
return ListTile(
|
|
|
|
onTap: () {
|
|
|
|
handleEditAction(event);
|
|
|
|
},
|
|
|
|
title: Row(
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Text(types.data
|
|
|
|
?.firstWhere((element) =>
|
|
|
|
element.objectId ==
|
|
|
|
event.eventType)
|
|
|
|
.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: () =>
|
|
|
|
handleDeleteAction(event),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
2021-10-22 23:08:09 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|