added the refresh feature to history screen viewmodel

This commit is contained in:
sathwikcs
2024-10-22 16:58:55 +05:30
parent 18cd04ed92
commit 18bcfb7549

View File

@@ -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(
}