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

Commit 4bf3bddf authored by Hasib Prince's avatar Hasib Prince
Browse files

App Lounge: fixed inconsistency of showing progress

parent 7f70f56e
Loading
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -19,15 +19,19 @@ class AppProgressViewModel @Inject constructor(
    suspend fun calculateProgress(
        fusedApp: FusedApp?,
        progress: DownloadProgress
    ): Pair<Long, Long> {
    ): Int {
        fusedApp?.let { app ->
            val appDownload = fusedManagerRepository.getDownloadList()
                .singleOrNull { it.id.contentEquals(app._id) && it.packageName.contentEquals(app.package_name) }
                ?: return Pair(1, 0)
                ?: 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)
            }
@@ -36,8 +40,16 @@ class AppProgressViewModel @Inject constructor(
                appDownload.downloadIdMap.keys.contains(item.key)
            }.values.sum()

            return Pair(totalSizeBytes, downloadedSoFar)
            return ((downloadedSoFar / totalSizeBytes.toDouble()) * 100).toInt()
        }
        return Pair(1, 0)
        return 0
    }

    private suspend fun isProgressValidForApp(
        fusedApp: FusedApp,
        downloadProgress: DownloadProgress
    ): Boolean {
        val download = fusedManagerRepository.getFusedDownload(downloadProgress.downloadId)
        return download.id == fusedApp._id
    }
}
+6 −5
Original line number Diff line number Diff line
@@ -242,21 +242,22 @@ class ApplicationListFragment : TimeoutFragment(R.layout.fragment_application_li

    private fun updateProgressOfDownloadingItems(
        recyclerView: RecyclerView,
        it: DownloadProgress
        downloadProgress: DownloadProgress
    ) {
        val adapter = recyclerView.adapter as ApplicationListRVAdapter
        lifecycleScope.launch {
            adapter.currentList.forEach { fusedApp ->
                if (fusedApp.status == Status.DOWNLOADING) {
                    val progress = appProgressViewModel.calculateProgress(fusedApp, it)
                    val downloadProgress =
                        ((progress.second / progress.first.toDouble()) * 100).toInt()
                    val progress = appProgressViewModel.calculateProgress(fusedApp, downloadProgress)
                    if (progress == -1) {
                        return@forEach
                    }
                    val viewHolder = recyclerView.findViewHolderForAdapterPosition(
                        adapter.currentList.indexOf(fusedApp)
                    )
                    viewHolder?.let {
                        (viewHolder as ApplicationListRVAdapter.ViewHolder).binding.installButton.text =
                            "$downloadProgress%"
                            "$progress%"
                    }
                }
            }
+4 −3
Original line number Diff line number Diff line
@@ -238,14 +238,15 @@ class HomeFragment : TimeoutFragment(R.layout.fragment_home), FusedAPIInterface
                if (fusedApp.status == Status.DOWNLOADING) {
                    val progress =
                        appProgressViewModel.calculateProgress(fusedApp, downloadProgress)
                    val downloadProgress =
                        ((progress.second / progress.first.toDouble()) * 100).toInt()
                    if (progress == -1) {
                        return@forEach
                    }
                    val childViewHolder = childRV.findViewHolderForAdapterPosition(
                        adapter.currentList.indexOf(fusedApp)
                    )
                    childViewHolder?.let {
                        (childViewHolder as HomeChildRVAdapter.ViewHolder).binding.installButton.text =
                            "$downloadProgress%"
                            "$progress%"
                    }
                }
            }
+2 −1
Original line number Diff line number Diff line
@@ -3,5 +3,6 @@ package foundation.e.apps.manager.download.data
data class DownloadProgress(
    var totalSizeBytes: MutableMap<Long, Long> = mutableMapOf(),
    var bytesDownloadedSoFar: MutableMap<Long, Long> = mutableMapOf(),
    var status: MutableMap<Long, Boolean> = mutableMapOf()
    var status: MutableMap<Long, Boolean> = mutableMapOf(),
    var downloadId: Long = -1
)
+2 −0
Original line number Diff line number Diff line
@@ -59,6 +59,8 @@ class DownloadProgressLD @Inject constructor(
                                val bytesDownloadedSoFar =
                                    cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))

                                downloadProgress.downloadId = id

                                if (!downloadProgress.totalSizeBytes.containsKey(id) ||
                                    downloadProgress.totalSizeBytes[id] != totalSizeBytes
                                ) {
Loading