295 lines
9.6 KiB
Dart
295 lines
9.6 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer';
|
|
import 'package:get/get.dart';
|
|
import 'package:onufitness/constants/api_endpoints.dart';
|
|
import 'package:onufitness/models/master_dropdowns/dietary_preferences_dropdown_response_model.dart';
|
|
import 'package:onufitness/models/master_dropdowns/health_conditions_dropdowns_response_model.dart';
|
|
import 'package:onufitness/screens/accounts/model/user_selected_health_cond_response_model.dart';
|
|
import 'package:onufitness/services/api_services/base_api_services.dart';
|
|
import 'package:onufitness/utils/custom_sneakbar.dart';
|
|
|
|
class HealthConditionUpdateController extends GetxController {
|
|
// Loading states
|
|
var isDietaryPreferencesLoading = false.obs;
|
|
var isHealthConditionsLoading = false.obs;
|
|
var isUserDataLoading = false.obs;
|
|
var isUpdateLoading = false.obs;
|
|
|
|
// API data lists
|
|
var apiDietaryPreferencesList = <DietaryPreferences>[].obs;
|
|
var apiHealthConditionsList = <HealthConditions>[].obs;
|
|
|
|
// Local dropdown lists
|
|
var localDietaryPreferencesList = <String>[].obs;
|
|
var localHealthConditionsList = <String>[].obs;
|
|
|
|
// Selected values
|
|
var selectedDietaryPreferences = "".obs;
|
|
var selectedDietaryPreferencesId = 0.obs;
|
|
var selectedHealthConditions = <String>[].obs;
|
|
var selectedHealthConditionsIds = <int>[].obs;
|
|
|
|
// User data
|
|
var userHealthData = Rxn<UserSelectedHealthConditionsResponseModel>();
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
}
|
|
|
|
//............... Fetch Dietary Preferences List................................................................
|
|
|
|
Future<void> fetchDietaryPreferences() async {
|
|
isDietaryPreferencesLoading(true);
|
|
try {
|
|
var response = await ApiBase.getRequest(
|
|
extendedURL: ApiUrl.fetchDietaryPreferences,
|
|
sendHeaders: false,
|
|
);
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
var responseData = dietaryPreferencesResponseModelFromJson(
|
|
response.body,
|
|
);
|
|
if (responseData.isSuccess == true) {
|
|
apiDietaryPreferencesList.assignAll(responseData.data ?? []);
|
|
|
|
localDietaryPreferencesList.clear();
|
|
for (var d in responseData.data!) {
|
|
localDietaryPreferencesList.add(d.dietaryPreferenceName.toString());
|
|
}
|
|
} else {
|
|
customSnackbar(
|
|
title: "Error",
|
|
message:
|
|
responseData.message ?? "Failed to load Dietary Preferences",
|
|
);
|
|
}
|
|
} else {
|
|
var responseData = dietaryPreferencesResponseModelFromJson(
|
|
response.body,
|
|
);
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: responseData.message ?? "Failed to load Dietary Preferences",
|
|
);
|
|
}
|
|
} catch (e) {
|
|
log("Error fetching dietary preferences: $e");
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: "Failed to load Dietary Preferences",
|
|
);
|
|
} finally {
|
|
isDietaryPreferencesLoading(false);
|
|
}
|
|
}
|
|
|
|
//............... Fetch Health Conditions List................................................................
|
|
|
|
Future<void> fetchHealthConditions() async {
|
|
isHealthConditionsLoading(true);
|
|
try {
|
|
var response = await ApiBase.getRequest(
|
|
extendedURL: ApiUrl.fetchHealthConditions,
|
|
sendHeaders: false,
|
|
);
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
var responseData = healthConditionsResponseModelFromJson(response.body);
|
|
if (responseData.isSuccess == true) {
|
|
apiHealthConditionsList.assignAll(responseData.data ?? []);
|
|
|
|
localHealthConditionsList.clear();
|
|
for (var health in responseData.data!) {
|
|
localHealthConditionsList.add(health.conditionName.toString());
|
|
}
|
|
} else {
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: responseData.message ?? "Failed to load Health Conditions",
|
|
);
|
|
}
|
|
} else {
|
|
var responseData = healthConditionsResponseModelFromJson(response.body);
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: responseData.message ?? "Failed to load Health Conditions",
|
|
);
|
|
}
|
|
} catch (e) {
|
|
log("Error fetching health conditions: $e");
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: "Failed to load Health Conditions",
|
|
);
|
|
} finally {
|
|
isHealthConditionsLoading(false);
|
|
}
|
|
}
|
|
|
|
//............... Fetch User Health Information................................................................
|
|
|
|
Future<void> fetchUserHealthInformation() async {
|
|
isUserDataLoading(true);
|
|
try {
|
|
var response = await ApiBase.getRequest(
|
|
extendedURL: '/api/Users/get-user-health-information',
|
|
sendHeaders: true,
|
|
);
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
var responseData = userSelectedHealthConditionsResponseModelFromJson(
|
|
response.body,
|
|
);
|
|
|
|
if (responseData.isSuccess == true) {
|
|
userHealthData.value = responseData;
|
|
|
|
// Set selected dietary preference
|
|
if (responseData.data?.dietaryPreferences?.isNotEmpty == true) {
|
|
var dietaryPref = responseData.data!.dietaryPreferences!.first;
|
|
selectedDietaryPreferences.value =
|
|
dietaryPref.dietaryPreferenceName ?? "";
|
|
selectedDietaryPreferencesId.value =
|
|
dietaryPref.dietaryPreferenceId ?? 0;
|
|
}
|
|
|
|
// Set selected health conditions
|
|
if (responseData.data?.healthConditions?.isNotEmpty == true) {
|
|
selectedHealthConditions.clear();
|
|
selectedHealthConditionsIds.clear();
|
|
|
|
for (var condition in responseData.data!.healthConditions!) {
|
|
if (condition.conditionName != null) {
|
|
selectedHealthConditions.add(condition.conditionName!);
|
|
}
|
|
if (condition.conditionId != null) {
|
|
selectedHealthConditionsIds.add(condition.conditionId!);
|
|
}
|
|
}
|
|
}
|
|
|
|
log("User health data loaded successfully");
|
|
} else {
|
|
customSnackbar(
|
|
title: "Error",
|
|
message:
|
|
responseData.message ??
|
|
"Failed to load user health information",
|
|
);
|
|
}
|
|
} else {
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: "Failed to load user health information",
|
|
);
|
|
}
|
|
} catch (e) {
|
|
log("Error fetching user health information: $e");
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: "Failed to load user health information",
|
|
);
|
|
} finally {
|
|
isUserDataLoading(false);
|
|
}
|
|
}
|
|
|
|
//............... Update User Health Information................................................................
|
|
|
|
Future<void> updateUserHealthInformation() async {
|
|
isUpdateLoading(true);
|
|
try {
|
|
// Prepare request body
|
|
Map<String, dynamic> requestBody = {
|
|
"dietaryPreferenceIDs": [selectedDietaryPreferencesId.value],
|
|
"conditionIDs": selectedHealthConditionsIds.toList(),
|
|
};
|
|
|
|
log("Update request body: ${jsonEncode(requestBody)}");
|
|
|
|
var response = await ApiBase.putRequest(
|
|
extendedURL: '/api/Users/update-user-health-information',
|
|
body: requestBody,
|
|
sendHeaders: true,
|
|
);
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
Get.back();
|
|
Future.delayed(Duration(milliseconds: 300), () {
|
|
customSnackbar(
|
|
title: "Success",
|
|
message: "Health information updated successfully",
|
|
duration: 2,
|
|
);
|
|
});
|
|
// Optionally refresh the data
|
|
await fetchUserHealthInformation();
|
|
} else {
|
|
try {
|
|
var errorResponse = jsonDecode(response.body);
|
|
customSnackbar(
|
|
title: "Error",
|
|
message:
|
|
errorResponse['message'] ??
|
|
"Failed to update health information",
|
|
duration: 2,
|
|
);
|
|
} catch (e) {
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: "Failed to update health information",
|
|
duration: 2,
|
|
);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
log("Error updating health information: $e");
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: "Failed to update health information",
|
|
duration: 2,
|
|
);
|
|
} finally {
|
|
isUpdateLoading(false);
|
|
}
|
|
}
|
|
|
|
//............... Health Conditions Selection Handler................................................................
|
|
|
|
void onHealthConditionsChanged(List<String> selectedItems) {
|
|
selectedHealthConditions.assignAll(selectedItems);
|
|
|
|
// Update selected IDs based on selected names
|
|
selectedHealthConditionsIds.clear();
|
|
for (String conditionName in selectedItems) {
|
|
var condition = apiHealthConditionsList.firstWhereOrNull(
|
|
(element) => element.conditionName == conditionName,
|
|
);
|
|
if (condition != null && condition.conditionId != null) {
|
|
selectedHealthConditionsIds.add(condition.conditionId!);
|
|
}
|
|
}
|
|
|
|
log("Selected health conditions: $selectedItems");
|
|
log(
|
|
"Selected health condition IDs: ${selectedHealthConditionsIds.toList()}",
|
|
);
|
|
}
|
|
|
|
//............... Clear Data on Dispose................................................................
|
|
|
|
@override
|
|
void onClose() {
|
|
// Clear all data when controller is disposed
|
|
apiDietaryPreferencesList.clear();
|
|
apiHealthConditionsList.clear();
|
|
localDietaryPreferencesList.clear();
|
|
localHealthConditionsList.clear();
|
|
selectedHealthConditions.clear();
|
|
selectedHealthConditionsIds.clear();
|
|
super.onClose();
|
|
}
|
|
}
|