mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	refactor: replace LiveData with StateFlow
This commit is contained in:
		
							parent
							
								
									8af59a7cee
								
							
						
					
					
						commit
						001a23683a
					
				
					 2 changed files with 33 additions and 11 deletions
				
			
		|  | @ -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. | ||||||
|  |  | ||||||
|  | @ -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 -> | ||||||
|  |             onChangeSwitchState(isChecked) | ||||||
|  |             _switchState.value = isChecked | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|         imageAdapter.currentImagesCountLiveData.observe(viewLifecycleOwner, Observer { |         viewLifecycleOwner.lifecycleScope.launch { | ||||||
|             if (switch?.isChecked == false && it == 0 && switch?.isVisible == true) { |             repeatOnLifecycle(Lifecycle.State.STARTED) { | ||||||
|                 binding?.allImagesUploadedOrMarked?.isVisible = true |                 combine( | ||||||
|             } else { |                     imageAdapter.currentImagesCount, | ||||||
|                 binding?.allImagesUploadedOrMarked?.isVisible = false |                     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 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 yuvraj-coder1
						yuvraj-coder1