history
This commit is contained in:
@@ -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<AttendanceData>()
|
||||
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())
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user