diff --git a/app/src/main/java/foundation/e/apps/api/DownloadManager.kt b/app/src/main/java/foundation/e/apps/api/DownloadManager.kt index c03400febbf3d4503fd9e6df355f58e6d14fe511..85b1ba5525f8e08f7740220e916803e080b44820 100644 --- a/app/src/main/java/foundation/e/apps/api/DownloadManager.kt +++ b/app/src/main/java/foundation/e/apps/api/DownloadManager.kt @@ -68,28 +68,32 @@ class DownloadManager @Inject constructor( filePath: String = "", downloadCompleted: ((Boolean, String) -> Unit)? ) { - downloadManager.query(downloadManagerQuery.setFilterById(downloadId)) - .use { cursor -> - if (cursor.moveToFirst()) { - val id = - cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID)) - val status = - cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)) - val totalSizeBytes = - cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)) - val bytesDownloadedSoFar = - cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)) - if (status == DownloadManager.STATUS_FAILED) { - Timber.d("Download Failed: $filePath=> $bytesDownloadedSoFar/$totalSizeBytes $status") - isDownloading = false - downloadCompleted?.invoke(false, filePath) - } else if (status == DownloadManager.STATUS_SUCCESSFUL) { - Timber.d("Download Successful: $filePath=> $bytesDownloadedSoFar/$totalSizeBytes $status") - isDownloading = false - downloadCompleted?.invoke(true, filePath) + try { + downloadManager.query(downloadManagerQuery.setFilterById(downloadId)) + .use { cursor -> + if (cursor.moveToFirst()) { + val id = + cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID)) + val status = + cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)) + val totalSizeBytes = + cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)) + val bytesDownloadedSoFar = + cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)) + if (status == DownloadManager.STATUS_FAILED) { + Timber.d("Download Failed: $filePath=> $bytesDownloadedSoFar/$totalSizeBytes $status") + isDownloading = false + downloadCompleted?.invoke(false, filePath) + } else if (status == DownloadManager.STATUS_SUCCESSFUL) { + Timber.d("Download Successful: $filePath=> $bytesDownloadedSoFar/$totalSizeBytes $status") + isDownloading = false + downloadCompleted?.invoke(true, filePath) + } } } - } + } catch (e: Exception) { + Timber.e(e) + } } private fun tickerFlow(period: Duration, initialDelay: Duration = Duration.ZERO) = flow { 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 ef93e0a160f03aa1deb2db8441ed43ccc0b5932e..261c627c30b16c8cd654faf15c76372a9ebd4bd7 100644 --- a/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt +++ b/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt @@ -160,10 +160,6 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { binding.applicationLayout.visibility = View.INVISIBLE - mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> - applicationViewModel.updateApplicationStatus(list) - } - applicationViewModel.fusedApp.observe(viewLifecycleOwner) { resultPair -> if (resultPair.second != ResultStatus.OK) { onTimeout() @@ -306,6 +302,10 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { binding.snackbarLayout.visibility = View.VISIBLE } fetchAppTracker(it) + + mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> + applicationViewModel.updateApplicationStatus(list) + } } applicationViewModel.errorMessageLiveData.observe(viewLifecycleOwner) { 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 8b99c88667c072b25cc8e3e486c3701c0fadd4e9..8f7a324118876c48224d322bc4eb09eaf8968f3d 100644 --- a/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt +++ b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt @@ -69,7 +69,6 @@ class ApplicationListFragment : TimeoutFragment(R.layout.fragment_application_li private var _binding: FragmentApplicationListBinding? = null private val binding get() = _binding!! - private var isDownloadObserverAdded = false /* * Prevent reloading apps. @@ -93,18 +92,18 @@ class ApplicationListFragment : TimeoutFragment(R.layout.fragment_application_li } } - private fun observeDownloadList() { + private fun observeDownloadList(adapter: ApplicationListRVAdapter) { mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> val appList = viewModel.appListLiveData.value?.data?.toMutableList() ?: emptyList() appList.let { mainActivityViewModel.updateStatusOfFusedApps(it, list) + adapter.setData(it) } /* * Done in one line, so that on Ctrl+click on appListLiveData, * we can see that it is being updated here. */ - viewModel.appListLiveData.apply { value?.setData(appList) } } } @@ -161,9 +160,12 @@ class ApplicationListFragment : TimeoutFragment(R.layout.fragment_application_li } else { isDetailsLoaded = true listAdapter?.setData(it.data!!) - if (!isDownloadObserverAdded) { - observeDownloadList() - isDownloadObserverAdded = true + listAdapter?.let { adapter -> + observeDownloadList(adapter) + } + + appProgressViewModel.downloadProgress.observe(viewLifecycleOwner) { + updateProgressOfDownloadingItems(binding.recyclerView, it) } } stopLoadingUI() @@ -255,10 +257,6 @@ class ApplicationListFragment : TimeoutFragment(R.layout.fragment_application_li } } } - - appProgressViewModel.downloadProgress.observe(viewLifecycleOwner) { - updateProgressOfDownloadingItems(binding.recyclerView, it) - } } private fun showLoadingUI() { @@ -298,7 +296,6 @@ class ApplicationListFragment : TimeoutFragment(R.layout.fragment_application_li } override fun onPause() { - isDownloadObserverAdded = false binding.shimmerLayout.stopShimmer() super.onPause() } 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 4299c44be5326ce5b6c2e698599ac29e86765447..e82cc5dadb2b5f4fa3f786c7c1393ef4be856dd7 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 @@ -158,17 +158,21 @@ class InstallAppWorker @AssistedInject constructor( } private suspend fun checkDownloadProcess(fusedDownload: FusedDownload) { - downloadManager.query(downloadManagerQuery.setFilterById(*fusedDownload.downloadIdMap.keys.toLongArray())) - .use { cursor -> - if (cursor.moveToFirst()) { - val status = - cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)) - - if (status == DownloadManager.STATUS_FAILED) { - fusedManagerRepository.installationIssue(fusedDownload) + try { + downloadManager.query(downloadManagerQuery.setFilterById(*fusedDownload.downloadIdMap.keys.toLongArray())) + .use { cursor -> + if (cursor.moveToFirst()) { + val status = + cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)) + + if (status == DownloadManager.STATUS_FAILED) { + fusedManagerRepository.installationIssue(fusedDownload) + } } } - } + } catch (e: Exception) { + Timber.e(e) + } } private suspend fun handleFusedDownloadStatus(fusedDownload: 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 9089b59d4d8b994a459120b45dc5757ee534e2ff..4bf1e3bd209fc9859ec7aaf9430323250566575b 100644 --- a/app/src/main/java/foundation/e/apps/search/SearchFragment.kt +++ b/app/src/main/java/foundation/e/apps/search/SearchFragment.kt @@ -161,36 +161,6 @@ class SearchFragment : layoutManager = LinearLayoutManager(view.context) } - mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> - val searchList = - searchViewModel.searchResult.value?.data?.first?.toMutableList() ?: emptyList() - searchList.let { - mainActivityViewModel.updateStatusOfFusedApps(searchList, list) - } - - /* - * Done in one line, so that on Ctrl+click on searchResult, - * we can see that it is being updated here. - */ - searchViewModel.searchResult.apply { value?.setData(Pair(searchList, value?.data?.second ?: false)) } - } - - /* - * Explanation of double observers in HomeFragment.kt - * Modified to check and search only if searchText in not blank, to prevent blank search. - */ - - mainActivityViewModel.internetConnection.observe(viewLifecycleOwner) { - if (searchText.isNotBlank()) { - refreshDataOrRefreshToken(mainActivityViewModel) - } - } - mainActivityViewModel.authData.observe(viewLifecycleOwner) { - if (searchText.isNotBlank()) { - refreshDataOrRefreshToken(mainActivityViewModel) - } - } - searchViewModel.searchResult.observe(viewLifecycleOwner) { if (it.data?.first.isNullOrEmpty()) { noAppsFoundLayout?.visibility = View.VISIBLE @@ -199,7 +169,13 @@ class SearchFragment : binding.loadingProgressBar.isVisible = it.data!!.second stopLoadingUI() noAppsFoundLayout?.visibility = View.GONE + searchHintLayout?.visibility = View.GONE + } + + listAdapter?.let { adapter -> + observeDownloadList(adapter) } + listAdapter?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { override fun onItemRangeInserted(positionStart: Int, itemCount: Int) { searchView?.run { @@ -227,6 +203,17 @@ class SearchFragment : } } + private fun observeDownloadList(applicationListRVAdapter: ApplicationListRVAdapter) { + mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> + val searchList = + searchViewModel.searchResult.value?.data?.first?.toMutableList() ?: emptyList() + searchList.let { + mainActivityViewModel.updateStatusOfFusedApps(searchList, list) + applicationListRVAdapter.setData(it) + } + } + } + override fun onTimeout() { if (!isTimeoutDialogDisplayed()) { binding.loadingProgressBar.isVisible = false diff --git a/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt b/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt index b08f430dad1486e01118ba8da0047a7c44a43cc8..d21a536467e9210f5157e3c252e5bd9e717eeba1 100644 --- a/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt +++ b/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt @@ -80,7 +80,7 @@ class GoogleSignInFragment : Fragment(R.layout.fragment_google_signin) { binding.webview.webViewClient = object : WebViewClient() { override fun onPageFinished(view: WebView, url: String) { - val cookies = CookieManager.getInstance().getCookie(url) + val cookies = CookieManager.getInstance().getCookie(url) ?: return val cookieMap = AC2DMUtil.parseCookieString(cookies) if (cookieMap.isNotEmpty() && cookieMap[AUTH_TOKEN] != null) { val oauthToken = cookieMap[AUTH_TOKEN] ?: "" 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 7c1f38b76e7d3a0cf9c37dd65033f3863b13436b..c8654b0a603efa1b43a3d3ba0eec717c3bc03d6b 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt @@ -86,6 +86,9 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte */ mainActivityViewModel.internetConnection.observe(viewLifecycleOwner) { + if (!updatesViewModel.updatesList.value?.first.isNullOrEmpty()) { + return@observe + } refreshDataOrRefreshToken(mainActivityViewModel) } mainActivityViewModel.authData.observe(viewLifecycleOwner) {