2021-10-22 23:08:09 +00:00
|
|
|
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart';
|
|
|
|
|
2021-10-24 21:53:44 +00:00
|
|
|
enum PortionCarbsParameter { carbsRatio, portionSize, carbsPerPortion }
|
|
|
|
|
2021-10-22 23:08:09 +00:00
|
|
|
class Meal {
|
|
|
|
late String? objectId;
|
|
|
|
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 int? delayedBolusDuration;
|
|
|
|
late double? delayedBolusRate;
|
|
|
|
late String? notes;
|
|
|
|
|
|
|
|
Meal(ParseObject object) {
|
|
|
|
objectId = object.get<String>('objectId');
|
|
|
|
value = object.get<String>('value')!;
|
|
|
|
source = object.get<ParseObject>('source') != null
|
|
|
|
? object.get<ParseObject>('source')!.get<String>('objectId')
|
|
|
|
: null;
|
|
|
|
category = object.get<ParseObject>('category') != null
|
|
|
|
? object.get<ParseObject>('category')!.get<String>('objectId')
|
|
|
|
: null;
|
|
|
|
portionType = object.get<ParseObject>('portionType') != null
|
|
|
|
? object.get<ParseObject>('portionType')!.get<String>('objectId')
|
|
|
|
: null;
|
|
|
|
carbsRatio = object.get<num>('carbsRatio') != null
|
|
|
|
? object.get<num>('carbsRatio')!.toDouble()
|
|
|
|
: null;
|
|
|
|
portionSize = object.get<num>('portionSize') != null
|
|
|
|
? object.get<num>('portionSize')!.toDouble()
|
|
|
|
: null;
|
|
|
|
carbsPerPortion = object.get<num>('carbsPerPortion') != null
|
|
|
|
? object.get<num>('carbsPerPortion')!.toDouble()
|
|
|
|
: null;
|
|
|
|
portionSizeAccuracy = object.get<ParseObject>('portionSizeAccuracy') != null
|
|
|
|
? object
|
|
|
|
.get<ParseObject>('portionSizeAccuracy')!
|
|
|
|
.get<String>('objectId')
|
|
|
|
: null;
|
|
|
|
carbsRatioAccuracy = object.get<ParseObject>('carbsRatioAccuracy') != null
|
|
|
|
? object.get<ParseObject>('carbsRatioAccuracy')!.get<String>('objectId')
|
|
|
|
: null;
|
|
|
|
delayedBolusDuration = object.get<num>('delayedBolusDuration') != null
|
|
|
|
? object.get<num>('delayedBolusDuration')!.toInt()
|
|
|
|
: null;
|
|
|
|
delayedBolusRate = object.get<num>('delayedBolusRate') != null
|
|
|
|
? object.get<num>('delayedBolusRate')!.toDouble()
|
|
|
|
: null;
|
|
|
|
notes = object.get<String>('notes');
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<List<Meal>> fetchAll() async {
|
|
|
|
QueryBuilder<ParseObject> query =
|
|
|
|
QueryBuilder<ParseObject>(ParseObject('Meal'));
|
|
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
|
|
return apiResponse.results!.map((e) => Meal(e as ParseObject)).toList();
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<Meal?> get(String objectId) async {
|
|
|
|
QueryBuilder<ParseObject> query =
|
|
|
|
QueryBuilder<ParseObject>(ParseObject('Meal'))
|
|
|
|
..whereEqualTo('objectId', objectId);
|
|
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
|
|
return Meal(apiResponse.result.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<void> save({
|
|
|
|
required String value,
|
|
|
|
String? source,
|
|
|
|
String? category,
|
|
|
|
String? portionType,
|
|
|
|
double? carbsRatio,
|
|
|
|
double? portionSize,
|
|
|
|
double? carbsPerPortion,
|
|
|
|
String? portionSizeAccuracy,
|
|
|
|
String? carbsRatioAccuracy,
|
|
|
|
int? delayedBolusDuration,
|
|
|
|
double? delayedBolusRate,
|
|
|
|
String? notes,
|
|
|
|
}) async {
|
|
|
|
final meal = ParseObject('Meal')
|
|
|
|
..set('value', value)
|
|
|
|
..set('carbsRatio', carbsRatio)
|
|
|
|
..set('portionSize', portionSize)
|
|
|
|
..set('carbsPerPortion', carbsPerPortion)
|
|
|
|
..set('delayedBolusDuration', delayedBolusDuration)
|
|
|
|
..set('delayedBolusRate', delayedBolusRate)
|
|
|
|
..set('notes', notes);
|
|
|
|
|
|
|
|
if (source != null) {
|
|
|
|
meal.set(
|
|
|
|
'source', (ParseObject('MealSource')..objectId = source).toPointer());
|
|
|
|
}
|
|
|
|
if (category != null) {
|
|
|
|
meal.set('category',
|
|
|
|
(ParseObject('MealCategory')..objectId = category).toPointer());
|
|
|
|
}
|
|
|
|
if (portionType != null) {
|
|
|
|
meal.set('portionType',
|
|
|
|
(ParseObject('MealPortionType')..objectId = portionType).toPointer());
|
|
|
|
}
|
|
|
|
if (portionSizeAccuracy != null) {
|
|
|
|
meal.set(
|
|
|
|
'portionSizeAccuracy',
|
|
|
|
(ParseObject('Accuracy')..objectId = portionSizeAccuracy)
|
|
|
|
.toPointer());
|
|
|
|
}
|
|
|
|
if (carbsRatioAccuracy != null) {
|
|
|
|
meal.set('carbsRatioAccuracy',
|
|
|
|
(ParseObject('Accuracy')..objectId = carbsRatioAccuracy).toPointer());
|
|
|
|
}
|
|
|
|
await meal.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<void> update(
|
|
|
|
String objectId, {
|
|
|
|
String? value,
|
|
|
|
String? source,
|
|
|
|
String? category,
|
|
|
|
String? portionType,
|
|
|
|
double? carbsRatio,
|
|
|
|
double? portionSize,
|
|
|
|
double? carbsPerPortion,
|
|
|
|
String? portionSizeAccuracy,
|
|
|
|
String? carbsRatioAccuracy,
|
|
|
|
int? delayedBolusDuration,
|
|
|
|
double? delayedBolusRate,
|
|
|
|
String? notes,
|
|
|
|
}) async {
|
|
|
|
var meal = ParseObject('Meal')..objectId = objectId;
|
|
|
|
if (value != null) {
|
|
|
|
meal.set('value', value);
|
|
|
|
}
|
|
|
|
if (source != null) {
|
|
|
|
meal.set(
|
|
|
|
'source', (ParseObject('MealSource')..objectId = source).toPointer());
|
|
|
|
}
|
|
|
|
if (category != null) {
|
|
|
|
meal.set('category',
|
|
|
|
(ParseObject('MealCategory')..objectId = category).toPointer());
|
|
|
|
}
|
|
|
|
if (portionType != null) {
|
|
|
|
meal.set('portionType',
|
|
|
|
(ParseObject('MealPortionType')..objectId = portionType).toPointer());
|
|
|
|
}
|
|
|
|
if (carbsRatio != null) {
|
|
|
|
meal.set('carbsRatio', carbsRatio);
|
|
|
|
}
|
|
|
|
if (portionSize != null) {
|
|
|
|
meal.set('portionSize', portionSize);
|
|
|
|
}
|
|
|
|
if (carbsPerPortion != null) {
|
|
|
|
meal.set('carbsPerPortion', carbsPerPortion);
|
|
|
|
}
|
|
|
|
if (portionSizeAccuracy != null) {
|
|
|
|
meal.set(
|
|
|
|
'portionSizeAccuracy',
|
|
|
|
(ParseObject('Accuracy')..objectId = portionSizeAccuracy)
|
|
|
|
.toPointer());
|
|
|
|
}
|
|
|
|
if (carbsRatioAccuracy != null) {
|
|
|
|
meal.set('carbsRatioAccuracy',
|
|
|
|
(ParseObject('Accuracy')..objectId = carbsRatioAccuracy).toPointer());
|
|
|
|
}
|
|
|
|
if (delayedBolusDuration != null) {
|
|
|
|
meal.set('delayedBolusDuration', delayedBolusDuration);
|
|
|
|
}
|
|
|
|
if (delayedBolusRate != null) {
|
|
|
|
meal.set('delayedBolusRate', delayedBolusRate);
|
|
|
|
}
|
|
|
|
if (notes != null) {
|
|
|
|
meal.set('notes', notes);
|
|
|
|
}
|
|
|
|
await meal.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> delete() async {
|
|
|
|
var meal = ParseObject('Meal')..objectId = objectId;
|
|
|
|
await meal.delete();
|
|
|
|
}
|
|
|
|
}
|