diff --git a/app/src/main/java/foundation/e/apps/AppProgressViewModel.kt b/app/src/main/java/foundation/e/apps/AppProgressViewModel.kt index 42b72c5f00a3c52e44eb91e574817db1ecafd834..ee1bfabcf326ff6eccf57168caf78b0fc345306b 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 258862e53a52b7179392bcfeecc43cd766423bca..476ded3653bfffbd63392319ad8bc3577c1b6b8d 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 a176df2064bc884706d1f473efffb3e999249585..aa379cfddfb67b6a7f311a4a2f230887fd6d92c0 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 781891810162a66a38cb23b444c73e8256eaf9d4..4df4506e4d600b45ff689b798a4ab2f514c05dfb 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 b606f3b9b0280624308c098a33b7be0342650c43..78155f69421b6b38e1d1abd9965eb10c255dbb48 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 3d66b351cbfcb03f26fb16bc09e8e0dac4b725de..c75f22ca383eebcace9aadc999770ab4138acb86 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 6dff83206a63636b17dde0604fdb436652c8ce69..bd89712a095c868bad6354059f1b3754dc10b30b 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%" } } }