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

Commit 95f3044b authored by George Lin's avatar George Lin
Browse files

Apply button

Consolidate the apply button functionality.

Test: Manually tested. See bug.
Bug: 362237825
Flag: com.android.systemui.shared.new_customization_picker_ui
Change-Id: I9766186e87d1b36a463a4337ebcb410ba35ddc67
parent 692e31db
Loading
Loading
Loading
Loading
+11 −1
Original line number Original line Diff line number Diff line
@@ -51,7 +51,17 @@ object ShapeAndGridFloatingSheetBinder {


        lifecycleOwner.lifecycleScope.launch {
        lifecycleOwner.lifecycleScope.launch {
            lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
            lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                launch { viewModel.optionItems.collect { options -> adapter.setItems(options) } }
                launch {
                    viewModel.optionItems.collect { options ->
                        adapter.setItems(options) {
                            val indexToFocus =
                                options.indexOfFirst { it.isSelected.value }.coerceAtLeast(0)
                            (gridOptionList.layoutManager as LinearLayoutManager).scrollToPosition(
                                indexToFocus
                            )
                        }
                    }
                }
            }
            }
        }
        }
    }
    }
+7 −2
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.wallpaper.customization.ui.binder
import android.widget.Button
import android.widget.Button
import android.widget.FrameLayout
import android.widget.FrameLayout
import android.widget.Toolbar
import android.widget.Toolbar
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.lifecycleScope
@@ -54,12 +55,16 @@ constructor(private val defaultToolbarBinder: DefaultToolbarBinder) : ToolbarBin
        lifecycleOwner.lifecycleScope.launch {
        lifecycleOwner.lifecycleScope.launch {
            lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
            lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                launch {
                launch {
                    viewModel.keyguardQuickAffordancePickerViewModel2.onApply.collect { onApply ->
                    viewModel.onApplyButtonClicked.collect { onApplyButtonClicked ->
                        applyButton.setOnClickListener {
                        applyButton.setOnClickListener {
                            onApply?.invoke()?.let { viewModel.deselectOption() }
                            onApplyButtonClicked?.invoke()?.let { viewModel.deselectOption() }
                        }
                        }
                    }
                    }
                }
                }

                launch { viewModel.isOnApplyVisible.collect { applyButton.isVisible = it } }

                launch { viewModel.isOnApplyEnabled.collect { applyButton.isEnabled = it } }
            }
            }
        }
        }
    }
    }
+12 −12
Original line number Original line Diff line number Diff line
@@ -83,12 +83,12 @@ constructor(
                started = SharingStarted.WhileSubscribed(),
                started = SharingStarted.WhileSubscribed(),
                initialValue = "",
                initialValue = "",
            )
            )
    private val _selectedQuickAffordances = MutableStateFlow<Map<String, String>>(emptyMap())
    private val _previewingQuickAffordances = MutableStateFlow<Map<String, String>>(emptyMap())
    val selectedQuickAffordances: Flow<Map<String, String>> =
    val previewingQuickAffordances: Flow<Map<String, String>> =
        _selectedQuickAffordances.asStateFlow()
        _previewingQuickAffordances.asStateFlow()


    fun resetPreview() {
    fun resetPreview() {
        _selectedQuickAffordances.tryEmit(emptyMap())
        _previewingQuickAffordances.tryEmit(emptyMap())
        _selectedSlotId.tryEmit(SLOT_ID_BOTTOM_START)
        _selectedSlotId.tryEmit(SLOT_ID_BOTTOM_START)
    }
    }


