import 'package:flutter/material.dart'; import 'package:ftc_mobile_app/models/profile_screen_model.dart'; import 'package:ftc_mobile_app/models/rota/LiveRoasterResponseData.dart'; import 'package:ftc_mobile_app/models/rota/WeekArrayData.dart'; import 'package:ftc_mobile_app/utilities/extensions/custom_extensions.dart'; import 'package:get/get.dart'; import 'package:intl/intl.dart'; import '../../ftc_mobile_app.dart'; class DashboardScreenController extends GetxController { static DashboardScreenController get instance => Get.find(); final GlobalKey screenKey = GlobalKey(); final selectedIndex = 1.obs; final myShiftsList = RxList(); final ongoingShift = Rx(null); final myProfileData = Rx(null); @override void onInit() { _initOngoingShift(); super.onInit(); } @override void onReady() { getProfileDetail(); getMyShifts(); super.onReady(); } void removeFocus() { FocusScope.of(screenKey.currentContext!).unfocus(); } _initOngoingShift() { final shift = LocalStorageManager.getOngoingShift(); if (shift != null) { //meaning shift expired already if (shift.endTime != null && shift.endTime!.isBefore(TimeOfDay.now())) { LocalStorageManager.removeOngoingShift(); } else { ongoingShift.value = shift; } } } getProfileDetail() async { final resp = await ClientService().getUserDetails(); if (resp is ProfileDataModel) { myProfileData.value = resp.data?.staffMembers?.firstOrNull; } } Future getMyShifts() async { var response = await RotaService().getMyShifts( startDateMills: DateTime.now().subtract(30.days).millisecondsSinceEpoch, endDateMills: DateTime.now().add(30.days).millisecondsSinceEpoch); if (response is LiveRoasterResponseData) { response.daysArray = (response.daysArray.isNullOrEmpty()) ? [] : (response.daysArray! ..sort((a, b) => a.shiftDate!.compareTo(b.shiftDate!))); List todayAndFutureShifts = []; //Extracting today's and future shifts for (final shift in response.daysArray!) { if (shift.shiftDateTime == null || shift.startTime == null || shift.endTime == null) { debugPrint( "shiftId: ${shift.id} has missing shiftDateTime or startTime or endTime. Please check"); return; } //Note: matching only date, month, year final isTodayShift = DateFormatter.dateFormatter.format(shift.shiftDateTime!) == DateFormatter.dateFormatter.format(DateTime.now()); final isFutureShift = shift.shiftDateTime!.isAfter(DateTime.now()); if (isTodayShift) { //checking and adding today's not expired shift // shift not expired yet if (shift.endTime!.isAfter(TimeOfDay.now())) { todayAndFutureShifts.add(shift); if (ongoingShift() == null) { LocalStorageManager.saveShiftData(data: shift); ongoingShift.value = shift; } } } else if (isFutureShift) { todayAndFutureShifts.add(shift); } } myShiftsList.value = todayAndFutureShifts.take(2).toList(); } else if (response is String && response.isNotEmpty) { FrequentFunctions.showToast(message: response); } } @override void dispose() { Get.delete(); super.dispose(); } }