228 lines
		
	
	
	
		
			7.5 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			228 lines
		
	
	
	
		
			7.5 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:diameter/components/data_table.dart';
 | |
| import 'package:diameter/models/log_entry.dart';
 | |
| import 'package:flutter/material.dart';
 | |
| import 'package:parse_server_sdk_flutter/parse_server_sdk.dart';
 | |
| 
 | |
| class LogMeal extends DataTableContent {
 | |
|   late String? objectId;
 | |
|   late String logEntry;
 | |
|   late String? meal;
 | |
|   late String value;
 | |
|   late String? source;
 | |
|   late String? category;
 | |
|   late String? portionType;
 | |
|   late double? carbsRatio;
 | |
|   late double? portionSize;
 | |
|   late double? carbsPerPortion;
 | |
|   late String? portionSizeAccuracy;
 | |
|   late String? carbsRatioAccuracy;
 | |
|   late double? bolus;
 | |
|   late int? delayedBolusDuration;
 | |
|   late double? delayedBolusRate;
 | |
|   late String? notes;
 | |
| 
 | |
|   LogMeal(ParseObject object) {
 | |
|     objectId = object.get<String>('objectId');
 | |
|     logEntry = object.get<ParseObject>('logEntry')!.get<String>('objectId')!;
 | |
|     meal = object.get<ParseObject>('meal')?.get<String>('objectId');
 | |
|     value = object.get<String>('value')!;
 | |
|     source = object.get<ParseObject>('source')?.get<String>('objectId');
 | |
|     category = object.get<ParseObject>('category')?.get<String>('objectId');
 | |
|     portionType = object.get<ParseObject>('portionType')?.get<String>('objectId');
 | |
|     carbsRatio = object.get<num>('carbsRatio')?.toDouble();
 | |
|     portionSize = object.get<num>('portionSize')?.toDouble();
 | |
|     carbsPerPortion = object.get<num>('carbsPerPortion')?.toDouble();
 | |
|     portionSizeAccuracy = object.get<ParseObject>('portionSizeAccuracy')?.get<String>('objectId');
 | |
|     carbsRatioAccuracy = object.get<ParseObject>('carbsRatioAccuracy')?.get<String>('objectId');
 | |
|     bolus = object.get<num>('bolus')?.toDouble();
 | |
|     delayedBolusDuration = object.get<num>('delayedBolusDuration')?.toInt();
 | |
|     delayedBolusRate = object.get<num>('delayedBolusRate')?.toDouble();
 | |
|     notes = object.get<String>('notes');
 | |
|   }
 | |
| 
 | |
|   static Future<LogMeal?> get(String objectId) async {
 | |
|     QueryBuilder<ParseObject> query =
 | |
|         QueryBuilder<ParseObject>(ParseObject('LogMeal'))
 | |
|           ..whereEqualTo('objectId', objectId);
 | |
|     final ParseResponse apiResponse = await query.query();
 | |
| 
 | |
|     if (apiResponse.success && apiResponse.results != null) {
 | |
|       return LogMeal(apiResponse.result.first);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   static Future<List<LogMeal>> fetchAllForLogEntry(LogEntry logEntry) async {
 | |
|     QueryBuilder<ParseObject> query = QueryBuilder<ParseObject>(
 | |
|         ParseObject('LogMeal'))
 | |
|       ..whereEqualTo('logEntry',
 | |
|           (ParseObject('LogEntry')..objectId = logEntry.objectId!).toPointer());
 | |
|     final ParseResponse apiResponse = await query.query();
 | |
| 
 | |
|     if (apiResponse.success && apiResponse.results != null) {
 | |
|       return apiResponse.results!
 | |
|           .map((e) => LogMeal(e as ParseObject))
 | |
|           .toList();
 | |
|     } else {
 | |
|       return [];
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   static Future<void> save({
 | |
|     required String value,
 | |
|     required String logEntry,
 | |
|     String? meal,
 | |
|     String? source,
 | |
|     String? category,
 | |
|     String? portionType,
 | |
|     double? carbsRatio,
 | |
|     double? portionSize,
 | |
|     double? carbsPerPortion,
 | |
|     String? portionSizeAccuracy,
 | |
|     String? carbsRatioAccuracy,
 | |
|     double? bolus,
 | |
|     int? delayedBolusDuration,
 | |
|     double? delayedBolusRate,
 | |
|     String? notes,
 | |
|   }) async {
 | |
|     final logMeal = ParseObject('LogMeal')
 | |
|       ..set('value', value)
 | |
|       ..set('logEntry',
 | |
|           (ParseObject('LogEntry')..objectId = logEntry).toPointer())
 | |
|       ..set('carbsRatio', carbsRatio)
 | |
|       ..set('portionSize', portionSize)
 | |
|       ..set('carbsPerPortion', carbsPerPortion)
 | |
|       ..set('bolus', bolus)
 | |
|       ..set('delayedBolusDuration', delayedBolusDuration)
 | |
|       ..set('delayedBolusRate', delayedBolusRate)
 | |
|       ..set('notes', notes);
 | |
| 
 | |
|     if (meal != null) {
 | |
|       logMeal.set('meal', (ParseObject('Meal')..objectId = meal).toPointer());
 | |
|     }
 | |
|     if (source != null) {
 | |
|       logMeal.set(
 | |
|           'source', (ParseObject('MealSource')..objectId = source).toPointer());
 | |
|     }
 | |
|     if (category != null) {
 | |
|       logMeal.set('category',
 | |
|           (ParseObject('MealCategory')..objectId = category).toPointer());
 | |
|     }
 | |
|     if (portionType != null) {
 | |
|       logMeal.set('portionType',
 | |
|           (ParseObject('MealPortionType')..objectId = portionType).toPointer());
 | |
|     }
 | |
|     if (portionSizeAccuracy != null) {
 | |
|       logMeal.set(
 | |
|           'portionSizeAccuracy',
 | |
|           (ParseObject('Accuracy')..objectId = portionSizeAccuracy)
 | |
|               .toPointer());
 | |
|     }
 | |
|     if (carbsRatioAccuracy != null) {
 | |
|       logMeal.set('carbsRatioAccuracy',
 | |
|           (ParseObject('Accuracy')..objectId = carbsRatioAccuracy).toPointer());
 | |
|     }
 | |
|     await logMeal.save();
 | |
|   }
 | |
| 
 | |
|   static Future<void> update(
 | |
|     String objectId, {
 | |
|     String? value,
 | |
|     String? meal,
 | |
|     String? source,
 | |
|     String? category,
 | |
|     String? portionType,
 | |
|     double? carbsRatio,
 | |
|     double? portionSize,
 | |
|     double? carbsPerPortion,
 | |
|     String? portionSizeAccuracy,
 | |
|     String? carbsRatioAccuracy,
 | |
|     double? bolus,
 | |
|     int? delayedBolusDuration,
 | |
|     double? delayedBolusRate,
 | |
|     String? notes,
 | |
|   }) async {
 | |
|     var logMeal = ParseObject('LogMeal')..objectId = objectId;
 | |
|     if (value != null) {
 | |
|       logMeal.set('value', value);
 | |
|     }
 | |
|     if (meal != null) {
 | |
|       logMeal.set('meal', (ParseObject('Meal')..objectId = meal).toPointer());
 | |
|     }
 | |
|     if (source != null) {
 | |
|       logMeal.set(
 | |
|           'source', (ParseObject('MealSource')..objectId = source).toPointer());
 | |
|     }
 | |
|     if (category != null) {
 | |
|       logMeal.set('category',
 | |
|           (ParseObject('MealCategory')..objectId = category).toPointer());
 | |
|     }
 | |
|     if (portionType != null) {
 | |
|       logMeal.set('portionType',
 | |
|           (ParseObject('MealPortionType')..objectId = portionType).toPointer());
 | |
|     }
 | |
|     if (carbsRatio != null) {
 | |
|       logMeal.set('carbsRatio', carbsRatio);
 | |
|     }
 | |
|     if (portionSize != null) {
 | |
|       logMeal.set('portionSize', portionSize);
 | |
|     }
 | |
|     if (carbsPerPortion != null) {
 | |
|       logMeal.set('carbsPerPortion', carbsPerPortion);
 | |
|     }
 | |
|     if (portionSizeAccuracy != null) {
 | |
|       logMeal.set(
 | |
|           'portionSizeAccuracy',
 | |
|           (ParseObject('Accuracy')..objectId = portionSizeAccuracy)
 | |
|               .toPointer());
 | |
|     }
 | |
|     if (carbsRatioAccuracy != null) {
 | |
|       logMeal.set('carbsRatioAccuracy',
 | |
|           (ParseObject('Accuracy')..objectId = carbsRatioAccuracy).toPointer());
 | |
|     }
 | |
|     if (bolus != null) {
 | |
|       logMeal.set('bolus', bolus);
 | |
|     }
 | |
|     if (delayedBolusDuration != null) {
 | |
|       logMeal.set('delayedBolusDuration', delayedBolusDuration);
 | |
|     }
 | |
|     if (delayedBolusRate != null) {
 | |
|       logMeal.set('delayedBolusRate', delayedBolusRate);
 | |
|     }
 | |
|     if (notes != null) {
 | |
|       logMeal.set('notes', notes);
 | |
|     }
 | |
|     await logMeal.save();
 | |
|   }
 | |
| 
 | |
|   Future<void> delete() async {
 | |
|     var logMeal = ParseObject('LogMeal')..objectId = objectId;
 | |
|     await logMeal.delete();
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   List<DataCell> asDataTableCells(List<Widget>? actions) {
 | |
|     return [
 | |
|       DataCell(Text(value)),
 | |
|       DataCell(Text('${(carbsPerPortion ?? '').toString()} g')),
 | |
|       DataCell(Text('${(bolus ?? '').toString()} U')),
 | |
|       DataCell(Text(delayedBolusRate != null
 | |
|           ? '${delayedBolusRate.toString()} U/${(delayedBolusDuration ?? '').toString()} min'
 | |
|           : '')),
 | |
|       DataCell(
 | |
|         Row(
 | |
|           children: actions ?? [],
 | |
|         ),
 | |
|       ),
 | |
|     ];
 | |
|   }
 | |
| 
 | |
|   static List<DataColumn> asDataTableColumns() {
 | |
|     return [
 | |
|       const DataColumn(label: Expanded(child: Text('Meal'))),
 | |
|       const DataColumn(label: Expanded(child: Text('Carbs'))),
 | |
|       const DataColumn(label: Expanded(child: Text('Bolus'))),
 | |
|       const DataColumn(label: Expanded(child: Text('Delayed Bolus'))),
 | |
|       const DataColumn(label: Expanded(child: Text('Actions'))),
 | |
|     ];
 | |
|   }
 | |
| }
 |