Refactor ProfileView a little bit

- Make welcome a computed property

- Reuse first header for consistency when loading

- Convert isDeleted to better isExisting (inverted)
This commit is contained in:
Davide De Rosa 2022-04-21 19:02:47 +02:00
parent fd7c232ecc
commit 46050e0141
4 changed files with 54 additions and 81 deletions

View File

@ -7,7 +7,6 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
0E0AD49027BD53CB00FBB520 /* ProfileView+Welcome.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0AD48F27BD53CB00FBB520 /* ProfileView+Welcome.swift */; };
0E0BD27327B2EA2C00583AC5 /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0BD27227B2EA2C00583AC5 /* MainView.swift */; }; 0E0BD27327B2EA2C00583AC5 /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0BD27227B2EA2C00583AC5 /* MainView.swift */; };
0E0BD27627B2EB2200583AC5 /* DonateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0BD27527B2EB2200583AC5 /* DonateView.swift */; }; 0E0BD27627B2EB2200583AC5 /* DonateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0BD27527B2EB2200583AC5 /* DonateView.swift */; };
0E0BD27927B2EBE500583AC5 /* ShortcutsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0BD27827B2EBE500583AC5 /* ShortcutsView.swift */; }; 0E0BD27927B2EBE500583AC5 /* ShortcutsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0BD27827B2EBE500583AC5 /* ShortcutsView.swift */; };
@ -184,7 +183,6 @@
/* End PBXCopyFilesBuildPhase section */ /* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
0E0AD48F27BD53CB00FBB520 /* ProfileView+Welcome.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ProfileView+Welcome.swift"; sourceTree = "<group>"; };
0E0BD27227B2EA2C00583AC5 /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = "<group>"; }; 0E0BD27227B2EA2C00583AC5 /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = "<group>"; };
0E0BD27527B2EB2200583AC5 /* DonateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DonateView.swift; sourceTree = "<group>"; }; 0E0BD27527B2EB2200583AC5 /* DonateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DonateView.swift; sourceTree = "<group>"; };
0E0BD27827B2EBE500583AC5 /* ShortcutsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutsView.swift; sourceTree = "<group>"; }; 0E0BD27827B2EBE500583AC5 /* ShortcutsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutsView.swift; sourceTree = "<group>"; };
@ -450,7 +448,6 @@
0EBC074B27EB673C00208AD9 /* ProfileView+Rename.swift */, 0EBC074B27EB673C00208AD9 /* ProfileView+Rename.swift */,
0E3CD482280DAE92007075C0 /* ProfileView+Toolbar.swift */, 0E3CD482280DAE92007075C0 /* ProfileView+Toolbar.swift */,
0E3B7FD527E5173A00C66F13 /* ProfileView+VPN.swift */, 0E3B7FD527E5173A00C66F13 /* ProfileView+VPN.swift */,
0E0AD48F27BD53CB00FBB520 /* ProfileView+Welcome.swift */,
0E71ACF027C1073800F85C4B /* ProviderLocationView.swift */, 0E71ACF027C1073800F85C4B /* ProviderLocationView.swift */,
0E71ACEE27C106B400F85C4B /* ProviderPresetView.swift */, 0E71ACEE27C106B400F85C4B /* ProviderPresetView.swift */,
0EBC075A27EC4FFF00208AD9 /* ReportIssueView.swift */, 0EBC075A27EC4FFF00208AD9 /* ReportIssueView.swift */,
@ -941,7 +938,6 @@
0E2A8D4F27B04BBA00207D04 /* OrganizerView.swift in Sources */, 0E2A8D4F27B04BBA00207D04 /* OrganizerView.swift in Sources */,
0E49F6BB27D7638300385834 /* EndpointAdvancedView+OpenVPN.swift in Sources */, 0E49F6BB27D7638300385834 /* EndpointAdvancedView+OpenVPN.swift in Sources */,
0E71ACEF27C106B500F85C4B /* ProviderPresetView.swift in Sources */, 0E71ACEF27C106B500F85C4B /* ProviderPresetView.swift in Sources */,
0E0AD49027BD53CB00FBB520 /* ProfileView+Welcome.swift in Sources */,
0EF2212F27E66F60001D0BD7 /* AddProfileView.swift in Sources */, 0EF2212F27E66F60001D0BD7 /* AddProfileView.swift in Sources */,
0EF0FAF627DD0211007EB181 /* PaywallView.swift in Sources */, 0EF0FAF627DD0211007EB181 /* PaywallView.swift in Sources */,
0E5349BE27C16A4500C71BB3 /* StyledPicker.swift in Sources */, 0E5349BE27C16A4500C71BB3 /* StyledPicker.swift in Sources */,

View File

