2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// OrganizerView+Profiles.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 4/2/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
|
|
|
|
|
|
|
|
extension OrganizerView {
|
2022-04-18 10:01:42 +00:00
|
|
|
struct ProfilesList: View {
|
2022-04-12 13:09:14 +00:00
|
|
|
@ObservedObject private var appManager: AppManager
|
|
|
|
|
|
|
|
@ObservedObject private var profileManager: ProfileManager
|
|
|
|
|
|
|
|
@ObservedObject private var providerManager: ProviderManager
|
|
|
|
|
|
|
|
// just to observe changes in profiles eligibility
|
|
|
|
@ObservedObject private var productManager: ProductManager
|
2022-04-18 10:01:42 +00:00
|
|
|
|
|
|
|
@Binding private var alertType: AlertType?
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
@State private var isFirstLaunch = true
|
|
|
|
|
2022-04-19 17:35:44 +00:00
|
|
|
@State private var localHeaders: [Profile.Header] = []
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@State private var selectedProfileId: UUID?
|
|
|
|
|
2022-04-18 10:01:42 +00:00
|
|
|
init(alertType: Binding<AlertType?>) {
|
2022-04-12 13:09:14 +00:00
|
|
|
appManager = .shared
|
|
|
|
profileManager = .shared
|
|
|
|
providerManager = .shared
|
|
|
|
productManager = .shared
|
2022-04-18 10:01:42 +00:00
|
|
|
_alertType = alertType
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
debugChanges()
|
2022-04-19 17:35:44 +00:00
|
|
|
return Group {
|
|
|
|
mainView
|
|
|
|
if localHeaders.isEmpty {
|
2022-04-18 10:01:42 +00:00
|
|
|
emptyView
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2022-04-19 17:35:44 +00:00
|
|
|
}.onAppear {
|
|
|
|
localHeaders = profileManager.headers.sorted()
|
|
|
|
performMigrationsIfNeeded()
|
|
|
|
}.onChange(of: profileManager.headers) { newHeaders in
|
|
|
|
withAnimation {
|
|
|
|
guard Set(newHeaders) != Set(localHeaders) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
localHeaders = newHeaders.sorted()
|
|
|
|
dismissSelectionIfDeleted(headers: newHeaders)
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
// from AddProfileView
|
|
|
|
.onReceive(profileManager.didCreateProfile) {
|
|
|
|
selectedProfileId = $0.id
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 10:01:42 +00:00
|
|
|
|
2022-04-19 17:35:44 +00:00
|
|
|
private var mainView: some View {
|
2022-04-18 10:01:42 +00:00
|
|
|
List {
|
|
|
|
Section {
|
2022-04-19 17:35:44 +00:00
|
|
|
ForEach(localHeaders, content: navigationLink(forHeader:))
|
2022-04-19 06:33:22 +00:00
|
|
|
.onDelete(perform: removeProfiles)
|
2022-04-18 10:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: l10n
|
|
|
|
private var emptyView: some View {
|
|
|
|
VStack {
|
|
|
|
Text("No profiles")
|
|
|
|
.themeInformativeText()
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
private func navigationLink(forHeader header: Profile.Header) -> some View {
|
|
|
|
NavigationLink(tag: header.id, selection: $selectedProfileId) {
|
|
|
|
ProfileView(header: header)
|
|
|
|
} label: {
|
|
|
|
if profileManager.isActiveProfile(header.id) {
|
|
|
|
ActiveProfileHeaderRow(header: header)
|
|
|
|
} else {
|
|
|
|
ProfileHeaderRow(header: header)
|
|
|
|
}
|
2022-04-18 20:34:30 +00:00
|
|
|
}.onAppear {
|
2022-04-19 16:45:08 +00:00
|
|
|
preselectIfActiveProfile(header.id)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 10:01:42 +00:00
|
|
|
extension OrganizerView.ProfilesList {
|
2022-04-12 13:09:14 +00:00
|
|
|
struct ActiveProfileHeaderRow: View {
|
|
|
|
@ObservedObject private var currentVPNState: VPNManager.ObservableState
|
|
|
|
|
|
|
|
private let header: Profile.Header
|
|
|
|
|
|
|
|
init(header: Profile.Header) {
|
|
|
|
currentVPNState = .shared
|
|
|
|
self.header = header
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
debugChanges()
|
|
|
|
return ProfileHeaderRow(header: header)
|
|
|
|
.withTrailingText(statusDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var statusDescription: String {
|
|
|
|
return currentVPNState.localizedStatusDescription(
|
|
|
|
withErrors: false,
|
|
|
|
withDataCount: false
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 10:01:42 +00:00
|
|
|
extension OrganizerView.ProfilesList {
|
2022-04-19 16:45:08 +00:00
|
|
|
private func preselectIfActiveProfile(_ id: UUID) {
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
// do not push profile if:
|
|
|
|
//
|
|
|
|
// - an alert is active, as it would break navigation
|
|
|
|
// - on iPad, as it's already shown
|
|
|
|
//
|
2022-04-19 16:45:08 +00:00
|
|
|
guard alertType == nil, themeIdiom != .pad, id == profileManager.activeHeader?.id else {
|
|
|
|
return
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2022-04-19 16:45:08 +00:00
|
|
|
guard isFirstLaunch else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isFirstLaunch = false
|
|
|
|
|
|
|
|
selectedProfileId = id
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func performMigrationsIfNeeded() {
|
|
|
|
Task {
|
|
|
|
await appManager.doMigrations(profileManager)
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 10:01:42 +00:00
|
|
|
|
2022-04-19 06:33:22 +00:00
|
|
|
private func removeProfiles(_ indexSet: IndexSet) {
|
2022-04-19 16:34:02 +00:00
|
|
|
withAnimation {
|
|
|
|
doRemoveProfiles(indexSet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func doRemoveProfiles(_ indexSet: IndexSet) {
|
2022-04-19 06:33:22 +00:00
|
|
|
var toDelete: [UUID] = []
|
|
|
|
indexSet.forEach {
|
2022-04-19 17:35:44 +00:00
|
|
|
toDelete.append(localHeaders[$0].id)
|
2022-04-19 06:33:22 +00:00
|
|
|
}
|
2022-04-19 16:34:02 +00:00
|
|
|
|
|
|
|
// clear selection before removal to avoid triggering a bogus navigation push
|
|
|
|
if let selectedProfileId = selectedProfileId, toDelete.contains(selectedProfileId) {
|
|
|
|
self.selectedProfileId = nil
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:33:22 +00:00
|
|
|
profileManager.removeProfiles(withIds: toDelete)
|
2022-04-19 17:35:44 +00:00
|
|
|
localHeaders = profileManager.headers.sorted()
|
2022-04-19 06:33:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private func dismissSelectionIfDeleted(headers: [Profile.Header]) {
|
|
|
|
if let selectedProfileId = selectedProfileId,
|
|
|
|
!profileManager.isExistingProfile(withId: selectedProfileId) {
|
|
|
|
|
|
|
|
self.selectedProfileId = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|