diff --git a/app/src/main/java/in/sminnovations/attendanceapp/screens/history/viewModel/historyVM.kt b/app/src/main/java/in/sminnovations/attendanceapp/screens/history/viewModel/historyVM.kt index 847a1c7..d37a97e 100644 --- a/app/src/main/java/in/sminnovations/attendanceapp/screens/history/viewModel/historyVM.kt +++ b/app/src/main/java/in/sminnovations/attendanceapp/screens/history/viewModel/historyVM.kt @@ -51,32 +51,39 @@ class HistoryViewModel @Inject constructor( loginDataStore.loginId.collect { id -> firebaseFirestore.collection("attendance_$id").get() .addOnSuccessListener { result -> + viewModelScope.launch { val attendanceList = result.documents.mapNotNull { document -> val date = document.id // Assuming the document ID is the date - - val checkInTimestamp = document.getTimestamp("checkIn") ?: Timestamp.now() - val checkOutTimestamp = document.getTimestamp("checkout") ?: Timestamp.now() + val checkInTimestamp = document.getString("checkIn") ?: "NA" + val checkOutTimestamp = document.getString("checkOut") ?: "NA" val checkIn = formatTimestamp(checkInTimestamp) val checkOut = formatTimestamp(checkOutTimestamp) AttendanceData(date, checkIn, checkOut) } - _listOfAttendanceDates.value = attendanceList + _listOfAttendanceDates.value = attendanceList + } } .addOnFailureListener { exception -> Log.e("Firestore", "Error getting documents: ", exception) - _listOfAttendanceDates.value = emptyList() + viewModelScope.launch { + _listOfAttendanceDates.value = emptyList() + } } } } } - private fun formatTimestamp(timestamp: Timestamp): String { - val sdf = SimpleDateFormat("dd MMMM yyyy hh:mm:ss a", Locale.getDefault()) // Format with AM/PM - return sdf.format(timestamp.toDate()) + private fun formatTimestamp(timestamp: Any): String { + val sdf = SimpleDateFormat("dd MMMM yyyy hh:mm a", Locale.getDefault()) + return if (timestamp is Timestamp) { + sdf.format(timestamp.toDate()) + } else { + timestamp.toString() + } } @@ -84,6 +91,7 @@ class HistoryViewModel @Inject constructor( + }