2022-05-03 10:32:03 +00:00
|
|
|
//
|
|
|
|
// OrganizerView+Profiles.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 5/3/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
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
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
|
2022-05-03 10:32:03 +00:00
|
|
|
|
|
|
|
init() {
|
|
|
|
profileManager = .shared
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var mainView: some View {
|
|
|
|
List {
|
|
|
|
if profileManager.hasProfiles {
|
2022-05-15 18:50:01 +00:00
|
|
|
|
2022-05-19 18:11:08 +00:00
|
|
|
// 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 {
|
2022-05-03 10:32:03 +00:00
|
|
|
Section {
|
|
|
|
profilesView
|
|
|
|
} header: {
|
|
|
|
Text(L10n.Global.Strings.profiles)
|
|
|
|
}
|
2022-05-03 10:48:43 +00:00
|
|
|
} else {
|
|
|
|
profilesView
|
2022-05-03 10:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}.themeAnimation(on: profileManager.headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var profilesView: some View {
|
|
|
|
ForEach(sortedHeaders, content: profileRow(forHeader:))
|
|
|
|
.onDelete(perform: removeProfiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var emptyView: some View {
|
|
|
|
VStack {
|
|
|
|
Text(L10n.Organizer.Empty.noProfiles)
|
|
|
|
.themeInformativeTextStyle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func profileRow(forHeader header: Profile.Header) -> some View {
|
2022-05-03 10:38:10 +00:00
|
|
|
NavigationLink(tag: header.id, selection: $profileManager.currentProfileId) {
|
2022-05-03 10:32:03 +00:00
|
|
|
ProfileView()
|
|
|
|
} label: {
|
|
|
|
profileLabel(forHeader: header)
|
|
|
|
}.contextMenu {
|
2022-05-15 20:22:19 +00:00
|
|
|
duplicateButton(forHeader: header)
|
|
|
|
deleteButton(forHeader: header)
|
2022-05-03 10:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func profileLabel(forHeader header: Profile.Header) -> some View {
|
|
|
|
ProfileRow(
|
|
|
|
header: header,
|
2022-05-20 06:27:31 +00:00
|
|
|
isActiveProfile: profileManager.isActiveProfile(header.id)
|
2022-05-03 10:32:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:22:19 +00:00
|
|
|
private func duplicateButton(forHeader header: Profile.Header) -> some View {
|
2022-05-03 10:32:03 +00:00
|
|
|
ProfileView.DuplicateButton(
|
|
|
|
header: header,
|
2022-05-03 14:45:36 +00:00
|
|
|
setAsCurrent: false
|
2022-05-03 10:32:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:22:19 +00:00
|
|
|
private func deleteButton(forHeader header: Profile.Header) -> some View {
|
|
|
|
DestructiveButton {
|
|
|
|
withAnimation {
|
|
|
|
profileManager.removeProfiles(withIds: [header.id])
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label(L10n.Global.Strings.delete, systemImage: themeDeleteImage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 10:32:03 +00:00
|
|
|
private var sortedHeaders: [Profile.Header] {
|
|
|
|
profileManager.headers
|
2022-07-03 21:59:30 +00:00
|
|
|
.sorted()
|
|
|
|
// .sorted {
|
|
|
|
// if profileManager.isActiveProfile($0.id) {
|
|
|
|
// return true
|
|
|
|
// } else if profileManager.isActiveProfile($1.id) {
|
|
|
|
// return false
|
|
|
|
// } else {
|
|
|
|
// return $0 < $1
|
|
|
|
// }
|
|
|
|
// }
|
2022-05-03 10:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func removeProfiles(at offsets: IndexSet) {
|
|
|
|
let currentHeaders = sortedHeaders
|
|
|
|
var toDelete: [UUID] = []
|
|
|
|
offsets.forEach {
|
|
|
|
toDelete.append(currentHeaders[$0].id)
|
|
|
|
}
|
2022-05-03 11:10:58 +00:00
|
|
|
withAnimation {
|
|
|
|
profileManager.removeProfiles(withIds: toDelete)
|
|
|
|
}
|
2022-05-03 10:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func performMigrationsIfNeeded() {
|
2022-10-13 06:53:50 +00:00
|
|
|
Task { @MainActor in
|
2022-06-15 18:53:37 +00:00
|
|
|
UpgradeManager.shared.doMigrations(profileManager)
|
2022-05-03 10:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|