diff --git a/app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherActivity.kt b/app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherActivity.kt index 4b2529dbd8c5..9f421caa352c 100644 --- a/app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherActivity.kt +++ b/app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherActivity.kt @@ -115,7 +115,7 @@ class TabSwitcherActivity : DuckDuckGoActivity(), TabSwitcherListener, Coroutine private val viewModel: TabSwitcherViewModel by bindViewModel() - private val tabsAdapter: TabSwitcherAdapter by lazy { TabSwitcherAdapter(this, webViewPreviewPersister, this, faviconManager) } + private val tabsAdapter: TabSwitcherAdapter by lazy { TabSwitcherAdapter(this, webViewPreviewPersister, this, faviconManager, dispatchers) } // we need to scroll to show selected tab, but only if it is the first time loading the tabs. private var firstTimeLoadingTabsList = true diff --git a/app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherAdapter.kt b/app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherAdapter.kt index b33be228fc2a..08cecc03ac9b 100644 --- a/app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherAdapter.kt +++ b/app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherAdapter.kt @@ -47,10 +47,11 @@ import com.duckduckgo.app.tabs.ui.TabSwitcherAdapter.TabViewHolder import com.duckduckgo.app.tabs.ui.TabSwitcherAdapter.TabViewHolder.GridTabViewHolder import com.duckduckgo.app.tabs.ui.TabSwitcherAdapter.TabViewHolder.ListTabViewHolder import com.duckduckgo.common.ui.view.show +import com.duckduckgo.common.utils.DispatcherProvider import com.duckduckgo.common.utils.swap import java.io.File -import java.util.Collections.swap import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import timber.log.Timber class TabSwitcherAdapter( @@ -58,6 +59,7 @@ class TabSwitcherAdapter( private val webViewPreviewPersister: WebViewPreviewPersister, private val lifecycleOwner: LifecycleOwner, private val faviconManager: FaviconManager, + private val dispatchers: DispatcherProvider, ) : Adapter() { private val list = mutableListOf() @@ -174,14 +176,23 @@ class TabSwitcherAdapter( private fun loadTabPreviewImage(tab: TabEntity, glide: RequestManager, holder: GridTabViewHolder) { val previewFile = tab.tabPreviewFile ?: return glide.clear(holder.tabPreview) - val cachedWebViewPreview = File(webViewPreviewPersister.fullPathForFile(tab.tabId, previewFile)) - if (!cachedWebViewPreview.exists()) return glide.clear(holder.tabPreview) - glide.load(cachedWebViewPreview) - .transition(DrawableTransitionOptions.withCrossFade()) - .into(holder.tabPreview) + lifecycleOwner.lifecycleScope.launch { + val cachedWebViewPreview = withContext(dispatchers.io()) { + File(webViewPreviewPersister.fullPathForFile(tab.tabId, previewFile)).takeIf { it.exists() } + } + + if (cachedWebViewPreview == null) { + glide.clear(holder.tabPreview) + return@launch + } + + glide.load(cachedWebViewPreview) + .transition(DrawableTransitionOptions.withCrossFade()) + .into(holder.tabPreview) - holder.tabPreview.show() + holder.tabPreview.show() + } } private fun attachClickListeners(holder: TabViewHolder, tab: TabEntity) {