2022-04-26 21:27:03 +00:00
|
|
|
//
|
|
|
|
// ProfileView+MainMenu.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/6/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-04-26 21:27:03 +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-26 21:27:03 +00:00
|
|
|
|
|
|
|
extension ProfileView {
|
|
|
|
struct MainMenu: View {
|
2023-07-03 14:41:49 +00:00
|
|
|
enum AlertType: Int, Identifiable {
|
2022-04-26 21:27:03 +00:00
|
|
|
case uninstallVPN
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
case deleteProfile
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
var id: Int {
|
|
|
|
rawValue
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
@ObservedObject private var profileManager: ProfileManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
@ObservedObject private var vpnManager: VPNManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-10-17 15:36:32 +00:00
|
|
|
@ObservedObject private var currentVPNState: ObservableVPNState
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
@ObservedObject private var currentProfile: ObservableProfile
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
@Binding private var modalType: ModalType?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:41:49 +00:00
|
|
|
@State private var isAlertPresented = false
|
|
|
|
|
|
|
|
@State private var alertType: AlertType?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
private let uninstallVPNTitle = L10n.Global.Strings.uninstall
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
private let deleteProfileTitle = L10n.Global.Strings.delete
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
init(currentProfile: ObservableProfile, modalType: Binding<ModalType?>) {
|
|
|
|
profileManager = .shared
|
|
|
|
vpnManager = .shared
|
2022-10-17 15:36:32 +00:00
|
|
|
currentVPNState = .shared
|
2022-04-26 21:27:03 +00:00
|
|
|
self.currentProfile = currentProfile
|
|
|
|
_modalType = modalType
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
2022-05-15 20:30:09 +00:00
|
|
|
mainView
|
2023-07-03 14:41:49 +00:00
|
|
|
.alert(
|
|
|
|
Text(Unlocalized.appName),
|
|
|
|
isPresented: $isAlertPresented,
|
|
|
|
presenting: alertType,
|
|
|
|
actions: alertActions,
|
|
|
|
message: alertMessage
|
|
|
|
)
|
2022-05-15 20:30:09 +00:00
|
|
|
}
|
2022-04-26 21:27:03 +00:00
|
|
|
}
|
2022-10-17 15:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension ProfileView {
|
|
|
|
struct ReconnectButton: View {
|
|
|
|
@ObservedObject private var vpnManager: VPNManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-10-17 15:36:32 +00:00
|
|
|
init() {
|
|
|
|
vpnManager = .shared
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-10-17 15:36:32 +00:00
|
|
|
var body: some View {
|
|
|
|
Button {
|
|
|
|
Task { @MainActor in
|
|
|
|
await vpnManager.reconnect()
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label(L10n.Global.Strings.reconnect, systemImage: themeReconnectImage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
#if !os(tvOS)
|
2022-04-26 21:27:03 +00:00
|
|
|
struct ShortcutsButton: View {
|
|
|
|
@ObservedObject private var productManager: ProductManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
@Binding private var modalType: ModalType?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
init(modalType: Binding<ModalType?>) {
|
|
|
|
productManager = .shared
|
|
|
|
_modalType = modalType
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Button {
|
|
|
|
presentShortcutsOrPaywall()
|
|
|
|
} label: {
|
|
|
|
Label(Unlocalized.Other.siri, systemImage: themeShortcutsImage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
#endif
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-27 14:49:23 +00:00
|
|
|
struct RenameButton: View {
|
|
|
|
@Binding private var modalType: ModalType?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-27 14:49:23 +00:00
|
|
|
init(modalType: Binding<ModalType?>) {
|
|
|
|
_modalType = modalType
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-27 14:49:23 +00:00
|
|
|
var body: some View {
|
|
|
|
Button {
|
|
|
|
modalType = .rename
|
|
|
|
} label: {
|
|
|
|
Label(L10n.Global.Strings.rename, systemImage: themeRenameProfileImage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
struct DuplicateButton: View {
|
2022-08-28 07:19:15 +00:00
|
|
|
@ObservedObject private var profileManager: ProfileManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-27 14:47:25 +00:00
|
|
|
private let header: Profile.Header
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-03 14:45:36 +00:00
|
|
|
private let setAsCurrent: Bool
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-03 14:45:36 +00:00
|
|
|
init(header: Profile.Header, setAsCurrent: Bool) {
|
2022-04-26 21:27:03 +00:00
|
|
|
profileManager = .shared
|
|
|
|
self.header = header
|
2022-05-03 14:45:36 +00:00
|
|
|
self.setAsCurrent = setAsCurrent
|
2022-04-26 21:27:03 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 21:27:03 +00:00
|
|
|
var body: some View {
|
|
|
|
Button {
|
|
|
|
duplicateProfile(withId: header.id)
|
|
|
|
} label: {
|
|
|
|
Label(L10n.Global.Strings.duplicate, systemImage: themeDuplicateImage)
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension ProfileView.MainMenu {
|
|
|
|
var header: Profile.Header {
|
|
|
|
currentProfile.value.header
|
|
|
|
}
|
|
|
|
|
|
|
|
var mainView: some View {
|
|
|
|
Menu {
|
|
|
|
ProfileView.ReconnectButton()
|
2023-12-16 19:58:54 +00:00
|
|
|
#if !os(tvOS)
|
2023-07-03 14:54:43 +00:00
|
|
|
ProfileView.ShortcutsButton(
|
|
|
|
modalType: $modalType
|
|
|
|
)
|
2023-12-16 19:58:54 +00:00
|
|
|
#endif
|
2023-07-03 14:54:43 +00:00
|
|
|
Divider()
|
|
|
|
ProfileView.RenameButton(
|
|
|
|
modalType: $modalType
|
|
|
|
)
|
|
|
|
ProfileView.DuplicateButton(
|
|
|
|
header: header,
|
|
|
|
setAsCurrent: true
|
|
|
|
)
|
|
|
|
uninstallVPNButton
|
|
|
|
Divider()
|
|
|
|
deleteProfileButton
|
|
|
|
} label: {
|
|
|
|
themeSettingsMenuImage.asSystemImage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func alertActions(_ alertType: AlertType) -> some View {
|
|
|
|
switch alertType {
|
|
|
|
case .uninstallVPN:
|
|
|
|
return Group {
|
|
|
|
Button(role: .destructive, action: uninstallVPN) {
|
|
|
|
Text(uninstallVPNTitle)
|
|
|
|
}
|
|
|
|
Button(role: .cancel) {
|
|
|
|
} label: {
|
|
|
|
Text(L10n.Global.Strings.cancel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case .deleteProfile:
|
|
|
|
return Group {
|
|
|
|
Button(role: .destructive, action: removeProfile) {
|
|
|
|
Text(deleteProfileTitle)
|
|
|
|
}
|
|
|
|
Button(role: .cancel) {
|
|
|
|
} label: {
|
|
|
|
Text(L10n.Global.Strings.cancel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func alertMessage(_ alertType: AlertType) -> some View {
|
|
|
|
switch alertType {
|
|
|
|
case .uninstallVPN:
|
|
|
|
return Text(L10n.Profile.Alerts.UninstallVpn.message)
|
|
|
|
|
|
|
|
case .deleteProfile:
|
|
|
|
return Text(L10n.Organizer.Alerts.RemoveProfile.message(header.name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var uninstallVPNButton: some View {
|
|
|
|
Button {
|
|
|
|
alertType = .uninstallVPN
|
|
|
|
isAlertPresented = true
|
|
|
|
} label: {
|
|
|
|
Label(uninstallVPNTitle, systemImage: themeUninstallImage)
|
|
|
|
}
|
|
|
|
}
|
2022-04-26 21:27:03 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var deleteProfileButton: some View {
|
2023-07-03 15:37:16 +00:00
|
|
|
Button(role: .destructive) {
|
2023-07-03 14:54:43 +00:00
|
|
|
alertType = .deleteProfile
|
|
|
|
isAlertPresented = true
|
|
|
|
} label: {
|
|
|
|
Label(deleteProfileTitle, systemImage: themeDeleteImage)
|
2022-04-26 21:27:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
#if !os(tvOS)
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension ProfileView.ShortcutsButton {
|
|
|
|
var isEligibleForSiri: Bool {
|
|
|
|
productManager.isEligible(forFeature: .siriShortcuts)
|
|
|
|
}
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
#endif
|
2023-07-03 14:54:43 +00:00
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension ProfileView.MainMenu {
|
|
|
|
func uninstallVPN() {
|
|
|
|
Task { @MainActor in
|
|
|
|
await vpnManager.uninstall()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeProfile() {
|
|
|
|
withAnimation {
|
|
|
|
profileManager.removeProfiles(withIds: [header.id])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
#if !os(tvOS)
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension ProfileView.ShortcutsButton {
|
|
|
|
func presentShortcutsOrPaywall() {
|
|
|
|
|
|
|
|
// eligibility: enter Siri shortcuts or present paywall
|
|
|
|
if isEligibleForSiri {
|
|
|
|
modalType = .shortcuts
|
|
|
|
} else {
|
|
|
|
modalType = .paywallShortcuts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
#endif
|
2023-07-03 14:54:43 +00:00
|
|
|
|
|
|
|
private extension ProfileView.DuplicateButton {
|
|
|
|
func duplicateProfile(withId id: UUID) {
|
|
|
|
profileManager.duplicateProfile(withId: id, setAsCurrent: setAsCurrent)
|
|
|
|
}
|
|
|
|
}
|