split log entries into sub lists by date
This commit is contained in:
		
							parent
							
								
									d887f477f9
								
							
						
					
					
						commit
						18e5381436
					
				
					 2 changed files with 103 additions and 62 deletions
				
			
		| 
						 | 
				
			
			@ -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) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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,8 +68,8 @@ class _LogScreenState extends State<LogScreen> {
 | 
			
		|||
        mainAxisAlignment: MainAxisAlignment.center,
 | 
			
		||||
        children: <Widget>[
 | 
			
		||||
          Expanded(
 | 
			
		||||
            child: FutureBuilder<List<LogEntry>?>(
 | 
			
		||||
              future: _logEntries,
 | 
			
		||||
            child: FutureBuilder<Map<DateTime, List<LogEntry>>?>(
 | 
			
		||||
                future: _logEntryDailyMap,
 | 
			
		||||
                builder: (context, snapshot) {
 | 
			
		||||
                  return ViewWithProgressIndicator(
 | 
			
		||||
                    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),
 | 
			
		||||
                          itemCount:
 | 
			
		||||
                              snapshot.data != null ? snapshot.data!.length : 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 = snapshot.data![index];
 | 
			
		||||
                            // TODO: split entries by day
 | 
			
		||||
                                              final logEntry = entryList[index];
 | 
			
		||||
                                              return ListTile(
 | 
			
		||||
                                                onTap: () {
 | 
			
		||||
                                                  Navigator.push(
 | 
			
		||||
                                                    context,
 | 
			
		||||
                                                    MaterialPageRoute(
 | 
			
		||||
                                                      builder: (context) =>
 | 
			
		||||
                                        LogEntryScreen(entry: logEntry),
 | 
			
		||||
                                                          LogEntryScreen(
 | 
			
		||||
                                                              entry: logEntry),
 | 
			
		||||
                                                    ),
 | 
			
		||||
                                ).then((message) => refresh(message: message));
 | 
			
		||||
                                                  ).then((message) => refresh(
 | 
			
		||||
                                                      message: message));
 | 
			
		||||
                                                },
 | 
			
		||||
                              title:
 | 
			
		||||
                                  // TODO: only display time here
 | 
			
		||||
                                  Text(DateTimeUtils.displayDateTime(
 | 
			
		||||
                                                title: Text(
 | 
			
		||||
                                                    DateTimeUtils.displayTime(
 | 
			
		||||
                                                        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
 | 
			
		||||
                                                subtitle: Text(logEntry
 | 
			
		||||
                                                            .mgPerDl !=
 | 
			
		||||
                                                        null
 | 
			
		||||
                                                    ? '${logEntry.mgPerDl.toString()} mg/dl'
 | 
			
		||||
                                                    : ''),
 | 
			
		||||
                                                trailing: Row(
 | 
			
		||||
                                mainAxisSize: MainAxisSize.min,
 | 
			
		||||
                                                  mainAxisSize:
 | 
			
		||||
                                                      MainAxisSize.min,
 | 
			
		||||
                                                  children: [
 | 
			
		||||
                                                    IconButton(
 | 
			
		||||
                                                      onPressed: () =>
 | 
			
		||||
                                        handleDeleteAction(logEntry),
 | 
			
		||||
                                    icon: const Icon(Icons.delete,
 | 
			
		||||
                                                          handleDeleteAction(
 | 
			
		||||
                                                              logEntry),
 | 
			
		||||
                                                      icon: const Icon(
 | 
			
		||||
                                                          Icons.delete,
 | 
			
		||||
                                                          color: Colors.blue),
 | 
			
		||||
                                                    )
 | 
			
		||||
                                                  ],
 | 
			
		||||
                                                ),
 | 
			
		||||
                                              );
 | 
			
		||||
                          },
 | 
			
		||||
                        ),
 | 
			
		||||
                                            })
 | 
			
		||||
                                        : Container(),
 | 
			
		||||
                                  ],
 | 
			
		||||
                                );
 | 
			
		||||
                              },
 | 
			
		||||
                            ),
 | 
			
		||||
                          ),
 | 
			
		||||
                  );
 | 
			
		||||
                }),
 | 
			
		||||
          ),
 | 
			
		||||
        ],
 | 
			
		||||
      ),
 | 
			
		||||
      // TODO: add button for active events
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue