fix: Исправление форматирования даты, и вывод ее в список

This commit is contained in:
yastruckov 2025-02-19 18:46:28 +03:00
parent f8bc61d090
commit b839938529
4 changed files with 87 additions and 27 deletions

View File

@ -20,6 +20,7 @@ import ru.myitschool.work.ui.qr.scan.QrScanDestination
import ru.myitschool.work.utils.UserState
import ru.myitschool.work.utils.collectWhenStarted
import ru.myitschool.work.utils.collectWithLifecycle
import ru.myitschool.work.utils.dateTimeConverter
class MainFragment : Fragment(R.layout.fragment_main) {
@ -34,6 +35,7 @@ class MainFragment : Fragment(R.layout.fragment_main) {
_binding = FragmentMainBinding.bind(view)
viewModel.getUserData()
viewModel.getLastEntryDate()
binding.logout.setOnClickListener { logout() }
binding.scan.setOnClickListener { onScanClick() }
@ -56,7 +58,30 @@ class MainFragment : Fragment(R.layout.fragment_main) {
binding.loading.visibility = View.GONE
binding.error.visibility = View.GONE
showUserData(state.employeeEntity)
}
}
}
viewModel.dateState.collectWhenStarted(this) { state ->
println(state)
when (state) {
is MainViewModel.DateState.Error -> {
binding.error.visibility = View.VISIBLE
binding.error.text = state.message
binding.loading.visibility = View.GONE
binding.content.visibility = View.GONE
}
is MainViewModel.DateState.Loading -> {
binding.error.visibility = View.GONE
binding.loading.visibility = View.VISIBLE
binding.lastEntry.visibility = View.GONE
binding.content.visibility = View.GONE
}
is MainViewModel.DateState.Success -> {
binding.error.visibility = View.GONE
binding.loading.visibility = View.GONE
binding.lastEntry.text = dateTimeConverter(state.data.scanTime)
binding.lastEntry.visibility = View.VISIBLE
binding.content.visibility = View.VISIBLE
}
}
}
@ -65,11 +90,13 @@ class MainFragment : Fragment(R.layout.fragment_main) {
binding.refresh.setOnClickListener {
viewModel.getUserData()
adapter.refresh()
viewModel.getLastEntryDate()
}
binding.content.adapter = adapter
viewModel.listState.collectWithLifecycle(this) { data ->
adapter.submitData(data)
}
adapter.loadStateFlow.collectWithLifecycle(this) { loadState ->
val state = loadState.refresh
binding.error.visibility = if (state is LoadState.Error) View.VISIBLE else View.GONE
@ -88,10 +115,7 @@ class MainFragment : Fragment(R.layout.fragment_main) {
findNavController().navigate(R.id.qrResultFragment, bundleToQrResult)
}
}
private fun logout() {
lifecycleScope.launch {
viewModel.clearUsername()
@ -100,8 +124,6 @@ class MainFragment : Fragment(R.layout.fragment_main) {
}
}
private fun showUserData(employeeEntity: EmployeeEntity) {
binding.apply {
fullname.text = employeeEntity.name

View File

@ -19,15 +19,24 @@ import ru.myitschool.work.data.profile.ProfileNetworkDataSource
import ru.myitschool.work.data.profile.ProfileRepoImpl
import ru.myitschool.work.data.entrance.employeeEntrances.EmployeeEntranceListNetworkDataSource
import ru.myitschool.work.data.entrance.employeeEntrances.EmployeeEntranceListRepoImpl
import ru.myitschool.work.data.entrance.lastEntrance.LastEntranceNetworkDataSource
import ru.myitschool.work.data.entrance.lastEntrance.LastEntranceRepoImpl
import ru.myitschool.work.domain.profile.GetProfileUseCase
import ru.myitschool.work.domain.employeeEntrance.employeeEntrances.GetEmployeeEntranceListUseCase
import ru.myitschool.work.domain.employeeEntrance.lastEntrance.GetLastEntranceUseCase
import ru.myitschool.work.entities.EmployeeEntranceEntity
import ru.myitschool.work.utils.UserState
class MainViewModel(
private val infoUseCase: GetProfileUseCase,
private val listUseCase: GetEmployeeEntranceListUseCase,
private val lastEntranceUseCase: GetLastEntranceUseCase,
application: Application
) : AndroidViewModel(application) {
private val _userState = MutableStateFlow<UserState>(UserState.Loading)
val userState: StateFlow<UserState> get() = _userState
private val _dateState = MutableStateFlow<DateState>(DateState.Loading)
val dateState: StateFlow<DateState> get() = _dateState
val listState = Pager(
config = PagingConfig(
@ -36,23 +45,14 @@ class MainViewModel(
maxSize = 30
)
) {
println("Creating PagingSource")
EmployeeEntranceListPagingSource(listUseCase::invoke)
}.flow.cachedIn(viewModelScope)
init {
viewModelScope.launch {
listState.collect { pagingData ->
if (pagingData.toString().isEmpty()) {
println("No data in paging data.")
} else {
println("Data received: $pagingData")
}
}
}
}
private val _userState = MutableStateFlow<UserState>(UserState.Loading)
val userState: StateFlow<UserState> get() = _userState
sealed class DateState {
data object Loading : DateState()
data class Success(val data : EmployeeEntranceEntity) : DateState()
data class Error(val message: String?) : DateState()
}
private val dataStoreManager = UserDataStoreManager(application)
@ -66,6 +66,19 @@ class MainViewModel(
)
}
}
fun getLastEntryDate(){
_dateState.value = DateState.Loading
viewModelScope.launch {
lastEntranceUseCase.invoke().fold(
onSuccess = { data ->
_dateState.value = DateState.Success(data)
},
onFailure = { e ->
_dateState.value = DateState.Error(e.message)
}
)
}
}
fun clearUsername() {
viewModelScope.launch{
@ -89,12 +102,21 @@ class MainViewModel(
context = extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as Application
)
)
val lastEntranceRepoImpl = LastEntranceRepoImpl(
networkDataSource = LastEntranceNetworkDataSource(
context = extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as Application
)
)
val infoUseCase = GetProfileUseCase(profileRepoImpl)
val listUseCase = GetEmployeeEntranceListUseCase(listInfoImpl)
val lastEntranceUseCase = GetLastEntranceUseCase(lastEntranceRepoImpl)
return MainViewModel(
infoUseCase, listUseCase, extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as Application
infoUseCase,
listUseCase,
lastEntranceUseCase,
extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as Application
) as T
}
}

View File

@ -1,15 +1,31 @@
package ru.myitschool.work.utils
import java.text.SimpleDateFormat
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Date
import java.util.Locale
fun dateConverter(date: Date?) : String {
fun dateTimeConverter(date: Date?) : String {
if (date != null) {
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
println(dateFormat.format(date).toString())
val dateFormat = SimpleDateFormat("HH:mm • yyyy-MM-dd", Locale.getDefault())
return dateFormat.format(date).toString()
}
return ""
}
}
fun monthConverter(date: Date?) : String {
if (date != null) {
val russianLocale = Locale("ru", "RU", "variant")
val localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
val formatter = DateTimeFormatter.ofPattern("d MMMM yyyy", russianLocale)
return localDate.format(formatter)
}
return ""
}
fun timeConverter(date: Date?) : String {
if (date != null) {
val dateFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
return dateFormat.format(date).toString()
}
return ""
}

View File

@ -11,7 +11,7 @@ import java.util.Date
import java.util.Locale
object DateSerializer : KSerializer<Date> {
private val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US)
private val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm", Locale.US)
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Date", PrimitiveKind.STRING)