passepartout-apple/Passepartout/App/Views/AddProviderViewModel.swift

190 lines
5.8 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// AddProviderViewModel.swift
// Passepartout
//
// Created by Davide De Rosa on 3/19/22.
2023-03-17 15:56:19 +00:00
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
2022-04-12 13:09:14 +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/>.
//
import Foundation
2022-06-23 21:31:01 +00:00
import PassepartoutLibrary
2022-04-12 13:09:14 +00:00
extension AddProviderView {
class ViewModel: ObservableObject {
enum PendingOperation {
case index
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
case provider(ProviderName)
}
var isUpdatingIndex: Bool {
if case .index = pendingOperation {
return true
}
return false
}
var isFetchingAnyProvider: Bool {
if case .provider = pendingOperation {
return true
}
return false
}
func isFetchingProvider(_ name: ProviderName) -> Bool {
if case .provider(name) = pendingOperation {
return true
}
return false
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@Published var selectedVPNProtocol: VPNProtocolType = .openVPN
@Published var selectedProvider: ProviderMetadata?
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@Published var pendingProfile: Profile = .placeholder
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@Published private(set) var pendingOperation: PendingOperation?
@Published var isPaywallPresented = false
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@Published private(set) var errorMessage: String?
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
func selectProvider(_ metadata: ProviderMetadata, _ providerManager: ProviderManager) {
errorMessage = nil
guard let server = providerManager.anyDefaultServer(
metadata.name,
vpnProtocol: selectedVPNProtocol
) else {
selectProviderAfterFetchingInfrastructure(metadata, providerManager)
2022-04-12 13:09:14 +00:00
return
}
doSelectProvider(metadata, server)
}
private func selectProviderAfterFetchingInfrastructure(_ metadata: ProviderMetadata, _ providerManager: ProviderManager) {
2022-04-12 13:09:14 +00:00
errorMessage = nil
pendingOperation = .provider(metadata.name)
Task { @MainActor in
2022-04-12 13:09:14 +00:00
do {
try await providerManager.fetchProviderPublisher(
withName: metadata.name,
vpnProtocol: pendingProfile.currentVPNProtocol,
priority: .remoteThenBundle
).async()
if let server = providerManager.anyDefaultServer(
metadata.name,
vpnProtocol: selectedVPNProtocol
) {
doSelectProvider(metadata, server)
} else {
errorMessage = L10n.AddProfile.Provider.Errors.noDefaultServer
}
} catch {
errorMessage = error.localizedDescription
}
pendingOperation = nil
}
}
private func doSelectProvider(_ metadata: ProviderMetadata, _ server: ProviderServer) {
pendingProfile = Profile(metadata, server: server)
selectedProvider = metadata
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
func updateIndex(_ providerManager: ProviderManager) {
errorMessage = nil
pendingOperation = .index
Task { @MainActor in
2022-04-12 13:09:14 +00:00
do {
try await providerManager.fetchProvidersIndexPublisher(
priority: .remoteThenBundle
).async()
} catch {
errorMessage = error.localizedDescription
}
pendingOperation = nil
}
}
func presentPaywall() {
isPaywallPresented = true
}
}
}
extension AddProviderView.NameView {
struct ViewModel: Equatable {
private var isNamePreset = false
2022-04-12 13:09:14 +00:00
var profileName = ""
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var isAskingOverwrite = false
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private(set) var errorMessage: String?
2023-03-17 20:55:47 +00:00
mutating func presetName(withMetadata metadata: ProviderMetadata) {
guard !isNamePreset else {
return
}
isNamePreset = true
profileName = metadata.fullName
}
2022-04-12 13:09:14 +00:00
@MainActor
2022-04-12 13:09:14 +00:00
mutating func addProfile(
_ profile: Profile,
to profileManager: ProfileManager,
replacingExisting: Bool
) -> Profile? {
profileName = profileName.stripped
guard !profileName.isEmpty else {
return nil
}
if !replacingExisting {
guard !profileManager.isExistingProfile(withName: profileName) else {
isAskingOverwrite = true
return nil
}
}
errorMessage = nil
let finalProfile = profile.renamed(to: profileName)
profileManager.saveProfile(finalProfile, isActive: nil)
return finalProfile
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private mutating func setMessage(forError error: Error) {
errorMessage = error.localizedDescription
}
}
}
extension Profile {
func renamed(to newName: String) -> Profile {
var profile = self
profile.header.name = newName
return profile
}
}