added the refresh feature to history screen viewmodel
This commit is contained in:
@@ -51,32 +51,39 @@ class HistoryViewModel @Inject constructor(
|
|||||||
loginDataStore.loginId.collect { id ->
|
loginDataStore.loginId.collect { id ->
|
||||||
firebaseFirestore.collection("attendance_$id").get()
|
firebaseFirestore.collection("attendance_$id").get()
|
||||||
.addOnSuccessListener { result ->
|
.addOnSuccessListener { result ->
|
||||||
|
viewModelScope.launch {
|
||||||
val attendanceList = result.documents.mapNotNull { document ->
|
val attendanceList = result.documents.mapNotNull { document ->
|
||||||
val date = document.id // Assuming the document ID is the date
|
val date = document.id // Assuming the document ID is the date
|
||||||
|
|
||||||
|
val checkInTimestamp = document.getString("checkIn") ?: "NA"
|
||||||
val checkInTimestamp = document.getTimestamp("checkIn") ?: Timestamp.now()
|
val checkOutTimestamp = document.getString("checkOut") ?: "NA"
|
||||||
val checkOutTimestamp = document.getTimestamp("checkout") ?: Timestamp.now()
|
|
||||||
|
|
||||||
val checkIn = formatTimestamp(checkInTimestamp)
|
val checkIn = formatTimestamp(checkInTimestamp)
|
||||||
val checkOut = formatTimestamp(checkOutTimestamp)
|
val checkOut = formatTimestamp(checkOutTimestamp)
|
||||||
|
|
||||||
AttendanceData(date, checkIn, checkOut)
|
AttendanceData(date, checkIn, checkOut)
|
||||||
}
|
}
|
||||||
_listOfAttendanceDates.value = attendanceList
|
_listOfAttendanceDates.value = attendanceList
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.addOnFailureListener { exception ->
|
.addOnFailureListener { exception ->
|
||||||
Log.e("Firestore", "Error getting documents: ", exception)
|
Log.e("Firestore", "Error getting documents: ", exception)
|
||||||
_listOfAttendanceDates.value = emptyList()
|
viewModelScope.launch {
|
||||||
|
_listOfAttendanceDates.value = emptyList()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun formatTimestamp(timestamp: Timestamp): String {
|
private fun formatTimestamp(timestamp: Any): String {
|
||||||
val sdf = SimpleDateFormat("dd MMMM yyyy hh:mm:ss a", Locale.getDefault()) // Format with AM/PM
|
val sdf = SimpleDateFormat("dd MMMM yyyy hh:mm a", Locale.getDefault())
|
||||||
return sdf.format(timestamp.toDate())
|
return if (timestamp is Timestamp) {
|
||||||
|
sdf.format(timestamp.toDate())
|
||||||
|
} else {
|
||||||
|
timestamp.toString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -84,6 +91,7 @@ class HistoryViewModel @Inject constructor(
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user