From 8187c0b2dc4d9926f480cef961d3902e5f8171c3 Mon Sep 17 00:00:00 2001 From: sathwikcs <85106054+sathwikcs@users.noreply.github.com> Date: Sat, 19 Oct 2024 11:49:21 +0530 Subject: [PATCH] history --- .../screens/history/viewModel/historyVM.kt | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/in/sminnovations/attendanceapp/screens/history/viewModel/historyVM.kt b/app/src/main/java/in/sminnovations/attendanceapp/screens/history/viewModel/historyVM.kt index 89b3721..847a1c7 100644 --- a/app/src/main/java/in/sminnovations/attendanceapp/screens/history/viewModel/historyVM.kt +++ b/app/src/main/java/in/sminnovations/attendanceapp/screens/history/viewModel/historyVM.kt @@ -3,6 +3,7 @@ package `in`.sminnovations.attendanceapp.screens.history.viewModel import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.google.firebase.Timestamp import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.toObject import dagger.hilt.android.lifecycle.HiltViewModel @@ -11,6 +12,9 @@ import `in`.sminnovations.attendanceapp.screens.login.data.LoginDataStore import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale import javax.inject.Inject @HiltViewModel @@ -40,23 +44,46 @@ class HistoryViewModel @Inject constructor( } } + + private fun getAttendanceData() { viewModelScope.launch { loginDataStore.loginId.collect { id -> - firebaseFirestore.collection("attendance_sath7349").get() + firebaseFirestore.collection("attendance_$id").get() .addOnSuccessListener { result -> val attendanceList = result.documents.mapNotNull { document -> - document.toObject() + 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 checkIn = formatTimestamp(checkInTimestamp) + val checkOut = formatTimestamp(checkOutTimestamp) + + AttendanceData(date, checkIn, checkOut) } - _listOfAttendanceDates.value = - attendanceList // Update the state flow with new data - }.addOnFailureListener { exception -> + _listOfAttendanceDates.value = attendanceList + } + .addOnFailureListener { exception -> Log.e("Firestore", "Error getting documents: ", exception) _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()) + } + + + + + + }