@ -30,8 +30,6 @@ extension ProfileView {
struct VPNSection: View { struct VPNSection: View {
@ObservedObject private var profileManager: ProfileManager @ObservedObject private var profileManager: ProfileManager
@ObservedObject private var currentProfile: ObservableProfile
@ObservedObject private var providerManager: ProviderManager @ObservedObject private var providerManager: ProviderManager
@ObservedObject private var vpnManager: VPNManager @ObservedObject private var vpnManager: VPNManager
@ -39,6 +37,10 @@ extension ProfileView {
@ObservedObject private var currentVPNState: VPNManager.ObservableState @ObservedObject private var currentVPNState: VPNManager.ObservableState
@ObservedObject private var productManager: ProductManager @ObservedObject private var productManager: ProductManager
@ObservedObject private var currentProfile: ObservableProfile
private let isLoaded: Bool
private var isActiveProfile: Bool { private var isActiveProfile: Bool {
profileManager.isCurrentProfileActive() profileManager.isCurrentProfileActive()
@ -48,26 +50,35 @@ extension ProfileView {
productManager.isEligible(forFeature: .siriShortcuts) productManager.isEligible(forFeature: .siriShortcuts)
} }
init(currentProfile: ObservableProfile) { init(currentProfile: ObservableProfile, isLoaded: Bool) {
profileManager = .shared profileManager = .shared
providerManager = .shared providerManager = .shared
vpnManager = .shared vpnManager = .shared
currentVPNState = .shared currentVPNState = .shared
productManager = .shared productManager = .shared
self.currentProfile = currentProfile self.currentProfile = currentProfile
self.isLoaded = isLoaded
} }
var body: some View { var body: some View {
if isActiveProfile { if isLoaded {
activeView if isActiveProfile {
activeView
} else {
inactiveSubview
}
} else { } else {
inactiveSubview loadingView
} }
} }
private var headerView: some View {
Text(Unlocalized.VPN.vpn)
}
private var activeView: some View { private var activeView: some View {
Section( Section(
header: Text(Unlocalized.VPN.vpn), header: headerView,
footer: Text(L10n.Profile.Sections.Vpn.footer) footer: Text(L10n.Profile.Sections.Vpn.footer)
.xxxThemeTruncation() .xxxThemeTruncation()
) { ) {
@ -93,7 +104,7 @@ extension ProfileView {
private var inactiveSubview: some View { private var inactiveSubview: some View {
Section( Section(
header: Text(Unlocalized.VPN.vpn) header: headerView
) { ) {
Button(L10n.Profile.Items.UseProfile.caption) { Button(L10n.Profile.Items.UseProfile.caption) {
withAnimation { withAnimation {
@ -106,6 +117,14 @@ extension ProfileView {
} }
} }
private var loadingView: some View {
Section(
header: headerView
) {
ProgressView()
}
}
private func toggleVPNAndDonateIntents() { private func toggleVPNAndDonateIntents() {
guard vpnManager.toggle() else { guard vpnManager.toggle() else {
return return

View File

@ -1,36 +0,0 @@
//
// ProfileView+Welcome.swift
// Passepartout
//
// Created by Davide De Rosa on 2/9/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
extension ProfileView {
struct WelcomeView: View {
var body: some View {
Text(L10n.Profile.Welcome.message)
.multilineTextAlignment(.center)
.themeInformativeText()
}
}
}

View File

@ -49,8 +49,8 @@ struct ProfileView: View {
private let header: Profile.Header private let header: Profile.Header
private var isDeleted: Bool { private var isExisting: Bool {
!profileManager.isExistingProfile(withId: header.id) profileManager.isExistingProfile(withId: header.id)
} }
@State private var modalType: ModalType? @State private var modalType: ModalType?
@ -67,14 +67,8 @@ struct ProfileView: View {
var body: some View { var body: some View {
debugChanges() debugChanges()
return Group { return Group {
if !isDeleted { if isExisting {
List { mainView
if isLoaded {
mainView
} else {
loadingSection
}
}
} else { } else {
welcomeView welcomeView
} }
@ -82,12 +76,12 @@ struct ProfileView: View {
ToolbarItemGroup(placement: .navigationBarTrailing) { ToolbarItemGroup(placement: .navigationBarTrailing) {
ShortcutsItem( ShortcutsItem(
modalType: $modalType modalType: $modalType
).disabled(isDeleted) ).disabled(!isExisting)
RenameItem( RenameItem(
currentProfile: profileManager.currentProfile, currentProfile: profileManager.currentProfile,
modalType: $modalType modalType: $modalType
).disabled(isDeleted) ).disabled(!isExisting)
} }
}.sheet(item: $modalType, content: presentedModal) }.sheet(item: $modalType, content: presentedModal)
.onAppear(perform: loadProfileIfNeeded) .onAppear(perform: loadProfileIfNeeded)
@ -96,26 +90,34 @@ struct ProfileView: View {
} }
private var title: String { private var title: String {
!isDeleted ? header.name : "" isExisting ? header.name : ""
} }
@ViewBuilder
private var mainView: some View { private var mainView: some View {
VPNSection(currentProfile: profileManager.currentProfile) List {
ProviderSection(currentProfile: profileManager.currentProfile) VPNSection(
ConfigurationSection( currentProfile: profileManager.currentProfile,
currentProfile: profileManager.currentProfile, isLoaded: isLoaded
modalType: $modalType )
) if isLoaded {
ExtraSection(currentProfile: profileManager.currentProfile) ProviderSection(currentProfile: profileManager.currentProfile)
DiagnosticsSection(currentProfile: profileManager.currentProfile) ConfigurationSection(
UninstallVPNSection() currentProfile: profileManager.currentProfile,
modalType: $modalType
)
ExtraSection(currentProfile: profileManager.currentProfile)
DiagnosticsSection(currentProfile: profileManager.currentProfile)
UninstallVPNSection()
}
}
} }
private var welcomeView: some View { private var welcomeView: some View {
WelcomeView() Text(L10n.Profile.Welcome.message)
.multilineTextAlignment(.center)
.themeInformativeText()
} }
@ViewBuilder @ViewBuilder
private func presentedModal(_ modalType: ModalType) -> some View { private func presentedModal(_ modalType: ModalType) -> some View {
switch modalType { switch modalType {
@ -155,14 +157,6 @@ struct ProfileView: View {
} }
} }
private var loadingSection: some View {
Section(
header: Text(Unlocalized.VPN.vpn)
) {
ProgressView()
}
}
private func loadProfileIfNeeded() { private func loadProfileIfNeeded() {
guard !isLoaded else { guard !isLoaded else {
return return