@@ -98,7 +98,7 @@ constructor(
                quickAffordanceInteractor.slots,
                quickAffordanceInteractor.slots,
                quickAffordanceInteractor.affordances,
                quickAffordanceInteractor.affordances,
                quickAffordanceInteractor.selections,
                quickAffordanceInteractor.selections,
                selectedQuickAffordances,
                previewingQuickAffordances,
                selectedSlotId,
                selectedSlotId,
            ) { slots, affordances, selections, selectedQuickAffordances, selectedSlotId ->
            ) { slots, affordances, selections, selectedQuickAffordances, selectedSlotId ->
                slots.associate { slot ->
                slots.associate { slot ->
@@ -194,7 +194,7 @@ constructor(
            val isNoneSelected =
            val isNoneSelected =
                combine(
                combine(
                        selectedSlotId,
                        selectedSlotId,
                        selectedQuickAffordances,
                        previewingQuickAffordances,
                        selectedAffordanceIds,
                        selectedAffordanceIds,
                    ) { selectedSlotId, selectedQuickAffordances, selectedAffordanceIds ->
                    ) { selectedSlotId, selectedQuickAffordances, selectedAffordanceIds ->
                        selectedQuickAffordances[selectedSlotId]?.let {
                        selectedQuickAffordances[selectedSlotId]?.let {
@@ -214,10 +214,10 @@ constructor(
                            if (!isSelected) {
                            if (!isSelected) {
                                {
                                {
                                    val newMap =
                                    val newMap =
                                        _selectedQuickAffordances.value.toMutableMap().apply {
                                        _previewingQuickAffordances.value.toMutableMap().apply {
                                            put(selectedSlotId, KEYGUARD_QUICK_AFFORDANCE_ID_NONE)
                                            put(selectedSlotId, KEYGUARD_QUICK_AFFORDANCE_ID_NONE)
                                        }
                                        }
                                    _selectedQuickAffordances.tryEmit(newMap)
                                    _previewingQuickAffordances.tryEmit(newMap)
                                }
                                }
                            } else {
                            } else {
                                null
                                null
@@ -230,7 +230,7 @@ constructor(
                    val isSelectedFlow: StateFlow<Boolean> =
                    val isSelectedFlow: StateFlow<Boolean> =
                        combine(
                        combine(
                                selectedSlotId,
                                selectedSlotId,
                                selectedQuickAffordances,
                                previewingQuickAffordances,
                                selectedAffordanceIds,
                                selectedAffordanceIds,
                            ) { selectedSlotId, selectedQuickAffordances, selectedAffordanceIds ->
                            ) { selectedSlotId, selectedQuickAffordances, selectedAffordanceIds ->
                                selectedQuickAffordances[selectedSlotId]?.let {
                                selectedQuickAffordances[selectedSlotId]?.let {
@@ -255,10 +255,10 @@ constructor(
                                    if (!isSelected) {
                                    if (!isSelected) {
                                        {
                                        {
                                            val newMap =
                                            val newMap =
                                                _selectedQuickAffordances.value
                                                _previewingQuickAffordances.value
                                                    .toMutableMap()
                                                    .toMutableMap()
                                                    .apply { put(selectedSlotId, affordance.id) }
                                                    .apply { put(selectedSlotId, affordance.id) }
                                            _selectedQuickAffordances.tryEmit(newMap)
                                            _previewingQuickAffordances.tryEmit(newMap)
                                        }
                                        }
                                    } else {
                                    } else {
                                        null
                                        null
@@ -287,7 +287,7 @@ constructor(
        }
        }


    val onApply: Flow<(() -> Unit)?> =
    val onApply: Flow<(() -> Unit)?> =
        selectedQuickAffordances.map {
        previewingQuickAffordances.map {
            if (it.isEmpty()) {
            if (it.isEmpty()) {
                null
                null
            } else {
            } else {
+4 −2
Original line number Original line Diff line number Diff line
@@ -58,6 +58,10 @@ constructor(
            previewingGridOptionKey ?: currentlySetGridOption.key.value
            previewingGridOptionKey ?: currentlySetGridOption.key.value
        }
        }


    fun resetPreview() {
        _previewingGridOptionKey.tryEmit(null)
    }

    val optionItems: Flow<List<OptionItemViewModel<GridIconViewModel>>> =
    val optionItems: Flow<List<OptionItemViewModel<GridIconViewModel>>> =
        interactor.gridOptions.filterNotNull().map { gridOptions ->
        interactor.gridOptions.filterNotNull().map { gridOptions ->
            gridOptions.map { toOptionItemViewModel(it) }
            gridOptions.map { toOptionItemViewModel(it) }
@@ -81,8 +85,6 @@ constructor(
            }
            }
        }
        }


    val isOnApplyEnabled: Flow<Boolean> = onApply.map { it != null }

    private fun toOptionItemViewModel(
    private fun toOptionItemViewModel(
        option: GridOptionModel
        option: GridOptionModel
    ): OptionItemViewModel<GridIconViewModel> {
    ): OptionItemViewModel<GridIconViewModel> {
+25 −0
Original line number Original line Diff line number Diff line
@@ -25,8 +25,13 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.assisted.AssistedInject
import dagger.hilt.android.scopes.ViewModelScoped
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn


class ThemePickerCustomizationOptionsViewModel
class ThemePickerCustomizationOptionsViewModel
@AssistedInject
@AssistedInject
@@ -53,6 +58,8 @@ constructor(


    override fun deselectOption(): Boolean {
    override fun deselectOption(): Boolean {
        keyguardQuickAffordancePickerViewModel2.resetPreview()
        keyguardQuickAffordancePickerViewModel2.resetPreview()
        shapeAndGridPickerViewModel.resetPreview()

        return defaultCustomizationOptionsViewModel.deselectOption()
        return defaultCustomizationOptionsViewModel.deselectOption()
    }
    }


@@ -110,6 +117,24 @@ constructor(
            }
            }
        }
        }


    @OptIn(ExperimentalCoroutinesApi::class)
    val onApplyButtonClicked =
        selectedOption
            .flatMapLatest {
                when (it) {
                    ThemePickerCustomizationOptionUtil.ThemePickerLockCustomizationOption
                        .SHORTCUTS -> keyguardQuickAffordancePickerViewModel2.onApply
                    ThemePickerCustomizationOptionUtil.ThemePickerHomeCustomizationOption
                        .APP_SHAPE_AND_GRID -> shapeAndGridPickerViewModel.onApply
                    else -> flow { emit(null) }
                }
            }
            .stateIn(viewModelScope, SharingStarted.Eagerly, null)

    val isOnApplyEnabled: Flow<Boolean> = onApplyButtonClicked.map { it != null }

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

    @ViewModelScoped
    @ViewModelScoped
    @AssistedFactory
    @AssistedFactory
    interface Factory : CustomizationOptionsViewModelFactory {
    interface Factory : CustomizationOptionsViewModelFactory {
Loading