Skip to content

Commit

Permalink
Move Tracker Animation Tile logic to TabSwitcherTileAnimationMonitor
Browse files Browse the repository at this point in the history
The logic for determining the visibility of the Tracker Animation Tile has been moved to a new `TabSwitcherTileAnimationMonitor` class.

This includes:
- Determining the tracker count in the last 7 days.
- Determining the number of open tabs.
- Checking if the animation tile is dismissed.
- Defining the minimum requirements for displaying the tile (minimum tracker count and minimum tab count).

The `WebTrackersBlockedAppRepository` has been updated to have a new public method to retrieve the number of tracker count for last 7 days.

The `TabSwitcherViewModel` was refactored to use the new `TabSwitcherTileAnimationMonitor` and the `observeAnimationTileVisibility` function.

The `createTrackerAnimationTile` and `getTrackerCountForLast7Days` were deleted.
  • Loading branch information
mikescamell committed Mar 6, 2025
1 parent e668ba2 commit 684efba
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2025 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.tabs.ui

import com.duckduckgo.app.tabs.model.TabDataRepository
import com.duckduckgo.app.tabs.store.TabSwitcherPrefsDataStore
import com.duckduckgo.app.trackerdetection.api.WebTrackersBlockedAppRepository
import com.duckduckgo.common.utils.DispatcherProvider
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import javax.inject.Inject

private const val MINIMUM_TRACKER_COUNT = 10
private const val MINIMUM_TAB_COUNT = 2

class TabSwitcherTileAnimationMonitor @Inject constructor(
private val dispatchProvider: DispatcherProvider,
private val tabSwitcherPrefsDataStore: TabSwitcherPrefsDataStore,
private val tabDataRepository: TabDataRepository,
private val webTrackersBlockedAppRepository: WebTrackersBlockedAppRepository,
) {

fun observeAnimationTileVisibility(): Flow<Boolean> = tabSwitcherPrefsDataStore.isAnimationTileDismissed()
.map { isAnimationTileDismissed ->
if (!isAnimationTileDismissed) {
shouldDisplayAnimationTile()
} else {
false
}
}.flowOn(dispatchProvider.io())

private suspend fun shouldDisplayAnimationTile(): Boolean {
val openedTabs = tabDataRepository.getOpenTabCount()
val trackerCountForLast7Days = webTrackersBlockedAppRepository.getTrackerCountForLast7Days()

return trackerCountForLast7Days >= MINIMUM_TRACKER_COUNT &&
openedTabs >= MINIMUM_TAB_COUNT
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import com.duckduckgo.common.utils.extensions.toBinaryString
import com.duckduckgo.di.scopes.ActivityScope
import com.duckduckgo.duckchat.api.DuckChat
import com.duckduckgo.duckchat.impl.DuckChatPixelName
import java.time.LocalDateTime
import javax.inject.Inject
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.first
Expand All @@ -65,6 +64,7 @@ class TabSwitcherViewModel @Inject constructor(
private val tabSwitcherAnimationFeature: TabSwitcherAnimationFeature,
private val webTrackersBlockedAppRepository: WebTrackersBlockedAppRepository,
private val tabSwitcherPrefsDataStore: TabSwitcherPrefsDataStore,
private val tabSwitcherTileAnimationMonitor: TabSwitcherTileAnimationMonitor,
) : ViewModel() {

val tabSwitcherItems: LiveData<List<TabSwitcherItem>> = tabRepository.liveTabs.switchMap { tabEntities ->
Expand Down Expand Up @@ -227,42 +227,26 @@ class TabSwitcherViewModel @Inject constructor(
}
}

fun onTrackerAnimationTileCloseClicked(isDismissed: Boolean = true) {
fun onTrackerAnimationTileCloseClicked(isDismissed: Boolean) {
viewModelScope.launch(dispatcherProvider.io()) {
tabSwitcherPrefsDataStore.setIsAnimationTileDismissed(isDismissed = isDismissed)
}
}

private suspend fun createTrackerAnimationTile(): TrackerAnimationTile {
val now = LocalDateTime.now()
val trackerCount =
webTrackersBlockedAppRepository.getTrackersCountBetween(
startTime = now.minusDays(7),
endTime = now,
)
return TrackerAnimationTile(trackerCount)
}

private suspend fun LiveDataScope<List<TabSwitcherItem>>.collectTabItemsWithOptionalAnimationTile(
tabEntities: List<TabEntity>,
) {
tabSwitcherPrefsDataStore.isAnimationTileDismissed().collect { isDismissed ->
tabSwitcherTileAnimationMonitor.observeAnimationTileVisibility().collect { isVisible ->
val tabItems = tabEntities.map { Tab(it) }
val trackerCount = getTrackerCountForLast7Days()

val tabSwitcherItems = if (!isDismissed && trackerCount >= 10 && tabEntities.count() >= 2) {
listOf(TrackerAnimationTile(trackerCount)) + tabItems
val tabSwitcherItems = if (isVisible) {
val trackerCountForLast7Days = webTrackersBlockedAppRepository.getTrackerCountForLast7Days()

listOf(TrackerAnimationTile(trackerCountForLast7Days)) + tabItems
} else {
tabItems
}
emit(tabSwitcherItems)
}
}

private suspend fun getTrackerCountForLast7Days(): Int {
return webTrackersBlockedAppRepository.getTrackersCountBetween(
startTime = LocalDateTime.now().minusDays(7),
endTime = LocalDateTime.now(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ class WebTrackersBlockedAppRepository @Inject constructor(appDatabase: AppDataba
}

// TODO move to public API if experiment kept
suspend fun getTrackersCountBetween(
suspend fun getTrackerCountForLast7Days(): Int {
return getTrackersCountBetween(
startTime = LocalDateTime.now().minusDays(7),
endTime = LocalDateTime.now(),
)
}

private suspend fun getTrackersCountBetween(
startTime: LocalDateTime,
endTime: LocalDateTime,
): Int = dao.getTrackersCountBetween(
Expand Down

0 comments on commit 684efba

Please sign in to comment.