2022-06-25 20:11:45 +00:00
|
|
|
//
|
|
|
|
// DefaultLightProfileManager.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 7/3/22.
|
2024-01-14 13:34:21 +00:00
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
2022-06-25 20:11:45 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout 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.
|
|
|
|
//
|
|
|
|
// Passepartout 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 Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
2023-04-04 07:50:45 +00:00
|
|
|
import Combine
|
2022-06-25 20:11:45 +00:00
|
|
|
import Foundation
|
|
|
|
import PassepartoutLibrary
|
|
|
|
|
2023-05-24 16:19:47 +00:00
|
|
|
final class DefaultLightProfile: LightProfile {
|
2022-06-25 20:11:45 +00:00
|
|
|
let id: UUID
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
let name: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
let vpnProtocol: String
|
|
|
|
|
|
|
|
let isActive: Bool
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
let providerName: String?
|
|
|
|
|
|
|
|
let providerServer: LightProviderServer?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
init(_ header: Profile.Header, vpnProtocol: String, isActive: Bool, providerServer: LightProviderServer?) {
|
|
|
|
id = header.id
|
|
|
|
name = header.name
|
|
|
|
self.vpnProtocol = vpnProtocol
|
|
|
|
self.isActive = isActive
|
|
|
|
providerName = header.providerName
|
|
|
|
self.providerServer = providerServer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:19:47 +00:00
|
|
|
final class DefaultLightProfileManager: LightProfileManager {
|
2022-08-28 07:19:15 +00:00
|
|
|
private let profileManager = ProfileManager.shared
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
private let providerManager = ProviderManager.shared
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
private var subscriptions: Set<AnyCancellable> = []
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
weak var delegate: LightProfileManagerDelegate?
|
|
|
|
|
|
|
|
init() {
|
|
|
|
profileManager.didUpdateProfiles
|
|
|
|
.receive(on: DispatchQueue.main)
|
2023-09-08 14:20:01 +00:00
|
|
|
.sink { [weak self] in
|
|
|
|
self?.delegate?.didUpdateProfiles()
|
2022-06-25 20:11:45 +00:00
|
|
|
}.store(in: &subscriptions)
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
var hasProfiles: Bool {
|
|
|
|
profileManager.hasProfiles
|
|
|
|
}
|
|
|
|
|
|
|
|
var profiles: [LightProfile] {
|
|
|
|
profileManager.profiles
|
|
|
|
.sorted {
|
|
|
|
$0.header < $1.header
|
|
|
|
}.map {
|
|
|
|
let server: ProviderServer?
|
2023-03-19 07:19:32 +00:00
|
|
|
if let serverId = $0.providerServerId {
|
2022-06-25 20:11:45 +00:00
|
|
|
server = providerManager.server(withId: serverId)
|
|
|
|
} else {
|
|
|
|
server = nil
|
|
|
|
}
|
|
|
|
return DefaultLightProfile(
|
|
|
|
$0.header,
|
|
|
|
vpnProtocol: $0.currentVPNProtocol.rawValue,
|
|
|
|
isActive: profileManager.isActiveProfile($0.id),
|
|
|
|
providerServer: server.map(DefaultLightProviderServer.init)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var activeProfileId: UUID? {
|
|
|
|
profileManager.activeProfileId
|
|
|
|
}
|
|
|
|
|
|
|
|
var activeProfileName: String? {
|
|
|
|
guard let header = profileManager.headers.first(where: {
|
|
|
|
$0.id == profileManager.activeProfileId
|
|
|
|
}) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return header.name
|
|
|
|
}
|
|
|
|
}
|