feat: Refactor project selection handling in attendance actions and controllers
This commit is contained in:
parent
a97fb1f541
commit
59e6634023
@ -15,6 +15,7 @@ import 'package:marco/model/employee_model.dart';
|
|||||||
import 'package:marco/model/attendance_log_model.dart';
|
import 'package:marco/model/attendance_log_model.dart';
|
||||||
import 'package:marco/model/regularization_log_model.dart';
|
import 'package:marco/model/regularization_log_model.dart';
|
||||||
import 'package:marco/model/attendance_log_view_model.dart';
|
import 'package:marco/model/attendance_log_view_model.dart';
|
||||||
|
import 'package:marco/controller/project_controller.dart';
|
||||||
|
|
||||||
final Logger log = Logger();
|
final Logger log = Logger();
|
||||||
|
|
||||||
@ -28,7 +29,6 @@ class AttendanceController extends GetxController {
|
|||||||
List<AttendanceLogViewModel> attendenceLogsView = [];
|
List<AttendanceLogViewModel> attendenceLogsView = [];
|
||||||
|
|
||||||
// Selected values
|
// Selected values
|
||||||
String? selectedProjectId;
|
|
||||||
String selectedTab = 'Employee List';
|
String selectedTab = 'Employee List';
|
||||||
|
|
||||||
// Date range for attendance filtering
|
// Date range for attendance filtering
|
||||||
@ -279,7 +279,7 @@ class AttendanceController extends GetxController {
|
|||||||
log.i("Date range selected: $startDateAttendance to $endDateAttendance");
|
log.i("Date range selected: $startDateAttendance to $endDateAttendance");
|
||||||
|
|
||||||
await controller.fetchAttendanceLogs(
|
await controller.fetchAttendanceLogs(
|
||||||
controller.selectedProjectId,
|
Get.find<ProjectController>().selectedProject?.id,
|
||||||
dateFrom: picked.start,
|
dateFrom: picked.start,
|
||||||
dateTo: picked.end,
|
dateTo: picked.end,
|
||||||
);
|
);
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import 'package:intl/intl.dart';
|
|||||||
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
||||||
import 'package:marco/controller/dashboard/attendance_screen_controller.dart';
|
import 'package:marco/controller/dashboard/attendance_screen_controller.dart';
|
||||||
import 'package:marco/helpers/utils/attendance_actions.dart';
|
import 'package:marco/helpers/utils/attendance_actions.dart';
|
||||||
|
import 'package:marco/controller/project_controller.dart';
|
||||||
|
|
||||||
class AttendanceActionButton extends StatefulWidget {
|
class AttendanceActionButton extends StatefulWidget {
|
||||||
final dynamic employee;
|
final dynamic employee;
|
||||||
@ -19,10 +20,11 @@ class AttendanceActionButton extends StatefulWidget {
|
|||||||
State<AttendanceActionButton> createState() => _AttendanceActionButtonState();
|
State<AttendanceActionButton> createState() => _AttendanceActionButtonState();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String?> _showCommentBottomSheet(BuildContext context, String actionText) async {
|
Future<String?> _showCommentBottomSheet(
|
||||||
|
BuildContext context, String actionText) async {
|
||||||
final TextEditingController commentController = TextEditingController();
|
final TextEditingController commentController = TextEditingController();
|
||||||
String? errorText;
|
String? errorText;
|
||||||
|
Get.find<ProjectController>().selectedProject?.id;
|
||||||
return showModalBottomSheet<String>(
|
return showModalBottomSheet<String>(
|
||||||
context: context,
|
context: context,
|
||||||
isScrollControlled: true,
|
isScrollControlled: true,
|
||||||
@ -80,7 +82,7 @@ Future<String?> _showCommentBottomSheet(BuildContext context, String actionText)
|
|||||||
),
|
),
|
||||||
const SizedBox(width: 12),
|
const SizedBox(width: 12),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: ElevatedButton(
|
child: ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
final comment = commentController.text.trim();
|
final comment = commentController.text.trim();
|
||||||
if (comment.isEmpty) {
|
if (comment.isEmpty) {
|
||||||
@ -105,7 +107,6 @@ Future<String?> _showCommentBottomSheet(BuildContext context, String actionText)
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
String capitalizeFirstLetter(String text) {
|
String capitalizeFirstLetter(String text) {
|
||||||
if (text.isEmpty) return text;
|
if (text.isEmpty) return text;
|
||||||
return text[0].toUpperCase() + text.substring(1);
|
return text[0].toUpperCase() + text.substring(1);
|
||||||
@ -163,7 +164,10 @@ class _AttendanceActionButtonState extends State<AttendanceActionButton> {
|
|||||||
void _handleButtonPressed(BuildContext context) async {
|
void _handleButtonPressed(BuildContext context) async {
|
||||||
widget.attendanceController.uploadingStates[uniqueLogKey]?.value = true;
|
widget.attendanceController.uploadingStates[uniqueLogKey]?.value = true;
|
||||||
|
|
||||||
if (widget.attendanceController.selectedProjectId == null) {
|
final projectController = Get.find<ProjectController>();
|
||||||
|
final selectedProjectId = projectController.selectedProject?.id;
|
||||||
|
|
||||||
|
if (selectedProjectId == null) {
|
||||||
showAppSnackbar(
|
showAppSnackbar(
|
||||||
title: "Project Required",
|
title: "Project Required",
|
||||||
message: "Please select a project first",
|
message: "Please select a project first",
|
||||||
@ -231,7 +235,7 @@ class _AttendanceActionButtonState extends State<AttendanceActionButton> {
|
|||||||
success = await widget.attendanceController.captureAndUploadAttendance(
|
success = await widget.attendanceController.captureAndUploadAttendance(
|
||||||
widget.employee.id,
|
widget.employee.id,
|
||||||
widget.employee.employeeId,
|
widget.employee.employeeId,
|
||||||
widget.attendanceController.selectedProjectId!,
|
selectedProjectId,
|
||||||
comment: userComment,
|
comment: userComment,
|
||||||
action: updatedAction,
|
action: updatedAction,
|
||||||
imageCapture: imageCapture,
|
imageCapture: imageCapture,
|
||||||
@ -242,7 +246,7 @@ class _AttendanceActionButtonState extends State<AttendanceActionButton> {
|
|||||||
success = await widget.attendanceController.captureAndUploadAttendance(
|
success = await widget.attendanceController.captureAndUploadAttendance(
|
||||||
widget.employee.id,
|
widget.employee.id,
|
||||||
widget.employee.employeeId,
|
widget.employee.employeeId,
|
||||||
widget.attendanceController.selectedProjectId!,
|
selectedProjectId,
|
||||||
comment: userComment,
|
comment: userComment,
|
||||||
action: updatedAction,
|
action: updatedAction,
|
||||||
imageCapture: imageCapture,
|
imageCapture: imageCapture,
|
||||||
@ -260,14 +264,11 @@ class _AttendanceActionButtonState extends State<AttendanceActionButton> {
|
|||||||
widget.attendanceController.uploadingStates[uniqueLogKey]?.value = false;
|
widget.attendanceController.uploadingStates[uniqueLogKey]?.value = false;
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
widget.attendanceController.fetchEmployeesByProject(
|
widget.attendanceController.fetchEmployeesByProject(selectedProjectId);
|
||||||
widget.attendanceController.selectedProjectId!);
|
widget.attendanceController.fetchAttendanceLogs(selectedProjectId);
|
||||||
widget.attendanceController
|
|
||||||
.fetchAttendanceLogs(widget.attendanceController.selectedProjectId!);
|
|
||||||
await widget.attendanceController.fetchRegularizationLogs(
|
|
||||||
widget.attendanceController.selectedProjectId!);
|
|
||||||
await widget.attendanceController
|
await widget.attendanceController
|
||||||
.fetchProjectData(widget.attendanceController.selectedProjectId!);
|
.fetchRegularizationLogs(selectedProjectId);
|
||||||
|
await widget.attendanceController.fetchProjectData(selectedProjectId);
|
||||||
widget.attendanceController.update();
|
widget.attendanceController.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:marco/helpers/utils/attendance_actions.dart';
|
import 'package:marco/helpers/utils/attendance_actions.dart';
|
||||||
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
||||||
|
import 'package:marco/controller/project_controller.dart';
|
||||||
|
import 'package:get/get.dart';
|
||||||
enum ButtonActions { approve, reject }
|
enum ButtonActions { approve, reject }
|
||||||
|
|
||||||
class RegularizeActionButton extends StatefulWidget {
|
class RegularizeActionButton extends StatefulWidget {
|
||||||
@ -51,60 +53,57 @@ class _RegularizeActionButtonState extends State<RegularizeActionButton> {
|
|||||||
Colors.grey;
|
Colors.grey;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _handlePress() async {
|
Future<void> _handlePress() async {
|
||||||
if (widget.attendanceController.selectedProjectId == null) {
|
final projectController = Get.find<ProjectController>();
|
||||||
showAppSnackbar(
|
final selectedProjectId = projectController.selectedProject?.id;
|
||||||
title: 'Warning',
|
|
||||||
message: 'Please select a project first',
|
|
||||||
type: SnackbarType.warning,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setState(() {
|
|
||||||
isUploading = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
widget.attendanceController.uploadingStates[widget.uniqueLogKey]?.value =
|
|
||||||
true;
|
|
||||||
|
|
||||||
final success =
|
|
||||||
await widget.attendanceController.captureAndUploadAttendance(
|
|
||||||
widget.log.id,
|
|
||||||
widget.log.employeeId,
|
|
||||||
widget.attendanceController.selectedProjectId!,
|
|
||||||
comment: _buttonComments[widget.action]!,
|
|
||||||
action: _buttonActionCodes[widget.action]!,
|
|
||||||
imageCapture: false,
|
|
||||||
);
|
|
||||||
|
|
||||||
|
if (selectedProjectId == null) {
|
||||||
showAppSnackbar(
|
showAppSnackbar(
|
||||||
title: success ? 'Success' : 'Error',
|
title: 'Warning',
|
||||||
message: success
|
message: 'Please select a project first',
|
||||||
? '${capitalizeFirstLetter(_buttonTexts[widget.action]!)} marked successfully!'
|
type: SnackbarType.warning,
|
||||||
: 'Failed to mark ${capitalizeFirstLetter(_buttonTexts[widget.action]!)}.',
|
|
||||||
type: success ? SnackbarType.success : SnackbarType.error,
|
|
||||||
);
|
);
|
||||||
|
return;
|
||||||
if (success) {
|
|
||||||
widget.attendanceController.fetchEmployeesByProject(
|
|
||||||
widget.attendanceController.selectedProjectId!);
|
|
||||||
widget.attendanceController
|
|
||||||
.fetchAttendanceLogs(widget.attendanceController.selectedProjectId!);
|
|
||||||
await widget.attendanceController.fetchRegularizationLogs(
|
|
||||||
widget.attendanceController.selectedProjectId!);
|
|
||||||
await widget.attendanceController
|
|
||||||
.fetchProjectData(widget.attendanceController.selectedProjectId!);
|
|
||||||
}
|
|
||||||
|
|
||||||
widget.attendanceController.uploadingStates[widget.uniqueLogKey]?.value =
|
|
||||||
false;
|
|
||||||
|
|
||||||
setState(() {
|
|
||||||
isUploading = false;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
isUploading = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
widget.attendanceController.uploadingStates[widget.uniqueLogKey]?.value = true;
|
||||||
|
|
||||||
|
final success = await widget.attendanceController.captureAndUploadAttendance(
|
||||||
|
widget.log.id,
|
||||||
|
widget.log.employeeId,
|
||||||
|
selectedProjectId,
|
||||||
|
comment: _buttonComments[widget.action]!,
|
||||||
|
action: _buttonActionCodes[widget.action]!,
|
||||||
|
imageCapture: false,
|
||||||
|
);
|
||||||
|
|
||||||
|
showAppSnackbar(
|
||||||
|
title: success ? 'Success' : 'Error',
|
||||||
|
message: success
|
||||||
|
? '${capitalizeFirstLetter(_buttonTexts[widget.action]!)} marked successfully!'
|
||||||
|
: 'Failed to mark ${capitalizeFirstLetter(_buttonTexts[widget.action]!)}.',
|
||||||
|
type: success ? SnackbarType.success : SnackbarType.error,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
widget.attendanceController.fetchEmployeesByProject(selectedProjectId);
|
||||||
|
widget.attendanceController.fetchAttendanceLogs(selectedProjectId);
|
||||||
|
await widget.attendanceController.fetchRegularizationLogs(selectedProjectId);
|
||||||
|
await widget.attendanceController.fetchProjectData(selectedProjectId);
|
||||||
|
}
|
||||||
|
|
||||||
|
widget.attendanceController.uploadingStates[widget.uniqueLogKey]?.value = false;
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
isUploading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final buttonText = _buttonTexts[widget.action]!;
|
final buttonText = _buttonTexts[widget.action]!;
|
||||||
|
|||||||
@ -78,7 +78,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
|
|||||||
titleSpacing: 0,
|
titleSpacing: 0,
|
||||||
centerTitle: false,
|
centerTitle: false,
|
||||||
leading: Padding(
|
leading: Padding(
|
||||||
padding: const EdgeInsets.only(top: 15.0), // Aligns with title
|
padding: const EdgeInsets.only(top: 15.0),
|
||||||
child: IconButton(
|
child: IconButton(
|
||||||
icon: const Icon(Icons.arrow_back_ios_new,
|
icon: const Icon(Icons.arrow_back_ios_new,
|
||||||
color: Colors.black, size: 20),
|
color: Colors.black, size: 20),
|
||||||
@ -127,10 +127,8 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
|
|||||||
builder: (controller) {
|
builder: (controller) {
|
||||||
final selectedProjectId =
|
final selectedProjectId =
|
||||||
Get.find<ProjectController>().selectedProjectId?.value;
|
Get.find<ProjectController>().selectedProjectId?.value;
|
||||||
|
|
||||||
final bool noProjectSelected =
|
final bool noProjectSelected =
|
||||||
selectedProjectId == null || selectedProjectId.isEmpty;
|
selectedProjectId == null || selectedProjectId.isEmpty;
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user