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

Commit 5efe6c94 authored by Hasib Prince's avatar Hasib Prince
Browse files

refactor: renamed and simplified code

parent 2d706532
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@

package foundation.e.apps.data.gitlab

import foundation.e.apps.data.gitlab.models.ProjectIdMapItem
import foundation.e.apps.data.gitlab.models.SystemAppProject
import retrofit2.Response
import retrofit2.http.GET

@@ -29,6 +29,6 @@ interface EligibleSystemAppsApi {
    }

    @GET("updatable_system_apps.json")
    suspend fun getAllEligibleApps(): Response<List<ProjectIdMapItem>>
    suspend fun getAllEligibleApps(): Response<List<SystemAppProject>>

}
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
package foundation.e.apps.data.gitlab

import foundation.e.apps.data.gitlab.models.UpdateDefinition
import foundation.e.apps.data.gitlab.models.SystemAppInfo
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
@@ -16,6 +16,6 @@ interface SystemAppDefinitionApi {
    suspend fun getSystemAppUpdateInfo(
        @Path("projectId") projectId: Int,
        @Path("releaseType") releaseType: String,
    ): Response<UpdateDefinition>
    ): Response<SystemAppInfo>

}
 No newline at end of file
+19 −29
Original line number Diff line number Diff line
@@ -18,7 +18,8 @@
package foundation.e.apps.data.gitlab

import foundation.e.apps.data.application.data.Application
import foundation.e.apps.data.gitlab.models.ProjectIdMapItem
import foundation.e.apps.data.gitlab.models.SystemAppInfo
import foundation.e.apps.data.gitlab.models.SystemAppProject
import foundation.e.apps.data.gitlab.models.toApplication
import javax.inject.Inject
import javax.inject.Singleton
@@ -30,17 +31,17 @@ class SystemAppsUpdatesRepository @Inject constructor(
    private val systemAppDefinitionApi: SystemAppDefinitionApi,
) {

    private var projectIdMap = mutableListOf<ProjectIdMapItem>()
    private var systemAppProjectList = mutableListOf<SystemAppProject>()

    suspend fun fetchAllEligibleApps() {
        val response = eligibleSystemAppsApi.getAllEligibleApps()
        if (response.isSuccessful && !response.body().isNullOrEmpty()) {
            response.body()?.let { projectIdMap.addAll(it) }
            response.body()?.let { systemAppProjectList.addAll(it) }
        }
    }

    fun getAllEligibleApps(): List<String> {
        return projectIdMap.map { it.packageName }
        return systemAppProjectList.map { it.packageName }
    }

    suspend fun getSystemAppUpdateInfo(
@@ -50,8 +51,15 @@ class SystemAppsUpdatesRepository @Inject constructor(
        device: String,
    ): Application? {

        fun isSystemAppBlacklisted(systemAppInfo: SystemAppInfo): Boolean {
            return (systemAppInfo.blacklistedAndroid?.contains(sdkLevel) == true
                    || systemAppInfo.blacklistedDevices?.contains(device) == true
                    || systemAppInfo.blacklistedDevices?.contains("${device}@${sdkLevel}") == true
                    )
        }

        val projectId =
            projectIdMap.find { it.packageName == packageName }?.projectId ?: return null
            systemAppProjectList.find { it.packageName == packageName }?.projectId ?: return null

        val response = systemAppDefinitionApi.getSystemAppUpdateInfo(projectId, releaseType)
        if (!response.isSuccessful) {
@@ -59,32 +67,14 @@ class SystemAppsUpdatesRepository @Inject constructor(
            return null
        }

        val updateDef = response.body()
        val systemAppInfo = response.body() ?: return null

        return when {
            updateDef == null -> {
                Timber.e("Null update definition: $packageName, $releaseType")
                null
            }
            updateDef.blacklistedAndroid?.contains(sdkLevel) == true -> {
                Timber.e("Ineligible sdk level: $packageName, $sdkLevel")
                null
            }
            updateDef.blacklistedDevices?.contains(device) == true -> {
                Timber.e("blacklisted device: $packageName, $device")
                null
            }
            updateDef.blacklistedDevices?.contains("${device}@${sdkLevel}") == true -> {
                // In case a device on a specific android version is blacklisted.
                // Eg: "redfin@31" would mean Pixel 5 on Android 12 cannot receive this update.
                Timber.e("blacklisted device: $packageName, ${device}@${sdkLevel}")
                null
            }
            else -> {
                val app = updateDef.toApplication()
                app
            }
        if (isSystemAppBlacklisted(systemAppInfo)) {
            Timber.e("Blacklisted system app: $packageName, $systemAppInfo")
            return null
        }

        return systemAppInfo.toApplication()
    }

}
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ import foundation.e.apps.data.enums.FilterLevel
import java.util.UUID

@JsonIgnoreProperties(ignoreUnknown = true)
data class UpdateDefinition(
data class SystemAppInfo(
    val name: String,
    @Json(name = "package_name") val packageName: String,
    @Json(name = "version_code") val versionCode: Int,
@@ -36,7 +36,7 @@ data class UpdateDefinition(
    @Json(name = "blacklisted_devices") val blacklistedDevices: List<String>?,
)

fun UpdateDefinition.toApplication(): Application {
fun SystemAppInfo.toApplication(): Application {
    return Application(
        _id = UUID.randomUUID().toString(),
        author = "Murena SAS",
+1 −1
Original line number Diff line number Diff line
package foundation.e.apps.data.gitlab.models

data class ProjectIdMapItem(
data class SystemAppProject(
    val packageName: String,
    val projectId: Int,
)