improved logic

This commit is contained in:
Vaibhav Surve 2025-12-13 14:36:54 +05:30
parent 0fa5a85d79
commit 6cd9fbe57b
4 changed files with 16 additions and 33 deletions

View File

@ -27,6 +27,7 @@ class DailyTaskPlanningController extends GetxController {
RxMap<String, RxBool> buildingLoadingStates = <String, RxBool>{}.obs;
final Set<String> buildingsWithDetails = <String>{};
RxMap<String, RxDouble> todaysAssignedMap = <String, RxDouble>{}.obs;
@override
void onInit() {
super.onInit();
@ -72,6 +73,8 @@ class DailyTaskPlanningController extends GetxController {
required int plannedTask,
required String description,
required List<String> taskTeam,
required String buildingId,
required String projectId,
DateTime? assignmentDate,
String? organizationId,
String? serviceId,
@ -93,6 +96,9 @@ class DailyTaskPlanningController extends GetxController {
if (response == true) {
logSafe("Task assigned successfully", level: LogLevel.info);
await fetchBuildingInfra(buildingId, projectId, serviceId);
Get.back();
showAppSnackbar(
title: "Success",
message: "Task assigned successfully!",
@ -164,23 +170,19 @@ class DailyTaskPlanningController extends GetxController {
level: LogLevel.error, error: e, stackTrace: stack);
} finally {
isFetchingTasks.value = false;
update();
update(); // dailyTasks is non-reactive
}
}
/// Fetch full infra for a single building (floors, workAreas, workItems).
/// Called lazily when user expands a building in the UI.
/// Fetch full infra for a single building (lazy)
Future<void> fetchBuildingInfra(
String buildingId, String projectId, String? serviceId) async {
if (buildingId.isEmpty) return;
// mark loading
buildingLoadingStates.putIfAbsent(buildingId, () => true.obs);
buildingLoadingStates[buildingId]!.value = true;
update();
buildingLoadingStates[buildingId]!.value = true; // Rx change is enough
try {
// Re-use getInfraDetails and find the building entry for the requested buildingId
final infraResponse =
await ApiService.getInfraDetails(projectId, serviceId: serviceId);
final infraData = infraResponse?['data'] as List<dynamic>? ?? [];
@ -196,7 +198,6 @@ class DailyTaskPlanningController extends GetxController {
return;
}
// Build floors & workAreas for this building
final building = Building(
id: buildingJson['id'],
name: buildingJson['buildingName'],
@ -211,7 +212,7 @@ class DailyTaskPlanningController extends GetxController {
return WorkArea(
id: areaJson['id'],
areaName: areaJson['areaName'],
workItems: [], // will populate later
workItems: [],
);
}).toList(),
);
@ -220,7 +221,6 @@ class DailyTaskPlanningController extends GetxController {
completedWork: (buildingJson['completedWork'] as num?)?.toDouble() ?? 0,
);
// For each workArea, fetch its work items and populate
await Future.wait(
building.floors.expand((f) => f.workAreas).map((area) async {
try {
@ -255,7 +255,6 @@ class DailyTaskPlanningController extends GetxController {
}
}));
// Merge/replace the building into dailyTasks
bool merged = false;
for (var t in dailyTasks) {
final idx = t.buildings
@ -267,7 +266,6 @@ class DailyTaskPlanningController extends GetxController {
}
}
if (!merged) {
// If not present, add a new TaskPlanningDetailsModel wrapper (fallback)
dailyTasks.add(TaskPlanningDetailsModel(
id: building.id,
name: building.name,
@ -280,7 +278,6 @@ class DailyTaskPlanningController extends GetxController {
));
}
// Mark as loaded
buildingsWithDetails.add(buildingId.toString());
} catch (e, stack) {
logSafe("Error fetching infra for building $buildingId",
@ -288,7 +285,7 @@ class DailyTaskPlanningController extends GetxController {
} finally {
buildingLoadingStates.putIfAbsent(buildingId, () => false.obs);
buildingLoadingStates[buildingId]!.value = false;
update();
update(); // dailyTasks mutated
}
}
@ -361,7 +358,7 @@ class DailyTaskPlanningController extends GetxController {
}
} finally {
isFetchingEmployees.value = false;
update();
// no update(): RxLists/RxBools notify observers
}
}
}

View File

@ -2075,7 +2075,6 @@ class ApiService {
final parsed = _parseAndDecryptResponse(response,
label: "Assign Daily Task", returnFullResponse: true);
if (parsed != null && parsed['success'] == true) {
Get.back(); // Retaining Get.back() as per original logic
return true;
}
return false;

View File

@ -82,10 +82,6 @@ class _AssignTaskBottomSheetState extends State<AssignTaskBottomSheet> {
serviceId: selectedService?.id,
organizationId: selectedOrganization?.id,
);
await controller.fetchTaskData(
selectedProjectId,
serviceId: selectedService?.id,
);
}
@override
@ -421,8 +417,10 @@ class _AssignTaskBottomSheetState extends State<AssignTaskBottomSheet> {
workItemId: widget.workItemId,
plannedTask: target.toInt(),
description: description,
taskTeam: selectedTeam.map((e) => e.id).toList(), // pass IDs
taskTeam: selectedTeam.map((e) => e.id).toList(),
assignmentDate: widget.assignmentDate,
buildingId: widget.buildingName,
projectId: selectedProjectId!,
organizationId: selectedOrganization?.id,
serviceId: selectedService?.id,
);

View File

@ -492,18 +492,7 @@ class _DailyTaskPlanningScreenState extends State<DailyTaskPlanningScreen>
),
);
final projectId =
widget.projectId;
if (projectId.isNotEmpty) {
await dailyTaskPlanningController
.fetchTaskData(
projectId,
serviceId:
serviceController
.selectedService
?.id,
);
}
}),
],
),