forked from eatvio/eatvio_app
188 lines
6.4 KiB
Dart
188 lines
6.4 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:eatvio_app/features/create_diet/repositories/create_diet_repository.dart';
|
|
import 'package:eatvio_app/features/create_diet/services/create_diet_service.dart';
|
|
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
|
import 'package:eatvio_app/features/glossary/models/glossary_model.dart';
|
|
import 'package:eatvio_app/features/glossary/repositories/glossary_repository.dart';
|
|
import 'package:eatvio_app/features/glossary/services/glossary_service.dart';
|
|
import 'package:retrofit/dio.dart' show HttpResponse;
|
|
|
|
import '../../fixtures/fixture_reader.dart';
|
|
|
|
class FixtureCreateDietRepository extends CreateDietRepository {
|
|
FixtureCreateDietRepository() : super(createDietService: _NoopCreateDietService());
|
|
|
|
final List<Map<String, dynamic>> createRequests = <Map<String, dynamic>>[];
|
|
final List<Map<String, dynamic>> updateRequests = <Map<String, dynamic>>[];
|
|
|
|
Map<int, Map<int, Map<int, DailySchedule>>> get nutritionFixture {
|
|
final nutritionData = FixtureReader.readAsMap('create_diet/nutrition_response.json');
|
|
final result = <int, Map<int, Map<int, DailySchedule>>>{};
|
|
|
|
for (final dayEntry in nutritionData.entries) {
|
|
final dayIndex = int.parse(dayEntry.key);
|
|
final dayData = dayEntry.value as Map<String, dynamic>;
|
|
final dayMap = <int, Map<int, DailySchedule>>{};
|
|
|
|
for (final mealEntry in dayData.entries) {
|
|
final mealIndex = int.parse(mealEntry.key);
|
|
final mealData = mealEntry.value as Map<String, dynamic>;
|
|
final mealMap = <int, DailySchedule>{};
|
|
|
|
for (final portionEntry in mealData.entries) {
|
|
final portionIndex = int.parse(portionEntry.key);
|
|
final portionData = portionEntry.value as Map<String, dynamic>;
|
|
|
|
// Convert portion string to Portion enum
|
|
final portionStr = portionData['portion'] as String;
|
|
Portion portion;
|
|
switch (portionStr) {
|
|
case 'gr':
|
|
case 'grams':
|
|
portion = Portion.grams;
|
|
break;
|
|
case 'milliliters':
|
|
portion = Portion.milliliters;
|
|
break;
|
|
case 'pcs':
|
|
portion = Portion.pcs;
|
|
break;
|
|
default:
|
|
portion = Portion.grams;
|
|
}
|
|
|
|
final pnvid = portionData['product_nutritional_value_id'];
|
|
final nutritionJson = portionData['nutrition'] as Map<String, dynamic>?;
|
|
final dailyNutrition = nutritionJson != null ? DailyNutrition.fromJson(nutritionJson) : null;
|
|
mealMap[portionIndex] = DailySchedule(
|
|
name: portionData['name'] as String,
|
|
portion: portion,
|
|
quantity: portionData['quantity'] is int ? portionData['quantity'] as int : int.parse(portionData['quantity'].toString()),
|
|
productNutritionalValueId: pnvid == null ? null : (pnvid is int ? pnvid : int.tryParse(pnvid.toString())),
|
|
dailyNutrition: dailyNutrition,
|
|
);
|
|
}
|
|
|
|
dayMap[mealIndex] = mealMap;
|
|
}
|
|
|
|
result[dayIndex] = dayMap;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
@override
|
|
Future<CreatedDiet> createDiet({
|
|
required String name,
|
|
required int duration,
|
|
required DietState state,
|
|
required String? meatType,
|
|
required String? carbType,
|
|
required String? vegType,
|
|
required String? lightType,
|
|
required String? details,
|
|
required File? image,
|
|
required List<List<List<DailySchedule>>>? dailySchedule,
|
|
}) async {
|
|
createRequests.add(<String, dynamic>{
|
|
'name': name,
|
|
'duration': duration,
|
|
'details': details,
|
|
'state': state.toString(),
|
|
'types': <String, bool>{'meat': meatType == '1', 'carb': carbType == '1', 'veg': vegType == '1', 'light': lightType == '1'},
|
|
});
|
|
final createdDietFixture = FixtureReader.readAsMap('create_diet/created_diet_response.json');
|
|
return CreatedDiet.fromJson(createdDietFixture);
|
|
}
|
|
|
|
@override
|
|
Future<Map<int, Map<int, Map<int, DailySchedule>>>> getDietPortionsNutrition(int dietId) async {
|
|
return nutritionFixture;
|
|
}
|
|
|
|
@override
|
|
Future<void> updateDiet({
|
|
required int dietId,
|
|
required DietState state,
|
|
String? name,
|
|
int? duration,
|
|
String? meatType,
|
|
String? carbType,
|
|
String? vegType,
|
|
String? lightType,
|
|
String? details,
|
|
File? image,
|
|
List<List<List<DailySchedule>>>? dailySchedule,
|
|
}) async {
|
|
updateRequests.add(<String, dynamic>{
|
|
'id': dietId,
|
|
'name': name,
|
|
'duration': duration,
|
|
'details': details,
|
|
'state': state.toString(),
|
|
if (meatType != null || carbType != null || vegType != null || lightType != null)
|
|
'types': <String, bool>{
|
|
if (meatType != null) 'meat': meatType == '1',
|
|
if (carbType != null) 'carb': carbType == '1',
|
|
if (vegType != null) 'veg': vegType == '1',
|
|
if (lightType != null) 'light': lightType == '1',
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
class FixtureGlossaryRepository extends GlossaryRepository {
|
|
FixtureGlossaryRepository() : super(glossaryService: _NoopGlossaryService());
|
|
|
|
@override
|
|
Future<List<GlossaryModel>> getGlossaryListByQuery(String query) async {
|
|
final results = FixtureReader.readAsMap('create_diet/glossary_search_response.json')['results'] as List<dynamic>;
|
|
return results.map((item) => GlossaryModel.fromJson(item as Map<String, dynamic>)).toList();
|
|
}
|
|
}
|
|
|
|
class _NoopCreateDietService implements CreateDietService {
|
|
@override
|
|
Future<CreatedDiet> createDraftedDiet({required FormData formData}) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<CreatedDiet> createPersonalDiet({required FormData formData}) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<CreatedDiet> createPublishedDiet({required FormData formData}) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<HttpResponse> getDietPortionsNutrition({required int diet}) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<void> updateDrafted({required int diet, required FormData formData}) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<void> updatePersonal({required int diet, required FormData formData}) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<void> updatePublished({required int diet, required FormData formData}) {
|
|
throw UnimplementedError();
|
|
}
|
|
}
|
|
|
|
class _NoopGlossaryService implements GlossaryService {
|
|
@override
|
|
noSuchMethod(Invocation invocation) => throw UnimplementedError('GlossaryService call ${invocation.memberName}');
|
|
}
|