2022-05-03 10:32:03 +00:00
|
|
|
//
|
|
|
|
// OrganizerView+Profiles.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 5/3/22.
|
2024-01-14 13:34:21 +00:00
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
2022-05-03 10:32: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/>.
|
|
|
|
//
|
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
#if !os(tvOS)
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
2023-04-04 07:50:45 +00:00
|
|
|
import SwiftUI
|
2022-05-03 10:32:03 +00:00
|
|
|
|
|
|
|
extension OrganizerView {
|
|
|
|
struct ProfilesList: View {
|
2022-08-28 07:19:15 +00:00
|
|
|
@ObservedObject private var profileManager: ProfileManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-03-17 15:49:35 +00:00
|
|
|
@Binding private var modalType: ModalType?
|
2022-05-03 10:32:03 +00:00
|
|
|
|
2023-03-17 15:49:35 +00:00
|
|
|
init(modalType: Binding<ModalType?>) {
|
2022-05-03 10:32:03 +00:00
|
|
|
profileManager = .shared
|
2023-03-17 15:49:35 +00:00
|
|
|
_modalType = modalType
|
2022-05-03 10:32:03 +00:00
|
|
|
}
|
2023-03-17 15:49:35 +00:00
|
|
|
|
2022-05-03 10:32:03 +00:00
|
|
|
var body: some View {
|
|
|
|
debugChanges()
|
|
|
|
return Group {
|
|
|
|
mainView
|
|
|
|
if !profileManager.hasProfiles {
|
|
|
|
emptyView
|
|
|
|
}
|
2022-10-13 06:53:50 +00:00
|
|
|
}.onAppear(perform: performMigrationsIfNeeded)
|
|
|
|
.onReceive(profileManager.didCreateProfile) {
|
2022-05-03 10:38:10 +00:00
|
|
|
profileManager.currentProfileId = $0.id
|
2022-05-03 10:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-17 15:36:32 +00:00
|
|
|
|
|
|
|
extension OrganizerView {
|
|
|
|
struct ProfileContextMenu: View {
|
|
|
|
@ObservedObject private var profileManager: ProfileManager
|
|
|
|
|
|
|
|
@ObservedObject private var currentVPNState: ObservableVPNState
|
|
|
|
|
|
|
|
let header: Profile.Header
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-10-17 15:36:32 +00:00
|
|
|
init(header: Profile.Header) {
|
|
|
|
profileManager = .shared
|
|
|
|
currentVPNState = .shared
|
|
|
|
self.header = header
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-10-17 15:36:32 +00:00
|
|
|
var body: some View {
|
2023-05-28 09:23:07 +00:00
|
|
|
if profileManager.isActiveProfile(header.id) {
|
|
|
|
reconnectButton
|
|
|
|
}
|
2022-10-17 15:36:32 +00:00
|
|
|
duplicateButton
|
|
|
|
deleteButton
|
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension OrganizerView.ProfilesList {
|
|
|
|
var mainView: some View {
|
|
|
|
List {
|
|
|
|
if profileManager.hasProfiles {
|
|
|
|
|
|
|
|
// FIXME: iPad multitasking, navigation binding does not clear on pop
|
|
|
|
// - if listStyle is different than .sidebar
|
|
|
|
// - if listStyle is .sidebar but List has no Section
|
|
|
|
if themeIsiPadMultitasking {
|
|
|
|
Section {
|
|
|
|
profilesView
|
|
|
|
} header: {
|
|
|
|
Text(L10n.Global.Strings.profiles)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
profilesView
|
|
|
|
}
|
|
|
|
}
|
2024-01-27 14:59:40 +00:00
|
|
|
}
|
|
|
|
.themeAnimation(on: profileManager.headers)
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var profilesView: some View {
|
|
|
|
ForEach(sortedProfiles, content: profileRow(forProfile:))
|
|
|
|
.onDelete(perform: removeProfiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
var emptyView: some View {
|
|
|
|
VStack {
|
|
|
|
Text(L10n.Organizer.Empty.noProfiles)
|
|
|
|
.themeInformativeTextStyle()
|
2022-10-17 15:36:32 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
2022-10-17 15:36:32 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
func profileRow(forProfile profile: Profile) -> some View {
|
|
|
|
NavigationLink(tag: profile.id, selection: $profileManager.currentProfileId) {
|
|
|
|
ProfileView()
|
|
|
|
} label: {
|
|
|
|
profileLabel(forProfile: profile)
|
2024-01-27 14:59:40 +00:00
|
|
|
}
|
|
|
|
.contextMenu {
|
2023-07-03 14:54:43 +00:00
|
|
|
OrganizerView.ProfileContextMenu(header: profile.header)
|
2022-10-17 15:36:32 +00:00
|
|
|
}
|
2024-02-03 10:52:29 +00:00
|
|
|
.themeListSelectionColor(isSelected: profileManager.isCurrentProfile(profile.id))
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
2022-10-17 15:36:32 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
func profileLabel(forProfile profile: Profile) -> some View {
|
|
|
|
OrganizerView.ProfileRow(
|
|
|
|
profile: profile,
|
|
|
|
isActiveProfile: profileManager.isActiveProfile(profile.id),
|
|
|
|
modalType: $modalType
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sortedProfiles: [Profile] {
|
|
|
|
profileManager.profiles
|
|
|
|
.sorted()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension OrganizerView.ProfileContextMenu {
|
|
|
|
var reconnectButton: some View {
|
|
|
|
ProfileView.ReconnectButton()
|
|
|
|
}
|
|
|
|
|
|
|
|
var duplicateButton: some View {
|
|
|
|
ProfileView.DuplicateButton(
|
|
|
|
header: header,
|
|
|
|
setAsCurrent: false
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var deleteButton: some View {
|
2023-07-03 15:37:16 +00:00
|
|
|
Button(role: .destructive) {
|
2023-07-03 14:54:43 +00:00
|
|
|
withAnimation {
|
|
|
|
profileManager.removeProfiles(withIds: [header.id])
|
2022-10-17 15:36:32 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
} label: {
|
|
|
|
Label(L10n.Global.Strings.delete, systemImage: themeDeleteImage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension OrganizerView.ProfilesList {
|
|
|
|
func removeProfiles(at offsets: IndexSet) {
|
|
|
|
let currentHeaders = sortedProfiles
|
|
|
|
var toDelete: [UUID] = []
|
|
|
|
offsets.forEach {
|
|
|
|
toDelete.append(currentHeaders[$0].id)
|
|
|
|
}
|
|
|
|
withAnimation {
|
|
|
|
profileManager.removeProfiles(withIds: toDelete)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func performMigrationsIfNeeded() {
|
|
|
|
Task { @MainActor in
|
2023-09-09 18:29:04 +00:00
|
|
|
UpgradeManager.shared.migrateData(profileManager: profileManager)
|
2022-10-17 15:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
#endif
|