244 lines
7.4 KiB
Dart
244 lines
7.4 KiB
Dart
import 'dart:developer';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:onufitness/constants/api_endpoints.dart';
|
|
import 'package:onufitness/models/master_dropdowns/fitness_goals_dropdown_response_model.dart';
|
|
import 'package:onufitness/screens/accounts/model/trainee_my_fitness_goal_response_model.dart';
|
|
import 'package:onufitness/screens/accounts/model/user_progress_response_model.dart';
|
|
import 'package:onufitness/services/api_services/base_api_services.dart';
|
|
import 'package:onufitness/utils/custom_sneakbar.dart';
|
|
|
|
class TraineeAccountController extends GetxController {
|
|
TextEditingController preferredGoalController = TextEditingController();
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
onInitApiCall();
|
|
preferredGoalController = TextEditingController();
|
|
}
|
|
|
|
onInitApiCall() async {
|
|
await fetchFitnessGoals(); // First fetch available goals
|
|
await fetchSelectedFitnessGoal(); // Then fetch user's selected goal
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
preferredGoalController.dispose();
|
|
super.onClose();
|
|
}
|
|
|
|
RxBool isMyFitnessGoalLoading = false.obs;
|
|
TraineeSelectedFitnessGoalResponseModel myFitnessGoal =
|
|
TraineeSelectedFitnessGoalResponseModel();
|
|
|
|
Future<bool> fetchSelectedFitnessGoal() async {
|
|
var ret = false;
|
|
isMyFitnessGoalLoading(true);
|
|
try {
|
|
var response = await ApiBase.getRequest(
|
|
extendedURL: "/api/Users/get-user-preferred-goal",
|
|
);
|
|
log("fetchSelectedFitnessGoal-----> :${response.body}");
|
|
myFitnessGoal = traineeSelectedFitnessGoalResponseModelFromJson(
|
|
response.body,
|
|
);
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
preferredGoalController.text =
|
|
myFitnessGoal.data!.fitnessGoalTitle.toString();
|
|
|
|
// Set the selected goal in dropdown
|
|
selectedFitnessGoal.value =
|
|
myFitnessGoal.data!.fitnessGoalTitle.toString();
|
|
selectedFitnessGoalId.value = myFitnessGoal.data!.fitnessGoalId ?? 0;
|
|
|
|
ret = true;
|
|
} else {
|
|
myFitnessGoal = TraineeSelectedFitnessGoalResponseModel();
|
|
ret = false;
|
|
}
|
|
} catch (e) {
|
|
myFitnessGoal = TraineeSelectedFitnessGoalResponseModel();
|
|
log("Logout Catch : ${e.toString()}");
|
|
ret = false;
|
|
}
|
|
isMyFitnessGoalLoading(false);
|
|
return ret;
|
|
}
|
|
|
|
//............... Fetch Fitness Goals List................................................................
|
|
|
|
var isFitnessGoalsLoading = false.obs;
|
|
var apiFitnessGoalsList = <SingleFitnessGoal>[].obs;
|
|
var selectedFitnessGoal = "".obs;
|
|
RxList<String> localFitnessGoalsList = <String>[].obs;
|
|
RxInt selectedFitnessGoalId = 0.obs;
|
|
|
|
Future<void> fetchFitnessGoals() async {
|
|
isFitnessGoalsLoading(true);
|
|
try {
|
|
var response = await ApiBase.getRequest(
|
|
extendedURL: ApiUrl.fetchFitnessGoals,
|
|
sendHeaders: false,
|
|
);
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
var responseData = fitnessGoalsResponseModelFromJson(response.body);
|
|
if (responseData.isSuccess == true) {
|
|
apiFitnessGoalsList.assignAll(responseData.data ?? []);
|
|
|
|
localFitnessGoalsList.clear();
|
|
|
|
for (var goal in responseData.data!) {
|
|
localFitnessGoalsList.add(goal.fitnessGoalTitle.toString());
|
|
}
|
|
}
|
|
} else {}
|
|
} catch (e) {
|
|
log(e.toString());
|
|
} finally {
|
|
isFitnessGoalsLoading(false);
|
|
}
|
|
}
|
|
|
|
//............... Update Fitness Goal API Call................................................................
|
|
|
|
RxBool isUpdateFitneesGoalLoading = false.obs;
|
|
|
|
Future<bool> updateMyFitnessGoalApiCall() async {
|
|
if (selectedFitnessGoalId.value == 0) {
|
|
Get.snackbar("Error", "Please select a fitness goal");
|
|
return false;
|
|
}
|
|
|
|
var ret = false;
|
|
isUpdateFitneesGoalLoading(true);
|
|
try {
|
|
var requestBody = {"fitnessGoalID": selectedFitnessGoalId.value};
|
|
|
|
var response = await ApiBase.patchRequest(
|
|
extendedURL: "/api/Users/update-user-preferred-goal",
|
|
body: requestBody,
|
|
);
|
|
|
|
log("Update fitness goal response: ${response.statusCode}");
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
Future.delayed(Duration(milliseconds: 500), () {
|
|
customSnackbar(
|
|
title: "Success",
|
|
message: "Fitness goal updated successfully",
|
|
duration: 1,
|
|
);
|
|
});
|
|
ret = true;
|
|
Get.back();
|
|
} else {
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: "Failed to update fitness goal",
|
|
duration: 2,
|
|
);
|
|
|
|
ret = false;
|
|
}
|
|
} catch (e) {
|
|
log("Update fitness goal catch: ${e.toString()}");
|
|
customSnackbar(
|
|
title: "Error",
|
|
message: "Failed to update fitness goal",
|
|
duration: 2,
|
|
);
|
|
ret = false;
|
|
}
|
|
isUpdateFitneesGoalLoading(false);
|
|
return ret;
|
|
}
|
|
|
|
//............... User Progress with Challenges Pagination................................................................
|
|
|
|
RxBool isUserProgressLoading = false.obs;
|
|
RxList<ChallengePogress> challengesList = <ChallengePogress>[].obs;
|
|
RxInt currentPage = 1.obs;
|
|
RxInt pageSize = 10.obs;
|
|
RxInt totalChallenges = 0.obs;
|
|
RxBool hasMoreChallenges = true.obs;
|
|
UserProgressResponseModel userProgressResponse = UserProgressResponseModel();
|
|
|
|
Future<bool> fetchUserProgress({bool loadMore = false}) async {
|
|
if (!loadMore) {
|
|
currentPage.value = 1;
|
|
challengesList.clear();
|
|
hasMoreChallenges.value = true;
|
|
}
|
|
|
|
if (!hasMoreChallenges.value && loadMore) {
|
|
return false;
|
|
}
|
|
|
|
var ret = false;
|
|
isUserProgressLoading(true);
|
|
|
|
try {
|
|
var response = await ApiBase.getRequest(
|
|
extendedURL:
|
|
"/api/Users/get-user-progress?PageNumber=${currentPage.value}&PageSize=${pageSize.value}",
|
|
);
|
|
|
|
log("User progress response: ${response.statusCode}");
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
userProgressResponse = userProgressResponseModelFromJson(response.body);
|
|
|
|
if (userProgressResponse.isSuccess == true &&
|
|
userProgressResponse.data != null) {
|
|
totalChallenges.value =
|
|
userProgressResponse.data!.totalChallenges ?? 0;
|
|
|
|
if (userProgressResponse.data!.challenges != null &&
|
|
userProgressResponse.data!.challenges!.isNotEmpty) {
|
|
if (loadMore) {
|
|
challengesList.addAll(userProgressResponse.data!.challenges!);
|
|
} else {
|
|
challengesList.assignAll(userProgressResponse.data!.challenges!);
|
|
}
|
|
|
|
currentPage.value++;
|
|
|
|
// Check if there are more challenges to load
|
|
if (challengesList.length >= totalChallenges.value) {
|
|
hasMoreChallenges.value = false;
|
|
}
|
|
|
|
ret = true;
|
|
} else {
|
|
hasMoreChallenges.value = false;
|
|
}
|
|
}
|
|
} else {
|
|
ret = false;
|
|
}
|
|
} catch (e) {
|
|
log("Fetch user progress catch: ${e.toString()}");
|
|
|
|
ret = false;
|
|
}
|
|
|
|
isUserProgressLoading(false);
|
|
return ret;
|
|
}
|
|
|
|
// Load more challenges for pagination
|
|
Future<void> loadMoreChallenges() async {
|
|
if (!isUserProgressLoading.value && hasMoreChallenges.value) {
|
|
await fetchUserProgress(loadMore: true);
|
|
}
|
|
}
|
|
|
|
// Refresh challenges list
|
|
Future<void> refreshChallenges() async {
|
|
await fetchUserProgress(loadMore: false);
|
|
}
|
|
}
|