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

Commit 5b0ab2c8 authored by Sayantan Roychowdhury's avatar Sayantan Roychowdhury
Browse files

App lounge: (issue_5168) remove dataStoreModule from MainActivity and move...

App lounge: (issue_5168) remove dataStoreModule from MainActivity and move logic to MainActivityViewModel
parent c9aa357c
Loading
Loading
Loading
Loading
Loading
+7 −39
Original line number Diff line number Diff line
@@ -46,7 +46,6 @@ import foundation.e.apps.updates.UpdatesNotifier
import foundation.e.apps.utils.enums.Status
import foundation.e.apps.utils.enums.User
import foundation.e.apps.utils.modules.CommonUtilsModule
import foundation.e.apps.utils.modules.DataStoreModule
import kotlinx.coroutines.launch
import java.io.File
import java.util.UUID
@@ -57,9 +56,6 @@ class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private val TAG = MainActivity::class.java.simpleName

    @Inject
    lateinit var dataStoreModule: DataStoreModule

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
@@ -98,7 +94,7 @@ class MainActivity : AppCompatActivity() {
                        }
                    }
                    User.UNAVAILABLE -> {
                        viewModel.destroyCredentials()
                        viewModel.destroyCredentials(null)
                    }
                    User.GOOGLE -> {
                        if (viewModel.authData.value == null && !viewModel.authRequestRunning) {
@@ -135,43 +131,15 @@ class MainActivity : AppCompatActivity() {
        }

        viewModel.authValidity.observe(this) {
            lifecycleScope.launch {
            if (it != true) {
                Log.d(TAG, "Authentication data validation failed!")
                    /*
                     * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5168
                     *
                     * Previously we were using viewmodel.destroyCredentials(), without
                     * a coroutine scope.
                     *
                     * If auth data is invalid, we need old credentials to be removed followed by
                     * generation of new auth data. If we use via viewmodel, the two jobs do not
                     * occur sequentially. Hence we run the suspend function directly
                     * inside a coroutine scope.
                     *
                     * Now destroyCredentials() no longer removes the user type from data store.
                     * (i.e. Google login or Anonymous).
                     * - If the type is User.ANONYMOUS then we do not prompt the user to login again,
                     *   we directly generate new auth data; which is the main Gitlab issue described above.
                     * - If not anonymous user, i.e. type is User.GOOGLE, in that case we clear
                     *   the USERTYPE value. This causes HomeFragment.onTosAccepted() to open
                     *   SignInFragment as we need fresh login from the user.
                     */
                    dataStoreModule.destroyCredentials()
                    dataStoreModule.userType.collect { user ->
                        if (!user.isBlank() && User.valueOf(user) == User.ANONYMOUS) {
                            Log.d(TAG, "Regenerating auth data for Anonymous user")
                viewModel.destroyCredentials { user ->
                    generateAuthDataBasedOnUserType(user)
                        } else {
                            Log.d(TAG, "Ask Google user to log in again")
                            dataStoreModule.clearUserType()
                        }
                }
            } else {
                Log.d(TAG, "Authentication data is valid!")
            }
        }
        }

        navController.addOnDestinationChangedListener { _, destination, _ ->
            if (!hasInternet) {
+24 −1
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import foundation.e.apps.manager.fused.FusedManagerRepository
import foundation.e.apps.utils.enums.Origin
import foundation.e.apps.utils.enums.Status
import foundation.e.apps.utils.enums.Type
import foundation.e.apps.utils.enums.User
import foundation.e.apps.utils.modules.DataStoreModule
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@@ -99,9 +100,31 @@ class MainActivityViewModel @Inject constructor(
        _authData.value = authData
    }

    fun destroyCredentials() {
    fun destroyCredentials(regenerateFunction: ((user: String) -> Unit)?) {
        viewModelScope.launch {
            /*
             * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5168
             *
             * Now destroyCredentials() no longer removes the user type from data store.
             * (i.e. Google login or Anonymous).
             * - If the type is User.ANONYMOUS then we do not prompt the user to login again,
             *   we directly generate new auth data; which is the main Gitlab issue described above.
             * - If not anonymous user, i.e. type is User.GOOGLE, in that case we clear
             *   the USERTYPE value. This causes HomeFragment.onTosAccepted() to open
             *   SignInFragment as we need fresh login from the user.
             */
            dataStoreModule.destroyCredentials()
            if (regenerateFunction != null) {
                dataStoreModule.userType.collect { user ->
                    if (!user.isBlank() && User.valueOf(user) == User.ANONYMOUS) {
                        Log.d(TAG, "Regenerating auth data for Anonymous user")
                        regenerateFunction(user)
                    } else {
                        Log.d(TAG, "Ask Google user to log in again")
                        dataStoreModule.clearUserType()
                    }
                }
            }
        }
    }