forked from eatvio/eatvio_app
213 lines
4.1 KiB
Dart
213 lines
4.1 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:eatvio_app/features/diary/models/diary_model.dart';
|
|
import 'package:eatvio_app/features/diary/models/portion.dart';
|
|
import 'package:eatvio_app/features/diary/models/product.dart';
|
|
import 'package:eatvio_app/features/diary/services/diary_service.dart';
|
|
|
|
class DiaryRepository {
|
|
final DiaryService diaryService;
|
|
|
|
DiaryRepository({required this.diaryService});
|
|
|
|
Future<Map<String, int>> getDiaryID() async {
|
|
try {
|
|
return await diaryService.getDiaryID();
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<DiaryModel> getDiary(int diaryID) async {
|
|
try {
|
|
return (await diaryService.getDiary(diaryID))['diary']!;
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getChartData(int diaryID) async {
|
|
try {
|
|
return (await diaryService.getChart(diaryID));
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<Map<String, List<Product>>> inputProduct(String name) async {
|
|
try {
|
|
return await diaryService.getInputProduct(name);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> changeSatiety(int satiety, int mealID, int diaryID) async {
|
|
try {
|
|
await diaryService.sendSatietyData(
|
|
diaryID,
|
|
mealID,
|
|
satiety,
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> changeMood(int mood, int mealID, int diaryID) async {
|
|
try {
|
|
await diaryService.sendMoodData(
|
|
diaryID,
|
|
mealID,
|
|
mood,
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> changeWakeup(
|
|
int diaryID,
|
|
String wakeup,
|
|
) async {
|
|
try {
|
|
await diaryService.sendWakeupData(
|
|
diaryID,
|
|
wakeup,
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> changeRetire(
|
|
int diaryID,
|
|
String retire,
|
|
) async {
|
|
try {
|
|
await diaryService.sendWakeupData(
|
|
diaryID,
|
|
retire,
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<Map<String, Portion>> addPortion(
|
|
int diaryID,
|
|
int mealID,
|
|
String name,
|
|
String product,
|
|
int quantity,
|
|
) async {
|
|
try {
|
|
return await diaryService.sendPortionData(
|
|
diaryID,
|
|
mealID,
|
|
name,
|
|
product,
|
|
quantity,
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<Map<String, String>> sendModeration(
|
|
String name,
|
|
String energy_on_100gr,
|
|
String proteins_on_100gr,
|
|
String fats_on_100gr,
|
|
String carbohydrates_on_100gr,
|
|
) async {
|
|
try {
|
|
return await diaryService.sendModerationData(
|
|
name,
|
|
double.parse(energy_on_100gr + ".0"),
|
|
double.parse(proteins_on_100gr + ".0"),
|
|
double.parse(fats_on_100gr + ".0"),
|
|
double.parse(carbohydrates_on_100gr + ".0"),
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> removePortion(
|
|
int diaryID,
|
|
int mealID,
|
|
int portionID,
|
|
) async {
|
|
try {
|
|
await diaryService.sendRemovePortionData(
|
|
diaryID,
|
|
mealID,
|
|
portionID,
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> uploadImage(
|
|
String path,
|
|
int diaryID,
|
|
int mealID,
|
|
) async {
|
|
try {
|
|
File file = File(path);
|
|
await diaryService.uploadImage(
|
|
diaryID,
|
|
mealID,
|
|
file,
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> removeImage(
|
|
int diaryID,
|
|
int mealID,
|
|
int photoID,
|
|
String path,
|
|
) async {
|
|
try {
|
|
await diaryService.sendRemovePhotoData(
|
|
diaryID,
|
|
mealID,
|
|
path,
|
|
photoID,
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> finishDay(
|
|
int diaryID,
|
|
TimeOfDay? wakeup,
|
|
TimeOfDay? retire,
|
|
String? comment,
|
|
) async {
|
|
try {
|
|
final formatter = DateFormat('HH:mm');
|
|
final wakeupString =
|
|
formatter.format(DateTime(2022, 1, 1, wakeup!.hour, wakeup.minute));
|
|
final retireString =
|
|
formatter.format(DateTime(2022, 1, 1, retire!.hour, retire.minute));
|
|
await diaryService.sendFinishDay(
|
|
diaryID,
|
|
wakeupString,
|
|
retireString,
|
|
comment ?? " ",
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
}
|