This commit is contained in:
sathwikcs
2025-02-05 18:05:14 +05:30
parent 5a5da9fe72
commit 70aafe73be

View File

@@ -24,7 +24,6 @@ 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
@@ -329,11 +328,10 @@ class HomeViewModel @Inject constructor(
try {
val checkOutTime = formatTimestamp(Timestamp.now())
document.reference.update("checkOut$checkInCount", checkOutTime).await()
val updatedDocument = document.reference.get().await()
val cloudTime = document.getString("totalWork") ?: "00:00:00"
val totalWork = calculateTotalWork(updatedDocument,cloudTime)
document.reference.update("totalWork", totalWork).await()
val totalWork = calculateTotalWork(updatedDocument)
document.reference.update("totalWork", totalWork.first).await()
document.reference.update("DayCompleted", totalWork.second).await()
_uiState.update {
it.copy(
isCheckedOut = true, qrCodeValue = null
@@ -345,16 +343,12 @@ class HomeViewModel @Inject constructor(
}
}
private fun calculateTotalWork(document: DocumentSnapshot,cloudTimestamp: String): String {
private fun calculateTotalWork(document: DocumentSnapshot): Pair<String,Boolean> {
val formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy hh:mm:ss a", Locale.getDefault())
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) {
var totalMinutes = 0L
for (i in 1..20) {
val checkIn = document.getString("checkIn$i") ?: break
val checkOut = document.getString("checkOut$i") ?: continue
val checkInTime = LocalDateTime.parse(checkIn, formatter)
val checkOutTime = LocalDateTime.parse(checkOut, formatter)
totalMinutes += ChronoUnit.MINUTES.between(checkInTime, checkOutTime)
@@ -362,7 +356,8 @@ class HomeViewModel @Inject constructor(
val hours = totalMinutes / 60
val minutes = totalMinutes % 60
return String.format("%02d:%02d:00", hours, minutes)
val isDayCompleted = totalMinutes >=420L
return Pair(String.format("%02d:%02d:00", hours, minutes),isDayCompleted)
}