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? { private fun getUserFromStorage() : UserDto? {
return authStorageDataSource.userInfo 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!!) suspend fun getEntrancesList() = repo.getEntrancesList(getUserFromStorage()?.login!!)
} }

View File

@ -1,5 +1,6 @@
package ru.myitschool.work.ui.admin package ru.myitschool.work.ui.admin
import android.annotation.SuppressLint
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.fragment.app.Fragment 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.databinding.FragmentProfileBinding
import ru.myitschool.work.ui.profile.ProfileViewModel import ru.myitschool.work.ui.profile.ProfileViewModel
import ru.myitschool.work.utils.collectWithLifecycle import ru.myitschool.work.utils.collectWithLifecycle
import ru.sicampus.bootcamp2025.ui.centerList.EntranceAdapter
class AdminFragment : Fragment(R.layout.fragment_admin) { class AdminFragment : Fragment(R.layout.fragment_admin) {
private var _viewBinding: FragmentAdminBinding? = null private var _viewBinding: FragmentAdminBinding? = null
@ -19,27 +21,47 @@ class AdminFragment : Fragment(R.layout.fragment_admin) {
private val viewModel by viewModels<AdminViewModel> { AdminViewModel.Factory } private val viewModel by viewModels<AdminViewModel> { AdminViewModel.Factory }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_viewBinding = FragmentAdminBinding.bind(view) _viewBinding = FragmentAdminBinding.bind(view)
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
val adapter = EntranceAdapter()
viewBinding.recyclerView.adapter = adapter
// viewBinding.findButton.setOnClickListener {
// viewModel.state.collectWithLifecycle(this) { state -> val login = viewBinding.userLogin.text.toString()
// viewModel.search(login)
// viewBinding.error.visibility = if (state is AdminViewModel.State.Error) View.VISIBLE else View.GONE }
//
//
// when(state) { viewModel.state.collectWithLifecycle(this) { state ->
// is AdminViewModel.State.Loading -> Unit
// is AdminViewModel.State.GoToInfo -> { viewBinding.error.visibility = if (state is AdminViewModel.State.Error) View.VISIBLE else View.GONE
// findNavController().navigate()
// }
// is ProfileViewModel.State.Error -> { when(state) {
// viewBinding.errorText.text = state.text 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() { 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.GetUserUseCase
import ru.myitschool.work.domain.user.UnBlockUseCase import ru.myitschool.work.domain.user.UnBlockUseCase
import ru.myitschool.work.domain.user.UserEntity import ru.myitschool.work.domain.user.UserEntity
import ru.myitschool.work.ui.profile.ProfileViewModel.State
class AdminViewModel( class AdminViewModel(
private val getUserUseCase: GetUserUseCase, private val getUserUseCase: GetUserUseCase,
@ -24,15 +25,9 @@ class AdminViewModel(
private val _state = MutableStateFlow<State>(State.Loading) private val _state = MutableStateFlow<State>(State.Loading)
val state = _state.asStateFlow() val state = _state.asStateFlow()
init { fun search(
updateStateGet() login : String,
} ) {
fun clickRefresh() {
updateStateGet()
}
fun updateStateGet() {
viewModelScope.launch { viewModelScope.launch {
_state.emit(State.Loading) _state.emit(State.Loading)
val entranceList : List<EntranceEntity> = getUserUseCase.getEntrancesList().fold( val entranceList : List<EntranceEntity> = getUserUseCase.getEntrancesList().fold(
@ -44,22 +39,54 @@ class AdminViewModel(
} }
) )
_state.emit( _state.emit(
getUserUseCase.invoke().fold( getUserUseCase.invoke(login).fold(
onSuccess = { data -> onSuccess = { data ->
Log.d("uraa", "успех успех ${data.toString()}") Log.d("uraa", "успех успех ${data.toString()}")
State.GoToInfo(data, entranceList) State.Show(data, entranceList)
}, },
onFailure = { error -> onFailure = { error ->
Log.d("kaput", error.message.toString()) Log.d("kaput", error.message.toString())
State.Error(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() { suspend fun blockUser() {
blockUseCase.invoke().fold( blockUseCase.invoke().fold(
onSuccess = { data -> onSuccess = { data ->
@ -86,8 +113,8 @@ class AdminViewModel(
sealed interface State { sealed interface State {
data object Loading: State data object Loading: State
data class GoToInfo( data class Show(
val profileInfo: UserEntity, val profileInfo : UserEntity,
val entrancesList : List<EntranceEntity> val entrancesList : List<EntranceEntity>
) : State ) : State
data class Error( data class Error(

View File

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

View File

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

View File

@ -34,7 +34,9 @@
tools:layout="@layout/fragment_qr_result" > tools:layout="@layout/fragment_qr_result" >
<action <action
android:id="@+id/action_fragment_qr_result_to_fragment_profile" 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> </fragment>