admin viewmodel and fragment

This commit is contained in:
Niktia 2025-02-20 15:49:24 +03:00
parent 3fe72d546f
commit d80a99ade0
4 changed files with 57 additions and 22 deletions

View File

@ -11,5 +11,5 @@ public class BlockUseCase (
private fun getUserFromStorage() : UserDto? {
return authStorageDataSource.userInfo
}
suspend operator fun invoke(value : String) = repo.block(getUserFromStorage()?.login!!)
suspend operator fun invoke() = repo.block(getUserFromStorage()?.login!!)
}

View File

@ -11,5 +11,5 @@ public class UnBlockUseCase (
private fun getUserFromStorage() : UserDto? {
return authStorageDataSource.userInfo
}
suspend operator fun invoke(value : String) = repo.unblock(getUserFromStorage()?.login!!)
suspend operator fun invoke() = repo.unblock(getUserFromStorage()?.login!!)
}

View File

@ -16,7 +16,6 @@ import ru.myitschool.work.utils.collectWithLifecycle
class AdminFragment : Fragment(R.layout.fragment_admin) {
private var _viewBinding: FragmentAdminBinding? = null
private val viewBinding: FragmentAdminBinding get() = _viewBinding!!
private val viewModel by viewModels<AdminViewModel> { AdminViewModel.Factory }
@ -24,23 +23,23 @@ class AdminFragment : Fragment(R.layout.fragment_admin) {
_viewBinding = FragmentAdminBinding.bind(view)
super.onViewCreated(view, savedInstanceState)
/*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
}
}
}*/
//
// 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
// }
// }
//
// }
}
override fun onDestroyView() {

View File

@ -10,12 +10,16 @@ import kotlinx.coroutines.launch
import ru.myitschool.work.data.auth.AuthStorageDataSource
import ru.myitschool.work.data.user.UserNetworkDataSource
import ru.myitschool.work.data.user.UserRepoImpl
import ru.myitschool.work.domain.user.BlockUseCase
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
class AdminViewModel(
private val getUserUseCase: GetUserUseCase
private val getUserUseCase: GetUserUseCase,
private val blockUseCase: BlockUseCase,
private val unBlockUseCase: UnBlockUseCase
) : ViewModel() {
private val _state = MutableStateFlow<State>(State.Loading)
val state = _state.asStateFlow()
@ -52,11 +56,33 @@ class AdminViewModel(
}
)
)
//_state.emit(State.Error("о нет ошибка ошибка помогите"))
_state.emit(State.Error("о нет ошибка ошибка помогите"))
}
}
suspend fun blockUser() {
blockUseCase.invoke().fold(
onSuccess = { data ->
Log.d("uraa", "успех успех ${data.toString()}")
},
onFailure = { error ->
Log.d("kaput", error.message.toString())
State.Error(error.message.toString())
}
)
}
suspend fun unblockUser() {
unBlockUseCase.invoke().fold(
onSuccess = { data ->
Log.d("uraa", "успех успех ${data.toString()}")
},
onFailure = { error ->
Log.d("kaput", error.message.toString())
State.Error(error.message.toString())
}
)
}
sealed interface State {
data object Loading: State
@ -78,6 +104,16 @@ class AdminViewModel(
userNetworkDataSource = UserNetworkDataSource()
),
authStorageDataSource = AuthStorageDataSource
),
blockUseCase = BlockUseCase(repo = UserRepoImpl(
userNetworkDataSource = UserNetworkDataSource()
),
authStorageDataSource = AuthStorageDataSource
),
unBlockUseCase = UnBlockUseCase(repo = UserRepoImpl(
userNetworkDataSource = UserNetworkDataSource()
),
authStorageDataSource = AuthStorageDataSource
)
) as T
}