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

Commit aeaa739b authored by Fabián Kozynski's avatar Fabián Kozynski Committed by Fabian Kozynski
Browse files

Mark current tiles as auto-added

If a tile is currently in the list of tile but could be auto-added if
not tracked, mark it as auto-added.

Note that this only applies to tiles that can be auto-added only once
(AutoAddTracking.IfNotAdded), as a way to indicate that the user has
already seen the tile (and possibly may have removed it). This doesn't
apply to tiles that are always tracked (AutoAddTracking.Always), as
those can be auto-removed and auto-added again when the feature is
enabled again.

Test: atest AutoAddInteractorTest
Fixes: 324960944
Flag: ACONFIG com.android.systemui.qs_new_pipeline NEXTFOOD
Change-Id: I6ca4261ed98548549ad257d0ea9e9af6f3c1c8ba
parent 2170c304
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import com.android.systemui.qs.pipeline.data.repository.FakeAutoAddRepository
import com.android.systemui.qs.pipeline.domain.autoaddable.FakeAutoAddable
import com.android.systemui.qs.pipeline.domain.model.AutoAddTracking
import com.android.systemui.qs.pipeline.domain.model.AutoAddable
import com.android.systemui.qs.pipeline.domain.model.TileModel
import com.android.systemui.qs.pipeline.shared.TileSpec
import com.android.systemui.qs.pipeline.shared.logging.QSPipelineLogger
import com.android.systemui.util.mockito.any
@@ -65,6 +66,7 @@ class AutoAddInteractorTest : SysuiTestCase() {
        MockitoAnnotations.initMocks(this)

        whenever(currentTilesInteractor.userId).thenReturn(MutableStateFlow(USER))
        whenever(currentTilesInteractor.currentTiles).thenReturn(MutableStateFlow(emptyList()))
    }

    @Test
@@ -201,6 +203,45 @@ class AutoAddInteractorTest : SysuiTestCase() {
            assertThat(autoAddedTiles).doesNotContain(SPEC)
        }

    @Test
    fun autoAddable_trackIfNotAdded_currentTile_markedAsAdded() =
        testScope.runTest {
            val fakeTile = FakeQSTile(USER).apply { tileSpec = SPEC.spec }
            val fakeCurrentTileModel = TileModel(SPEC, fakeTile)
            whenever(currentTilesInteractor.currentTiles)
                .thenReturn(MutableStateFlow(listOf(fakeCurrentTileModel)))

            val autoAddedTiles by collectLastValue(autoAddRepository.autoAddedTiles(USER))
            val fakeAutoAddable = FakeAutoAddable(SPEC, AutoAddTracking.IfNotAdded(SPEC))

            underTest = createInteractor(setOf(fakeAutoAddable))
            runCurrent()

            assertThat(autoAddedTiles).contains(SPEC)
        }

    @Test
    fun autoAddable_trackIfNotAdded_tileAddedToCurrentTiles_markedAsAdded() =
        testScope.runTest {
            val fakeTile = FakeQSTile(USER).apply { tileSpec = SPEC.spec }
            val fakeCurrentTileModel = TileModel(SPEC, fakeTile)
            val currentTilesFlow = MutableStateFlow(emptyList<TileModel>())

            whenever(currentTilesInteractor.currentTiles).thenReturn(currentTilesFlow)

            val autoAddedTiles by collectLastValue(autoAddRepository.autoAddedTiles(USER))
            val fakeAutoAddable = FakeAutoAddable(SPEC, AutoAddTracking.IfNotAdded(SPEC))

            underTest = createInteractor(setOf(fakeAutoAddable))
            runCurrent()

            assertThat(autoAddedTiles).doesNotContain(SPEC)

            currentTilesFlow.value = listOf(fakeCurrentTileModel)

            assertThat(autoAddedTiles).contains(SPEC)
        }

    private fun createInteractor(autoAddables: Set<AutoAddable>): AutoAddInteractor {
        return AutoAddInteractor(
                autoAddables,
+58 −40
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.take
@@ -55,6 +56,7 @@ constructor(
) : Dumpable {

    private val initialized = AtomicBoolean(false)
    private lateinit var currentTilesInteractor: CurrentTilesInteractor

    /** Start collection of signals following the user from [currentTilesInteractor]. */
    fun init(currentTilesInteractor: CurrentTilesInteractor) {
@@ -62,11 +64,34 @@ constructor(
            return
        }

        this.currentTilesInteractor = currentTilesInteractor
        dumpManager.registerNormalDumpable(TAG, this)

        scope.launch {
            currentTilesInteractor.userId.collectLatest { userId ->
                coroutineScope {
                    launch { collectAutoAddSignalsForUser(userId) }
                    launch { markTrackIfNotAddedTilesThatAreCurrent(userId) }
                }
            }
        }
    }

    private suspend fun markTrackIfNotAddedTilesThatAreCurrent(userId: Int) {
        val trackIfNotAddedSpecs =
            autoAddables
                .map { it.autoAddTracking }
                .filterIsInstance<AutoAddTracking.IfNotAdded>()
                .map { it.spec }
        currentTilesInteractor.currentTiles
            .map { tiles -> tiles.map { it.spec } }
            .collect {
                it.filter { it in trackIfNotAddedSpecs }
                    .forEach { spec -> repository.markTileAdded(userId, spec) }
            }
    }

    private suspend fun CoroutineScope.collectAutoAddSignalsForUser(userId: Int) {
        val previouslyAdded = repository.autoAddedTiles(userId).stateIn(this)

        autoAddables
@@ -90,11 +115,7 @@ constructor(
                    is AutoAddSignal.Add -> {
                        if (signal.spec !in previouslyAdded.value) {
                            currentTilesInteractor.addTile(signal.spec, signal.position)
                                        qsPipelineLogger.logTileAutoAdded(
                                            userId,
                                            signal.spec,
                                            signal.position
                                        )
                            qsPipelineLogger.logTileAutoAdded(userId, signal.spec, signal.position)
                            repository.markTileAdded(userId, signal.spec)
                        }
                    }
@@ -110,9 +131,6 @@ constructor(
                }
            }
    }
            }
        }
    }

    override fun dump(pw: PrintWriter, args: Array<out String>) {
        with(pw.asIndenting()) {