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

Commit 6c12f40f authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

refac:3870: Create LoginUseCase to fed up LoginViewModel.

parent dd53844b
Loading
Loading
Loading
Loading
+1 −113
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import javax.inject.Singleton
@Singleton
class GPlayAuthentication @Inject constructor(
    private val appLoungeDataStore: AppLoungeDataStore,
    private val appLoungePreference: AppLoungePreference,
    private val playStoreAuthenticator: PlayStoreAuthenticator,
    private val json: Json,
    @ApplicationContext private val context: Context,
@@ -33,122 +32,11 @@ class GPlayAuthentication @Inject constructor(
    private val locale: Locale
        get() = context.resources.configuration.locales[0]


    // Move to LoginUseCase ? or AuthenticationUseCase ?

    suspend fun setGoogleUser(email: String, oauth: String): List<AuthObject> = withContext(ioCoroutineScope.coroutineContext) {
        appLoungeDataStore.saveGoogleLogin(email, oauth)
        appLoungeDataStore.saveUserType(User.GOOGLE)


        val result = buildFreshGoogleLoginToken()
        if (result.isSuccess) {
            appLoungeDataStore.saveAuthData(result.getOrNull())

        }

        buildAuthObjectList(result, User.GOOGLE)
    }

    suspend fun setAnonymousUser(): List<AuthObject> = withContext(ioCoroutineScope.coroutineContext) {
        appLoungeDataStore.saveUserType(User.GOOGLE)
        val result = buildFreshAnonymousLoginToken()
        if (result.isSuccess) {
            appLoungeDataStore.saveAuthData(result.getOrNull())
        }

        buildAuthObjectList(result, User.GOOGLE)
    }


    suspend fun setNoGoogleMode() = withContext(ioCoroutineScope.coroutineContext) {
        appLoungePreference.run {
            disablePlayStore()
            enableOpenSource()
            enablePwa()
        }
        appLoungeDataStore.saveUserType(User.NO_GOOGLE)
    }

    suspend fun fetchAuthObjects(authTypes: List<String> = listOf()): List<AuthObject> {
        val userType = appLoungeDataStore.getUserType()

        if (authTypes.isEmpty()) {
            // use saved state.
            val savedAuth = getSavedAuthData()
            if (savedAuth != null) {
                return buildAuthObjectList(Result.success(savedAuth), userType = userType)
            }
        }

        val result = updateToken()

        return if (result?.isSuccess == true) {
            buildAuthObjectList(result, User.GOOGLE)
        } else if (userType != User.UNAVAILABLE && (appLoungePreference.isOpenSourceSelected() || appLoungePreference.isPWASelected())) {
            listOf(AuthObject.CleanApk(ResultSupreme.Success(Unit), userType,))
        } else {
            emptyList()
        }
    }


    private fun getSavedAuthData(): AuthData? {
        val authJson = appLoungeDataStore.authData.getSync()
        return if (authJson.isBlank()) null
        else try {
            json.decodeFromString<AuthData>(authJson)
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }

    private fun buildAuthObjectList(gPlayloginResult: Result<AuthData>, userType: User): List<AuthObject> {
        val authObjectsLocal = ArrayList<AuthObject>()

        // Google Login case:
        val result: ResultSupreme<AuthData?>
        if (gPlayloginResult.isSuccess) {
            result = ResultSupreme.create(
                status = ResultStatus.OK,
                data = gPlayloginResult.getOrNull()
            )
            result.otherPayload = gPlayloginResult.getOrNull()?.email
        } else {
            result = ResultSupreme.create(status = ResultStatus.UNKNOWN, exception = gPlayloginResult.exceptionOrNull() as? Exception)
        }

        authObjectsLocal.add(AuthObject.GPlayAuth(result, userType))


        // Clean apk
        if (userType != User.UNAVAILABLE && (appLoungePreference.isOpenSourceSelected() || appLoungePreference.isPWASelected())) {
            authObjectsLocal.add(AuthObject.CleanApk(ResultSupreme.Success(Unit), userType,))
        }

        return authObjectsLocal
    }

    suspend fun logout() {
        appLoungeDataStore.destroyCredentials()
        appLoungeDataStore.saveUserType(null)
        // reset app source preferences on logout.
        appLoungePreference.run {
            enableOpenSource()
            enablePwa()
            enablePlayStore()
        }
    }


    /////////// END.

    suspend fun refresh() {
        updateToken()
    }

    private suspend fun updateToken(): Result<AuthData>? {
    suspend fun updateToken(): Result<AuthData>? {
        val userType = appLoungeDataStore.getUserType()

        val result = when (userType) {
+107 −0
Original line number Diff line number Diff line
package foundation.e.apps.domain.usecases

import com.aurora.gplayapi.data.models.AuthData
import foundation.e.apps.data.ResultSupreme
import foundation.e.apps.data.enums.ResultStatus
import foundation.e.apps.data.enums.User
import foundation.e.apps.data.login.AuthObject
import foundation.e.apps.data.preference.AppLoungeDataStore
import foundation.e.apps.data.preference.AppLoungePreference
import foundation.e.apps.di.qualifiers.IoCoroutineScope
import foundation.e.apps.domain.procedures.GPlayAuthentication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class LoginUseCase @Inject constructor(
    private val gplayAuthentication: GPlayAuthentication,
    private val appLoungeDataStore: AppLoungeDataStore,
    private val appLoungePreference: AppLoungePreference,
    @IoCoroutineScope private val ioCoroutineScope: CoroutineScope,
) {
    suspend fun setGoogleUser(email: String, oauth: String) {
        withContext(ioCoroutineScope.coroutineContext) {
            appLoungeDataStore.saveGoogleLogin(email, oauth)
            appLoungeDataStore.saveUserType(User.GOOGLE)
        }
    }

    suspend fun setAnonymousUser() {
        withContext(ioCoroutineScope.coroutineContext) {
            appLoungeDataStore.saveUserType(User.GOOGLE)
        }
    }


    suspend fun setNoGoogleMode() {
        withContext(ioCoroutineScope.coroutineContext) {
            appLoungePreference.run {
                disablePlayStore()
                enableOpenSource()
                enablePwa()
            }
            appLoungeDataStore.saveUserType(User.NO_GOOGLE)
        }
    }

    suspend fun fetchAuthObjects(authTypes: List<String> = listOf()): List<AuthObject> {
        val userType = appLoungeDataStore.getUserType()

        if (authTypes.isEmpty()) {
            // use saved state.
            val savedAuth = appLoungeDataStore.getAuthData()
            if (savedAuth != AuthData("", "")) {
                return buildAuthObjectList(Result.success(savedAuth), userType = userType)
            }
        }

        val result = gplayAuthentication.updateToken()

        return if (result?.isSuccess == true) {
            buildAuthObjectList(result, User.GOOGLE)
        } else if (userType != User.UNAVAILABLE && (appLoungePreference.isOpenSourceSelected() || appLoungePreference.isPWASelected())) {
            listOf(AuthObject.CleanApk(ResultSupreme.Success(Unit), userType,))
        } else {
            emptyList()
        }
    }

    private fun buildAuthObjectList(gPlayloginResult: Result<AuthData>, userType: User): List<AuthObject> {
        val authObjectsLocal = ArrayList<AuthObject>()

        // Google Login case:
        val result: ResultSupreme<AuthData?>
        if (gPlayloginResult.isSuccess) {
            result = ResultSupreme.create(
                status = ResultStatus.OK,
                data = gPlayloginResult.getOrNull()
            )
            result.otherPayload = gPlayloginResult.getOrNull()?.email
        } else {
            result = ResultSupreme.create(status = ResultStatus.UNKNOWN, exception = gPlayloginResult.exceptionOrNull() as? Exception)
        }

        authObjectsLocal.add(AuthObject.GPlayAuth(result, userType))


        // Clean apk
        if (userType != User.UNAVAILABLE && (appLoungePreference.isOpenSourceSelected() || appLoungePreference.isPWASelected())) {
            authObjectsLocal.add(AuthObject.CleanApk(ResultSupreme.Success(Unit), userType,))
        }

        return authObjectsLocal
    }

    suspend fun logout() {
        appLoungeDataStore.destroyCredentials()
        appLoungeDataStore.saveUserType(null)
        // reset app source preferences on logout.
        appLoungePreference.run {
            enableOpenSource()
            enablePwa()
            enablePlayStore()
        }
    }
}
 No newline at end of file
+7 −7
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import foundation.e.apps.data.Stores
import foundation.e.apps.data.enums.Source
import foundation.e.apps.data.login.AuthObject
import foundation.e.apps.domain.procedures.GPlayAuthentication
import foundation.e.apps.domain.usecases.LoginUseCase
import foundation.e.apps.ui.parentFragment.LoadingViewModel
import kotlinx.coroutines.launch
import okhttp3.Cache
@@ -36,7 +36,7 @@ import javax.inject.Inject
 */
@HiltViewModel
class LoginViewModel @Inject constructor(
    private val gPlayAuthentication: GPlayAuthentication,
    private val loginUseCase: LoginUseCase,
    private val cache: Cache,
    private val stores: Stores
) : ViewModel() {
@@ -58,7 +58,7 @@ class LoginViewModel @Inject constructor(
     */
    fun startLoginFlow(clearList: List<String> = listOf()) {
        viewModelScope.launch {
            val authObjectsLocal = gPlayAuthentication.fetchAuthObjects(clearList)
            val authObjectsLocal = loginUseCase.fetchAuthObjects(clearList)
            authObjects.postValue(authObjectsLocal)
        }
    }
@@ -71,7 +71,7 @@ class LoginViewModel @Inject constructor(
    fun initialAnonymousLogin(onUserSaved: () -> Unit) {
        viewModelScope.launch {
            stores.enableStore(Source.PLAY_STORE)
            gPlayAuthentication.setAnonymousUser()
            loginUseCase.setAnonymousUser()
            onUserSaved()
            startLoginFlow()
        }
@@ -86,7 +86,7 @@ class LoginViewModel @Inject constructor(
    fun initialGoogleLogin(email: String, oauthToken: String, onUserSaved: () -> Unit) {
        viewModelScope.launch {
            stores.enableStore(Source.PLAY_STORE)
            gPlayAuthentication.setGoogleUser(email, oauthToken)
            loginUseCase.setGoogleUser(email, oauthToken)
            onUserSaved()
            startLoginFlow()
        }
@@ -102,7 +102,7 @@ class LoginViewModel @Inject constructor(
    fun initialNoGoogleLogin(onUserSaved: () -> Unit) {
        viewModelScope.launch {
            stores.disableStore(Source.PLAY_STORE)
            gPlayAuthentication.setNoGoogleMode()
            loginUseCase.setNoGoogleMode()
            onUserSaved()
            startLoginFlow()
        }
@@ -137,7 +137,7 @@ class LoginViewModel @Inject constructor(
    fun logout() {
        viewModelScope.launch {
            cache.evictAll()
            gPlayAuthentication.logout()
            loginUseCase.logout()
            authObjects.postValue(listOf())
        }
    }