refactor: replace LiveData with StateFlow

This commit is contained in:
yuvraj-coder1 2025-01-03 22:50:43 +05:30
parent 8af59a7cee
commit 001a23683a
2 changed files with 33 additions and 11 deletions

View file

@ -7,8 +7,6 @@ import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.Toast import android.widget.Toast
import androidx.constraintlayout.widget.Group import androidx.constraintlayout.widget.Group
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
@ -26,6 +24,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.util.TreeMap import java.util.TreeMap
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
@ -108,8 +107,8 @@ class ImageAdapter(
/** /**
* Stores the number of images currently visible on the screen * Stores the number of images currently visible on the screen
*/ */
private val _currentImagesCount = MutableLiveData(0) private val _currentImagesCount = MutableStateFlow(0)
val currentImagesCountLiveData: LiveData<Int> = _currentImagesCount val currentImagesCount = _currentImagesCount
/** /**
* Coroutine Dispatchers and Scope. * Coroutine Dispatchers and Scope.

View file

@ -13,8 +13,11 @@ import android.widget.Switch
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import fr.free.nrw.commons.contributions.Contribution import fr.free.nrw.commons.contributions.Contribution
@ -39,6 +42,10 @@ import fr.free.nrw.commons.theme.BaseActivity
import fr.free.nrw.commons.upload.FileProcessor import fr.free.nrw.commons.upload.FileProcessor
import fr.free.nrw.commons.upload.FileUtilsWrapper import fr.free.nrw.commons.upload.FileUtilsWrapper
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import java.util.TreeMap import java.util.TreeMap
import javax.inject.Inject import javax.inject.Inject
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
@ -81,6 +88,12 @@ class ImageFragment :
*/ */
var allImages: ArrayList<Image> = ArrayList() var allImages: ArrayList<Image> = ArrayList()
/**
* Keeps track of switch state
*/
private val _switchState = MutableStateFlow(false)
val switchState = _switchState.asStateFlow()
/** /**
* View model Factory. * View model Factory.
*/ */
@ -215,15 +228,25 @@ class ImageFragment :
switch = binding?.switchWidget switch = binding?.switchWidget
switch?.visibility = View.VISIBLE switch?.visibility = View.VISIBLE
switch?.setOnCheckedChangeListener { _, isChecked -> onChangeSwitchState(isChecked) } _switchState.value = switch?.isChecked ?: false
switch?.setOnCheckedChangeListener { _, isChecked ->
imageAdapter.currentImagesCountLiveData.observe(viewLifecycleOwner, Observer { onChangeSwitchState(isChecked)
if (switch?.isChecked == false && it == 0 && switch?.isVisible == true) { _switchState.value = isChecked
binding?.allImagesUploadedOrMarked?.isVisible = true }
} else {
binding?.allImagesUploadedOrMarked?.isVisible = false viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
combine(
imageAdapter.currentImagesCount,
switchState
) { imageCount, isChecked ->
imageCount to isChecked
}.collect { (imageCount, isChecked) ->
binding?.allImagesUploadedOrMarked?.isVisible =
!isChecked && imageCount == 0 && (switch?.isVisible == true)
}
}
} }
})
selectorRV = binding?.selectorRv selectorRV = binding?.selectorRv
loader = binding?.loader loader = binding?.loader