fix: paging fixed

This commit is contained in:
a1pha 2025-02-20 18:18:57 +03:00
parent c3adf16666
commit aba81f744f
4 changed files with 61 additions and 39 deletions

View File

@ -1,14 +1,14 @@
package ru.myitschool.work.ui.admin.search
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
@ -38,17 +38,18 @@ class AdminViewModel(
private val _state = MutableStateFlow<State>(State.Waiting)
val state = _state.asStateFlow()
// private var _listState: Flow<PagingData<PassEntity>>? = null
// val listState: Flow<PagingData<PassEntity>> get() = _listState!!
private val _listState = MutableLiveData<Pager<Int, PassEntity>>()
val listState = _listState.value?.flow?.cachedIn(viewModelScope)
private var currentLogin: String = "pivanov"
private val _currentLogin = MutableLiveData<String>()
private val currentLogin: LiveData<String> get() = _currentLogin
fun onFind(login: String) {
viewModelScope.launch {
_state.emit(State.Loading)
getUserByLoginUseCase(login).fold(
onSuccess = { data ->
currentLogin = login
_currentLogin.postValue(login)
_state.emit(State.Show(data))
setUpPager(login)
},
@ -64,7 +65,7 @@ class AdminViewModel(
private fun updateState() {
viewModelScope.launch {
_state.emit(State.Loading)
getUserByLoginUseCase(currentLogin!!).fold(
getUserByLoginUseCase(currentLogin.value!!).fold(
onSuccess = { _state.emit(State.Show(it)) },
onFailure = { _state.emit(State.Error(it.message.toString())) }
)
@ -72,27 +73,26 @@ class AdminViewModel(
}
private fun setUpPager(login: String) {
// _listState = Pager(
// config = PagingConfig(
// pageSize = 10,
// enablePlaceholders = false,
// maxSize = 50
// )
// ) {
// UsersPassesPagingSource(getUsersPassesUseCase::invoke, login)
// }.flow
// .cachedIn(viewModelScope)
_listState.value = Pager(
config = PagingConfig(
pageSize = 10,
enablePlaceholders = false,
maxSize = 50
)
) {
UsersPassesPagingSource(getUsersPassesUseCase::invoke, login)
}
}
fun onBlock() {
viewModelScope.launch {
blockUserUseCase(currentLogin!!)
blockUserUseCase(currentLogin.value!!)
}
}
fun unblock() {
viewModelScope.launch {
unBlockUserUseCase(currentLogin!!)
unBlockUserUseCase(currentLogin.value!!)
}
}

View File

@ -27,6 +27,9 @@ class ViewUserAsAdminFragment : Fragment(R.layout.fragment_user) {
binding.findUser.visibleOrGone(false)
binding.logout.visibleOrGone(false)
val adapter = PassesListAdapter()
binding.passes.adapter = adapter
viewModel.state.collectWithLifecycle(this) { state ->
binding.refresh.isRefreshing = state is AdminViewModel.State.Loading
binding.content.visibleOrGone(state is AdminViewModel.State.Show)
@ -48,23 +51,23 @@ class ViewUserAsAdminFragment : Fragment(R.layout.fragment_user) {
}
}
// viewModel.listState.collectWithLifecycle(this) { listState ->
// adapter.submitData(listState)
// }
viewModel.listState?.collectWithLifecycle(this) { listState ->
adapter.submitData(listState)
}
// adapter.loadStateFlow.collectWithLifecycle(this) { data ->
// val dataState = data.refresh
// binding.refresh.isRefreshing = dataState is LoadState.Loading
// binding.error.visibleOrGone(dataState is LoadState.Error)
//
// if (dataState is LoadState.Error) {
// binding.error.text = dataState.error.toString()
// }
// }
adapter.loadStateFlow.collectWithLifecycle(this) { data ->
val dataState = data.refresh
binding.refresh.isRefreshing = dataState is LoadState.Loading
binding.error.visibleOrGone(dataState is LoadState.Error)
if (dataState is LoadState.Error) {
binding.error.text = dataState.error.toString()
}
}
binding.refresh.setOnRefreshListener {
viewModel.onRefresh()
// adapter.refresh()
adapter.refresh()
}

View File

@ -1,17 +1,15 @@
package ru.myitschool.work.ui.profile
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import ru.myitschool.work.databinding.PassItemBinding
import ru.myitschool.work.domain.entities.PassEntity
class PassesListAdapter:
ListAdapter<PassEntity, PassesListAdapter.ViewHolder>(CenterDiff) {
class PassesListAdapter :
PagingDataAdapter<PassEntity, PassesListAdapter.ViewHolder>(CenterDiff) {
class ViewHolder(
private val binding: PassItemBinding,
@ -40,6 +38,12 @@ class PassesListAdapter:
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(getItem(position))
holder.bind(
getItem(position) ?: PassEntity(
type = "Loading...",
name = "Loading...",
time = "Loading..."
)
)
}
}

View File

@ -7,6 +7,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.paging.LoadState
import com.squareup.picasso.Picasso
import ru.myitschool.work.R
import ru.myitschool.work.databinding.FragmentUserBinding
@ -36,20 +37,34 @@ class UserFragment : Fragment(R.layout.fragment_user) {
is UserViewModel.State.Loading -> Unit
is UserViewModel.State.Show -> {
val user = state.userEntity
adapter.submitList(state.passes)
binding.scan.visibleOrGone(!user.isCardBlocked)
binding.findUser.visibleOrGone(user.isAdmin)
binding.fullname.text = user.name
binding.position.text = user.position
binding.lastEntry.text = user.lastVisit
Picasso.get().load(user.photoUrl).into(binding.photo)
}
}
}
viewModel.listState.collectWithLifecycle(this) { listState ->
adapter.submitData(listState)
}
adapter.loadStateFlow.collectWithLifecycle(this) { data ->
Log.d("info", data.refresh.toString())
val dataState = data.refresh
binding.refresh.isRefreshing = dataState is LoadState.Loading
binding.error.visibleOrGone(dataState is LoadState.Error)
if (dataState is LoadState.Error) {
binding.error.text = dataState.error.toString()
}
}
binding.refresh.setOnRefreshListener {
viewModel.onRefresh()
adapter.refresh()
}
binding.logout.setOnClickListener {