120 lines
3.6 KiB
Dart
120 lines
3.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:ftc_mobile_app/models/profileData/user_data.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 '../../ftc_mobile_app.dart';
|
|
|
|
class DashboardScreenController extends GetxController {
|
|
static DashboardScreenController get instance =>
|
|
Get.find<DashboardScreenController>();
|
|
|
|
final GlobalKey<ScaffoldState> screenKey = GlobalKey<ScaffoldState>();
|
|
|
|
final selectedIndex = 1.obs;
|
|
final myShiftsList = RxList<DaysArrayData>();
|
|
final ongoingShift = Rx<DaysArrayData?>(null);
|
|
final myProfileData = Rx<UserData?>(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().showLoader();
|
|
|
|
if (resp.success == true) {
|
|
myProfileData.value = resp.data?.users?.firstOrNull;
|
|
} else {
|
|
if (resp.message.isNotNullOrEmpty()) {
|
|
FrequentFunctions.showToast(message: resp.message!);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> 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<DaysArrayData> 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<DashboardScreenController>();
|
|
super.dispose();
|
|
}
|
|
}
|