From 0eb2f328dc5efe699d78cf2224cb59d636c14cfd Mon Sep 17 00:00:00 2001 From: "codrut.topliceanu" Date: Fri, 12 Jun 2020 17:39:12 +0300 Subject: [PATCH] For #11371 - Fix for Dynamic Download Dialog --- .../fenix/browser/BaseBrowserFragment.kt | 50 +++++++++++++------ 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt index e52c2fd34..d90f4e6f3 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt @@ -357,9 +357,13 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session downloadJobStatus == AbstractFetchDownloadService.DownloadJobStatus.FAILED ) { - saveDownloadDialogState(session, downloadState, downloadJobStatus) + saveDownloadDialogState( + downloadState.sessionId, + downloadState, + downloadJobStatus + ) - DynamicDownloadDialog( + val dynamicDownloadDialog = DynamicDownloadDialog( container = view.browserLayout, downloadState = downloadState, didFail = downloadJobStatus == AbstractFetchDownloadService.DownloadJobStatus.FAILED, @@ -375,20 +379,26 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session }, view = view.viewDynamicDownloadDialog, toolbarHeight = toolbarHeight, - onDismiss = { sharedViewModel.downloadDialogState.remove(session.id) } - ).show() - browserToolbarView.expand() + onDismiss = { sharedViewModel.downloadDialogState.remove(downloadState.sessionId) } + ) + + // Don't show the dialog if we aren't in the tab that started the download + if (downloadState.sessionId == sessionManager.selectedSession?.id) { + dynamicDownloadDialog.show() + browserToolbarView.expand() + } } } + resumeDownloadDialogState(sessionManager.selectedSession?.id, + store, view, context, toolbarHeight) + downloadsFeature.set( downloadFeature, owner = this, view = view ) - resumeDownloadDialogState(session, store, view, context, toolbarHeight) - pipFeature = PictureInPictureFeature( store = store, activity = requireActivity(), @@ -531,6 +541,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session override fun onSessionSelected(session: Session) { fullScreenChanged(false) browserToolbarView.expand() + resumeDownloadDialogState(session.id, store, view, context, toolbarHeight) } }, owner = viewLifecycleOwner) @@ -583,14 +594,16 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session * other fragments navigation. * */ private fun saveDownloadDialogState( - session: Session, + sessionId: String?, downloadState: DownloadState, downloadJobStatus: AbstractFetchDownloadService.DownloadJobStatus ) { - sharedViewModel.downloadDialogState[session.id] = Pair( - downloadState, - downloadJobStatus == AbstractFetchDownloadService.DownloadJobStatus.FAILED - ) + sessionId?.let { id -> + sharedViewModel.downloadDialogState[id] = Pair( + downloadState, + downloadJobStatus == AbstractFetchDownloadService.DownloadJobStatus.FAILED + ) + } } /** @@ -600,20 +613,25 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session * download, because [DownloadsFeature] clears any queued downloads onStop. * */ private fun resumeDownloadDialogState( - session: Session, + sessionId: String?, store: BrowserStore, view: View, context: Context, toolbarHeight: Int ) { val savedDownloadState = - sharedViewModel.downloadDialogState[session.id] ?: return + sharedViewModel.downloadDialogState[sessionId] + + if (savedDownloadState == null || sessionId == null) { + view.viewDynamicDownloadDialog.visibility = View.GONE + return + } val onTryAgain: (Long) -> Unit = { savedDownloadState.first?.let { dlState -> store.dispatch( ContentAction.UpdateDownloadAction( - session.id, dlState.copy(skipConfirmation = true) + sessionId, dlState.copy(skipConfirmation = true) ) ) } @@ -630,7 +648,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session } val onDismiss: () -> Unit = - { sharedViewModel.downloadDialogState.remove(session.id) } + { sharedViewModel.downloadDialogState.remove(sessionId) } DynamicDownloadDialog( container = view.browserLayout,