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

Commit 509bf227 authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

refac:3870: Get saved token directly from appLoungeDataStore.

parent 3fbccc51
Loading
Loading
Loading
Loading
+0 −9
Original line number Diff line number Diff line
@@ -30,15 +30,6 @@ import javax.inject.Singleton
class AuthenticatorRepository @Inject constructor(
    private val appLoungeDataStore: AppLoungeDataStore
) {

    fun getGPlayAuthOrThrow(): AuthData {
        return kotlin.runCatching {
            appLoungeDataStore.getAuthData()
        }.getOrElse {
            throw GPlayLoginException(false, "AuthData is not available", appLoungeDataStore.getUser())
        }
    }

    suspend fun setGPlayAuth(auth: AuthData) {
        appLoungeDataStore.saveAuthData(auth)
    }
+49 −32
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
package foundation.e.apps.data.playstore

import android.content.Context
import com.aurora.gplayapi.data.models.AuthData
import com.aurora.gplayapi.data.models.Category
import com.aurora.gplayapi.data.models.ContentRating
import com.aurora.gplayapi.data.models.PlayFile
@@ -47,6 +48,7 @@ import foundation.e.apps.data.login.AuthenticatorRepository
import foundation.e.apps.data.login.PlayStoreAuthenticator
import foundation.e.apps.data.playstore.utils.GPlayHttpClient
import foundation.e.apps.data.playstore.utils.GplayHttpRequestException
import foundation.e.apps.data.preference.AppLoungeDataStore
import foundation.e.apps.domain.procedures.GPlayAuthentication
import foundation.e.apps.utils.SystemInfoProvider
import kotlinx.coroutines.CancellationException
@@ -63,7 +65,8 @@ class PlayStoreRepository @Inject constructor(
    private val authenticatorRepository: AuthenticatorRepository,
    private val gPlayAuthentication: GPlayAuthentication,
    private val applicationDataManager: ApplicationDataManager,
    private val playStoreSearchHelper: PlayStoreSearchHelper
    private val playStoreSearchHelper: PlayStoreSearchHelper,
    private val appLoungeDataStore: AppLoungeDataStore
) : StoreRepository {

    override suspend fun getHomeScreenData(list: MutableList<Home>): List<Home> {
@@ -201,11 +204,12 @@ class PlayStoreRepository @Inject constructor(
            appDetails.toApplication(context)
        }

    private fun getAppDetailsHelper(): AppDetailsHelper {
        val authData = authenticatorRepository.getGPlayAuthOrThrow()
    private suspend fun getAppDetailsHelper(): AppDetailsHelper {
        return doAuthenticatedRequest { authData ->
            val appDetailsHelper = AppDetailsHelper(authData).using(gPlayHttpClient)

        return appDetailsHelper
            appDetailsHelper
        }
    }

    private suspend fun refreshPlayStoreAuthentication() {
@@ -272,15 +276,18 @@ class PlayStoreRepository @Inject constructor(
            throw IllegalStateException("Could not get download details for $idOrPackageName")
        }

        doAuthenticatedRequest { authData ->

            // Don't store auth data in a variable. Always get GPlay auth from repository,
            // because auth might get refreshed while fetching app details.
        val purchaseHelper = PurchaseHelper(authenticatorRepository.getGPlayAuthOrThrow())
            val purchaseHelper = PurchaseHelper(authData)
                .using(gPlayHttpClient)

            buildList { addAll(purchaseHelper.purchase(idOrPackageName, version, offer)) }
        }
    }

    fun isAnonymousUser() = authenticatorRepository.getGPlayAuthOrThrow().isAnonymous
    fun isAnonymousUser() = appLoungeDataStore.getAuthData().isAnonymous

    suspend fun getOnDemandModule(
        packageName: String,
@@ -288,8 +295,8 @@ class PlayStoreRepository @Inject constructor(
        versionCode: Long,
        offerType: Int
    ): List<PlayFile> {
        return doAuthenticatedRequest { authData ->
            val downloadData = mutableListOf<PlayFile>()
        val authData = authenticatorRepository.getGPlayAuthOrThrow()

            withContext(Dispatchers.IO) {
                val purchaseHelper = PurchaseHelper(authData).using(gPlayHttpClient)
@@ -297,30 +304,40 @@ class PlayStoreRepository @Inject constructor(
                    purchaseHelper.purchase(packageName, versionCode, offerType, moduleName)
                )
            }
        return downloadData
            downloadData
        }
    }

    suspend fun getContentRatingWithId(
        appPackage: String,
        contentRating: ContentRating
    ): ContentRating {
        val authData = authenticatorRepository.getGPlayAuthOrThrow()
        return doAuthenticatedRequest { authData ->
            val contentRatingHelper = ContentRatingHelper(authData)

        return withContext(Dispatchers.IO) {
            withContext(Dispatchers.IO) {
                contentRatingHelper.updateContentRatingWithId(
                    appPackage,
                    contentRating
                )
            }
        }
    }

    suspend fun getEnglishContentRating(packageName: String): ContentRating {
        val authData = authenticatorRepository.getGPlayAuthOrThrow()
        return doAuthenticatedRequest { authData ->
            val contentRatingHelper = ContentRatingHelper(authData)

        return withContext(Dispatchers.IO) {
            withContext(Dispatchers.IO) {
                contentRatingHelper.getEnglishContentRating(packageName)
            }
        }
    }

    //inline fun <T> runSuspendCatching(block: () -> T): Result<T> {

    private suspend fun <T> doAuthenticatedRequest(request: suspend (AuthData) -> T): T {
        val authData = appLoungeDataStore.getAuthData()
        return request(authData)
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -301,9 +301,9 @@ class AgeRatingProvider : ContentProvider() {

    private fun hasAuthData(): Boolean {
        return try {
            authenticatorRepository.getGPlayAuthOrThrow()
            appLoungeDataStore.getAuthData()
            true
        } catch (e: GPlayLoginException) {
        } catch (e: NoSuchElementException) {
            Timber.e("No AuthData to check content rating")
            false
        }