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('objectId'); logEntry = object.get('logEntry')!.get('objectId')!; meal = object.get('meal')?.get('objectId'); value = object.get('value')!; source = object.get('source')?.get('objectId'); category = object.get('category')?.get('objectId'); portionType = object.get('portionType')?.get('objectId'); carbsRatio = object.get('carbsRatio')?.toDouble(); portionSize = object.get('portionSize')?.toDouble(); carbsPerPortion = object.get('carbsPerPortion')?.toDouble(); portionSizeAccuracy = object.get('portionSizeAccuracy')?.get('objectId'); carbsRatioAccuracy = object.get('carbsRatioAccuracy')?.get('objectId'); bolus = object.get('bolus')?.toDouble(); delayedBolusDuration = object.get('delayedBolusDuration')?.toInt(); delayedBolusRate = object.get('delayedBolusRate')?.toDouble(); notes = object.get('notes'); } static Future get(String objectId) async { QueryBuilder query = QueryBuilder(ParseObject('LogMeal')) ..whereEqualTo('objectId', objectId); final ParseResponse apiResponse = await query.query(); if (apiResponse.success && apiResponse.results != null) { return LogMeal(apiResponse.result.first); } } static Future> fetchAllForLogEntry(LogEntry logEntry) async { QueryBuilder query = QueryBuilder( 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 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 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 delete() async { var logMeal = ParseObject('LogMeal')..objectId = objectId; await logMeal.delete(); } @override List asDataTableCells(List? 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 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'))), ]; } }