history
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package `in`.sminnovations.attendanceapp.screens.history
|
package `in`.sminnovations.attendanceapp.screens.history.ui
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
@@ -9,14 +9,21 @@ import androidx.compose.foundation.lazy.items
|
|||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import `in`.sminnovations.attendanceapp.screens.history.components.EnhancedAttendanceCard
|
import `in`.sminnovations.attendanceapp.screens.history.components.EnhancedAttendanceCard
|
||||||
import `in`.sminnovations.attendanceapp.screens.history.data.Attendance
|
import `in`.sminnovations.attendanceapp.screens.history.viewModel.HistoryViewModel
|
||||||
|
import `in`.sminnovations.attendanceapp.screens.login.viewModel.LoginViewModel
|
||||||
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun HistoryScreen() {
|
fun HistoryScreen(
|
||||||
|
historyViewModel: HistoryViewModel = hiltViewModel(),
|
||||||
|
) {
|
||||||
|
val attendanceData = historyViewModel.listOfAttendanceDates.collectAsState().value
|
||||||
Column(modifier = Modifier.fillMaxSize()) {
|
Column(modifier = Modifier.fillMaxSize()) {
|
||||||
Text(
|
Text(
|
||||||
text = "Your Attendance History",
|
text = "Your Attendance History",
|
||||||
@@ -33,7 +40,7 @@ fun HistoryScreen() {
|
|||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.padding(vertical = 8.dp)
|
.padding(vertical = 8.dp)
|
||||||
) {
|
) {
|
||||||
items(sampleAttendanceList) { attendance ->
|
items(attendanceData) { attendance ->
|
||||||
EnhancedAttendanceCard(attendance = attendance)
|
EnhancedAttendanceCard(attendance = attendance)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,10 +48,3 @@ fun HistoryScreen() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
val sampleAttendanceList = listOf(
|
|
||||||
Attendance("2024-10-10", true, "9:00 AM", "5:00 PM"),
|
|
||||||
Attendance("2024-10-11", false, null, null),
|
|
||||||
Attendance("2024-10-12", true, "9:15 AM", "5:15 PM"),
|
|
||||||
Attendance("2024-10-13", true, "9:45 AM", "7:00 PM")
|
|
||||||
|
|
||||||
)
|
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package `in`.sminnovations.attendanceapp.screens.history.viewModel
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.google.firebase.firestore.FirebaseFirestore
|
||||||
|
import com.google.firebase.firestore.toObject
|
||||||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import `in`.sminnovations.attendanceapp.screens.history.data.AttendanceData
|
||||||
|
import `in`.sminnovations.attendanceapp.screens.login.data.LoginDataStore
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class HistoryViewModel @Inject constructor(
|
||||||
|
private val loginDataStore: LoginDataStore,
|
||||||
|
private val firebaseFirestore: FirebaseFirestore,
|
||||||
|
) : ViewModel() {
|
||||||
|
|
||||||
|
private val _isLoggedIn = MutableStateFlow(false)
|
||||||
|
val isLoggedIn = _isLoggedIn.asStateFlow()
|
||||||
|
|
||||||
|
|
||||||
|
private val _listOfAttendanceDates = MutableStateFlow<List<AttendanceData>>(emptyList())
|
||||||
|
val listOfAttendanceDates = _listOfAttendanceDates.asStateFlow()
|
||||||
|
|
||||||
|
|
||||||
|
init {
|
||||||
|
viewModelScope.launch {
|
||||||
|
getAttendanceData()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun refresh() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
getAttendanceData()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getAttendanceData() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
loginDataStore.loginId.collect { id ->
|
||||||
|
firebaseFirestore.collection("attendance_sath7349").get()
|
||||||
|
.addOnSuccessListener { result ->
|
||||||
|
val attendanceList = result.documents.mapNotNull { document ->
|
||||||
|
document.toObject<AttendanceData>()
|
||||||
|
}
|
||||||
|
_listOfAttendanceDates.value =
|
||||||
|
attendanceList // Update the state flow with new data
|
||||||
|
}.addOnFailureListener { exception ->
|
||||||
|
Log.e("Firestore", "Error getting documents: ", exception)
|
||||||
|
_listOfAttendanceDates.value = emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user