From ea509841b836ec30bc3d428dfc09f8ca72b268d7 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Wed, 29 Jun 2022 19:40:17 +0600 Subject: [PATCH] Fixed: showing progress in detail page and memory leak --- .../e/apps/application/ApplicationFragment.kt | 6 +++++ .../ApplicationListFragment.kt | 2 ++ .../model/ApplicationListRVAdapter.kt | 24 +++++++++++++++---- .../download/data/DownloadProgressLD.kt | 6 ++--- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt b/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt index 261c627c3..6e884d471 100644 --- a/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt +++ b/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt @@ -679,10 +679,16 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { override fun onDestroyView() { super.onDestroyView() + _binding?.recyclerView?.adapter = null _binding = null applicationIcon = null } + override fun onPause() { + super.onPause() + mainActivityViewModel.downloadList.removeObservers(viewLifecycleOwner) + } + private fun shareApp(name: String, shareUrl: String): Intent { val shareIntent = Intent().apply { action = Intent.ACTION_SEND 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 8f7a32411..cb02d6939 100644 --- a/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt +++ b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt @@ -109,6 +109,7 @@ class ApplicationListFragment : TimeoutFragment(R.layout.fragment_application_li override fun onDestroyView() { super.onDestroyView() + _binding?.recyclerView?.adapter = null _binding = null } @@ -297,6 +298,7 @@ class ApplicationListFragment : TimeoutFragment(R.layout.fragment_application_li override fun onPause() { binding.shimmerLayout.stopShimmer() + mainActivityViewModel.downloadList.removeObservers(viewLifecycleOwner) super.onPause() } diff --git a/app/src/main/java/foundation/e/apps/applicationlist/model/ApplicationListRVAdapter.kt b/app/src/main/java/foundation/e/apps/applicationlist/model/ApplicationListRVAdapter.kt index c6ed486a5..d310cbdb4 100644 --- a/app/src/main/java/foundation/e/apps/applicationlist/model/ApplicationListRVAdapter.kt +++ b/app/src/main/java/foundation/e/apps/applicationlist/model/ApplicationListRVAdapter.kt @@ -66,8 +66,8 @@ class ApplicationListRVAdapter( private val pkgManagerModule: PkgManagerModule, private val pwaManagerModule: PWAManagerModule, private val user: User, - private val lifecycleOwner: LifecycleOwner, - private val paidAppHandler: ((FusedApp) -> Unit)? = null + private var lifecycleOwner: LifecycleOwner?, + private var paidAppHandler: ((FusedApp) -> Unit)? = null ) : ListAdapter(ApplicationDiffUtil()) { private val TAG = ApplicationListRVAdapter::class.java.simpleName @@ -200,7 +200,9 @@ class ApplicationListRVAdapter( } private fun removeIsPurchasedObserver(holder: ViewHolder) { - holder.isPurchasedLiveData.removeObservers(lifecycleOwner) + lifecycleOwner?.let { + holder.isPurchasedLiveData.removeObservers(it) + } } private fun ApplicationListItemBinding.setupInstallButton( @@ -301,7 +303,10 @@ class ApplicationListRVAdapter( searchApp: FusedApp, view: View ) { - privacyInfoViewModel.getAppPrivacyInfoLiveData(searchApp).observe(lifecycleOwner) { + if (lifecycleOwner == null) { + return + } + privacyInfoViewModel.getAppPrivacyInfoLiveData(searchApp).observe(lifecycleOwner!!) { showPrivacyScore() val calculatedScore = privacyInfoViewModel.calculatePrivacyScore(searchApp) if (it.isSuccess() && calculatedScore != -1) { @@ -402,7 +407,10 @@ class ApplicationListRVAdapter( materialButton.text = "" applicationListItemBinding.progressBarInstall.visibility = View.VISIBLE holder.isPurchasedLiveData = appInfoFetchViewModel.isAppPurchased(searchApp) - holder.isPurchasedLiveData.observe(lifecycleOwner) { + if (lifecycleOwner == null) { + return + } + holder.isPurchasedLiveData.observe(lifecycleOwner!!) { materialButton.isEnabled = true applicationListItemBinding.progressBarInstall.visibility = View.GONE materialButton.text = @@ -471,4 +479,10 @@ class ApplicationListRVAdapter( private fun cancelDownload(searchApp: FusedApp) { fusedAPIInterface.cancelDownload(searchApp) } + + override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { + super.onDetachedFromRecyclerView(recyclerView) + lifecycleOwner = null + paidAppHandler = null + } } 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 bba555692..f196fe356 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 @@ -1,6 +1,7 @@ package foundation.e.apps.manager.download.data import android.app.DownloadManager +import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LiveData import androidx.lifecycle.Observer @@ -30,11 +31,8 @@ class DownloadProgressLD @Inject constructor( override fun observe(owner: LifecycleOwner, observer: Observer) { job = Job() super.observe(owner, observer) - - val hasActiveObservers = hasActiveObservers() - launch { - while (hasActiveObservers) { + while (hasActiveObservers() || owner.lifecycle.currentState == Lifecycle.State.RESUMED) { val downloads = fusedManagerRepository.getDownloadList() val downloadingList = downloads.map { it.downloadIdMap }.filter { it.values.contains(false) } -- GitLab