split log entries into sub lists by date

This commit is contained in:
spinel 2021-10-23 22:57:37 +02:00
parent d887f477f9
commit 18e5381436
2 changed files with 103 additions and 62 deletions

View File

@ -42,11 +42,25 @@ class LogEntry {
notes = object.get<String>('notes'); notes = object.get<String>('notes');
} }
static Future<Map<DateTime, List<LogEntry>>> createDailyEntryMap() async {
Map<DateTime, List<LogEntry>> dateMap = <DateTime, List<LogEntry>>{};
List<LogEntry> entries = await fetchAll();
DateTime? date;
for (LogEntry entry in entries) {
date = DateTime.utc(entry.time.year, entry.time.month, entry.time.day);
dateMap.putIfAbsent(date, () => <LogEntry>[]).add(entry);
}
return dateMap;
}
static Future<List<LogEntry>> fetchAllForRange(DateTimeRange range) async { static Future<List<LogEntry>> fetchAllForRange(DateTimeRange range) async {
QueryBuilder<ParseObject> query = QueryBuilder<ParseObject> query =
QueryBuilder<ParseObject>(ParseObject('LogEntry')) QueryBuilder<ParseObject>(ParseObject('LogEntry'))
..whereGreaterThanOrEqualsTo('time', range.start) ..whereGreaterThanOrEqualsTo('time', range.start)
..whereLessThanOrEqualTo('time', range.end); ..whereLessThanOrEqualTo('time', range.end)
..orderByAscending('time');
final ParseResponse apiResponse = await query.query(); final ParseResponse apiResponse = await query.query();
if (apiResponse.success && apiResponse.results != null) { if (apiResponse.success && apiResponse.results != null) {
@ -59,8 +73,11 @@ class LogEntry {
} }
static Future<List<LogEntry>> fetchAll() async { static Future<List<LogEntry>> fetchAll() async {
// TODO: consider adding pagination/lazy loading here
QueryBuilder<ParseObject> query = QueryBuilder<ParseObject> query =
QueryBuilder<ParseObject>(ParseObject('LogEntry')); QueryBuilder<ParseObject>(ParseObject('LogEntry'))
..orderByAscending('time');
;
final ParseResponse apiResponse = await query.query(); final ParseResponse apiResponse = await query.query();
if (apiResponse.success && apiResponse.results != null) { if (apiResponse.success && apiResponse.results != null) {

View File

@ -16,11 +16,11 @@ class LogScreen extends StatefulWidget {
} }
class _LogScreenState extends State<LogScreen> { class _LogScreenState extends State<LogScreen> {
late Future<List<LogEntry>?> _logEntries; late Future<Map<DateTime, List<LogEntry>>?> _logEntryDailyMap;
void refresh({String? message}) { void refresh({String? message}) {
setState(() { setState(() {
_logEntries = LogEntry.fetchAll(); _logEntryDailyMap = LogEntry.createDailyEntryMap();
}); });
setState(() { setState(() {
if (message != null) { if (message != null) {
@ -68,8 +68,8 @@ class _LogScreenState extends State<LogScreen> {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
Expanded( Expanded(
child: FutureBuilder<List<LogEntry>?>( child: FutureBuilder<Map<DateTime, List<LogEntry>>?>(
future: _logEntries, future: _logEntryDailyMap,
builder: (context, snapshot) { builder: (context, snapshot) {
return ViewWithProgressIndicator( return ViewWithProgressIndicator(
snapshot: snapshot, snapshot: snapshot,
@ -83,51 +83,75 @@ class _LogScreenState extends State<LogScreen> {
), ),
], ],
) )
: ListView.builder( : SingleChildScrollView(
child: ListView.builder(
shrinkWrap: true,
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
itemCount: itemCount: snapshot.data != null
snapshot.data != null ? snapshot.data!.length : 0, ? 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) { itemBuilder: (context, index) {
final logEntry = snapshot.data![index]; final logEntry = entryList[index];
// TODO: split entries by day
return ListTile( return ListTile(
onTap: () { onTap: () {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => builder: (context) =>
LogEntryScreen(entry: logEntry), LogEntryScreen(
entry: logEntry),
), ),
).then((message) => refresh(message: message)); ).then((message) => refresh(
message: message));
}, },
title: title: Text(
// TODO: only display time here DateTimeUtils.displayTime(
Text(DateTimeUtils.displayDateTime(
logEntry.time)), logEntry.time)),
// TODO: display according to settings
// TODO: add additional fields (event icons...) // TODO: add additional fields (event icons...)
// TODO: display glucose in colors according to target settings // TODO: display glucose in colors according to target settings
subtitle: Text(logEntry.mgPerDl != null subtitle: Text(logEntry
.mgPerDl !=
null
? '${logEntry.mgPerDl.toString()} mg/dl' ? '${logEntry.mgPerDl.toString()} mg/dl'
: ''), : ''),
trailing: Row( trailing: Row(
mainAxisSize: MainAxisSize.min, mainAxisSize:
MainAxisSize.min,
children: [ children: [
IconButton( IconButton(
onPressed: () => onPressed: () =>
handleDeleteAction(logEntry), handleDeleteAction(
icon: const Icon(Icons.delete, logEntry),
icon: const Icon(
Icons.delete,
color: Colors.blue), color: Colors.blue),
) )
], ],
), ),
); );
}, })
), : Container(),
],
); );
}, },
), ),
), ),
);
}),
),
], ],
), ),
// TODO: add button for active events // TODO: add button for active events