141 lines
3.3 KiB
Dart
141 lines
3.3 KiB
Dart
import 'package:get/get.dart';
|
|
import '../../data/fetch/attedance_api.dart';
|
|
import '../../data/model/user_model.dart';
|
|
import '../../data/shared/auth_storage.dart';
|
|
|
|
class DashboardController extends GetxController {
|
|
final _api = AttedanceApi();
|
|
final _storage = AuthStorage();
|
|
|
|
final user = Rxn<UserModel>();
|
|
final dailyList = <DailyModel>[].obs;
|
|
|
|
final datangInfo = '- WIB'.obs;
|
|
final pulangInfo = '- WIB'.obs;
|
|
final isLoadingDaily = false.obs;
|
|
final selectedDate = ''.obs;
|
|
|
|
final isLoading = false.obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
_initialize();
|
|
}
|
|
|
|
Future<void> _initialize() async {
|
|
selectedDate.value = _todayDate();
|
|
|
|
await fetchUser();
|
|
|
|
if (user.value != null) {
|
|
await fetchDaily();
|
|
} else {
|
|
print("USER NULL - fetchDaily dibatalkan");
|
|
}
|
|
}
|
|
|
|
/// ================= REFRESH =================
|
|
Future<void> refreshAll() async {
|
|
await fetchUser();
|
|
await fetchDaily();
|
|
}
|
|
|
|
/// ================= FETCH USER =================
|
|
Future<void> fetchUser() async {
|
|
try {
|
|
isLoading.value = true;
|
|
|
|
final userData = await _storage.getUser();
|
|
|
|
if (userData == null) {
|
|
print("User kosong");
|
|
isLoading.value = false;
|
|
return;
|
|
}
|
|
|
|
// ✅ FIX: langsung assign
|
|
user.value = userData;
|
|
|
|
print("USER FETCHED: ${user.value?.id}");
|
|
|
|
isLoading.value = false;
|
|
} catch (e) {
|
|
print("ERROR FETCH USER: $e");
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
/// ================= LOGOUT =================
|
|
Future<void> logout() async {
|
|
await _storage.logout();
|
|
}
|
|
|
|
/// ================= FETCH DAILY =================
|
|
Future<void> fetchDaily() async {
|
|
final uid = user.value?.id;
|
|
|
|
if (uid == null) {
|
|
print("UID NULL - Tidak bisa fetchDaily");
|
|
return;
|
|
}
|
|
|
|
isLoadingDaily.value = true;
|
|
|
|
try {
|
|
print("FETCH DAILY UID: $uid");
|
|
print("DATE: ${selectedDate.value}");
|
|
|
|
final res = await _api.getDailyAttendance(
|
|
userId: uid,
|
|
date: selectedDate.value,
|
|
);
|
|
|
|
print("API RESULT: $res");
|
|
|
|
if (res['ok'] == true && res['data'] is List) {
|
|
List listData = res['data'];
|
|
|
|
dailyList.value = listData.map((e) => DailyModel.fromJson(e)).toList();
|
|
|
|
if (dailyList.isNotEmpty) {
|
|
final today = dailyList.first;
|
|
|
|
if (today.jam.contains('-')) {
|
|
final split = today.jam.split('-');
|
|
|
|
datangInfo.value = "${split[0].trim()} WIB";
|
|
pulangInfo.value = "${split.last.trim()} WIB";
|
|
} else {
|
|
_resetAttendance();
|
|
}
|
|
}
|
|
|
|
print("TOTAL DATA DASHBOARD: ${dailyList.length}");
|
|
} else {
|
|
_resetAttendance();
|
|
}
|
|
} catch (e) {
|
|
print("ERROR FETCH DAILY: $e");
|
|
_resetAttendance();
|
|
} finally {
|
|
isLoadingDaily.value = false;
|
|
}
|
|
}
|
|
|
|
/// ================= RESET =================
|
|
void _resetAttendance() {
|
|
dailyList.clear();
|
|
datangInfo.value = '- WIB';
|
|
pulangInfo.value = '- WIB';
|
|
}
|
|
|
|
/// ================= DATE =================
|
|
String _todayDate() {
|
|
final now = DateTime.now();
|
|
return '${now.year.toString().padLeft(4, '0')}-'
|
|
'${now.month.toString().padLeft(2, '0')}-'
|
|
'${now.day.toString().padLeft(2, '0')}';
|
|
}
|
|
}
|