<commit_before>//
//  This file is part of Blokada.
//
//  This Source Code Form is subject to the terms of the Mozilla Public
//  License, v. 2.0. If a copy of the MPL was not distributed with this
//  file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
//  Copyright © 2020 Blocka AB. All rights reserved.
//
//  @author <NAME>
//

import Foundation
import Combine

class ActivityViewModel: ObservableObject {

    private let log = Logger("ActivityVM")

    private lazy var activityRepo = Repos.activityRepo
    private lazy var cloudRepo = Repos.cloudRepo

    private lazy var envService = Services.env

    private var cancellables = Set<AnyCancellable>()

    @Published var logRetention = ""
    @Published var logRetentionSelected = ""

    var allEntries = [HistoryEntry]()
    @Published var entries = [HistoryEntry]()

    @Published var whitelist = [String]()
    @Published var blacklist = [String]()

    @Published var devices = [String]()

    @Published var sorting = 0 {
        didSet {
            refreshStats()
        }
    }

    @Published var filtering = 0 {
        didSet {
            refreshStats()
        }
    }

    @Published var device = "." {
        didSet {
            refreshStats()
        }
    }

    @Published var search = "" {
        didSet {
            apply()
        }
    }
    
    init() {
        onEntriesUpdated()
        onAllowedListUpdated()
        onDeniedListUpdated()
        onActivityRetentionUpdated()
        onDevicesUpdated()
    }

    private func onEntriesUpdated() {
        activityRepo.entriesHot
        .receive(on: RunLoop.main)
        .sink(onValue: { it in
            self.allEntries = it
            self.apply()
            self.objectWillChange.send()
        })
        .store(in: &cancellables)
    }

    private func onAllowedListUpdated() {
        activityRepo.allowedListHot
        .receive(on: RunLoop.main)
        .sink(onValue: { it in
            self.whitelist = it
            self.apply()
            self.objectWillChange.send()
        })
        .store(in: &cancellables)
    }

    private func onDeniedListUpdated() {
        activityRepo.deniedListHot
        .receive(on: RunLoop.main)
        .sink(onValue: { it in
            self.blacklist = it
            self.apply()
            self.objectWillChange.send()
        })
        .store(in: &cancellables)
    }

    private func onActivityRetentionUpdated() {
        cloudRepo.activityRetentionHot
        .receive(on: RunLoop.main)
        .sink(onValue: { it in
            self.logRetention = it
        })
        .store(in: &cancellables)
    }

    private func onDevicesUpdated() {
        activityRepo.devicesHot
        .receive(on: RunLoop.main)
        .sink(onValue: { it in
            self.devices = it.filter { $0 != self.envService.deviceName }
            self.apply()
            self.objectWillChange.send()
        })
        .store(in: &cancellables)
    }<commit_msg>ios: dont show unnamed device in activity<commit_after>//
//  This file is part of Blokada.
//
//  This Source Code Form is subject to the terms of the Mozilla Public
//  License, v. 2.0. If a copy of the MPL was not distributed with this
//  file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
//  Copyright © 2020 Blocka AB. All rights reserved.
//
//  @author <NAME>
//

import Foundation
import Combine

class ActivityViewModel: ObservableObject {

    private let log = Logger("ActivityVM")

    private lazy var activityRepo = Repos.activityRepo
    private lazy var cloudRepo = Repos.cloudRepo

    private lazy var envService = Services.env

    private var cancellables = Set<AnyCancellable>()

    @Published var logRetention = ""
    @Published var logRetentionSelected = ""

    var allEntries = [HistoryEntry]()
    @Published var entries = [HistoryEntry]()

    @Published var whitelist = [String]()
    @Published var blacklist = [String]()

    @Published var devices = [String]()

    @Published var sorting = 0 {
        didSet {
            refreshStats()
        }
    }

    @Published var filtering = 0 {
        didSet {
            refreshStats()
        }
    }

    @Published var device = "." {
        didSet {
            refreshStats()
        }
    }

    @Published var search = "" {
        didSet {
            apply()
        }
    }
    
    init() {
        onEntriesUpdated()
        onAllowedListUpdated()
        onDeniedListUpdated()
        onActivityRetentionUpdated()
        onDevicesUpdated()
    }

    private func onEntriesUpdated() {
        activityRepo.entriesHot
        .receive(on: RunLoop.main)
        .sink(onValue: { it in
            self.allEntries = it
            self.apply()
            self.objectWillChange.send()
        })
        .store(in: &cancellables)
    }

    private func onAllowedListUpdated() {
        activityRepo.allowedListHot
        .receive(on: RunLoop.main)
        .sink(onValue: { it in
            self.whitelist = it
            self.apply()
            self.objectWillChange.send()
        })
        .store(in: &cancellables)
    }

    private func onDeniedListUpdated() {
        activityRepo.deniedListHot
        .receive(on: RunLoop.main)
        .sink(onValue: { it in
            self.blacklist = it
            self.apply()
            self.objectWillChange.send()
        })
        .store(in: &cancellables)
    }

    private func onActivityRetentionUpdated() {
        cloudRepo.activityRetentionHot
        .receive(on: RunLoop.main)
        .sink(onValue: { it in
            self.logRetention = it
        })
        .store(in: &cancellables)
    }

    private func onDevicesUpdated() {
        activityRepo.devicesHot
        .receive(on: RunLoop.main)
        .sink(onValue: { it in
            self.devices = it.filter { $0 != self.envService.deviceName }.filter { !$0.isEmpty }
            self.apply()
            self.objectWillChange.send()
        })
        .store(in: &cancellables)
    }
{'fraction_non_alphanumeric': 0.10245696139060671, 'fraction_numerical': 0.003844225305030921, 'mean_word_length': 1.871401151631478, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4658814', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1705, 'n_words': 426}