Skip to content

Commit 6f1378c

Browse files
committed
Move Tracker Animation Tile logic to TabSwitcherTileAnimationMonitor
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.
1 parent e613820 commit 6f1378c

File tree

3 files changed

+69
-24
lines changed

3 files changed

+69
-24
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2025 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.app.tabs.ui
18+
19+
import com.duckduckgo.app.tabs.model.TabDataRepository
20+
import com.duckduckgo.app.tabs.store.TabSwitcherPrefsDataStore
21+
import com.duckduckgo.app.trackerdetection.api.WebTrackersBlockedAppRepository
22+
import com.duckduckgo.common.utils.DispatcherProvider
23+
import kotlinx.coroutines.flow.Flow
24+
import kotlinx.coroutines.flow.flowOn
25+
import kotlinx.coroutines.flow.map
26+
import javax.inject.Inject
27+
28+
private const val MINIMUM_TRACKER_COUNT = 10
29+
private const val MINIMUM_TAB_COUNT = 2
30+
31+
class TabSwitcherTileAnimationMonitor @Inject constructor(
32+
private val dispatchProvider: DispatcherProvider,
33+
private val tabSwitcherPrefsDataStore: TabSwitcherPrefsDataStore,
34+
private val tabDataRepository: TabDataRepository,
35+
private val webTrackersBlockedAppRepository: WebTrackersBlockedAppRepository,
36+
) {
37+
38+
fun observeAnimationTileVisibility(): Flow<Boolean> = tabSwitcherPrefsDataStore.isAnimationTileDismissed()
39+
.map { isAnimationTileDismissed ->
40+
if (!isAnimationTileDismissed) {
41+
shouldDisplayAnimationTile()
42+
} else {
43+
false
44+
}
45+
}.flowOn(dispatchProvider.io())
46+
47+
private suspend fun shouldDisplayAnimationTile(): Boolean {
48+
val openedTabs = tabDataRepository.getOpenTabCount()
49+
val trackerCountForLast7Days = webTrackersBlockedAppRepository.getTrackerCountForLast7Days()
50+
51+
return trackerCountForLast7Days >= MINIMUM_TRACKER_COUNT &&
52+
openedTabs >= MINIMUM_TAB_COUNT
53+
}
54+
}

app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherViewModel.kt

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import com.duckduckgo.common.utils.extensions.toBinaryString
4545
import com.duckduckgo.di.scopes.ActivityScope
4646
import com.duckduckgo.duckchat.api.DuckChat
4747
import com.duckduckgo.duckchat.impl.DuckChatPixelName
48-
import java.time.LocalDateTime
4948
import javax.inject.Inject
5049
import kotlinx.coroutines.flow.SharingStarted
5150
import kotlinx.coroutines.flow.first
@@ -65,6 +64,7 @@ class TabSwitcherViewModel @Inject constructor(
6564
private val tabSwitcherAnimationFeature: TabSwitcherAnimationFeature,
6665
private val webTrackersBlockedAppRepository: WebTrackersBlockedAppRepository,
6766
private val tabSwitcherPrefsDataStore: TabSwitcherPrefsDataStore,
67+
private val tabSwitcherTileAnimationMonitor: TabSwitcherTileAnimationMonitor,
6868
) : ViewModel() {
6969

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

230-
fun onTrackerAnimationTileCloseClicked(isDismissed: Boolean = true) {
230+
fun onTrackerAnimationTileCloseClicked(isDismissed: Boolean) {
231231
viewModelScope.launch(dispatcherProvider.io()) {
232232
tabSwitcherPrefsDataStore.setIsAnimationTileDismissed(isDismissed = isDismissed)
233233
}
234234
}
235235

236-
private suspend fun createTrackerAnimationTile(): TrackerAnimationTile {
237-
val now = LocalDateTime.now()
238-
val trackerCount =
239-
webTrackersBlockedAppRepository.getTrackersCountBetween(
240-
startTime = now.minusDays(7),
241-
endTime = now,
242-
)
243-
return TrackerAnimationTile(trackerCount)
244-
}
245-
246236
private suspend fun LiveDataScope<List<TabSwitcherItem>>.collectTabItemsWithOptionalAnimationTile(
247237
tabEntities: List<TabEntity>,
248238
) {
249-
tabSwitcherPrefsDataStore.isAnimationTileDismissed().collect { isDismissed ->
239+
tabSwitcherTileAnimationMonitor.observeAnimationTileVisibility().collect { isVisible ->
250240
val tabItems = tabEntities.map { Tab(it) }
251-
val trackerCount = getTrackerCountForLast7Days()
252241

253-
val tabSwitcherItems = if (!isDismissed && trackerCount >= 10 && tabEntities.count() >= 2) {
254-
listOf(TrackerAnimationTile(trackerCount)) + tabItems
242+
val tabSwitcherItems = if (isVisible) {
243+
val trackerCountForLast7Days = webTrackersBlockedAppRepository.getTrackerCountForLast7Days()
244+
245+
listOf(TrackerAnimationTile(trackerCountForLast7Days)) + tabItems
255246
} else {
256247
tabItems
257248
}
258249
emit(tabSwitcherItems)
259250
}
260251
}
261-
262-
private suspend fun getTrackerCountForLast7Days(): Int {
263-
return webTrackersBlockedAppRepository.getTrackersCountBetween(
264-
startTime = LocalDateTime.now().minusDays(7),
265-
endTime = LocalDateTime.now(),
266-
)
267-
}
268252
}

app/src/main/java/com/duckduckgo/app/trackerdetection/api/WebTrackersBlockedAppRepository.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ class WebTrackersBlockedAppRepository @Inject constructor(appDatabase: AppDataba
4242
}
4343

4444
// TODO move to public API if experiment kept
45-
suspend fun getTrackersCountBetween(
45+
suspend fun getTrackerCountForLast7Days(): Int {
46+
return getTrackersCountBetween(
47+
startTime = LocalDateTime.now().minusDays(7),
48+
endTime = LocalDateTime.now(),
49+
)
50+
}
51+
52+
private suspend fun getTrackersCountBetween(
4653
startTime: LocalDateTime,
4754
endTime: LocalDateTime,
4855
): Int = dao.getTrackersCountBetween(

0 commit comments

Comments
 (0)