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

Commit de75a621 authored by Abhishek Aggarwal's avatar Abhishek Aggarwal
Browse files

refactor(source): remove unused Stores wrapper

parent 2d907726
Loading
Loading
Loading
Loading
+0 −107
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 e Foundation
 * Copyright (C) 2024 MURENA SAS
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package foundation.e.apps.data

import foundation.e.apps.data.cleanapk.repositories.CleanApkAppsRepository
import foundation.e.apps.data.cleanapk.repositories.CleanApkPwaRepository
import foundation.e.apps.data.di.qualifiers.IoCoroutineScope
import foundation.e.apps.data.enums.Source
import foundation.e.apps.data.playstore.PlayStoreRepository
import foundation.e.apps.domain.auth.UserCapabilitiesProvider
import foundation.e.apps.domain.source.SourceSelectionRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class Stores @Inject constructor(
    playStoreRepository: PlayStoreRepository,
    cleanApkAppsRepository: CleanApkAppsRepository,
    cleanApkPwaRepository: CleanApkPwaRepository,
    private val sourceSelectionRepository: SourceSelectionRepository,
    private val userCapabilitiesProvider: UserCapabilitiesProvider,
    @IoCoroutineScope coroutineScope: CoroutineScope,
) {
    private val storeRepositoryRegistry = StoreRepositoryRegistry(
        playStoreRepository = playStoreRepository,
        cleanApkAppsRepository = cleanApkAppsRepository,
        cleanApkPwaRepository = cleanApkPwaRepository,
    )
    private val enabledSourceState = EnabledSourceState(
        sourceSelectionRepository = sourceSelectionRepository,
        userCapabilitiesProvider = userCapabilitiesProvider,
        storeRepositoryRegistry = storeRepositoryRegistry,
        coroutineScope = coroutineScope,
    )
    private val enabledStoreRepositoryProvider = EnabledStoreRepositoryProvider(
        enabledSourceState = enabledSourceState,
        storeRepositoryRegistry = storeRepositoryRegistry,
    )
    val enabledStoresFlow: StateFlow<Set<Source>> = enabledSourceState.enabledSourcesFlow

    /**
     * Returns a synchronous snapshot of the currently enabled stores.
     *
     * Prefer [awaitEnabledStores] for first-load or auth-sensitive decisions that must wait for
     * hydrated capability state.
     */
    fun getStores(): Map<Source, StoreRepository> {
        return storeRepositoryRegistry.asMapFor(enabledSourceState.getEnabledSources())
    }

    suspend fun awaitEnabledStores(): Map<Source, StoreRepository> {
        return enabledStoreRepositoryProvider.awaitEnabledStores()
    }

    /**
     * Returns a synchronous snapshot of enabled search sources.
     *
     * Prefer [awaitEnabledSearchSources] when a caller must not observe pre-hydration state.
     */
    fun getEnabledSearchSources(): List<Source> = enabledSourceState.getEnabledSearchSources()

    suspend fun awaitEnabledSearchSources(): List<Source> = enabledSourceState.awaitEnabledSearchSources()

    suspend fun awaitStore(source: Source): StoreRepository? = enabledStoreRepositoryProvider.awaitStore(source)

    /**
     * Returns whether the given store is enabled in the current synchronous snapshot.
     *
     * Prefer [awaitIsStoreEnabled] when correctness depends on hydrated capability state.
     */
    fun isStoreEnabled(source: Source): Boolean = enabledSourceState.isSourceEnabled(source)

    suspend fun awaitIsStoreEnabled(source: Source): Boolean = enabledSourceState.awaitIsSourceEnabled(source)

    /**
     * Indicates whether this instance has resolved at least one awaited capability snapshot.
     */
    val hasHydratedCapabilities: Boolean
        get() = enabledSourceState.hasHydratedCapabilities
}

internal fun Stores.enabledStoreChanges(): Flow<Set<Source>> =
    enabledStoresFlow
        .filter { hasHydratedCapabilities }
        .drop(1)
+0 −197
Original line number Diff line number Diff line
package foundation.e.apps.data

import com.google.common.truth.Truth.assertThat
import foundation.e.apps.TestUserCapabilitiesProvider
import foundation.e.apps.data.cleanapk.repositories.CleanApkAppsRepository
import foundation.e.apps.data.cleanapk.repositories.CleanApkPwaRepository
import foundation.e.apps.data.enums.Source
import foundation.e.apps.data.playstore.PlayStoreRepository
import foundation.e.apps.domain.auth.UserCapabilities
import foundation.e.apps.domain.source.SourceSelection
import foundation.e.apps.domain.source.SourceSelectionRepository
import io.mockk.mockk
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import org.junit.Before
import org.junit.Test

class StoresTest {

    private val playStoreRepository: PlayStoreRepository = mockk(relaxed = true)
    private val cleanApkAppsRepository: CleanApkAppsRepository = mockk(relaxed = true)
    private val cleanApkPwaRepository: CleanApkPwaRepository = mockk(relaxed = true)
    private lateinit var sourceSelectionRepository: FakeSourceSelectionRepository
    private lateinit var userCapabilitiesProvider: TestUserCapabilitiesProvider
    private lateinit var stores: Stores

