@@ -21,6 +21,7 @@ import android.os.Bundle
2121import android.util.Log
2222import android.view.View
2323import android.widget.Toast
24+ import androidx.annotation.Keep
2425import androidx.core.content.ContextCompat
2526import androidx.fragment.app.Fragment
2627import androidx.fragment.app.activityViewModels
@@ -59,10 +60,12 @@ import com.google.android.samples.dynamicfeatures.state.UpdateViewModel
5960import com.google.android.samples.dynamicfeatures.state.UpdateViewModelProviderFactory
6061import kotlinx.coroutines.ExperimentalCoroutinesApi
6162import kotlinx.coroutines.flow.collect
63+ import kotlinx.coroutines.flow.drop
6264import kotlinx.coroutines.flow.launchIn
6365import kotlinx.coroutines.flow.onEach
6466
6567@OptIn(ExperimentalCoroutinesApi ::class )
68+ @Keep
6669class MainFragment : Fragment (R .layout.fragment_main) {
6770
6871 private var bindings: FragmentMainBinding ? = null
@@ -110,27 +113,31 @@ class MainFragment : Fragment(R.layout.fragment_main) {
110113 val colorBackgroundOff = ContextCompat .getColor(requireContext(), R .color.background)
111114 val drawable = btnToggleLight.drawable as AnimatedVectorDrawable
112115 viewLifecycleOwner.lifecycleScope.launchWhenResumed {
113- colorViewModel.lightsOn.collect { lightsOn: Boolean ->
114- if (lightsOn) {
115- // The user has turned on the flashlight.
116- drawable.start()
117- view.setBackgroundColor(colorViewModel.backgroundColor.value)
118- } else {
119- // The user has turned off the flashlight. Reset the icon and color:
120- drawable.reset()
121- view.setBackgroundColor(colorBackgroundOff)
122- // then check if the color was picked from a photo,
123- // and launch a review if yes:
124- if (colorViewModel.colorWasPicked) {
125- colorViewModel.notifyPickedColorConsumed()
126- val reviewInfo = reviewViewModel.obtainReviewInfo()
127- if (reviewInfo != null ) {
128- reviewManager.launchReview(requireActivity(), reviewInfo)
129- reviewViewModel.notifyAskedForReview()
116+ colorViewModel.lightsOn
117+ .onEach { lightsOn: Boolean ->
118+ if (lightsOn) {
119+ // The user has turned on the flashlight.
120+ drawable.start()
121+ view.setBackgroundColor(colorViewModel.backgroundColor.value)
122+ } else {
123+ // The user has turned off the flashlight. Reset the icon and color:
124+ drawable.reset()
125+ view.setBackgroundColor(colorBackgroundOff)
126+ }
127+ }
128+ .drop(1 ) // for launching the review ignore the starting value
129+ .collect { lightsOn ->
130+ // Check if the color was picked from a photo,
131+ // and launch a review if yes:
132+ if (! lightsOn && colorViewModel.colorWasPicked) {
133+ colorViewModel.notifyPickedColorConsumed()
134+ val reviewInfo = reviewViewModel.obtainReviewInfo()
135+ if (reviewInfo != null ) {
136+ reviewManager.launchReview(requireActivity(), reviewInfo)
137+ reviewViewModel.notifyAskedForReview()
138+ }
130139 }
131140 }
132- }
133- }
134141 }
135142 }
136143
0 commit comments