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

Commit ad981196 authored by Matheo Levallois's avatar Matheo Levallois
Browse files

Merge branch '3334-fix-cyclomatic-complexity-of-enqueueFasedDownload-method' into 'main'

refactor: refactor enqueueFusedDownload method to fix cyclomatic complexity and nested block depth

See merge request !615
parents f1f29f27 78fb4c55
Loading
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@
    <ID>ChainWrapping:ValidateAppAgeLimitUseCase.kt$ValidateAppAgeLimitUseCase$&amp;&amp;</ID>
    <ID>CommentSpacing:ParentalControlAuthenticator.kt$ParentalControlAuthenticator$//Parental Control Key</ID>
    <ID>CommentSpacing:ParentalControlAuthenticator.kt$ParentalControlAuthenticator$//Parental Control intent</ID>
    <ID>CyclomaticComplexMethod:AppInstallProcessor.kt$AppInstallProcessor$suspend fun enqueueFusedDownload( appInstall: AppInstall, isAnUpdate: Boolean = false, isSystemApp: Boolean = false )</ID>
    <ID>EmptyCatchBlock:NativeDeviceInfoProviderModule.kt$NativeDeviceInfoProviderModule${ }</ID>
    <ID>EmptyFunctionBlock:CleanApkAuthenticator.kt$CleanApkAuthenticator${}</ID>
    <ID>FinalNewline:ContentRatingValidity.kt$foundation.e.apps.domain.model.ContentRatingValidity.kt</ID>
@@ -233,7 +232,6 @@
    <ID>MultiLineIfElse:SearchViewModel.kt$SearchViewModel$searchRepository.getSearchSuggestions(it)</ID>
    <ID>MultiLineIfElse:SettingsFragment.kt$SettingsFragment$true</ID>
    <ID>MultiLineIfElse:ValidateAppAgeLimitUseCase.kt$ValidateAppAgeLimitUseCase$fetchedContentRating</ID>
    <ID>NestedBlockDepth:AppInstallProcessor.kt$AppInstallProcessor$suspend fun enqueueFusedDownload( appInstall: AppInstall, isAnUpdate: Boolean = false, isSystemApp: Boolean = false )</ID>
    <ID>NewLineAtEndOfFile:ContentRatingValidity.kt$foundation.e.apps.domain.model.ContentRatingValidity.kt</ID>
    <ID>NewLineAtEndOfFile:SystemAppsUpdatesRepository.kt$foundation.e.apps.data.gitlab.SystemAppsUpdatesRepository.kt</ID>
    <ID>NoBlankLineBeforeRbrace:AgeGroupApi.kt$AgeGroupApi$ </ID>
+26 −19
Original line number Diff line number Diff line
@@ -139,25 +139,7 @@ class AppInstallProcessor @Inject constructor(
                return
            }

            val ageLimitValidationResult = validateAppAgeLimitUseCase.invoke(appInstall)
            if (ageLimitValidationResult.data?.isValid != true) {
                if (ageLimitValidationResult.isSuccess()) {
                    awaitInvokeAgeLimitEvent(appInstall.name)
                    if (ageLimitValidationResult.data?.requestPin == true){
                        val isAuthenticated = ParentalControlAuthenticator.awaitAuthentication()
                        if (isAuthenticated) {
                            ageLimitValidationResult.setData(ContentRatingValidity(true))
                        }
                    }
                } else {
                    EventBus.invokeEvent(AppEvent.ErrorMessageDialogEvent(R.string.data_load_error_desc))
                }

                if (ageLimitValidationResult.data?.isValid != true) {
                    appInstallComponents.appManagerWrapper.cancelDownload(appInstall)
                    return
                }
            }
            if (!validateAgeLimit(appInstall)) return

            if (!context.isNetworkAvailable()) {
                appInstallComponents.appManagerWrapper.installationIssue(appInstall)
@@ -184,6 +166,31 @@ class AppInstallProcessor @Inject constructor(
        }
    }

    private suspend fun validateAgeLimit(appInstall: AppInstall): Boolean {
        val ageLimitValidationResult = validateAppAgeLimitUseCase.invoke(appInstall)
        if (ageLimitValidationResult.data?.isValid == true) {
            return true
        }
        if (ageLimitValidationResult.isSuccess()) {
            awaitInvokeAgeLimitEvent(appInstall.name)
            if (ageLimitValidationResult.data?.requestPin == true) {
                val isAuthenticated = ParentalControlAuthenticator.awaitAuthentication()
                if (isAuthenticated) {
                    ageLimitValidationResult.setData(ContentRatingValidity(true))
                }
            }
        } else {
            EventBus.invokeEvent(AppEvent.ErrorMessageDialogEvent(R.string.data_load_error_desc))
        }

        var ageIsValid = true
        if (ageLimitValidationResult.data?.isValid != true) {
            appInstallComponents.appManagerWrapper.cancelDownload(appInstall)
            ageIsValid = false
        }
        return ageIsValid
    }

    suspend fun awaitInvokeAgeLimitEvent(type: String) {
        val deferred = CompletableDeferred<Unit>()
        EventBus.invokeEvent(AppEvent.AgeLimitRestrictionEvent(type, deferred))