This commit is contained in:
sathwikcs
2025-02-05 17:15:52 +05:30
parent ed001bd266
commit 30adc892d4

View File

@@ -24,6 +24,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.util.Date
@@ -262,24 +263,7 @@ class HomeViewModel @Inject constructor(
// println(a)
// return true
// }
// fun processAttendance(userId: String) {
// if (userId.isBlank()) return
//
// viewModelScope.launch {
// try {
// val currentDate = getCurrentFormattedDate()
// val attendanceDoc = getAttendanceDocument(userId, currentDate)
//
// if (attendanceDoc?.exists() == true) {
// handleExistingAttendance(attendanceDoc)
// } else {
// handleNewAttendance(userId, currentDate)
// }
// } catch (e: Exception) {
// handleError("Error: ${e.message}")
// }
// }
// }
@@ -316,6 +300,11 @@ class HomeViewModel @Inject constructor(
firebaseFirestore.collection("attendance_$userId").document(currentDate)
.set(checkInData).await()
_uiState.update {
it.copy(
isCheckedIn = true, qrCodeValue = null
)
}
} catch (e: Exception) {
handleError("Failed to check in: ${e.message}")
}
@@ -325,6 +314,12 @@ class HomeViewModel @Inject constructor(
private suspend fun processCheckIn(document: DocumentSnapshot, checkInCount: Int) {
try {
document.reference.update("checkIn$checkInCount", formatTimestamp(Timestamp.now())).await()
_uiState.update {
it.copy(
isCheckedIn = true, qrCodeValue = null
)
}
} catch (e: Exception) {
handleError("Failed to check in: ${e.message}")
}
@@ -336,18 +331,26 @@ class HomeViewModel @Inject constructor(
document.reference.update("checkOut$checkInCount", checkOutTime).await()
val updatedDocument = document.reference.get().await()
val totalWork = calculateTotalWork(updatedDocument)
val cloudTime = document.getString("totalWork") ?: "00:00:00"
val totalWork = calculateTotalWork(updatedDocument,cloudTime)
document.reference.update("totalWork", totalWork).await()
_uiState.update {
it.copy(
isCheckedOut = true, qrCodeValue = null
)
}
} catch (e: Exception) {
handleError("Failed to check out: ${e.message}")
}
}
private fun calculateTotalWork(document: DocumentSnapshot): String {
private fun calculateTotalWork(document: DocumentSnapshot,cloudTimestamp: String): String {
val formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy hh:mm:ss a", Locale.getDefault())
var totalMinutes = 0L
val formatterTime = DateTimeFormatter.ofPattern("hh:mm:ss")
val givenTimeFromCloud = LocalTime.parse(cloudTimestamp, formatterTime)
val finalTime =givenTimeFromCloud.minute.toLong()
var totalMinutes = finalTime
for (i in 1..10) {
val checkIn = document.getString("checkIn$i") ?: break
val checkOut = document.getString("checkOut$i") ?: continue