2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// OnDemandView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/23/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/>.
|
|
|
|
//
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
2023-04-04 07:50:45 +00:00
|
|
|
import SwiftUI
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
struct OnDemandView: View {
|
|
|
|
@ObservedObject private var productManager: ProductManager
|
|
|
|
|
|
|
|
@ObservedObject private var currentProfile: ObservableProfile
|
|
|
|
|
|
|
|
@State private var onDemand = Profile.OnDemand()
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
init(currentProfile: ObservableProfile) {
|
|
|
|
productManager = .shared
|
|
|
|
self.currentProfile = currentProfile
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
debugChanges()
|
|
|
|
return List {
|
2023-07-23 06:44:46 +00:00
|
|
|
enabledView
|
|
|
|
if onDemand.isEnabled && onDemand.policy != .any {
|
2022-04-12 13:09:14 +00:00
|
|
|
mainView
|
2023-07-23 06:44:46 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}.navigationTitle(L10n.OnDemand.title)
|
|
|
|
.toolbar {
|
|
|
|
CopySavingButton(
|
|
|
|
original: $currentProfile.value.onDemand,
|
|
|
|
copy: $onDemand,
|
|
|
|
mapping: \.stripped,
|
|
|
|
label: themeSaveButtonLabel
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Siri
|
|
|
|
.onChange(of: onDemand.withMobileNetwork, perform: donateMobileIntent)
|
|
|
|
.onChange(of: onDemand.withSSIDs, perform: donateNetworkIntents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension OnDemandView {
|
|
|
|
var enabledView: some View {
|
2022-04-12 13:09:14 +00:00
|
|
|
Section {
|
2022-04-23 10:08:24 +00:00
|
|
|
Toggle(L10n.Global.Strings.enabled, isOn: $onDemand.isEnabled.themeAnimation())
|
2023-07-23 06:44:46 +00:00
|
|
|
if onDemand.isEnabled {
|
|
|
|
themeTextPicker(
|
|
|
|
L10n.Global.Strings.policy,
|
|
|
|
selection: $onDemand.policy,
|
|
|
|
values: [.any, .including, .excluding],
|
|
|
|
description: \.localizedDescription
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} footer: {
|
2023-07-31 06:25:30 +00:00
|
|
|
Text(policyFooterDescription)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-23 06:44:46 +00:00
|
|
|
var policyFooterDescription: String {
|
2023-07-30 15:59:29 +00:00
|
|
|
guard onDemand.isEnabled else {
|
|
|
|
return "" // better animation than removing footer completely
|
|
|
|
}
|
2023-07-23 06:44:46 +00:00
|
|
|
let suffix: String
|
|
|
|
switch onDemand.policy {
|
|
|
|
case .any:
|
|
|
|
suffix = L10n.OnDemand.Sections.Policy.Footer.any
|
|
|
|
|
|
|
|
case .including, .excluding:
|
|
|
|
if onDemand.policy == .including {
|
2023-10-10 21:00:19 +00:00
|
|
|
suffix = L10n.OnDemand.Sections.Policy.Footer.including
|
2023-07-23 06:44:46 +00:00
|
|
|
} else {
|
2023-10-10 21:00:19 +00:00
|
|
|
suffix = L10n.OnDemand.Sections.Policy.Footer.excluding
|
2023-07-23 06:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return L10n.OnDemand.Sections.Policy.footer(suffix)
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@ViewBuilder
|
2023-07-03 14:54:43 +00:00
|
|
|
var mainView: some View {
|
2022-04-12 13:09:14 +00:00
|
|
|
if Utils.hasCellularData() {
|
2022-05-01 17:43:33 +00:00
|
|
|
Section {
|
2022-04-12 13:09:14 +00:00
|
|
|
Toggle(L10n.OnDemand.Items.Mobile.caption, isOn: $onDemand.withMobileNetwork)
|
2022-05-01 17:43:33 +00:00
|
|
|
} header: {
|
2023-07-23 06:44:46 +00:00
|
|
|
Text(L10n.Global.Strings.networks)
|
2022-05-15 22:45:26 +00:00
|
|
|
}
|
|
|
|
} else if Utils.hasEthernet() {
|
|
|
|
Section {
|
|
|
|
Toggle(L10n.OnDemand.Items.Ethernet.caption, isOn: $onDemand.withEthernetNetwork)
|
|
|
|
} header: {
|
2023-07-23 06:44:46 +00:00
|
|
|
Text(L10n.Global.Strings.networks)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-01 17:43:33 +00:00
|
|
|
Section {
|
2023-07-23 06:44:46 +00:00
|
|
|
SSIDList(withSSIDs: $onDemand.withSSIDs)
|
|
|
|
} header: {
|
|
|
|
if !Utils.hasCellularData() && !Utils.hasEthernet() {
|
|
|
|
Text(L10n.Global.Strings.networks)
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var isEligibleForSiri: Bool {
|
|
|
|
productManager.isEligible(forFeature: .siriShortcuts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension OnDemandView {
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
// eligibility: donate intents if eligible for Siri
|
2023-07-03 14:54:43 +00:00
|
|
|
func donateMobileIntent(_ isEnabled: Bool) {
|
2022-04-12 13:09:14 +00:00
|
|
|
guard isEligibleForSiri else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
IntentDispatcher.donateTrustCellularNetwork()
|
|
|
|
IntentDispatcher.donateUntrustCellularNetwork()
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
// eligibility: donate intents if eligible for Siri
|
2023-07-03 14:54:43 +00:00
|
|
|
func donateNetworkIntents(_: [String: Bool]) {
|
2022-04-12 13:09:14 +00:00
|
|
|
guard isEligibleForSiri else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
IntentDispatcher.donateTrustCurrentNetwork()
|
|
|
|
IntentDispatcher.donateUntrustCurrentNetwork()
|
|
|
|
}
|
|
|
|
}
|