refactor(diary): Refactor product input

This commit is contained in:
2024-07-15 17:44:33 +03:00
parent ae213f39a5
commit 8610cacc6f
4 changed files with 35 additions and 293 deletions

View File

@@ -228,11 +228,34 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
selectedMood: meal.mood,
selectedSatiety: meal.satiety,
);
print(updatedState.currentMeal.id);
emit(updatedState);
}
}
Future<void> _inputProduct(
ProductInput event,
Emitter<DiaryState> 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<void> _clearInput(
ClearInput event,
Emitter<DiaryState> emit,
@@ -332,44 +355,6 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
return response;
}
Future<void> _inputProduct(
ProductInput event,
Emitter<DiaryState> emit,
) async {
if (state is DiaryStateLoaded) {
var currentState = state as DiaryStateLoaded;
final Map<String, dynamic> 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<dynamic>) {
// 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<void> _removePortion(
RemovePortionEvent event,
Emitter<DiaryState> emit,