    @Before
    fun setUp() {
        sourceSelectionRepository = FakeSourceSelectionRepository(
            SourceSelection(
                isOpenSourceSelected = true,
                isPlayStoreSelected = true,
                isPwaSelected = false,
            )
        )
        userCapabilitiesProvider = TestUserCapabilitiesProvider()

        buildStores()
    }

    @Test
    fun getStoresReturnsOnlyEnabledSources() {
        sourceSelectionRepository.saveSourceSelection(
            SourceSelection(
                isOpenSourceSelected = false,
                isPlayStoreSelected = true,
                isPwaSelected = true,
            )
        )

        val result = stores.getStores()

        assertThat(result.keys).containsExactly(Source.PLAY_STORE, Source.PWA)
        assertThat(result[Source.PLAY_STORE]).isSameInstanceAs(playStoreRepository)
        assertThat(result[Source.PWA]).isSameInstanceAs(cleanApkPwaRepository)
    }

    @Test
    fun getEnabledSearchSourcesReturnsOrderedEnabledSources() {
        sourceSelectionRepository.saveSourceSelection(SourceSelection.DEFAULT)

        val result = stores.getEnabledSearchSources()

        assertThat(result)
            .isEqualTo(listOf(Source.PLAY_STORE, Source.OPEN_SOURCE, Source.PWA))
    }

    @Test
    fun getEnabledSearchSourcesReturnsEmptyWhenNoneEnabled() {
        sourceSelectionRepository.saveSourceSelection(
            SourceSelection(
                isOpenSourceSelected = false,
                isPlayStoreSelected = false,
                isPwaSelected = false,
            )
        )

        val result = stores.getEnabledSearchSources()

        assertThat(result).isEqualTo(emptyList<Source>())
    }

    @Test
    fun getStoresRespectsUserCapabilities() {
        sourceSelectionRepository.saveSourceSelection(SourceSelection.DEFAULT)
        buildStores()
        userCapabilitiesProvider.update(
            UserCapabilities(
                canAccessPlayStore = false,
                canAccessOpenSource = true,
                canAccessPwa = false,
                canPurchaseApps = false,
            )
        )

        val result = stores.getStores()

        assertThat(result.keys).containsExactly(Source.OPEN_SOURCE)
    }

    @Test
    fun isStoreEnabledReflectsSelectionFlags() {
        sourceSelectionRepository.saveSourceSelection(
            SourceSelection(
                isOpenSourceSelected = false,
                isPlayStoreSelected = false,
                isPwaSelected = true,
            )
        )

        val enabled = stores.isStoreEnabled(Source.PWA)
        val disabled = stores.isStoreEnabled(Source.PLAY_STORE)

        assertThat(enabled).isTrue()
        assertThat(disabled).isFalse()
    }

    @Test
    fun enabledStoresFlowReflectsInitialSelection() {
        sourceSelectionRepository.saveSourceSelection(
            SourceSelection(
                isOpenSourceSelected = false,
                isPlayStoreSelected = true,
                isPwaSelected = true,
            )
        )
        buildStores()

        assertThat(stores.enabledStoresFlow.value)
            .containsExactly(Source.PLAY_STORE, Source.PWA)
    }

    @Test
    fun storesHydrateCapabilitiesDuringInitialization() {
        assertThat(stores.hasHydratedCapabilities).isTrue()
    }

    @Test
    fun enabledStoresFlowUpdatesAfterSelectionChanges() {
        sourceSelectionRepository.saveSourceSelection(
            SourceSelection(
                isOpenSourceSelected = false,
                isPlayStoreSelected = true,
                isPwaSelected = false,
            )
        )
        buildStores()

        sourceSelectionRepository.saveSourceSelection(
            SourceSelection(
                isOpenSourceSelected = true,
                isPlayStoreSelected = true,
                isPwaSelected = false,
            )
        )
        assertThat(stores.enabledStoresFlow.value)
            .containsExactly(Source.PLAY_STORE, Source.OPEN_SOURCE)

        sourceSelectionRepository.saveSourceSelection(
            SourceSelection(
                isOpenSourceSelected = true,
                isPlayStoreSelected = false,
                isPwaSelected = false,
            )
        )
        assertThat(stores.enabledStoresFlow.value)
            .containsExactly(Source.OPEN_SOURCE)
    }

    private fun buildStores() {
        stores = Stores(
            playStoreRepository,
            cleanApkAppsRepository,
            cleanApkPwaRepository,
            sourceSelectionRepository,
            userCapabilitiesProvider,
            CoroutineScope(SupervisorJob() + Dispatchers.Unconfined),
        )
    }

    private class FakeSourceSelectionRepository(
        initialSourceSelection: SourceSelection,
    ) : SourceSelectionRepository {
        private val sourceSelectionState = MutableStateFlow(initialSourceSelection)

        override val sourceSelection: StateFlow<SourceSelection> = sourceSelectionState

        override fun currentSourceSelection(): SourceSelection = sourceSelectionState.value

        override fun saveSourceSelection(sourceSelection: SourceSelection) {
            sourceSelectionState.value = sourceSelection
        }
    }
}