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

Commit cd30e5b2 authored by Nishant D's avatar Nishant D
Browse files

Merge branch '1456-integrate-anonymous-user' into '1430-anonymous-domain-layer'

Anonymous User flow view model

See merge request !348
parents e4219815 eeab7fa2
Loading
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -133,6 +133,13 @@ android {
    kotlin.sourceSets.all {
        languageSettings.optIn("kotlin.RequiresOptIn")
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
            returnDefaultValues = true
        }
    }
}

kapt {
@@ -256,4 +263,5 @@ dependencies {
    testImplementation 'org.mockito:mockito-core:5.0.0'
    testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'
    testImplementation 'org.robolectric:robolectric:4.9'
    testImplementation 'org.json:json:20180813' // Added to avoid SystemInfoProvider.getAppBuildInfo() mock error
}
+9 −0
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import dagger.hilt.components.SingletonComponent
import foundation.e.apps.data.login.LoginSourceCleanApk
import foundation.e.apps.data.login.LoginSourceGPlay
import foundation.e.apps.data.login.LoginSourceInterface
import foundation.e.apps.domain.login.repository.LoginRepositoryImpl
import foundation.e.apps.domain.login.usecase.UserLoginUseCase

@InstallIn(SingletonComponent::class)
@Module
@@ -36,4 +38,11 @@ object LoginModule {
    ): List<LoginSourceInterface> {
        return listOf(gPlay, cleanApk)
    }

    @Provides
    fun provideLoginUserCase(
        loginRepositoryImpl: LoginRepositoryImpl
    ): UserLoginUseCase {
        return UserLoginUseCase(loginRepositoryImpl)
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ class UserLoginUseCase @Inject constructor(

    fun anonymousUser(): Flow<Resource<AuthData>> = flow {
        try {
            emit(Resource.Loading)
            emit(Resource.Loading())
            val userResponse = loginRepository.anonymousUser()
            emit(Resource.Success(userResponse))
        } catch (e: Exception) {
+25 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2023
 * Apps  Quickly and easily install Android apps onto your device!
 *
 * 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.presentation.login

data class LoginState(
    val isLoading: Boolean = false,
    val isLoggedIn: Boolean = false,
    val error: String = ""
)
+32 −1
Original line number Diff line number Diff line
@@ -15,14 +15,21 @@
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps.data.login
package foundation.e.apps.presentation.login

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import foundation.e.apps.data.enums.User
import foundation.e.apps.data.login.AuthObject
import foundation.e.apps.data.login.LoginSourceRepository
import foundation.e.apps.domain.login.usecase.UserLoginUseCase
import foundation.e.apps.ui.parentFragment.LoadingViewModel
import foundation.e.apps.utils.Resource
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject

@@ -33,6 +40,7 @@ import javax.inject.Inject
@HiltViewModel
class LoginViewModel @Inject constructor(
    private val loginSourceRepository: LoginSourceRepository,
    private val userLoginUseCase: UserLoginUseCase
) : ViewModel() {

    /**
@@ -131,4 +139,27 @@ class LoginViewModel @Inject constructor(
            authObjects.postValue(listOf())
        }
    }

    private val _loginState: MutableLiveData<LoginState> = MutableLiveData()
    val loginState: LiveData<LoginState> = _loginState

    fun authenticateAnonymousUser() {
        viewModelScope.launch {
            userLoginUseCase.anonymousUser().onEach { result ->
                when (result) {
                    is Resource.Success -> {
                        _loginState.value = LoginState(isLoggedIn = true)
                    }
                    is Resource.Error -> {
                        _loginState.value = LoginState(
                            error = result.message ?: "An unexpected error occured"
                        )
                    }
                    is Resource.Loading -> {
                        _loginState.value = LoginState(isLoading = true)
                    }
                }
            }.collect()
        }
    }
}
Loading