From 70aafe73beff38452ac8f46822b722ff058de133 Mon Sep 17 00:00:00 2001 From: sathwikcs <85106054+sathwikcs@users.noreply.github.com> Date: Wed, 5 Feb 2025 18:05:14 +0530 Subject: [PATCH] homVM --- .../screens/home/viewModel/homeVM.kt | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/in/sminnovations/attendanceapp/screens/home/viewModel/homeVM.kt b/app/src/main/java/in/sminnovations/attendanceapp/screens/home/viewModel/homeVM.kt index afb85b6..ca8f7c4 100644 --- a/app/src/main/java/in/sminnovations/attendanceapp/screens/home/viewModel/homeVM.kt +++ b/app/src/main/java/in/sminnovations/attendanceapp/screens/home/viewModel/homeVM.kt @@ -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 { 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) }