feat: enhance job fetching logic and add auto-refresh after job addition

This commit is contained in:
Vaibhav Surve 2025-11-19 11:28:39 +05:30
parent 804f0eba7b
commit 0e575c393d
2 changed files with 33 additions and 22 deletions

View File

@ -3,16 +3,17 @@ import 'package:get/get.dart';
import 'package:marco/helpers/services/api_service.dart';
import 'package:marco/model/employees/employee_model.dart';
import 'package:marco/helpers/widgets/my_snackbar.dart';
import 'package:marco/controller/service_project/service_project_details_screen_controller.dart';
class AddServiceProjectJobController extends GetxController {
// Form Controllers
// Form Controllers
final titleCtrl = TextEditingController();
final descCtrl = TextEditingController();
final tagCtrl = TextEditingController();
final FocusNode searchFocusNode = FocusNode();
final RxBool showEmployeePicker = true.obs;
// Observables
// Observables
final startDate = Rx<DateTime?>(DateTime.now());
final dueDate = Rx<DateTime?>(DateTime.now().add(const Duration(days: 1)));
final enteredTags = <String>[].obs;
@ -21,7 +22,7 @@ class AddServiceProjectJobController extends GetxController {
final selectedAssignees = <EmployeeModel>[].obs;
final isSearchingEmployees = false.obs;
// Loading states
// Loading states
final isLoading = false.obs;
final isAllEmployeeLoading = false.obs;
final allEmployees = <EmployeeModel>[].obs;
@ -40,7 +41,6 @@ class AddServiceProjectJobController extends GetxController {
super.onClose();
}
/// Toggle employee selection
void toggleAssignee(EmployeeModel employee) {
if (selectedAssignees.contains(employee)) {
@ -74,6 +74,11 @@ class AddServiceProjectJobController extends GetxController {
);
if (success) {
// 🔥 Auto-refresh job list in ServiceProjectDetailsController
if (Get.isRegistered<ServiceProjectDetailsController>()) {
Get.find<ServiceProjectDetailsController>().refreshJobsAfterAdd();
}
Get.back();
showAppSnackbar(
title: "Success",

View File

@ -32,7 +32,7 @@ class ServiceProjectDetailsController extends GetxController {
var pageNumber = 1;
final int pageSize = 20;
var hasMoreJobs = true.obs;
// Inside ServiceProjectDetailsController
var isTagging = false.obs;
var attendanceMessage = ''.obs;
var attendanceLog = Rxn<JobAttendanceResponse>();
@ -44,7 +44,7 @@ class ServiceProjectDetailsController extends GetxController {
@override
void onInit() {
super.onInit();
fetchProjectJobs(initialLoad: true); // fetch job list initially
fetchProjectJobs(); // always load jobs even without projectId
}
// -------------------- Project --------------------
@ -53,7 +53,7 @@ class ServiceProjectDetailsController extends GetxController {
fetchProjectDetail();
pageNumber = 1;
hasMoreJobs.value = true;
fetchProjectJobs(initialLoad: true);
fetchProjectJobs(); // no initialLoad
}
Future<void> fetchProjectTeams() async {
@ -109,7 +109,6 @@ class ServiceProjectDetailsController extends GetxController {
}
}
// Add this method to your controller
Future<void> fetchJobAttendanceLog(String attendanceId) async {
if (attendanceId.isEmpty) {
attendanceMessage.value = "Invalid attendance ID";
@ -135,34 +134,31 @@ class ServiceProjectDetailsController extends GetxController {
}
}
// -------------------- Job List --------------------
Future<void> fetchProjectJobs({bool initialLoad = false}) async {
if (projectId.value.isEmpty && !initialLoad) {
jobErrorMessage.value = "Invalid project ID";
return;
}
if (!hasMoreJobs.value && !initialLoad) return;
// -------------------- Job List (modified to always load) --------------------
Future<void> fetchProjectJobs() async {
if (!hasMoreJobs.value) return;
isJobLoading.value = true;
jobErrorMessage.value = '';
try {
final result = await ApiService.getServiceProjectJobListApi(
projectId: projectId.value,
projectId: projectId.value, // allows empty projectId
pageNumber: pageNumber,
pageSize: pageSize,
isActive: true,
);
if (result != null && result.data != null) {
if (initialLoad) {
jobList.value = result.data?.data ?? [];
final newJobs = result.data?.data ?? [];
if (pageNumber == 1) {
jobList.value = newJobs;
} else {
jobList.addAll(result.data?.data ?? []);
jobList.addAll(newJobs);
}
hasMoreJobs.value = (result.data?.data?.length ?? 0) == pageSize;
hasMoreJobs.value = newJobs.length == pageSize;
if (hasMoreJobs.value) pageNumber++;
} else {
jobErrorMessage.value = result?.message ?? "Failed to fetch job list";
@ -180,9 +176,10 @@ class ServiceProjectDetailsController extends GetxController {
Future<void> refresh() async {
pageNumber = 1;
hasMoreJobs.value = true;
await Future.wait([
fetchProjectDetail(),
fetchProjectJobs(initialLoad: true),
fetchProjectJobs(),
]);
}
@ -304,4 +301,13 @@ class ServiceProjectDetailsController extends GetxController {
isTagging.value = false;
}
}
// ------------------------------------------------------------
// 🔥 AUTO REFRESH JOB LIST AFTER ADDING A JOB
// ------------------------------------------------------------
Future<void> refreshJobsAfterAdd() async {
pageNumber = 1;
hasMoreJobs.value = true;
await fetchProjectJobs();
}
}