2021-10-22 23:08:09 +00:00
|
|
|
import 'package:diameter/components/dialogs.dart';
|
|
|
|
import 'package:diameter/components/progress_indicator.dart';
|
|
|
|
import 'package:diameter/config.dart';
|
|
|
|
import 'package:diameter/models/log_entry.dart';
|
|
|
|
import 'package:diameter/navigation.dart';
|
|
|
|
import 'package:diameter/screens/log/log_entry.dart';
|
|
|
|
import 'package:diameter/utils/date_time_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class LogScreen extends StatefulWidget {
|
|
|
|
static const String routeName = '/log';
|
|
|
|
const LogScreen({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_LogScreenState createState() => _LogScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LogScreenState extends State<LogScreen> {
|
2021-10-23 20:57:37 +00:00
|
|
|
late Future<Map<DateTime, List<LogEntry>>?> _logEntryDailyMap;
|
2021-10-22 23:08:09 +00:00
|
|
|
|
|
|
|
void refresh({String? message}) {
|
|
|
|
setState(() {
|
2021-10-23 20:57:37 +00:00
|
|
|
_logEntryDailyMap = LogEntry.createDailyEntryMap();
|
2021-10-22 23:08:09 +00:00
|
|
|
});
|
|
|
|
setState(() {
|
|
|
|
if (message != null) {
|
|
|
|
var snackBar = SnackBar(
|
|
|
|
content: Text(message),
|
|
|
|
duration: const Duration(seconds: 2),
|
|
|
|
);
|
|
|
|
ScaffoldMessenger.of(context)
|
|
|
|
..removeCurrentSnackBar()
|
|
|
|
..showSnackBar(snackBar);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDelete(LogEntry logEntry) {
|
|
|
|
logEntry.delete().then((_) => refresh(message: 'Log Entry deleted'));
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleDeleteAction(LogEntry logEntry) async {
|
|
|
|
if (showConfirmationDialogOnDelete) {
|
|
|
|
Dialogs.showConfirmationDialog(
|
|
|
|
context: context,
|
|
|
|
onConfirm: () => onDelete(logEntry),
|
|
|
|
message: 'Are you sure you want to delete this Log Entry?',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
onDelete(logEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(title: const Text('Log Entries'), actions: <Widget>[
|
|
|
|
IconButton(onPressed: refresh, icon: const Icon(Icons.refresh))
|
|
|
|
]),
|
|
|
|
drawer: const Navigation(currentLocation: LogScreen.routeName),
|
|
|
|
body: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
Expanded(
|
2021-10-23 20:57:37 +00:00
|
|
|
child: FutureBuilder<Map<DateTime, List<LogEntry>>?>(
|
|
|
|
future: _logEntryDailyMap,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return ViewWithProgressIndicator(
|
|
|
|
snapshot: snapshot,
|
|
|
|
child: snapshot.data == null || snapshot.data!.isEmpty
|
|
|
|
? Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: const [
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.all(10.0),
|
|
|
|
child: Text('No Log Entries'),
|
2021-10-22 23:08:09 +00:00
|
|
|
),
|
2021-10-23 20:57:37 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
: SingleChildScrollView(
|
|
|
|
child: ListView.builder(
|
|
|
|
shrinkWrap: true,
|
|
|
|
padding: const EdgeInsets.all(10.0),
|
|
|
|
itemCount: snapshot.data != null
|
|
|
|
? snapshot.data!.length
|
|
|
|
: 0,
|
|
|
|
itemBuilder: (context, dateIndex) {
|
|
|
|
List<DateTime> dateList =
|
|
|
|
snapshot.data!.keys.toList();
|
|
|
|
dateList.sort((a, b) => a.compareTo(b) * -1);
|
|
|
|
final date = dateList[dateIndex];
|
|
|
|
final entryList = snapshot.data![date];
|
|
|
|
return ListBody(
|
|
|
|
children: [
|
|
|
|
Text(DateTimeUtils.displayDate(date)),
|
|
|
|
entryList != null && entryList.isNotEmpty
|
|
|
|
? ListView.builder(
|
|
|
|
shrinkWrap: true,
|
|
|
|
itemCount: entryList.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final logEntry = entryList[index];
|
|
|
|
return ListTile(
|
|
|
|
onTap: () {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) =>
|
|
|
|
LogEntryScreen(
|
|
|
|
entry: logEntry),
|
|
|
|
),
|
|
|
|
).then((message) => refresh(
|
|
|
|
message: message));
|
|
|
|
},
|
|
|
|
title: Text(
|
|
|
|
DateTimeUtils.displayTime(
|
|
|
|
logEntry.time)),
|
|
|
|
// TODO: add additional fields (event icons...)
|
|
|
|
// TODO: display glucose in colors according to target settings
|
|
|
|
subtitle: Text(logEntry
|
|
|
|
.mgPerDl !=
|
|
|
|
null
|
|
|
|
? '${logEntry.mgPerDl.toString()} mg/dl'
|
|
|
|
: ''),
|
|
|
|
trailing: Row(
|
|
|
|
mainAxisSize:
|
|
|
|
MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () =>
|
|
|
|
handleDeleteAction(
|
|
|
|
logEntry),
|
|
|
|
icon: const Icon(
|
|
|
|
Icons.delete,
|
|
|
|
color: Colors.blue),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
: Container(),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}),
|
2021-10-22 23:08:09 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
// TODO: add button for active events
|
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => const LogEntryScreen(),
|
|
|
|
),
|
|
|
|
).then((message) => refresh(message: message));
|
|
|
|
},
|
|
|
|
child: const Icon(Icons.add),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|