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-22 13:58:03 +00:00
|
|
|
@State private var isPresentingProfile = false
|
2022-04-12 13:09:14 +00:00
|
|
|
|
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-22 13:58:03 +00:00
|
|
|
return ZStack {
|
2022-04-22 17:02:54 +00:00
|
|
|
hiddenProfileLink
|
2022-04-19 17:35:44 +00:00
|
|
|
mainView
|
2022-04-21 09:43:47 +00:00
|
|
|
if profileManager.headers.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 {
|
|
|
|
performMigrationsIfNeeded()
|
2022-04-21 09:43:47 +00:00
|
|
|
}.onChange(of: profileManager.headers) {
|
|
|
|
dismissSelectionIfDeleted(headers: $0)
|
2022-04-19 17:35:44 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
// from AddProfileView
|
|
|
|
.onReceive(profileManager.didCreateProfile) {
|
2022-04-22 13:58:03 +00:00
|
|
|
presentProfile(withId: $0.id)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2022-04-22 13:46:24 +00:00
|
|
|
activeHeaders.map { headers in
|
2022-04-25 14:04:56 +00:00
|
|
|
Section(
|
|
|
|
header: Text(L10n.Organizer.Sections.active)
|
|
|
|
) {
|
2022-04-22 13:46:24 +00:00
|
|
|
ForEach(headers, content: profileButton(forHeader:))
|
|
|
|
.onDelete(perform: removeActiveProfile)
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 14:04:56 +00:00
|
|
|
let headers = otherHeaders
|
|
|
|
if !headers.isEmpty {
|
|
|
|
Section(
|
|
|
|
header: Text(L10n.Global.Strings.profiles)
|
|
|
|
) {
|
|
|
|
ForEach(headers, content: profileButton(forHeader:))
|
|
|
|
.onDelete(perform: removeOtherProfiles)
|
|
|
|
}
|
2022-04-18 10:01:42 +00:00
|
|
|
}
|
2022-04-23 10:08:24 +00:00
|
|
|
}.themeAnimation(on: profileManager.headers)
|
2022-04-18 10:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var emptyView: some View {
|
|
|
|
VStack {
|
2022-04-12 16:51:24 +00:00
|
|
|
Text(L10n.Organizer.Empty.noProfiles)
|
2022-04-23 09:42:26 +00:00
|
|
|
.themeInformativeTextStyle()
|
2022-04-18 10:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-22 17:02:54 +00:00
|
|
|
|
2022-04-22 13:58:03 +00:00
|
|
|
private func profileButton(forHeader header: Profile.Header) -> some View {
|
|
|
|
Button {
|
|
|
|
presentProfile(withId: header.id)
|
2022-04-12 13:09:14 +00:00
|
|
|
} label: {
|
2022-04-21 17:33:47 +00:00
|
|
|
ProfileHeaderRow(
|
|
|
|
header: header,
|
|
|
|
isActive: profileManager.isActiveProfile(header.id)
|
|
|
|
)
|
2022-04-26 20:33:16 +00:00
|
|
|
}.contextMenu {
|
2022-04-27 14:47:25 +00:00
|
|
|
ProfileView.DuplicateButton(
|
|
|
|
header: header,
|
|
|
|
switchCurrentProfile: false
|
|
|
|
)
|
2022-04-23 09:27:17 +00:00
|
|
|
}.themeTextButtonStyle()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2022-04-22 17:02:54 +00:00
|
|
|
|
|
|
|
private var hiddenProfileLink: some View {
|
|
|
|
NavigationLink("", isActive: $isPresentingProfile) {
|
|
|
|
ProfileView()
|
|
|
|
}.onAppear(perform: presentActiveProfile)
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 10:01:42 +00:00
|
|
|
extension OrganizerView.ProfilesList {
|
2022-04-22 13:46:24 +00:00
|
|
|
private var activeHeaders: [Profile.Header]? {
|
|
|
|
guard let activeHeader = profileManager.activeHeader else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return [activeHeader]
|
|
|
|
}
|
|
|
|
|
|
|
|
private var otherHeaders: [Profile.Header] {
|
|
|
|
profileManager.headers
|
|
|
|
.filter {
|
|
|
|
!profileManager.isActiveProfile($0.id)
|
|
|
|
}.sorted()
|
2022-04-19 21:38:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 13:58:03 +00:00
|
|
|
private func presentActiveProfile() {
|
2022-04-23 08:04:10 +00:00
|
|
|
|
|
|
|
// do not present profile if:
|
|
|
|
//
|
|
|
|
// - an alert is active, as it would break navigation
|
|
|
|
// - on iPad, as it's already shown
|
|
|
|
//
|
|
|
|
guard alertType == nil, themeIdiom != .pad else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-22 13:58:03 +00:00
|
|
|
guard isFirstLaunch, profileManager.hasActiveProfile else {
|
2022-04-19 16:45:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
isFirstLaunch = false
|
2022-04-22 13:58:03 +00:00
|
|
|
isPresentingProfile = true
|
|
|
|
}
|
|
|
|
|
|
|
|
private func presentProfile(withId id: UUID) {
|
|
|
|
do {
|
|
|
|
try profileManager.loadCurrentProfile(withId: id, makeReady: true)
|
|
|
|
isPresentingProfile = true
|
|
|
|
} catch {
|
|
|
|
pp_log.error("Unable to load profile: \(error)")
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 13:46:24 +00:00
|
|
|
private func removeActiveProfile(_ indexSet: IndexSet) {
|
|
|
|
guard let activeHeader = activeHeaders?.first else {
|
|
|
|
assertionFailure("Removing active profile while nil?")
|
|
|
|
return
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2022-04-22 13:46:24 +00:00
|
|
|
removeProfiles(withIds: [activeHeader.id])
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2022-04-18 10:01:42 +00:00
|
|
|
|
2022-04-22 13:46:24 +00:00
|
|
|
private func removeOtherProfiles(_ indexSet: IndexSet) {
|
|
|
|
let currentHeaders = otherHeaders
|
2022-04-19 06:33:22 +00:00
|
|
|
var toDelete: [UUID] = []
|
|
|
|
indexSet.forEach {
|
2022-04-21 09:43:47 +00:00
|
|
|
toDelete.append(currentHeaders[$0].id)
|
2022-04-19 06:33:22 +00:00
|
|
|
}
|
2022-04-22 13:46:24 +00:00
|
|
|
removeProfiles(withIds: toDelete)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func removeProfiles(withIds toDelete: [UUID]) {
|
2022-04-19 16:34:02 +00:00
|
|
|
|
|
|
|
// clear selection before removal to avoid triggering a bogus navigation push
|
2022-04-22 13:58:03 +00:00
|
|
|
if toDelete.contains(profileManager.currentProfile.value.id) {
|
|
|
|
isPresentingProfile = false
|
2022-04-19 16:34:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:33:22 +00:00
|
|
|
profileManager.removeProfiles(withIds: toDelete)
|
|
|
|
}
|
2022-04-26 20:33:16 +00:00
|
|
|
|
2022-04-22 13:46:24 +00:00
|
|
|
private func performMigrationsIfNeeded() {
|
|
|
|
Task {
|
|
|
|
await appManager.doMigrations(profileManager)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private func dismissSelectionIfDeleted(headers: [Profile.Header]) {
|
2022-04-22 13:58:03 +00:00
|
|
|
if isPresentingProfile, !profileManager.isCurrentProfileExisting() {
|
|
|
|
isPresentingProfile = false
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|