Добавил обновление по свайпу

This commit is contained in:
v228a 2025-02-19 11:35:14 +03:00
parent 9cf6d4c4dc
commit 59d6519f56
4 changed files with 25 additions and 4 deletions

View File

@ -35,6 +35,7 @@ android {
} }
dependencies { dependencies {
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
defaultLibrary() defaultLibrary()
implementation(Dependencies.AndroidX.activity) implementation(Dependencies.AndroidX.activity)

View File

@ -5,6 +5,7 @@ import android.view.View
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import ru.myitschool.work.R import ru.myitschool.work.R
@ -22,11 +23,20 @@ class ProfileFragment : Fragment(R.layout.fragment_profile) {
private val viewModel: ProfileViewModel by viewModels() private val viewModel: ProfileViewModel by viewModels()
private lateinit var swipeRefreshLayout: SwipeRefreshLayout
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
_binding = FragmentProfileBinding.bind(view) _binding = FragmentProfileBinding.bind(view)
initCallback() initCallback()
subscribe() subscribe()
swipeRefreshLayout = binding.swipeRefreshLayout
swipeRefreshLayout.setOnRefreshListener {
viewModel.updateUserInfo()
}
} }
private fun subscribe() { private fun subscribe() {
@ -36,11 +46,15 @@ class ProfileFragment : Fragment(R.layout.fragment_profile) {
binding.loading.visibleOrGone(state is ProfileViewModel.State.Loading) binding.loading.visibleOrGone(state is ProfileViewModel.State.Loading)
when(state) { when(state) {
is ProfileViewModel.State.Loading -> Unit is ProfileViewModel.State.Loading -> {
swipeRefreshLayout.isRefreshing = true
}
is ProfileViewModel.State.Error -> { is ProfileViewModel.State.Error -> {
swipeRefreshLayout.isRefreshing = false
binding.error.text = state.errorText binding.error.text = state.errorText
} }
is ProfileViewModel.State.Show -> { is ProfileViewModel.State.Show -> {
swipeRefreshLayout.isRefreshing = false
binding.fullname.text = state.fullname binding.fullname.text = state.fullname
binding.position.text = state.position binding.position.text = state.position
binding.lastEntry.text = state.lastEntry binding.lastEntry.text = state.lastEntry

View File

@ -52,7 +52,7 @@ class ProfileViewModel @Inject constructor(
} }
} }
private fun updateUserInfo() { fun updateUserInfo() {
viewModelScope.launch { viewModelScope.launch {
_state.update { State.Loading } _state.update { State.Loading }
getUserInfoUseCase.get().invoke().fold( getUserInfoUseCase.get().invoke().fold(

View File

@ -1,7 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:padding="16dp"> android:padding="16dp">
@ -118,3 +123,4 @@
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>