Skip to content

Commit

Permalink
Fix alpha resetting for the TrackerAnimationTile
Browse files Browse the repository at this point in the history
The previous commit fixed issues with drag and drop of tabs and glitches. Unfortunately it meant that the alpha state of the TrackerAnimationTile seemed to change as well, this seems to be due to the TabSwitcherItemDiffCallback being called and then triggering changes.

Now we pass through the isDragging state to the diff callback so that we can ensure that if we update the list that we maintain the TrackerAnimationTile alpha state, otherwise it would just reset to default.
  • Loading branch information
mikescamell committed Mar 6, 2025
1 parent 880f06a commit 0f66c87
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ package com.duckduckgo.app.browser.tabs.adapter
import android.os.Bundle
import androidx.recyclerview.widget.DiffUtil
import com.duckduckgo.app.tabs.ui.TabSwitcherItem
import com.duckduckgo.app.tabs.ui.TabSwitcherItem.TrackerAnimationTile.ANIMATED_TILE_DEFAULT_ALPHA
import com.duckduckgo.app.tabs.ui.TabSwitcherItem.TrackerAnimationTile.ANIMATED_TILE_NO_REPLACE_ALPHA

class TabSwitcherItemDiffCallback(old: List<TabSwitcherItem>, new: List<TabSwitcherItem>) : DiffUtil.Callback() {
class TabSwitcherItemDiffCallback(
old: List<TabSwitcherItem>,
new: List<TabSwitcherItem>,
private val isDragging: Boolean,
) : DiffUtil.Callback() {

// keep a local copy of the lists to avoid any changes to the lists during the diffing process
private val oldList = old.toList()
Expand Down Expand Up @@ -72,6 +78,12 @@ class TabSwitcherItemDiffCallback(old: List<TabSwitcherItem>, new: List<TabSwitc
diffBundle.putString(DIFF_KEY_PREVIEW, newItem.tabEntity.tabPreviewFile)
}
}
oldItem is TabSwitcherItem.TrackerAnimationTile && newItem is TabSwitcherItem.TrackerAnimationTile -> {
diffBundle.putFloat(
DIFF_ALPHA,
if (isDragging) ANIMATED_TILE_NO_REPLACE_ALPHA else ANIMATED_TILE_DEFAULT_ALPHA,
)
}
}

return diffBundle
Expand Down Expand Up @@ -114,5 +126,6 @@ class TabSwitcherItemDiffCallback(old: List<TabSwitcherItem>, new: List<TabSwitc
const val DIFF_KEY_URL = "url"
const val DIFF_KEY_PREVIEW = "previewImage"
const val DIFF_KEY_VIEWED = "viewed"
const val DIFF_ALPHA = "alpha"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import com.duckduckgo.app.browser.databinding.ItemTabListBinding
import com.duckduckgo.app.browser.favicon.FaviconManager
import com.duckduckgo.app.browser.tabpreview.WebViewPreviewPersister
import com.duckduckgo.app.browser.tabs.adapter.TabSwitcherItemDiffCallback
import com.duckduckgo.app.browser.tabs.adapter.TabSwitcherItemDiffCallback.Companion.DIFF_ALPHA
import com.duckduckgo.app.browser.tabs.adapter.TabSwitcherItemDiffCallback.Companion.DIFF_KEY_PREVIEW
import com.duckduckgo.app.browser.tabs.adapter.TabSwitcherItemDiffCallback.Companion.DIFF_KEY_TITLE
import com.duckduckgo.app.browser.tabs.adapter.TabSwitcherItemDiffCallback.Companion.DIFF_KEY_URL
Expand All @@ -53,6 +54,8 @@ import com.duckduckgo.app.tabs.ui.TabSwitcherAdapter.TabSwitcherViewHolder.Compa
import com.duckduckgo.app.tabs.ui.TabSwitcherAdapter.TabSwitcherViewHolder.Companion.LIST_TAB
import com.duckduckgo.app.tabs.ui.TabSwitcherAdapter.TabSwitcherViewHolder.Companion.LIST_TRACKER_ANIMATION_TILE
import com.duckduckgo.app.tabs.ui.TabSwitcherAdapter.TabSwitcherViewHolder.TabViewHolder
import com.duckduckgo.app.tabs.ui.TabSwitcherItem.TrackerAnimationTile.ANIMATED_TILE_DEFAULT_ALPHA
import com.duckduckgo.app.tabs.ui.TabSwitcherItem.TrackerAnimationTile.ANIMATED_TILE_NO_REPLACE_ALPHA
import com.duckduckgo.common.ui.view.show
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.common.utils.swap
Expand All @@ -63,10 +66,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber

private const val ANIMATED_TILE_NO_REPLACE_ALPHA = 0.4f
private const val ANIMATED_TILE_DEFAULT_ALPHA = 1f
private const val ALPHA = "alpha"

class TabSwitcherAdapter(
private val itemClickListener: TabSwitcherListener,
private val webViewPreviewPersister: WebViewPreviewPersister,
Expand Down Expand Up @@ -242,7 +241,7 @@ class TabSwitcherAdapter(
GRID_TRACKER_ANIMATION_TILE, LIST_TRACKER_ANIMATION_TILE -> {
for (payload in payloads) {
val bundle = payload as Bundle
holder.itemView.alpha = bundle.getFloat("alpha", ANIMATED_TILE_DEFAULT_ALPHA)
holder.itemView.alpha = bundle.getFloat(DIFF_ALPHA, ANIMATED_TILE_DEFAULT_ALPHA)
}
}
}
Expand Down Expand Up @@ -342,7 +341,7 @@ class TabSwitcherAdapter(
}

fun updateData(updatedList: List<TabSwitcherItem>) {
val diffResult = DiffUtil.calculateDiff(TabSwitcherItemDiffCallback(list, updatedList))
val diffResult = DiffUtil.calculateDiff(TabSwitcherItemDiffCallback(list, updatedList, isDragging))
list.clear()
list.addAll(updatedList)
diffResult.dispatchUpdatesTo(this)
Expand Down Expand Up @@ -370,7 +369,7 @@ class TabSwitcherAdapter(
notifyItemChanged(
animatedTilePosition,
Bundle().apply {
putFloat(ALPHA, alpha)
putFloat(DIFF_ALPHA, alpha)
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ import com.duckduckgo.app.tabs.model.TabEntity
sealed class TabSwitcherItem(val id: String) {

data class Tab(val tabEntity: TabEntity) : TabSwitcherItem(tabEntity.tabId)
data object TrackerAnimationTile : TabSwitcherItem("TrackerAnimationTile")
data object TrackerAnimationTile : TabSwitcherItem("TrackerAnimationTile") {

const val ANIMATED_TILE_NO_REPLACE_ALPHA = 0.4f
const val ANIMATED_TILE_DEFAULT_ALPHA = 1f
}
}

0 comments on commit 0f66c87

Please sign in to comment.