passepartout-apple/Passepartout/App/Views/OrganizerView+Profiles.swift

205 lines
6.4 KiB
Swift
Raw Normal View History

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 {
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
@Binding private var alertType: AlertType?
2022-04-12 13:09:14 +00:00
@State private var isFirstLaunch = true
@State private var isPresentingProfile = false
2022-04-12 13:09:14 +00:00
init(alertType: Binding<AlertType?>) {
2022-04-12 13:09:14 +00:00
appManager = .shared
profileManager = .shared
providerManager = .shared
productManager = .shared
_alertType = alertType
2022-04-12 13:09:14 +00:00
}
var body: some View {
debugChanges()
return ZStack {
hiddenProfileLink
mainView
if profileManager.headers.isEmpty {
emptyView
2022-04-12 13:09:14 +00:00
}
}.onAppear {
performMigrationsIfNeeded()
}.onChange(of: profileManager.headers) {
dismissSelectionIfDeleted(headers: $0)
}
2022-04-12 13:09:14 +00:00
// from AddProfileView
.onReceive(profileManager.didCreateProfile) {
presentProfile(withId: $0.id)
2022-04-12 13:09:14 +00:00
}
}
private var mainView: some View {
List {
activeHeaders.map { headers in
Section(
header: Text(L10n.Organizer.Sections.active)
) {
ForEach(headers, content: profileButton(forHeader:))
.onDelete(perform: removeActiveProfile)
}
}
let headers = otherHeaders
if !headers.isEmpty {
Section(
header: Text(L10n.Global.Strings.profiles)
) {
ForEach(headers, content: profileButton(forHeader:))
.onDelete(perform: removeOtherProfiles)
}
}
2022-04-23 10:08:24 +00:00
}.themeAnimation(on: profileManager.headers)
}
private var emptyView: some View {
VStack {
Text(L10n.Organizer.Empty.noProfiles)
.themeInformativeTextStyle()
}
}
private func profileButton(forHeader header: Profile.Header) -> some View {
Button {
presentProfile(withId: header.id)
2022-04-12 13:09:14 +00:00
} label: {
ProfileHeaderRow(
header: header,
isActive: profileManager.isActiveProfile(header.id)
)
2022-04-26 20:33:16 +00:00
}.contextMenu {
ProfileView.DuplicateButton(header: header)
2022-04-23 09:27:17 +00:00
}.themeTextButtonStyle()
2022-04-12 13:09:14 +00:00
}
private var hiddenProfileLink: some View {
NavigationLink("", isActive: $isPresentingProfile) {
ProfileView()
}.onAppear(perform: presentActiveProfile)
}
2022-04-12 13:09:14 +00:00
}
}
extension OrganizerView.ProfilesList {
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()
}
private func presentActiveProfile() {
// 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
}
guard isFirstLaunch, profileManager.hasActiveProfile else {
return
}
isFirstLaunch = false
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
}
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
}
removeProfiles(withIds: [activeHeader.id])
2022-04-12 13:09:14 +00:00
}
private func removeOtherProfiles(_ indexSet: IndexSet) {
let currentHeaders = otherHeaders
var toDelete: [UUID] = []
indexSet.forEach {
toDelete.append(currentHeaders[$0].id)
}
removeProfiles(withIds: toDelete)
}
private func removeProfiles(withIds toDelete: [UUID]) {
// clear selection before removal to avoid triggering a bogus navigation push
if toDelete.contains(profileManager.currentProfile.value.id) {
isPresentingProfile = false
}
profileManager.removeProfiles(withIds: toDelete)
}
2022-04-26 20:33:16 +00:00
private func performMigrationsIfNeeded() {
Task {
await appManager.doMigrations(profileManager)
}
}
2022-04-12 13:09:14 +00:00
private func dismissSelectionIfDeleted(headers: [Profile.Header]) {
if isPresentingProfile, !profileManager.isCurrentProfileExisting() {
isPresentingProfile = false
2022-04-12 13:09:14 +00:00
}
}
}