diff --git a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt index e6b4c91fa208287ca39ad198e7df354487c28ee1..9fb625e20c2532c202293a7d9b00b36d38ec8aac 100644 --- a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt @@ -39,6 +39,7 @@ import foundation.e.apps.api.fused.FusedAPIRepository import foundation.e.apps.api.fused.data.FusedApp import foundation.e.apps.manager.database.fusedDownload.FusedDownload 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.Status import foundation.e.apps.utils.enums.Type @@ -55,6 +56,7 @@ class MainActivityViewModel @Inject constructor( private val dataStoreModule: DataStoreModule, private val fusedAPIRepository: FusedAPIRepository, private val fusedManagerRepository: FusedManagerRepository, + private val pkgManagerModule: PkgManagerModule ) : ViewModel() { val authDataJson: LiveData = dataStoreModule.authData.asLiveData() @@ -278,4 +280,13 @@ class MainActivityViewModel @Inject constructor( val internetConnection = liveData { emitSource(ReactiveNetwork().observeInternetConnectivity().asLiveData(Dispatchers.Default)) } + + fun updateStatusOfFusedApps(fusedAppList: List, fusedDownloadList: List) { + fusedAppList.forEach { + val downloadingItem = fusedDownloadList.find { fusedDownload -> + fusedDownload.origin == it.origin && (fusedDownload.packageName == it.package_name || fusedDownload.id == it._id) + } + it.status = downloadingItem?.status ?: pkgManagerModule.getPackageStatus(it.package_name, it.latest_version_code) + } + } } 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 28937de9f9f079dac03b6d2fc7e5a6423111622b..7420575c704290337621848e01ccb7cce51c4d57 100644 --- a/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt +++ b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt @@ -84,15 +84,11 @@ class ApplicationListFragment : Fragment(R.layout.fragment_application_list), Fu private fun observeDownloadList() { mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> - val categoryList = viewModel.appListLiveData.value?.toMutableList() - if (!categoryList.isNullOrEmpty()) { - list.forEach { - categoryList.find { app -> - app.origin == it.origin && (app.package_name == it.packageName || app._id == it.id) - }?.status = it.status - } - viewModel.appListLiveData.value = categoryList + val appList = viewModel.appListLiveData.value?.toMutableList() + appList?.let { + mainActivityViewModel.updateStatusOfFusedApps(it, list) } + viewModel.appListLiveData.value = appList } } @@ -120,7 +116,11 @@ class ApplicationListFragment : Fragment(R.layout.fragment_application_list), Fu ) { fusedApp -> ApplicationDialogFragment( title = getString(R.string.dialog_title_paid_app, fusedApp.name), - message = getString(R.string.dialog_paidapp_message, fusedApp.name, fusedApp.price), + message = getString( + R.string.dialog_paidapp_message, + fusedApp.name, + fusedApp.price + ), positiveButtonText = getString(R.string.dialog_confirm), positiveButtonAction = { getApplication(fusedApp) diff --git a/app/src/main/java/foundation/e/apps/home/model/HomeParentRVAdapter.kt b/app/src/main/java/foundation/e/apps/home/model/HomeParentRVAdapter.kt index 5d32668340ae803712a921da458565bdbaa4c75a..6d5060b5744a9c170f1de73bb2eda3408f3fccda 100644 --- a/app/src/main/java/foundation/e/apps/home/model/HomeParentRVAdapter.kt +++ b/app/src/main/java/foundation/e/apps/home/model/HomeParentRVAdapter.kt @@ -29,7 +29,6 @@ import foundation.e.apps.api.fused.FusedAPIInterface import foundation.e.apps.api.fused.data.FusedApp import foundation.e.apps.api.fused.data.FusedHome import foundation.e.apps.databinding.HomeParentListItemBinding -import foundation.e.apps.manager.database.fusedDownload.FusedDownload import foundation.e.apps.manager.pkg.PkgManagerModule import foundation.e.apps.utils.enums.User @@ -43,6 +42,7 @@ class HomeParentRVAdapter( ) : ListAdapter(FusedHomeDiffUtil()) { private val viewPool = RecyclerView.RecycledViewPool() + private var isDownloadObserverAdded = false inner class ViewHolder(val binding: HomeParentListItemBinding) : RecyclerView.ViewHolder(binding.root) @@ -79,26 +79,11 @@ class HomeParentRVAdapter( homeChildRVAdapter: RecyclerView.Adapter<*>? ) { mainActivityViewModel.downloadList.observe(lifecycleOwner) { - updateInstallingAppStatus(it, fusedHome) + mainActivityViewModel.updateStatusOfFusedApps(fusedHome.list, it) (homeChildRVAdapter as HomeChildRVAdapter).setData(fusedHome.list) } } - private fun updateInstallingAppStatus( - downloadList: List, - fusedHome: FusedHome - ) { - downloadList.forEach { fusedDownload -> - findInstallingApp(fusedHome, fusedDownload)?.status = fusedDownload.status - } - } - - private fun findInstallingApp(fusedHome: FusedHome, fusedDownload: FusedDownload): FusedApp? { - return fusedHome.list.find { app -> - app.origin == fusedDownload.origin && (app.package_name == fusedDownload.packageName || app._id == fusedDownload.id) - } - } - fun setData(newList: List) { submitList(newList.map { it.copy() }) } diff --git a/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt b/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt index f0722088943d99c250faee6d9fd493fdd8e1ea36..196950bea8161e0385c89584af96772e1303f016 100644 --- a/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt +++ b/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt @@ -119,13 +119,15 @@ class InstallAppWorker @AssistedInject constructor( ) { databaseRepository.getDownloadFlowById(it.id).takeWhile { isDownloading } .collect { fusedDownload -> - fusedDownload?.let { - Log.d( - TAG, - "doWork: flow collect ===> ${fusedDownload.name} ${fusedDownload.status}" - ) - handleFusedDownloadStatus(fusedDownload) + if (fusedDownload == null) { + isDownloading = false + return@collect } + Log.d( + TAG, + "doWork: flow collect ===> ${fusedDownload.name} ${fusedDownload.status}" + ) + handleFusedDownloadStatus(fusedDownload) } } 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 77d8e04714f302ecbdbfa0db3680fd32b752b6b2..47c72bf8758987f82f56c3c8155d5693a8dc5405 100644 --- a/app/src/main/java/foundation/e/apps/search/SearchFragment.kt +++ b/app/src/main/java/foundation/e/apps/search/SearchFragment.kt @@ -164,15 +164,11 @@ class SearchFragment : } mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> - val searchResult = searchViewModel.searchResult.value?.toMutableList() - if (!searchResult.isNullOrEmpty()) { - list.forEach { - searchResult.find { app -> - app.origin == it.origin && (app.package_name == it.packageName || app._id == it.id) - }?.status = it.status - } - searchViewModel.searchResult.value = searchResult + val searchList = searchViewModel.searchResult.value?.toMutableList() + searchList?.let { + mainActivityViewModel.updateStatusOfFusedApps(searchList, list) } + searchViewModel.searchResult.value = searchList } searchViewModel.searchResult.observe(viewLifecycleOwner) { 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 973dfd2e6bb8fef7a2165e596546ef206fa269d1..ae7975c852240a3357bb9841ead16520bb169c60 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt @@ -61,6 +61,8 @@ class UpdatesFragment : Fragment(R.layout.fragment_updates), FusedAPIInterface { private val mainActivityViewModel: MainActivityViewModel by activityViewModels() private val appProgressViewModel: AppProgressViewModel by viewModels() + private var isDownloadObserverAdded = false + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) _binding = FragmentUpdatesBinding.bind(view) @@ -93,7 +95,11 @@ class UpdatesFragment : Fragment(R.layout.fragment_updates), FusedAPIInterface { ) { fusedApp -> ApplicationDialogFragment( title = getString(R.string.dialog_title_paid_app, fusedApp.name), - message = getString(R.string.dialog_paidapp_message, fusedApp.name, fusedApp.price), + message = getString( + R.string.dialog_paidapp_message, + fusedApp.name, + fusedApp.price + ), positiveButtonText = getString(R.string.dialog_confirm), positiveButtonAction = { }, @@ -111,20 +117,12 @@ class UpdatesFragment : Fragment(R.layout.fragment_updates), FusedAPIInterface { updateProgressOfDownloadingItems(recyclerView, it) } - mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> - val updatesList = updatesViewModel.updatesList.value?.toMutableList() - if (!updatesList.isNullOrEmpty()) { - list.forEach { - updatesList.find { app -> - app.origin == it.origin && (app.package_name == it.packageName || app._id == it.id) - }?.status = it.status - } - updatesViewModel.updatesList.value = updatesList - } - } - updatesViewModel.updatesList.observe(viewLifecycleOwner) { listAdapter?.setData(it) + if (!isDownloadObserverAdded) { + observeDownloadList() + isDownloadObserverAdded = true + } binding.progressBar.visibility = View.GONE recyclerView.visibility = View.VISIBLE if (!it.isNullOrEmpty()) { @@ -137,6 +135,16 @@ class UpdatesFragment : Fragment(R.layout.fragment_updates), FusedAPIInterface { } } + private fun observeDownloadList() { + mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> + val appList = updatesViewModel.updatesList.value?.toMutableList() + appList?.let { + mainActivityViewModel.updateStatusOfFusedApps(appList, list) + } + updatesViewModel.updatesList.value = appList + } + } + private fun updateProgressOfDownloadingItems( recyclerView: RecyclerView, downloadProgress: DownloadProgress