mirror of
https://github.com/passepartoutvpn/passepartout-apple.git
synced 2025-02-16 12:52:11 +00:00
Move VPNToggle to standalone view
This commit is contained in:
parent
ccde6a30cf
commit
1ff19b331f
@ -63,6 +63,7 @@
|
||||
0E71ACFD27C1321A00F85C4B /* ActivityView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E71ACFC27C1321A00F85C4B /* ActivityView.swift */; };
|
||||
0E7577D72816A3B200081CBE /* DestructiveButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E7577D62816A3B200081CBE /* DestructiveButton.swift */; };
|
||||
0E7577DD2816C3AD00081CBE /* ProfileView+Buttons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E7577DC2816C3AD00081CBE /* ProfileView+Buttons.swift */; };
|
||||
0E7577DF2817E22C00081CBE /* VPNToggle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E7577DE2817E22C00081CBE /* VPNToggle.swift */; };
|
||||
0E90DFE627BACC1500EF5078 /* AddHostViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E90DFE527BACC1500EF5078 /* AddHostViewModel.swift */; };
|
||||
0E92D7C627F103300033CB7B /* ProfileView+Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E92D7C527F103300033CB7B /* ProfileView+Configuration.swift */; };
|
||||
0E92D7C927F1042A0033CB7B /* ProfileView+Extra.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E92D7C827F1042A0033CB7B /* ProfileView+Extra.swift */; };
|
||||
@ -247,6 +248,7 @@
|
||||
0E71ACFC27C1321A00F85C4B /* ActivityView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityView.swift; sourceTree = "<group>"; };
|
||||
0E7577D62816A3B200081CBE /* DestructiveButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DestructiveButton.swift; sourceTree = "<group>"; };
|
||||
0E7577DC2816C3AD00081CBE /* ProfileView+Buttons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ProfileView+Buttons.swift"; sourceTree = "<group>"; };
|
||||
0E7577DE2817E22C00081CBE /* VPNToggle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VPNToggle.swift; sourceTree = "<group>"; };
|
||||
0E90DFE527BACC1500EF5078 /* AddHostViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddHostViewModel.swift; sourceTree = "<group>"; };
|
||||
0E92D7C527F103300033CB7B /* ProfileView+Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ProfileView+Configuration.swift"; sourceTree = "<group>"; };
|
||||
0E92D7C827F1042A0033CB7B /* ProfileView+Extra.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ProfileView+Extra.swift"; sourceTree = "<group>"; };
|
||||
@ -462,6 +464,7 @@
|
||||
0ED89C1B27DE3ABC008B36D6 /* ShortcutsView+Add.swift */,
|
||||
0E71ACFA27C12E5300F85C4B /* VersionView.swift */,
|
||||
0EF708312811CC8400A3A308 /* VPNStatusText.swift */,
|
||||
0E7577DE2817E22C00081CBE /* VPNToggle.swift */,
|
||||
0E065F102813269500062CAF /* WelcomeView.swift */,
|
||||
);
|
||||
path = Views;
|
||||
@ -944,6 +947,7 @@
|
||||
0E2C172B27CB63F9007E8488 /* Reviewer.swift in Sources */,
|
||||
0E71ACDD27C0295C00F85C4B /* View+Extensions.swift in Sources */,
|
||||
0E34A2B627CAA8CC00C73B67 /* Core+L10n.swift in Sources */,
|
||||
0E7577DF2817E22C00081CBE /* VPNToggle.swift in Sources */,
|
||||
0E6059CF27FCC618003F4063 /* SwiftGen+Assets.swift in Sources */,
|
||||
0E2A8D4927ADF87F00207D04 /* PassepartoutApp.swift in Sources */,
|
||||
0EBC075527EBC83800208AD9 /* MailComposerView.swift in Sources */,
|
||||
|
@ -42,19 +42,6 @@ extension ProfileView {
|
||||
|
||||
@ObservedObject private var currentProfile: ObservableProfile
|
||||
|
||||
@State private var isLocallyEnabled = false
|
||||
|
||||
@State private var canToggle = true
|
||||
|
||||
private var isEnabled: Binding<Bool> {
|
||||
.init {
|
||||
isLocallyEnabled
|
||||
} set: {
|
||||
isLocallyEnabled = $0
|
||||
toggleVPNAndDonateIntents()
|
||||
}
|
||||
}
|
||||
|
||||
private let isLoading: Bool
|
||||
|
||||
private var isActiveProfile: Bool {
|
||||
@ -98,12 +85,20 @@ extension ProfileView {
|
||||
footer: Text(L10n.Profile.Sections.Vpn.footer)
|
||||
.xxxThemeTruncation()
|
||||
) {
|
||||
Toggle(vpnToggleString, isOn: isEnabled)
|
||||
.onAppear {
|
||||
isLocallyEnabled = currentVPNState.isEnabled
|
||||
}.onChange(of: currentVPNState.isEnabled) {
|
||||
isLocallyEnabled = $0
|
||||
}.disabled(!canToggle)
|
||||
VPNToggle(rateLimit: Constants.RateLimit.vpnToggle) {
|
||||
|
||||
// eligibility: donate intents if eligible for Siri
|
||||
if isEligibleForSiri {
|
||||
pp_log.debug("Donating connection intents...")
|
||||
|
||||
IntentDispatcher.donateEnableVPN()
|
||||
IntentDispatcher.donateDisableVPN()
|
||||
IntentDispatcher.donateConnection(
|
||||
with: currentProfile.value,
|
||||
providerManager: providerManager
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Text(L10n.Profile.Items.ConnectionStatus.caption)
|
||||
.withTrailingText(currentVPNState.localizedStatusDescription(
|
||||
@ -113,12 +108,6 @@ extension ProfileView {
|
||||
}
|
||||
}
|
||||
|
||||
private var vpnToggleString: String {
|
||||
// let V = L10n.Profile.Items.Vpn.self
|
||||
// return currentVPNState.isEnabled ? V.TurnOff.caption : V.TurnOn.caption
|
||||
L10n.Global.Strings.enabled
|
||||
}
|
||||
|
||||
private var inactiveSubview: some View {
|
||||
Section(
|
||||
header: headerView
|
||||
@ -144,29 +133,5 @@ extension ProfileView {
|
||||
ProgressView()
|
||||
}
|
||||
}
|
||||
|
||||
private func toggleVPNAndDonateIntents() {
|
||||
guard vpnManager.toggle() else {
|
||||
return
|
||||
}
|
||||
|
||||
// rate limit toggle actions
|
||||
canToggle = false
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(Constants.RateLimit.vpnToggle)) {
|
||||
canToggle = true
|
||||
}
|
||||
|
||||
// eligibility: donate intents if eligible for Siri
|
||||
if isEligibleForSiri {
|
||||
pp_log.debug("Donating connection intents...")
|
||||
|
||||
IntentDispatcher.donateEnableVPN()
|
||||
IntentDispatcher.donateDisableVPN()
|
||||
IntentDispatcher.donateConnection(
|
||||
with: currentProfile.value,
|
||||
providerManager: providerManager
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
81
Passepartout/App/Views/VPNToggle.swift
Normal file
81
Passepartout/App/Views/VPNToggle.swift
Normal file
@ -0,0 +1,81 @@
|
||||
//
|
||||
// VPNToggle.swift
|
||||
// Passepartout
|
||||
//
|
||||
// Created by Davide De Rosa on 4/26/22.
|
||||
// Copyright (c) 2022 Davide De Rosa. All rights reserved.
|
||||
//
|
||||
// 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 SwiftUI
|
||||
import PassepartoutCore
|
||||
|
||||
struct VPNToggle: View {
|
||||
@ObservedObject private var vpnManager: VPNManager
|
||||
|
||||
@ObservedObject private var currentVPNState: VPNManager.ObservableState
|
||||
|
||||
private let rateLimit: Int
|
||||
|
||||
private let onToggle: (() -> Void)?
|
||||
|
||||
private var isEnabled: Binding<Bool> {
|
||||
.init {
|
||||
isLocallyEnabled
|
||||
} set: {
|
||||
isLocallyEnabled = $0
|
||||
toggleVPN()
|
||||
}
|
||||
}
|
||||
|
||||
// local copy grants immediate visual feedback and smooth animation
|
||||
@State private var isLocallyEnabled = false
|
||||
|
||||
@State private var canToggle = true
|
||||
|
||||
init(rateLimit: Int, onToggle: (() -> Void)? = nil) {
|
||||
vpnManager = .shared
|
||||
currentVPNState = .shared
|
||||
self.rateLimit = rateLimit
|
||||
self.onToggle = onToggle
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Toggle(L10n.Global.Strings.enabled, isOn: isEnabled)
|
||||
.onAppear {
|
||||
isLocallyEnabled = currentVPNState.isEnabled
|
||||
}.onChange(of: currentVPNState.isEnabled) {
|
||||
isLocallyEnabled = $0
|
||||
}.disabled(!canToggle)
|
||||
}
|
||||
|
||||
private func toggleVPN() {
|
||||
guard vpnManager.toggle() else {
|
||||
return
|
||||
}
|
||||
|
||||
// rate limit toggle actions
|
||||
canToggle = false
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(rateLimit)) {
|
||||
canToggle = true
|
||||
}
|
||||
|
||||
onToggle?()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user