From 052cb9cd6343e76519f7533b61a1f13d2526fc82 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Fri, 24 Jun 2022 18:40:03 +0600 Subject: [PATCH 1/2] fixed: update button status during update --- .../java/foundation/e/apps/updates/UpdatesFragment.kt | 9 +++++++++ 1 file changed, 9 insertions(+) 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 7c1f38b76..e0030cf39 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt @@ -53,6 +53,7 @@ import foundation.e.apps.utils.modules.CommonUtilsModule.safeNavigate import foundation.e.apps.utils.modules.PWAManagerModule import foundation.e.apps.utils.parentFragment.TimeoutFragment import kotlinx.coroutines.launch +import timber.log.Timber import javax.inject.Inject @AndroidEntryPoint @@ -86,9 +87,14 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte */ mainActivityViewModel.internetConnection.observe(viewLifecycleOwner) { + Timber.d("<<< refreshing data: internet connection: $it") + if(!updatesViewModel.updatesList.value?.first.isNullOrEmpty()) { + return@observe + } refreshDataOrRefreshToken(mainActivityViewModel) } mainActivityViewModel.authData.observe(viewLifecycleOwner) { + Timber.d("<<< refreshing data: authData") refreshDataOrRefreshToken(mainActivityViewModel) } @@ -129,6 +135,7 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte } updatesViewModel.updatesList.observe(viewLifecycleOwner) { + Timber.d("<<< Update list observer: ${it.first.size}") listAdapter?.setData(it.first) if (!isDownloadObserverAdded) { observeDownloadList() @@ -190,6 +197,7 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte } override fun refreshData(authData: AuthData) { + Timber.d("<<< refresh data") showLoadingUI() updatesViewModel.getUpdates(authData) binding.button.setOnClickListener { @@ -220,6 +228,7 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte private fun observeDownloadList() { mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> + Timber.d("<<< download list callback") val appList = updatesViewModel.updatesList.value?.first?.toMutableList() ?: emptyList() appList.let { mainActivityViewModel.updateStatusOfFusedApps(appList, list) -- GitLab From 5bdfe7d81dc439aa0462d4d845a849d212441a47 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Mon, 27 Jun 2022 16:16:26 +0600 Subject: [PATCH 2/2] fixed: showing status in Install button fixed: Category page and search page had issues to showing correct status in install button.That's fixed --- .../foundation/e/apps/api/DownloadManager.kt | 44 +++++++++-------- .../e/apps/application/ApplicationFragment.kt | 8 ++-- .../ApplicationListFragment.kt | 19 ++++---- .../manager/workmanager/InstallAppWorker.kt | 22 +++++---- .../e/apps/search/SearchFragment.kt | 47 +++++++------------ .../signin/google/GoogleSignInFragment.kt | 2 +- .../e/apps/updates/UpdatesFragment.kt | 8 +--- 7 files changed, 68 insertions(+), 82 deletions(-) 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 c03400feb..85b1ba552 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 ef93e0a16..261c627c3 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 8b99c8866..8f7a32411 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 4299c44be..e82cc5dad 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 9089b59d4..4bf1e3bd2 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 b08f430da..d21a53646 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 e0030cf39..c8654b0a6 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt @@ -53,7 +53,6 @@ import foundation.e.apps.utils.modules.CommonUtilsModule.safeNavigate import foundation.e.apps.utils.modules.PWAManagerModule import foundation.e.apps.utils.parentFragment.TimeoutFragment import kotlinx.coroutines.launch -import timber.log.Timber import javax.inject.Inject @AndroidEntryPoint @@ -87,14 +86,12 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte */ mainActivityViewModel.internetConnection.observe(viewLifecycleOwner) { - Timber.d("<<< refreshing data: internet connection: $it") - if(!updatesViewModel.updatesList.value?.first.isNullOrEmpty()) { + if (!updatesViewModel.updatesList.value?.first.isNullOrEmpty()) { return@observe } refreshDataOrRefreshToken(mainActivityViewModel) } mainActivityViewModel.authData.observe(viewLifecycleOwner) { - Timber.d("<<< refreshing data: authData") refreshDataOrRefreshToken(mainActivityViewModel) } @@ -135,7 +132,6 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte } updatesViewModel.updatesList.observe(viewLifecycleOwner) { - Timber.d("<<< Update list observer: ${it.first.size}") listAdapter?.setData(it.first) if (!isDownloadObserverAdded) { observeDownloadList() @@ -197,7 +193,6 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte } override fun refreshData(authData: AuthData) { - Timber.d("<<< refresh data") showLoadingUI() updatesViewModel.getUpdates(authData) binding.button.setOnClickListener { @@ -228,7 +223,6 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte private fun observeDownloadList() { mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list -> - Timber.d("<<< download list callback") val appList = updatesViewModel.updatesList.value?.first?.toMutableList() ?: emptyList() appList.let { mainActivityViewModel.updateStatusOfFusedApps(appList, list) -- GitLab