summaryrefslogtreecommitdiff
path: root/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/TrackersAndAppsListsUseCase.kt
blob: 8292a6d1d9cd85913699926faf3728cbaf2f3742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (C) 2023 MURENA SAS
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package foundation.e.advancedprivacy.domain.usecases

import foundation.e.advancedprivacy.data.repositories.AppListsRepository
import foundation.e.advancedprivacy.domain.entities.ApplicationDescription
import foundation.e.advancedprivacy.features.trackers.AppWithTrackersCount
import foundation.e.advancedprivacy.features.trackers.TrackerWithAppsCount
import foundation.e.advancedprivacy.trackers.data.StatsDatabase
import foundation.e.advancedprivacy.trackers.data.TrackersRepository
import foundation.e.advancedprivacy.trackers.domain.entities.Tracker
import kotlinx.coroutines.flow.first

class TrackersAndAppsListsUseCase(
    private val statsDatabase: StatsDatabase,
    private val trackersRepository: TrackersRepository,
    private val appListsRepository: AppListsRepository,
) {

    suspend fun getAppsAndTrackersCounts(): Pair<List<AppWithTrackersCount>, List<TrackerWithAppsCount>> {
        val trackersAndAppsIds = statsDatabase.getDistinctTrackerAndApp()
        val trackersAndApps = mapIdsToEntities(trackersAndAppsIds)
        val (countByApp, countByTracker) = foldToCountByEntityMaps(trackersAndApps)

        val appList = buildAppList(countByApp)
        val trackerList = buildTrackerList(countByTracker)
        return appList to trackerList
    }

    private fun buildTrackerList(countByTracker: Map<Tracker, Int>): List<TrackerWithAppsCount> {
        return countByTracker.map { (tracker, count) ->
            TrackerWithAppsCount(tracker = tracker, appsCount = count)
        }.sortedByDescending { it.appsCount }
    }

    private suspend fun buildAppList(countByApp: Map<ApplicationDescription, Int>): List<AppWithTrackersCount> {
        return appListsRepository.apps().first().map { app: ApplicationDescription ->
            AppWithTrackersCount(app = app, trackersCount = countByApp[app] ?: 0)
        }.sortedByDescending { it.trackersCount }
    }

    private suspend fun mapIdsToEntities(trackersAndAppsIds: List<Pair<String, String>>): List<Pair<Tracker, ApplicationDescription>> {
        return trackersAndAppsIds.mapNotNull { (trackerId, apId) ->
            trackersRepository.getTracker(trackerId)?.let { tracker ->
                appListsRepository.getDisplayableApp(apId)?.let { app ->
                    tracker to app
                }
            }
            // appListsRepository.getDisplayableApp() may transform many apId to one
            // ApplicationDescription, so the lists is not distinct anymore.
        }.distinct()
    }

    private fun foldToCountByEntityMaps(trackersAndApps: List<Pair<Tracker, ApplicationDescription>>):
        Pair<Map<ApplicationDescription, Int>, Map<Tracker, Int>> {
            return trackersAndApps.fold(
                mutableMapOf<ApplicationDescription, Int>() to mutableMapOf<Tracker, Int>()
            ) { (countByApp, countByTracker), (tracker, app) ->
                countByApp[app] = countByApp.getOrDefault(app, 0) + 1
                countByTracker[tracker] = countByTracker.getOrDefault(tracker, 0) + 1
                countByApp to countByTracker
            }
        }
}