From 4bf3bddf5b7c1d4451f4a4a2b9bfe84d064575d7 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Tue, 7 Jun 2022 15:44:57 +0600 Subject: [PATCH] App Lounge: fixed inconsistency of showing progress --- .../foundation/e/apps/AppProgressViewModel.kt | 20 +++++++++++++++---- .../ApplicationListFragment.kt | 11 +++++----- .../foundation/e/apps/home/HomeFragment.kt | 7 ++++--- .../manager/download/data/DownloadProgress.kt | 3 ++- .../download/data/DownloadProgressLD.kt | 2 ++ .../e/apps/search/SearchFragment.kt | 7 ++++--- .../e/apps/updates/UpdatesFragment.kt | 7 ++++--- 7 files changed, 38 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/AppProgressViewModel.kt b/app/src/main/java/foundation/e/apps/AppProgressViewModel.kt index 42b72c5f0..ee1bfabcf 100644 --- a/app/src/main/java/foundation/e/apps/AppProgressViewModel.kt +++ b/app/src/main/java/foundation/e/apps/AppProgressViewModel.kt @@ -19,15 +19,19 @@ class AppProgressViewModel @Inject constructor( suspend fun calculateProgress( fusedApp: FusedApp?, progress: DownloadProgress - ): Pair { + ): 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 } } diff --git a/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt index 258862e53..476ded365 100644 --- a/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt +++ b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt @@ -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%" } } } diff --git a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt index a176df206..aa379cfdd 100644 --- a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt +++ b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt @@ -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%" } } } diff --git a/app/src/main/java/foundation/e/apps/manager/download/data/DownloadProgress.kt b/app/src/main/java/foundation/e/apps/manager/download/data/DownloadProgress.kt index 781891810..4df4506e4 100644 --- a/app/src/main/java/foundation/e/apps/manager/download/data/DownloadProgress.kt +++ b/app/src/main/java/foundation/e/apps/manager/download/data/DownloadProgress.kt @@ -3,5 +3,6 @@ package foundation.e.apps.manager.download.data data class DownloadProgress( var totalSizeBytes: MutableMap = mutableMapOf(), var bytesDownloadedSoFar: MutableMap = mutableMapOf(), - var status: MutableMap = mutableMapOf() + var status: MutableMap = mutableMapOf(), + var downloadId: Long = -1 ) diff --git a/app/src/main/java/foundation/e/apps/manager/download/data/DownloadProgressLD.kt b/app/src/main/java/foundation/e/apps/manager/download/data/DownloadProgressLD.kt index b606f3b9b..78155f694 100644 --- a/app/src/main/java/foundation/e/apps/manager/download/data/DownloadProgressLD.kt +++ b/app/src/main/java/foundation/e/apps/manager/download/data/DownloadProgressLD.kt @@ -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 ) { diff --git a/app/src/main/java/foundation/e/apps/search/SearchFragment.kt b/app/src/main/java/foundation/e/apps/search/SearchFragment.kt index 3d66b351c..c75f22ca3 100644 --- a/app/src/main/java/foundation/e/apps/search/SearchFragment.kt +++ b/app/src/main/java/foundation/e/apps/search/SearchFragment.kt @@ -258,14 +258,15 @@ class SearchFragment : 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 viewHolder = recyclerView?.findViewHolderForAdapterPosition( adapter.currentList.indexOf(fusedApp) ) viewHolder?.let { (viewHolder as ApplicationListRVAdapter.ViewHolder).binding.installButton.text = - "$downloadProgress%" + "$progress%" } } } diff --git a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt index 6dff83206..bd89712a0 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt @@ -224,14 +224,15 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte 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 viewHolder = recyclerView.findViewHolderForAdapterPosition( adapter.currentList.indexOf(fusedApp) ) viewHolder?.let { (viewHolder as ApplicationListRVAdapter.ViewHolder).binding.installButton.text = - "$downloadProgress%" + "$progress%" } } } -- GitLab