diff --git a/lib/features/diary/presentation/bloc/diary_bloc.dart b/lib/features/diary/presentation/bloc/diary_bloc.dart index c50210b..06d76c4 100644 --- a/lib/features/diary/presentation/bloc/diary_bloc.dart +++ b/lib/features/diary/presentation/bloc/diary_bloc.dart @@ -228,11 +228,34 @@ class DiaryBloc extends Bloc { selectedMood: meal.mood, selectedSatiety: meal.satiety, ); - print(updatedState.currentMeal.id); emit(updatedState); } } + Future _inputProduct( + ProductInput event, + Emitter emit, + ) async { + if (state is DiaryStateLoaded) { + var currentState = state as DiaryStateLoaded; + + final productNutritionalValues = await diaryRepository.inputProduct(event.input); + + // Create a new controller with the input text and update the autocomplete list + var updatedState = currentState.copyWith( + productNameController: TextEditingController(text: event.input), + autocompleteList: productNutritionalValues.map((product) => product['name']).toList(), + ); + if (event.input.length > 3) { + updatedState = updatedState.copyWith(moderationExpanded: true); + } else { + updatedState = updatedState.copyWith(moderationExpanded: false); + } + + emit(updatedState); // Emit the updated state + } + } + FutureOr _clearInput( ClearInput event, Emitter emit, @@ -332,44 +355,6 @@ class DiaryBloc extends Bloc { return response; } - Future _inputProduct( - ProductInput event, - Emitter emit, - ) async { - if (state is DiaryStateLoaded) { - var currentState = state as DiaryStateLoaded; - - final Map credentials = {'name': event.input}; - final uri = Uri.parse( - '${dotenv.env['SERVER_ROOT']!}/diary/product_nutritional_values/filter?${Uri(queryParameters: credentials).query}', - ); - - final response = await http.get( - uri, - headers: { - 'Content-Type': 'application/json', - }, - ); - - final productNutritionalValues = jsonDecode(response.body)['product_nutritional_value']; - - if (productNutritionalValues is List) { - // Create a new controller with the input text and update the autocomplete list - var updatedState = currentState.copyWith( - productNameController: TextEditingController(text: event.input), - autocompleteList: productNutritionalValues.map((product) => product['name']).toList(), - ); - if (event.input.length > 3) { - updatedState = updatedState.copyWith(moderationExpanded: true); - } else { - updatedState = updatedState.copyWith(moderationExpanded: false); - } - - emit(updatedState); // Emit the updated state - } - } - } - Future _removePortion( RemovePortionEvent event, Emitter emit, diff --git a/lib/features/diary/repositories/diary_repository.dart b/lib/features/diary/repositories/diary_repository.dart index 68511c7..ce9e0fa 100644 --- a/lib/features/diary/repositories/diary_repository.dart +++ b/lib/features/diary/repositories/diary_repository.dart @@ -22,6 +22,14 @@ class DiaryRepository { } } + Future> inputProduct(String name) async { + try { + return await diaryService.sendInputProduct(name); + } catch (e) { + rethrow; + } + } + Future changeSatiety(int satiety, int mealID, int diaryID) async { try { await diaryService.sendSatietyData( diff --git a/lib/features/diary/services/diary_service.dart b/lib/features/diary/services/diary_service.dart index b412594..f1ad9d9 100644 --- a/lib/features/diary/services/diary_service.dart +++ b/lib/features/diary/services/diary_service.dart @@ -17,6 +17,9 @@ abstract class DiaryService { @GET('/diary/{id}/dailyNutritionData/') Future getChart(@Path() String id); + @GET('/diary/product_nutritional_values/filter?{name}') + Future> sendInputProduct(@Path() String name); + @POST('/diary/{diary_id}/meal/{meal_id}/satiety/fill') Future> sendSatietyData( @Path() int diary_id, diff --git a/lib/features/diary/services/diary_service.g.dart b/lib/features/diary/services/diary_service.g.dart deleted file mode 100644 index a33a19e..0000000 --- a/lib/features/diary/services/diary_service.g.dart +++ /dev/null @@ -1,254 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'diary_service.dart'; - -// ************************************************************************** -// RetrofitGenerator -// ************************************************************************** - -// ignore_for_file: unnecessary_brace_in_string_interps,no_leading_underscores_for_local_identifiers,unused_element - -class _DiaryService implements DiaryService { - _DiaryService( - this._dio, { - this.baseUrl, - }); - - final Dio _dio; - - String? baseUrl; - - @override - Future> getDiaryID() async { - final _extra = {}; - final queryParameters = {}; - final _headers = {}; - const Map? _data = null; - final _result = await _dio - .fetch>(_setStreamType>(Options( - method: 'GET', - headers: _headers, - extra: _extra, - ) - .compose( - _dio.options, - '/diary/index', - queryParameters: queryParameters, - data: _data, - ) - .copyWith( - baseUrl: _combineBaseUrls( - _dio.options.baseUrl, - baseUrl, - )))); - final _value = _result.data!.cast(); - return _value; - } - - @override - Future> getDiary(int id) async { - final _extra = {}; - final queryParameters = {}; - final _headers = {}; - const Map? _data = null; - final _result = await _dio.fetch>( - _setStreamType>(Options( - method: 'GET', - headers: _headers, - extra: _extra, - ) - .compose( - _dio.options, - '/diary/${id}/show/', - queryParameters: queryParameters, - data: _data, - ) - .copyWith( - baseUrl: _combineBaseUrls( - _dio.options.baseUrl, - baseUrl, - )))); - var _value = _result.data!.map((k, dynamic v) => - MapEntry(k, DiaryModel.fromJson(v as Map))); - return _value; - } - - @override - Future getChart(String id) async { - final _extra = {}; - final queryParameters = {}; - final _headers = {}; - const Map? _data = null; - final _result = await _dio - .fetch>(_setStreamType(Options( - method: 'GET', - headers: _headers, - extra: _extra, - ) - .compose( - _dio.options, - '/diary/${id}/dailyNutritionData/', - queryParameters: queryParameters, - data: _data, - ) - .copyWith( - baseUrl: _combineBaseUrls( - _dio.options.baseUrl, - baseUrl, - )))); - final _value = DiaryModel.fromJson(_result.data!); - return _value; - } - - @override - Future> sendSatietyData( - int diary_id, - int meal_id, - int satiety, - ) async { - final _extra = {}; - final queryParameters = {r'satiety': satiety}; - final _headers = {}; - const Map? _data = null; - final _result = await _dio - .fetch>(_setStreamType>(Options( - method: 'POST', - headers: _headers, - extra: _extra, - ) - .compose( - _dio.options, - '/diary/${diary_id}/meal/${meal_id}/satiety/fill', - queryParameters: queryParameters, - data: _data, - ) - .copyWith( - baseUrl: _combineBaseUrls( - _dio.options.baseUrl, - baseUrl, - )))); - final _value = _result.data!.cast(); - return _value; - } - - @override - Future> sendMoodData( - int diary_id, - int meal_id, - int mood, - ) async { - final _extra = {}; - final queryParameters = {r'mood': mood}; - final _headers = {}; - const Map? _data = null; - final _result = await _dio - .fetch>(_setStreamType>(Options( - method: 'POST', - headers: _headers, - extra: _extra, - ) - .compose( - _dio.options, - '/diary/${diary_id}/meal/${meal_id}/mood/fill', - queryParameters: queryParameters, - data: _data, - ) - .copyWith( - baseUrl: _combineBaseUrls( - _dio.options.baseUrl, - baseUrl, - )))); - final _value = _result.data!.cast(); - return _value; - } - - @override - Future sendWakeupData( - int diary_id, - String wakeup, - ) async { - final _extra = {}; - final queryParameters = {r'wakeup': wakeup}; - final _headers = {}; - const Map? _data = null; - final _result = await _dio - .fetch>(_setStreamType(Options( - method: 'POST', - headers: _headers, - extra: _extra, - ) - .compose( - _dio.options, - '/diary/${diary_id}/wakeup/fill', - queryParameters: queryParameters, - data: _data, - ) - .copyWith( - baseUrl: _combineBaseUrls( - _dio.options.baseUrl, - baseUrl, - )))); - final _value = DiaryModel.fromJson(_result.data!); - return _value; - } - - @override - Future sendRetire( - int diary_id, - String retire, - ) async { - final _extra = {}; - final queryParameters = {r'retire': retire}; - final _headers = {}; - const Map? _data = null; - final _result = await _dio - .fetch>(_setStreamType(Options( - method: 'POST', - headers: _headers, - extra: _extra, - ) - .compose( - _dio.options, - '/diary/${diary_id}/retire/fill', - queryParameters: queryParameters, - data: _data, - ) - .copyWith( - baseUrl: _combineBaseUrls( - _dio.options.baseUrl, - baseUrl, - )))); - final _value = DiaryModel.fromJson(_result.data!); - return _value; - } - - RequestOptions _setStreamType(RequestOptions requestOptions) { - if (T != dynamic && - !(requestOptions.responseType == ResponseType.bytes || - requestOptions.responseType == ResponseType.stream)) { - if (T == String) { - requestOptions.responseType = ResponseType.plain; - } else { - requestOptions.responseType = ResponseType.json; - } - } - return requestOptions; - } - - String _combineBaseUrls( - String dioBaseUrl, - String? baseUrl, - ) { - if (baseUrl == null || baseUrl.trim().isEmpty) { - return dioBaseUrl; - } - - final url = Uri.parse(baseUrl); - - if (url.isAbsolute) { - return url.toString(); - } - - return Uri.parse(dioBaseUrl).resolveUri(url).toString(); - } -}