2021-10-22 23:08:09 +00:00
|
|
|
import 'package:diameter/components/detail.dart';
|
|
|
|
import 'package:diameter/components/dialogs.dart';
|
|
|
|
import 'package:diameter/components/forms.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/navigation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class LogEventDetailScreen extends StatefulWidget {
|
|
|
|
static const String routeName = '/log-event';
|
|
|
|
final LogEntry? logEntry;
|
|
|
|
final LogEntry? endLogEntry;
|
|
|
|
final LogEvent? logEvent;
|
|
|
|
const LogEventDetailScreen(
|
|
|
|
{Key? key, this.logEntry, this.endLogEntry, this.logEvent})
|
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_LogEventDetailScreenState createState() => _LogEventDetailScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LogEventDetailScreenState extends State<LogEventDetailScreen> {
|
|
|
|
final GlobalKey<FormState> _logEventForm = GlobalKey<FormState>();
|
|
|
|
|
|
|
|
final _notesController = TextEditingController(text: '');
|
|
|
|
String? _eventType;
|
|
|
|
bool _hasEndTime = false;
|
|
|
|
|
|
|
|
late Future<List<LogEventType>> _logEventTypes;
|
|
|
|
|
2021-10-27 19:00:28 +00:00
|
|
|
bool _isSaving = false;
|
|
|
|
|
2021-10-22 23:08:09 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
if (widget.logEvent != null) {
|
|
|
|
_notesController.text = widget.logEvent!.notes ?? '';
|
|
|
|
_eventType = widget.logEvent!.eventType;
|
|
|
|
_hasEndTime = widget.logEvent!.hasEndTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logEventTypes = LogEventType.fetchAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleSaveAction() async {
|
2021-10-27 19:00:28 +00:00
|
|
|
setState(() {
|
|
|
|
_isSaving = true;
|
|
|
|
});
|
2021-10-22 23:08:09 +00:00
|
|
|
if (_logEventForm.currentState!.validate()) {
|
|
|
|
bool isNew = widget.logEvent == null;
|
|
|
|
isNew
|
|
|
|
? await LogEvent.save(
|
|
|
|
logEntry: widget.logEntry!.objectId!,
|
|
|
|
eventType: _eventType!,
|
|
|
|
time: widget.logEntry!.time,
|
|
|
|
hasEndTime: _hasEndTime,
|
|
|
|
notes: _notesController.text,
|
|
|
|
)
|
|
|
|
: await LogEvent.update(
|
|
|
|
widget.logEvent!.objectId!,
|
|
|
|
eventType: _eventType!,
|
|
|
|
time: widget.logEntry!.time,
|
|
|
|
hasEndTime: _hasEndTime,
|
|
|
|
notes: _notesController.text,
|
|
|
|
);
|
|
|
|
Navigator.pop(context, '${isNew ? 'New' : ''} Event Saved');
|
|
|
|
}
|
2021-10-27 19:00:28 +00:00
|
|
|
setState(() {
|
|
|
|
_isSaving = false;
|
|
|
|
});
|
2021-10-22 23:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleCancelAction() {
|
|
|
|
bool isNew = widget.logEvent == null;
|
|
|
|
if (showConfirmationDialogOnCancel &&
|
|
|
|
((isNew &&
|
|
|
|
(_notesController.text != '' ||
|
|
|
|
_eventType != null ||
|
|
|
|
_hasEndTime)) ||
|
|
|
|
(!isNew &&
|
|
|
|
(_notesController.text != (widget.logEvent!.notes ?? '') ||
|
|
|
|
_eventType != widget.logEvent!.eventType ||
|
|
|
|
_hasEndTime != widget.logEvent!.hasEndTime)))) {
|
|
|
|
Dialogs.showCancelConfirmationDialog(
|
|
|
|
context: context,
|
|
|
|
isNew: isNew,
|
|
|
|
onSave: handleSaveAction,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
bool isNew = widget.logEvent == null;
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(isNew ? 'New Event' : 'Edit Event'),
|
|
|
|
),
|
|
|
|
drawer: const Navigation(currentLocation: LogEventDetailScreen.routeName),
|
|
|
|
body: SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: <Widget>[
|
|
|
|
StyledForm(
|
|
|
|
formState: _logEventForm,
|
|
|
|
fields: [
|
|
|
|
StyledFutureDropdownButton<LogEventType>(
|
|
|
|
selectedItem: _eventType,
|
|
|
|
label: 'Event Type',
|
|
|
|
items: _logEventTypes,
|
|
|
|
getItemValue: (item) => item.objectId,
|
|
|
|
renderItem: (item) => Text(item.value),
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_eventType = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
StyledBooleanFormField(
|
|
|
|
value: _hasEndTime,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_hasEndTime = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
label: 'active',
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
controller: _notesController,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Notes',
|
|
|
|
alignLabelWithHint: true,
|
|
|
|
),
|
|
|
|
keyboardType: TextInputType.multiline,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
// ActiveLogEventListScreen(onSetEndTime: onSetEndTime)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
bottomNavigationBar: DetailBottomRow(
|
|
|
|
onCancel: handleCancelAction,
|
2021-10-27 19:00:28 +00:00
|
|
|
onSave: _isSaving ? null : handleSaveAction,
|
2021-10-22 23:08:09 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|