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

Commit 882c3deb authored by George Lin's avatar George Lin
Browse files

Fix grid screen options (2/2)

Do the following measures to avoid too many unnecessary emits of data
1. Make data flow shared
2. Make state flow lazily emit

Test: Made sure that the data flow emit only necessary data
Bug: 362237825
Flag: com.android.systemui.shared.new_customization_picker_ui
Change-Id: I23026c83412d2f2a7b20a808c9bab943affdb4fc
parent 065184fb
Loading
Loading
Loading
Loading
+23 −27
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn

class ShapeAndGridPickerViewModel
@@ -47,33 +48,34 @@ constructor(
) {
    // The currently-set system grid option
    val selectedGridOption =
        interactor.selectedGridOption.filterNotNull().map { toOptionItemViewModel(it) }
    private val _previewingGridOptionKey = MutableStateFlow<String?>(null)
        interactor.selectedGridOption
            .filterNotNull()
            .map { toOptionItemViewModel(it) }
            .shareIn(scope = viewModelScope, started = SharingStarted.Lazily, replay = 1)
    private val overridingGridOptionKey = MutableStateFlow<String?>(null)
    // If the previewing key is null, use the currently-set system grid option
    val previewingGridOptionKey =
        combine(selectedGridOption, _previewingGridOptionKey) {
            currentlySetGridOption,
            previewingGridOptionKey ->
            previewingGridOptionKey ?: currentlySetGridOption.key.value
        combine(overridingGridOptionKey, selectedGridOption) {
            overridingGridOptionKey,
            selectedGridOption ->
            overridingGridOptionKey ?: selectedGridOption.key.value
        }

    fun resetPreview() {
        _previewingGridOptionKey.tryEmit(null)
        overridingGridOptionKey.value = null
    }

    val optionItems: Flow<List<OptionItemViewModel<GridIconViewModel>>> =
        interactor.gridOptions.filterNotNull().map { gridOptions ->
            gridOptions.map { toOptionItemViewModel(it) }
        }
        interactor.gridOptions
            .filterNotNull()
            .map { gridOptions -> gridOptions.map { toOptionItemViewModel(it) } }
            .shareIn(scope = viewModelScope, started = SharingStarted.Lazily, replay = 1)

    val onApply: Flow<(suspend () -> Unit)?> =
        combine(selectedGridOption, _previewingGridOptionKey) {
            selectedGridOption,
            previewingGridOptionKey ->
            if (
                previewingGridOptionKey == null ||
                    previewingGridOptionKey == selectedGridOption.key.value
            ) {
        combine(previewingGridOptionKey, selectedGridOption) {
            previewingGridOptionKey,
            selectedGridOption ->
            if (previewingGridOptionKey == selectedGridOption.key.value) {
                null
            } else {
                { interactor.applySelectedOption(previewingGridOptionKey) }
@@ -93,17 +95,11 @@ constructor(
                    )
            )
        val isSelected =
            _previewingGridOptionKey
                .map {
                    if (it == null) {
                        option.isCurrent
                    } else {
                        it == option.key
                    }
                }
            previewingGridOptionKey
                .map { it == option.key }
                .stateIn(
                    scope = viewModelScope,
                    started = SharingStarted.Eagerly,
                    started = SharingStarted.Lazily,
                    initialValue = false,
                )

@@ -116,7 +112,7 @@ constructor(
            onClicked =
                isSelected.map {
                    if (!it) {
                        { _previewingGridOptionKey.value = option.key }
                        { overridingGridOptionKey.value = option.key }
                    } else {
                        null
                    }
+3 −3
Original line number Diff line number Diff line
@@ -176,7 +176,7 @@ class ShapeAndGridPickerViewModelTest {
            )
        }

    private fun assertOptionItem(
    private fun TestScope.assertOptionItem(
        optionItem: OptionItemViewModel<GridIconViewModel>?,
        key: String,
        payload: GridIconViewModel?,
@@ -186,11 +186,11 @@ class ShapeAndGridPickerViewModelTest {
        isEnabled: Boolean,
    ) {
        checkNotNull(optionItem)
        assertThat(optionItem.key.value).isEqualTo(key)
        assertThat(collectLastValue(optionItem.key)()).isEqualTo(key)
        assertThat(optionItem.text).isEqualTo(text)
        assertThat(optionItem.payload).isEqualTo(payload)
        assertThat(optionItem.isTextUserVisible).isEqualTo(isTextUserVisible)
        assertThat(optionItem.isSelected.value).isEqualTo(isSelected)
        assertThat(collectLastValue(optionItem.isSelected)()).isEqualTo(isSelected)
        assertThat(optionItem.isEnabled).isEqualTo(isEnabled)
    }
}