/* * Copyright (C) 2022-2023 MURENA SAS * Copyright (C) 2021 E FOUNDATION * * 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 . */ package foundation.e.advancedprivacy.features.trackers import android.net.Uri import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import androidx.navigation.NavDirections import foundation.e.advancedprivacy.domain.entities.ApplicationDescription import foundation.e.advancedprivacy.domain.usecases.TrackersAndAppsListsUseCase import foundation.e.advancedprivacy.domain.usecases.TrackersStatisticsUseCase import foundation.e.advancedprivacy.trackers.domain.entities.Tracker import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class TrackersViewModel( private val trackersStatisticsUseCase: TrackersStatisticsUseCase, private val trackersAndAppsListsUseCase: TrackersAndAppsListsUseCase ) : ViewModel() { private val _state = MutableStateFlow(TrackersState()) val state = _state.asStateFlow() private val _singleEvents = MutableSharedFlow() val singleEvents = _singleEvents.asSharedFlow() private val _navigate = MutableSharedFlow() val navigate = _navigate.asSharedFlow() suspend fun doOnStartedState() = withContext(Dispatchers.IO) { trackersStatisticsUseCase.listenUpdates().collect { trackersStatisticsUseCase.getDayMonthYearStatistics() .let { (day, month, year) -> _state.update { s -> s.copy( dayStatistics = day, monthStatistics = month, yearStatistics = year ) } } trackersAndAppsListsUseCase.getAppsAndTrackersCounts().let { (appList, trackerList) -> _state.update { it.copy(apps = appList, trackers = trackerList) } } } } fun onClickTracker(tracker: Tracker) = viewModelScope.launch { _navigate.emit(TrackersFragmentDirections.gotoTrackerDetailsFragment(trackerId = tracker.id)) } fun onClickApp(app: ApplicationDescription) = viewModelScope.launch { _navigate.emit(TrackersFragmentDirections.gotoAppTrackersFragment(appUid = app.uid)) } fun onClickLearnMore() = viewModelScope.launch { _singleEvents.emit(SingleEvent.OpenUrl(Uri.parse(URL_LEARN_MORE_ABOUT_TRACKERS))) } sealed class SingleEvent { data class ErrorEvent(val error: String) : SingleEvent() data class OpenUrl(val url: Uri) : SingleEvent() } }