Skip to content

Commit

Permalink
Android ANR: com.duckduckgo.app.tabs.ui.TabSwitcherAdapter.loadTabPre…
Browse files Browse the repository at this point in the history
…viewImage (#5432)

Task/Issue URL:
https://app.asana.com/0/488551667048375/1209069285792730/f

### Description
Moved file operations to background.

### Steps to test this PR

- [x] Install from this branch.
- [x] Open many tabs and go to the Tab Switcher screen.
- [x] Notice the tabs are loaded properly and the app doesn't freeze.

### NO UI changes
  • Loading branch information
anikiki authored Jan 6, 2025
1 parent 11dcf5b commit ad28ee1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ 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(
private val itemClickListener: TabSwitcherListener,
private val webViewPreviewPersister: WebViewPreviewPersister,
private val lifecycleOwner: LifecycleOwner,
private val faviconManager: FaviconManager,
private val dispatchers: DispatcherProvider,
) : Adapter<TabViewHolder>() {

private val list = mutableListOf<TabEntity>()
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ad28ee1

Please sign in to comment.