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

Commit 10f75d80 authored by Josh's avatar Josh
Browse files

Moved Shortcut helper customization mode state management

The customization mode state management has been moved from the UI layer
to a repository to allow for multiple points of entry to customization
mode.

Fix: 403240110
Flag: com.android.systemui.extended_apps_shortcut_category
Test: ShortcutHelperViewModelTest,
ShortcutCustomizationModeRepositoryTest

Change-Id: I042a0d8a3731c73193304a6f3f40f476d40f05e0
parent 9a3fd080
Loading
Loading
Loading
Loading
+82 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.keyboard.shortcut.data.repository

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.keyboard.shortcut.shortcutHelperCustomizationModeRepository
import com.android.systemui.keyboard.shortcut.shortcutHelperTestHelper
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class ShortcutCustomizationModeRepositoryTest : SysuiTestCase() {

    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val repo = kosmos.shortcutHelperCustomizationModeRepository
    private val testScope = kosmos.testScope
    private val helper = kosmos.shortcutHelperTestHelper

    @Test
    fun customizationMode_disabledByDefault() {
        testScope.runTest {
            val customizationMode by collectLastValue(repo.isCustomizationModeEnabled)

            assertThat(customizationMode).isFalse()
        }
    }

    @Test
    fun customizationMode_enabledOnRequest_whenShortcutHelperIsOpen() {
        testScope.runTest {
            val customizationMode by collectLastValue(repo.isCustomizationModeEnabled)
            helper.showFromActivity()
            repo.toggleCustomizationMode(isCustomizing = true)
            assertThat(customizationMode).isTrue()
        }
    }

    @Test
    fun customizationMode_disabledOnRequest_whenShortcutHelperIsOpen() {
        testScope.runTest {
            val customizationMode by collectLastValue(repo.isCustomizationModeEnabled)
            helper.showFromActivity()
            repo.toggleCustomizationMode(isCustomizing = true)
            repo.toggleCustomizationMode(isCustomizing = false)
            assertThat(customizationMode).isFalse()
        }
    }

    @Test
    fun customizationMode_disabledWhenShortcutHelperIsDismissed() {
        testScope.runTest {
            val customizationMode by collectLastValue(repo.isCustomizationModeEnabled)
            helper.showFromActivity()
            repo.toggleCustomizationMode(isCustomizing = true)
            helper.hideFromActivity()
            assertThat(customizationMode).isFalse()
        }
    }
}
+47 −3
Original line number Diff line number Diff line
@@ -56,7 +56,6 @@ import com.android.systemui.keyboard.shortcut.shortcutHelperViewModel
import com.android.systemui.keyboard.shortcut.ui.model.IconSource
import com.android.systemui.keyboard.shortcut.ui.model.ShortcutCategoryUi
import com.android.systemui.keyboard.shortcut.ui.model.ShortcutsUiState
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.model.sysUiState
@@ -66,7 +65,8 @@ import com.android.systemui.settings.userTracker
import com.android.systemui.shared.system.QuickStepContract.SYSUI_STATE_SHORTCUT_HELPER_SHOWING
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
@@ -89,7 +89,6 @@ class ShortcutHelperViewModelTest : SysuiTestCase() {

    private val kosmos =
        testKosmos().useUnconfinedTestDispatcher().also {
            it.testDispatcher = UnconfinedTestDispatcher()
            it.shortcutHelperSystemShortcutsSource = fakeSystemSource
            it.shortcutHelperMultiTaskingShortcutsSource = fakeMultiTaskingSource
            it.shortcutHelperAppCategoriesShortcutsSource = FakeKeyboardShortcutGroupsSource()
@@ -445,6 +444,51 @@ class ShortcutHelperViewModelTest : SysuiTestCase() {
            assertThat((uiState as? ShortcutsUiState.Active)?.searchQuery).isEqualTo("")
        }

    @Test
    fun shortcutsUiState_customizationModeDisabledByDefault() {
        testScope.runTest {
            testHelper.showFromActivity()
            val uiState by collectLastValue(viewModel.shortcutsUiState)

            assertFalse((uiState as ShortcutsUiState.Active).isCustomizationModeEnabled)
        }
    }

    @Test
    fun shortcutsUiState_customizationModeEnabledOnRequest() {
        testScope.runTest {
            testHelper.showFromActivity()
            val uiState by collectLastValue(viewModel.shortcutsUiState)
            viewModel.toggleCustomizationMode(true)

            assertTrue((uiState as ShortcutsUiState.Active).isCustomizationModeEnabled)
        }
    }

    @Test
    fun shortcutsUiState_customizationModeDisabledOnRequest() {
        testScope.runTest {
            testHelper.showFromActivity()
            val uiState by collectLastValue(viewModel.shortcutsUiState)
            viewModel.toggleCustomizationMode(true)
            viewModel.toggleCustomizationMode(false)

            assertFalse((uiState as ShortcutsUiState.Active).isCustomizationModeEnabled)
        }
    }

    @Test
    fun shortcutsUiState_customizationModeDisabledWhenShortcutHelperIsReopened() {
        testScope.runTest {
            testHelper.showFromActivity()
            val uiState by collectLastValue(viewModel.shortcutsUiState)
            viewModel.toggleCustomizationMode(true)
            closeAndReopenShortcutHelper()

            assertFalse((uiState as ShortcutsUiState.Active).isCustomizationModeEnabled)
        }
    }

    private fun openHelperAndSearchForFooString() {
        testHelper.showFromActivity()
        viewModel.onSearchQueryChanged("foo")
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.keyboard.shortcut.data.repository

import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine

class ShortcutHelperCustomizationModeRepository
@Inject
constructor(shortcutHelperStateRepository: ShortcutHelperStateRepository) {
    private val _isCustomizationModeEnabled = MutableStateFlow(false)
    val isCustomizationModeEnabled =
        combine(_isCustomizationModeEnabled, shortcutHelperStateRepository.state) {
            isCustomizationModeEnabled,
            shortcutHelperState ->
            isCustomizationModeEnabled && shortcutHelperState is ShortcutHelperState.Active
        }

    fun toggleCustomizationMode(isCustomizing: Boolean) {
        _isCustomizationModeEnabled.value = isCustomizing
    }
}
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.keyboard.shortcut.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.keyboard.shortcut.data.repository.ShortcutHelperCustomizationModeRepository
import javax.inject.Inject

@SysUISingleton
class ShortcutHelperCustomizationModeInteractor
@Inject
constructor(private val customizationModeRepository: ShortcutHelperCustomizationModeRepository) {
    val customizationMode = customizationModeRepository.isCustomizationModeEnabled

    fun toggleCustomizationMode(isCustomizing: Boolean) {
        customizationModeRepository.toggleCustomizationMode(isCustomizing)
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -85,9 +85,12 @@ constructor(
                    shortcutsUiState = shortcutsUiState,
                    onKeyboardSettingsClicked = { onKeyboardSettingsClicked(dialog) },
                    onSearchQueryChanged = { shortcutHelperViewModel.onSearchQueryChanged(it) },
                    onCustomizationRequested = {
                    onShortcutCustomizationRequested = {
                        shortcutCustomizationDialogStarter.onShortcutCustomizationRequested(it)
                    },
                    onCustomizationModeToggled = { isCustomizing ->
                        shortcutHelperViewModel.toggleCustomizationMode(isCustomizing)
                    },
                )
                dialog.setOnDismissListener { shortcutHelperViewModel.onViewClosed() }
                dialog.setTitle(stringResource(R.string.shortcut_helper_title))
Loading