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');
}
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 {
QueryBuilder<ParseObject> query =
QueryBuilder<ParseObject>(ParseObject('LogEntry'))
..whereGreaterThanOrEqualsTo('time', range.start)
..whereLessThanOrEqualTo('time', range.end);
..whereLessThanOrEqualTo('time', range.end)
..orderByAscending('time');
final ParseResponse apiResponse = await query.query();
if (apiResponse.success && apiResponse.results != null) {
@ -59,8 +73,11 @@ class LogEntry {
}
static Future<List<LogEntry>> fetchAll() async {
// TODO: consider adding pagination/lazy loading here
QueryBuilder<ParseObject> query =
QueryBuilder<ParseObject>(ParseObject('LogEntry'));
QueryBuilder<ParseObject>(ParseObject('LogEntry'))
..orderByAscending('time');
;
final ParseResponse apiResponse = await query.query();
if (apiResponse.success && apiResponse.results != null) {

View File

@ -16,11 +16,11 @@ class LogScreen extends StatefulWidget {
}
class _LogScreenState extends State<LogScreen> {
late Future<List<LogEntry>?> _logEntries;
late Future<Map<DateTime, List<LogEntry>>?> _logEntryDailyMap;
void refresh({String? message}) {
setState(() {
_logEntries = LogEntry.fetchAll();
_logEntryDailyMap = LogEntry.createDailyEntryMap();
});
setState(() {
if (message != null) {
@ -68,65 +68,89 @@ class _LogScreenState extends State<LogScreen> {
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: FutureBuilder<List<LogEntry>?>(
future: _logEntries,
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'),
),
],
)
: ListView.builder(
padding: const EdgeInsets.all(10.0),
itemCount:
snapshot.data != null ? snapshot.data!.length : 0,
itemBuilder: (context, index) {
final logEntry = snapshot.data![index];
// TODO: split entries by day
return ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
LogEntryScreen(entry: logEntry),
),
).then((message) => refresh(message: message));
},
title:
// TODO: only display time here
Text(DateTimeUtils.displayDateTime(
logEntry.time)),
// TODO: display according to settings
// 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),
)
],
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'),
),
);
},
),
);
},
),
],
)
: 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(),
],
);
},
),
),
);
}),
),
],
),