Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a5041023 authored by George Lin's avatar George Lin
Browse files

Introduce isApplyInProgress data flow

If isApplyInProgress true, isApplyButtonEnabled will be false which
prevent us from 2 cases
1. User clicks the enabled apply button again
2. Discard dialog pops up when on complete and the system setting is yet
   fully complete

Test: Manually tested that discard dialog does not pop up
Bug: 409895694
Flag: com.android.systemui.shared.new_customization_picker_ui
Change-Id: I995b128e575eaa4121d86c51137395a18366e117
parent 4888b97c
Loading
Loading
Loading
Loading
+7 −12
Original line number Diff line number Diff line
@@ -472,10 +472,8 @@ constructor(
                isClockColorEdited ||
                isSliderProgressEdited
        }
    private val onApplyClicked: MutableStateFlow<Boolean> = MutableStateFlow(false)
    val onApply: Flow<(suspend () -> Unit)?> =
        combine(
            onApplyClicked,
            isEdited,
            previewingClock,
            previewingClockSize,
@@ -483,17 +481,15 @@ constructor(
            previewingSliderProgress,
            previewingClockPresetIndexedStyle,
        ) { array ->
            val onApplyClicked: Boolean = array[0] as Boolean
            val isEdited: Boolean = array[1] as Boolean
            val clock: ClockMetadataModel = array[2] as ClockMetadataModel
            val size: ClockSize = array[3] as ClockSize
            val previewingColorId: String = array[4] as String
            val previewProgress: Int = array[5] as Int
            val isEdited: Boolean = array[0] as Boolean
            val clock: ClockMetadataModel = array[1] as ClockMetadataModel
            val size: ClockSize = array[2] as ClockSize
            val previewingColorId: String = array[3] as String
            val previewProgress: Int = array[4] as Int
            val clockAxisStyle: ClockAxisStyle =
                (array[6] as? IndexedStyle)?.style ?: ClockAxisStyle()
            if (isEdited && !onApplyClicked) {
                (array[5] as? IndexedStyle)?.style ?: ClockAxisStyle()
            if (isEdited) {
                {
                    this.onApplyClicked.value = true
                    clockPickerInteractor.applyClock(
                        clockId = clock.clockId,
                        size = size,
@@ -521,7 +517,6 @@ constructor(
        overridingSliderProgress.value = null
        overridingClockPresetIndexedStyle.value = null
        _selectedTab.value = Tab.STYLE
        onApplyClicked.value = false
    }

    suspend fun buildPreviewConfig(previewContext: Context): ClockPreviewConfig {
+6 −11
Original line number Diff line number Diff line
@@ -86,12 +86,6 @@ constructor(
        quickAffordanceInteractor.selections
            .map { it.groupBy { selectionModel -> selectionModel.slotId } }
            .shareIn(viewModelScope, SharingStarted.WhileSubscribed(), 1)
    // optimisticUpdateQuickAffordances updates right after applying button is clicked, while the
    // actual update of selectedQuickAffordancesGroupBySlotId later updates until the system
    // completes the update task. This can make sure the apply button state updates before we return
    // to the previous screen.
    private val optimisticUpdateQuickAffordances: MutableStateFlow<Map<String, String>?> =
        MutableStateFlow(null)

    val previewingQuickAffordances =
        combine(
@@ -265,18 +259,19 @@ constructor(
        }

    val onApply: Flow<(suspend () -> Unit)?> =
        combine(overridingQuickAffordances, optimisticUpdateQuickAffordances) {
        combine(overridingQuickAffordances, selectedQuickAffordancesGroupBySlotId) {
            overridingQuickAffordances,
            optimisticUpdateQuickAffordances ->
            selectedQuickAffordancesGroupBySlotId ->
            // If all overridingQuickAffordances is empty or are same as the
            // optimisticUpdateQuickAffordances, it is not yet edited
            // selectedQuickAffordancesGroupBySlotId, it is not yet edited
            val isQuickAffordancesEdited =
                (!overridingQuickAffordances.all { (slotId, overridingQuickAffordanceId) ->
                    optimisticUpdateQuickAffordances?.get(slotId) == overridingQuickAffordanceId
                    selectedQuickAffordancesGroupBySlotId[slotId]
                        ?.map { it.affordanceId }
                        ?.contains(overridingQuickAffordanceId) ?: false
                })
            if (isQuickAffordancesEdited) {
                {
                    this.optimisticUpdateQuickAffordances.value = overridingQuickAffordances
                    overridingQuickAffordances.forEach { entry ->
                        val slotId = entry.key
                        val affordanceId = entry.value
+8 −3
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
@@ -167,7 +168,7 @@ constructor(
                null
            }
        }

    private val isApplyInProgress: MutableStateFlow<Boolean> = MutableStateFlow(false)
    @OptIn(ExperimentalCoroutinesApi::class)
    val onApplyButtonClicked: Flow<((onComplete: () -> Unit) -> Unit)?> =
        selectedOption
@@ -204,8 +205,10 @@ constructor(
                        if (onApplyJob?.isActive != true) {
                            onApplyJob =
                                viewModelScope.launch {
                                    isApplyInProgress.value = true
                                    onApply()
                                    onComplete()
                                    isApplyInProgress.value = false
                                    onApplyJob = null
                                }
                        }
@@ -217,8 +220,10 @@ constructor(
            .stateIn(viewModelScope, SharingStarted.Eagerly, null)

    val isApplyButtonEnabled: StateFlow<Boolean> =
        onApplyButtonClicked
            .map { it != null }
        combine(isApplyInProgress, onApplyButtonClicked) { isApplyInProgress, onApplyButtonClicked
                ->
                !isApplyInProgress && onApplyButtonClicked != null
            }
            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), false)

    val isApplyButtonVisible: Flow<Boolean> = selectedOption.map { it != null }