adminFragment

This commit is contained in:
Niktia 2025-02-20 16:25:58 +03:00
parent b8147eff8b
commit 1af9aad25b
6 changed files with 88 additions and 35 deletions

View File

@ -10,7 +10,8 @@ class GetUserUseCase(
private fun getUserFromStorage() : UserDto? {
return authStorageDataSource.userInfo
}
suspend operator fun invoke() = repo.getUser(getUserFromStorage()?.login!!)
suspend operator fun invoke(user_login : String?) = repo.getUser(user_login
?: getUserFromStorage()?.login!!)
suspend fun getEntrancesList() = repo.getEntrancesList(getUserFromStorage()?.login!!)
}

View File

@ -1,5 +1,6 @@
package ru.myitschool.work.ui.admin
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
@ -12,6 +13,7 @@ import ru.myitschool.work.databinding.FragmentAdminBinding
import ru.myitschool.work.databinding.FragmentProfileBinding
import ru.myitschool.work.ui.profile.ProfileViewModel
import ru.myitschool.work.utils.collectWithLifecycle
import ru.sicampus.bootcamp2025.ui.centerList.EntranceAdapter
class AdminFragment : Fragment(R.layout.fragment_admin) {
private var _viewBinding: FragmentAdminBinding? = null
@ -19,27 +21,47 @@ class AdminFragment : Fragment(R.layout.fragment_admin) {
private val viewModel by viewModels<AdminViewModel> { AdminViewModel.Factory }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_viewBinding = FragmentAdminBinding.bind(view)
super.onViewCreated(view, savedInstanceState)
val adapter = EntranceAdapter()
viewBinding.recyclerView.adapter = adapter
//
// viewModel.state.collectWithLifecycle(this) { state ->
//
// viewBinding.error.visibility = if (state is AdminViewModel.State.Error) View.VISIBLE else View.GONE
//
//
// when(state) {
// is AdminViewModel.State.Loading -> Unit
// is AdminViewModel.State.GoToInfo -> {
// findNavController().navigate()
// }
// is ProfileViewModel.State.Error -> {
// viewBinding.errorText.text = state.text
// }
// }
//
// }
viewBinding.findButton.setOnClickListener {
val login = viewBinding.userLogin.text.toString()
viewModel.search(login)
}
viewModel.state.collectWithLifecycle(this) { state ->
viewBinding.error.visibility = if (state is AdminViewModel.State.Error) View.VISIBLE else View.GONE
when(state) {
is AdminViewModel.State.Loading -> Unit
is AdminViewModel.State.Show -> {
viewBinding.noData.visibility = View.GONE
viewBinding.name.text = "Должность: ${state.profileInfo.position}"
if (state.profileInfo.lastEntry == null) viewBinding.lastEntry.text = "Время последнего входа: Нет данных"
else viewBinding.lastEntry.text = "Время последнего входа: ${state.profileInfo.lastEntry}"
Picasso.get().load(state.profileInfo.avatarUrl).resize(100, 100).centerCrop().into(viewBinding.imageView)
if (state.entrancesList.isEmpty()) {
viewBinding.noData.visibility = View.VISIBLE
}
else {
viewBinding.recyclerView.visibility = View.VISIBLE
adapter.submitList(state.entrancesList)
}
}
is AdminViewModel.State.Error -> {
viewBinding.errorText.text = state.text
}
}
}
}
override fun onDestroyView() {

View File

@ -15,6 +15,7 @@ import ru.myitschool.work.domain.user.EntranceEntity
import ru.myitschool.work.domain.user.GetUserUseCase
import ru.myitschool.work.domain.user.UnBlockUseCase
import ru.myitschool.work.domain.user.UserEntity
import ru.myitschool.work.ui.profile.ProfileViewModel.State
class AdminViewModel(
private val getUserUseCase: GetUserUseCase,
@ -24,15 +25,9 @@ class AdminViewModel(
private val _state = MutableStateFlow<State>(State.Loading)
val state = _state.asStateFlow()
init {
updateStateGet()
}
fun clickRefresh() {
updateStateGet()
}
fun updateStateGet() {
fun search(
login : String,
) {
viewModelScope.launch {
_state.emit(State.Loading)
val entranceList : List<EntranceEntity> = getUserUseCase.getEntrancesList().fold(
@ -44,22 +39,54 @@ class AdminViewModel(
}
)
_state.emit(
getUserUseCase.invoke().fold(
getUserUseCase.invoke(login).fold(
onSuccess = { data ->
Log.d("uraa", "успех успех ${data.toString()}")
State.GoToInfo(data, entranceList)
State.Show(data, entranceList)
},
onFailure = { error ->
Log.d("kaput", error.message.toString())
State.Error(error.message.toString())
}
) as State
)
)
_state.emit(State.Error("о нет ошибка ошибка помогите"))
_state.emit(State.Error("Не удалось загрузить профиль"))
}
}
// fun clickRefresh() {
// updateStateGet()
// }
// fun updateStateGet() {
// viewModelScope.launch {
// _state.emit(State.Loading)
// val entranceList : List<EntranceEntity> = getUserUseCase.getEntrancesList().fold(
// onSuccess = { list ->
// list
// },
// onFailure = {
// emptyList()
// }
// )
// _state.emit(
// getUserUseCase.invoke().fold(
// onSuccess = { data ->
// Log.d("uraa", "успех успех ${data.toString()}")
//
// State.Show(data, entranceList)
// },
// onFailure = { error ->
// Log.d("kaput", error.message.toString())
// State.Error(error.message.toString())
// }
// ) as State
// )
// _state.emit(State.Error("о нет ошибка ошибка помогите"))
// }
// }
suspend fun blockUser() {
blockUseCase.invoke().fold(
onSuccess = { data ->
@ -86,8 +113,8 @@ class AdminViewModel(
sealed interface State {
data object Loading: State
data class GoToInfo(
val profileInfo: UserEntity,
data class Show(
val profileInfo : UserEntity,
val entrancesList : List<EntranceEntity>
) : State
data class Error(

View File

@ -1,5 +1,6 @@
package ru.myitschool.work.ui.profile
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log

View File

@ -41,7 +41,7 @@ class ProfileViewModel(
}
)
_state.emit(
getUserUseCase.invoke().fold(
getUserUseCase.invoke(null).fold(
onSuccess = { data ->
Log.d("uraa", "успех успех ${data.toString()}")
State.Show(data, entranceList)

View File

@ -34,7 +34,9 @@
tools:layout="@layout/fragment_qr_result" >
<action
android:id="@+id/action_fragment_qr_result_to_fragment_profile"
app:destination="@id/fragment_profile" />
app:destination="@id/fragment_profile"
app:popUpTo="@id/fragment_qr_result"
app:popUpToInclusive="true"/>
</fragment>