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

Commit 079673f1 authored by Hasib Prince's avatar Hasib Prince
Browse files

Viewmodels are refactored, (test needed)

parent 1350a08f
Loading
Loading
Loading
Loading
+1 −36
Original line number Diff line number Diff line
@@ -38,41 +38,6 @@ class AppProgressViewModel @Inject constructor(
        fusedApp: FusedApp?,
        progress: DownloadProgress
    ): Int {
        fusedApp?.let { app ->
            val appDownload = fusedManagerRepository.getDownloadList()
                .singleOrNull { it.id.contentEquals(app._id) && it.packageName.contentEquals(app.package_name) }
                ?: return 0

            if (!appDownload.id.contentEquals(app._id) || !appDownload.packageName.contentEquals(app.package_name)) {
                return@let
            }

            if (!isProgressValidForApp(fusedApp, progress)) {
                return -1
            }

            val downloadingMap = progress.totalSizeBytes.filter { item ->
                appDownload.downloadIdMap.keys.contains(item.key) && item.value > 0
            }

            if (appDownload.downloadIdMap.size > downloadingMap.size) { // All files for download are not ready yet
                return 0
            }

            val totalSizeBytes = downloadingMap.values.sum()
            val downloadedSoFar = progress.bytesDownloadedSoFar.filter { item ->
                appDownload.downloadIdMap.keys.contains(item.key)
            }.values.sum()
            return ((downloadedSoFar / totalSizeBytes.toDouble()) * 100).toInt()
        }
        return 0
    }

    private suspend fun isProgressValidForApp(
        fusedApp: FusedApp,
        downloadProgress: DownloadProgress
    ): Boolean {
        val download = fusedManagerRepository.getFusedDownload(downloadProgress.downloadId)
        return download.id == fusedApp._id
        return fusedManagerRepository.calculateProgress(fusedApp, progress)
    }
}
+4 −25
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import foundation.e.apps.manager.database.fusedDownload.FusedDownload
import foundation.e.apps.manager.download.data.DownloadProgress
import foundation.e.apps.manager.download.data.DownloadProgressLD
import foundation.e.apps.manager.fused.FusedManagerRepository
import foundation.e.apps.manager.pkg.PkgManagerModule
import foundation.e.apps.utils.enums.Origin
import foundation.e.apps.utils.enums.ResultStatus
import foundation.e.apps.utils.enums.Status
@@ -44,7 +43,6 @@ class ApplicationViewModel @Inject constructor(
    downloadProgressLD: DownloadProgressLD,
    private val fusedAPIRepository: FusedAPIRepository,
    private val fusedManagerRepository: FusedManagerRepository,
    private val pkgManagerModule: PkgManagerModule
) : ViewModel() {

    val fusedApp: MutableLiveData<Pair<FusedApp, ResultStatus>> = MutableLiveData()
@@ -110,36 +108,17 @@ class ApplicationViewModel @Inject constructor(
    }

    fun handleRatingFormat(rating: Double): String {
        return if (rating % 1 == 0.0) {
            rating.toInt().toString()
        } else {
            rating.toString()
        }
        return fusedManagerRepository.handleRatingFormat(rating)
    }

    suspend fun calculateProgress(progress: DownloadProgress): Pair<Long, Long> {
        fusedApp.value?.first?.let { app ->
            val appDownload = fusedManagerRepository.getDownloadList()
                .singleOrNull { it.id.contentEquals(app._id) }
            val downloadingMap = progress.totalSizeBytes.filter { item ->
                appDownload?.downloadIdMap?.keys?.contains(item.key) == true
            }
            val totalSizeBytes = downloadingMap.values.sum()
            val downloadedSoFar = progress.bytesDownloadedSoFar.filter { item ->
                appDownload?.downloadIdMap?.keys?.contains(item.key) == true
            }.values.sum()

            return Pair(totalSizeBytes, downloadedSoFar)
        }
        return Pair(1, 0)
        return fusedManagerRepository.getCalculateProgressWithTotalSize(fusedApp.value?.first, progress)
    }

    fun updateApplicationStatus(downloadList: List<FusedDownload>) {
        fusedApp.value?.first?.let { app ->
            val downloadingItem =
                downloadList.find { it.origin == app.origin && (it.packageName == app.package_name || it.id == app.package_name) }
            appStatus.value =
                downloadingItem?.status ?: fusedAPIRepository.getFusedAppInstallationStatus(app)
            appStatus.value = fusedManagerRepository.getDownloadingItemStatus(app, downloadList)
                ?: fusedAPIRepository.getFusedAppInstallationStatus(app)
        }
    }
}
+78 −0
Original line number Diff line number Diff line
@@ -4,7 +4,9 @@ import android.os.Build
import androidx.annotation.RequiresApi
import androidx.lifecycle.LiveData
import androidx.lifecycle.asFlow
import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.manager.database.fusedDownload.FusedDownload
import foundation.e.apps.manager.download.data.DownloadProgress
import foundation.e.apps.utils.enums.Status
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
@@ -86,4 +88,80 @@ class FusedManagerRepository @Inject constructor(

    fun validateFusedDownload(fusedDownload: FusedDownload) =
        fusedDownload.packageName.isNotEmpty() && fusedDownload.downloadURLList.isNotEmpty()

    suspend fun calculateProgress(
        fusedApp: FusedApp?,
        progress: DownloadProgress
    ): Int {
        fusedApp?.let { app ->
            val appDownload = getDownloadList()
                .singleOrNull { it.id.contentEquals(app._id) && it.packageName.contentEquals(app.package_name) }
                ?: return 0

            if (!appDownload.id.contentEquals(app._id) || !appDownload.packageName.contentEquals(app.package_name)) {
                return@let
            }

            if (!isProgressValidForApp(fusedApp, progress)) {
                return -1
            }

            val downloadingMap = progress.totalSizeBytes.filter { item ->
                appDownload.downloadIdMap.keys.contains(item.key) && item.value > 0
            }

            if (appDownload.downloadIdMap.size > downloadingMap.size) { // All files for download are not ready yet
                return 0
            }

            val totalSizeBytes = downloadingMap.values.sum()
            val downloadedSoFar = progress.bytesDownloadedSoFar.filter { item ->
                appDownload.downloadIdMap.keys.contains(item.key)
            }.values.sum()
            return ((downloadedSoFar / totalSizeBytes.toDouble()) * 100).toInt()
        }
        return 0
    }

    private suspend fun isProgressValidForApp(
        fusedApp: FusedApp,
        downloadProgress: DownloadProgress
    ): Boolean {
        val download = getFusedDownload(downloadProgress.downloadId)
        return download.id == fusedApp._id
    }

    fun handleRatingFormat(rating: Double): String {
        return if (rating % 1 == 0.0) {
            rating.toInt().toString()
        } else {
            rating.toString()
        }
    }

    suspend fun getCalculateProgressWithTotalSize(fusedApp: FusedApp?, progress: DownloadProgress): Pair<Long, Long> {
        fusedApp?.let { app ->
            val appDownload = getDownloadList()
                .singleOrNull { it.id.contentEquals(app._id) }
            val downloadingMap = progress.totalSizeBytes.filter { item ->
                appDownload?.downloadIdMap?.keys?.contains(item.key) == true
            }
            val totalSizeBytes = downloadingMap.values.sum()
            val downloadedSoFar = progress.bytesDownloadedSoFar.filter { item ->
                appDownload?.downloadIdMap?.keys?.contains(item.key) == true
            }.values.sum()

            return Pair(totalSizeBytes, downloadedSoFar)
        }
        return Pair(1, 0)
    }

    fun getDownloadingItemStatus(fusedApp: FusedApp?, downloadList: List<FusedDownload>): Status? {
        fusedApp?.let { app ->
            val downloadingItem =
                downloadList.find { it.origin == app.origin && (it.packageName == app.package_name || it.id == app.package_name) }
            return downloadingItem?.status
        }
        return null
    }
}
+1 −6
Original line number Diff line number Diff line
@@ -43,12 +43,7 @@ class UpdatesViewModel @Inject constructor(
    fun getUpdates(authData: AuthData) {
        viewModelScope.launch {
            val updatesResult = updatesManagerRepository.getUpdates(authData)
            updatesList.postValue(
                Pair(
                    updatesResult.first.filter { !(!it.isFree && authData.isAnonymous) },
                    updatesResult.second
                )
            )
            updatesList.postValue(updatesResult)
        }
    }

+4 −1
Original line number Diff line number Diff line
@@ -28,7 +28,10 @@ class UpdatesManagerRepository @Inject constructor(
) {

    suspend fun getUpdates(authData: AuthData): Pair<List<FusedApp>, ResultStatus> {
        return updatesManagerImpl.getUpdates(authData)
        return updatesManagerImpl.getUpdates(authData).run {
            val filteredApps = first.filter { !(!it.isFree && authData.isAnonymous) }
            Pair(filteredApps, this.second)
        }
    }

    fun getApplicationCategoryPreference(): String {
Loading