From 46050e014170f9b91b2d48a5abf4f4ade453e42c Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Thu, 21 Apr 2022 19:02:47 +0200 Subject: [PATCH] Refactor ProfileView a little bit - Make welcome a computed property - Reuse first header for consistency when loading - Convert isDeleted to better isExisting (inverted) --- Passepartout.xcodeproj/project.pbxproj | 4 -- Passepartout/App/Views/ProfileView+VPN.swift | 35 ++++++++--- .../App/Views/ProfileView+Welcome.swift | 36 ----------- Passepartout/App/Views/ProfileView.swift | 60 +++++++++---------- 4 files changed, 54 insertions(+), 81 deletions(-) delete mode 100644 Passepartout/App/Views/ProfileView+Welcome.swift diff --git a/Passepartout.xcodeproj/project.pbxproj b/Passepartout.xcodeproj/project.pbxproj index bfe9707f..c6a77547 100644 --- a/Passepartout.xcodeproj/project.pbxproj +++ b/Passepartout.xcodeproj/project.pbxproj @@ -7,7 +7,6 @@ objects = { /* 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 */; }; 0E0BD27627B2EB2200583AC5 /* DonateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0BD27527B2EB2200583AC5 /* DonateView.swift */; }; 0E0BD27927B2EBE500583AC5 /* ShortcutsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0BD27827B2EBE500583AC5 /* ShortcutsView.swift */; }; @@ -184,7 +183,6 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 0E0AD48F27BD53CB00FBB520 /* ProfileView+Welcome.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ProfileView+Welcome.swift"; sourceTree = ""; }; 0E0BD27227B2EA2C00583AC5 /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = ""; }; 0E0BD27527B2EB2200583AC5 /* DonateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DonateView.swift; sourceTree = ""; }; 0E0BD27827B2EBE500583AC5 /* ShortcutsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutsView.swift; sourceTree = ""; }; @@ -450,7 +448,6 @@ 0EBC074B27EB673C00208AD9 /* ProfileView+Rename.swift */, 0E3CD482280DAE92007075C0 /* ProfileView+Toolbar.swift */, 0E3B7FD527E5173A00C66F13 /* ProfileView+VPN.swift */, - 0E0AD48F27BD53CB00FBB520 /* ProfileView+Welcome.swift */, 0E71ACF027C1073800F85C4B /* ProviderLocationView.swift */, 0E71ACEE27C106B400F85C4B /* ProviderPresetView.swift */, 0EBC075A27EC4FFF00208AD9 /* ReportIssueView.swift */, @@ -941,7 +938,6 @@ 0E2A8D4F27B04BBA00207D04 /* OrganizerView.swift in Sources */, 0E49F6BB27D7638300385834 /* EndpointAdvancedView+OpenVPN.swift in Sources */, 0E71ACEF27C106B500F85C4B /* ProviderPresetView.swift in Sources */, - 0E0AD49027BD53CB00FBB520 /* ProfileView+Welcome.swift in Sources */, 0EF2212F27E66F60001D0BD7 /* AddProfileView.swift in Sources */, 0EF0FAF627DD0211007EB181 /* PaywallView.swift in Sources */, 0E5349BE27C16A4500C71BB3 /* StyledPicker.swift in Sources */, diff --git a/Passepartout/App/Views/ProfileView+VPN.swift b/Passepartout/App/Views/ProfileView+VPN.swift index 625e0a3d..6137b5dd 100644 --- a/Passepartout/App/Views/ProfileView+VPN.swift +++ b/Passepartout/App/Views/ProfileView+VPN.swift @@ -30,8 +30,6 @@ extension ProfileView { struct VPNSection: View { @ObservedObject private var profileManager: ProfileManager - @ObservedObject private var currentProfile: ObservableProfile - @ObservedObject private var providerManager: ProviderManager @ObservedObject private var vpnManager: VPNManager @@ -39,6 +37,10 @@ extension ProfileView { @ObservedObject private var currentVPNState: VPNManager.ObservableState @ObservedObject private var productManager: ProductManager + + @ObservedObject private var currentProfile: ObservableProfile + + private let isLoaded: Bool private var isActiveProfile: Bool { profileManager.isCurrentProfileActive() @@ -48,26 +50,35 @@ extension ProfileView { productManager.isEligible(forFeature: .siriShortcuts) } - init(currentProfile: ObservableProfile) { + init(currentProfile: ObservableProfile, isLoaded: Bool) { profileManager = .shared providerManager = .shared vpnManager = .shared currentVPNState = .shared productManager = .shared self.currentProfile = currentProfile + self.isLoaded = isLoaded } var body: some View { - if isActiveProfile { - activeView + if isLoaded { + if isActiveProfile { + activeView + } else { + inactiveSubview + } } else { - inactiveSubview + loadingView } } + private var headerView: some View { + Text(Unlocalized.VPN.vpn) + } + private var activeView: some View { Section( - header: Text(Unlocalized.VPN.vpn), + header: headerView, footer: Text(L10n.Profile.Sections.Vpn.footer) .xxxThemeTruncation() ) { @@ -93,7 +104,7 @@ extension ProfileView { private var inactiveSubview: some View { Section( - header: Text(Unlocalized.VPN.vpn) + header: headerView ) { Button(L10n.Profile.Items.UseProfile.caption) { withAnimation { @@ -106,6 +117,14 @@ extension ProfileView { } } + private var loadingView: some View { + Section( + header: headerView + ) { + ProgressView() + } + } + private func toggleVPNAndDonateIntents() { guard vpnManager.toggle() else { return diff --git a/Passepartout/App/Views/ProfileView+Welcome.swift b/Passepartout/App/Views/ProfileView+Welcome.swift deleted file mode 100644 index 92f4d1e5..00000000 --- a/Passepartout/App/Views/ProfileView+Welcome.swift +++ /dev/null @@ -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 . -// - -import SwiftUI - -extension ProfileView { - struct WelcomeView: View { - var body: some View { - Text(L10n.Profile.Welcome.message) - .multilineTextAlignment(.center) - .themeInformativeText() - } - } -} diff --git a/Passepartout/App/Views/ProfileView.swift b/Passepartout/App/Views/ProfileView.swift index 0286da73..d1e44653 100644 --- a/Passepartout/App/Views/ProfileView.swift +++ b/Passepartout/App/Views/ProfileView.swift @@ -49,8 +49,8 @@ struct ProfileView: View { private let header: Profile.Header - private var isDeleted: Bool { - !profileManager.isExistingProfile(withId: header.id) + private var isExisting: Bool { + profileManager.isExistingProfile(withId: header.id) } @State private var modalType: ModalType? @@ -67,14 +67,8 @@ struct ProfileView: View { var body: some View { debugChanges() return Group { - if !isDeleted { - List { - if isLoaded { - mainView - } else { - loadingSection - } - } + if isExisting { + mainView } else { welcomeView } @@ -82,12 +76,12 @@ struct ProfileView: View { ToolbarItemGroup(placement: .navigationBarTrailing) { ShortcutsItem( modalType: $modalType - ).disabled(isDeleted) + ).disabled(!isExisting) RenameItem( currentProfile: profileManager.currentProfile, modalType: $modalType - ).disabled(isDeleted) + ).disabled(!isExisting) } }.sheet(item: $modalType, content: presentedModal) .onAppear(perform: loadProfileIfNeeded) @@ -96,26 +90,34 @@ struct ProfileView: View { } private var title: String { - !isDeleted ? header.name : "" + isExisting ? header.name : "" } - @ViewBuilder private var mainView: some View { - VPNSection(currentProfile: profileManager.currentProfile) - ProviderSection(currentProfile: profileManager.currentProfile) - ConfigurationSection( - currentProfile: profileManager.currentProfile, - modalType: $modalType - ) - ExtraSection(currentProfile: profileManager.currentProfile) - DiagnosticsSection(currentProfile: profileManager.currentProfile) - UninstallVPNSection() + List { + VPNSection( + currentProfile: profileManager.currentProfile, + isLoaded: isLoaded + ) + if isLoaded { + ProviderSection(currentProfile: profileManager.currentProfile) + ConfigurationSection( + currentProfile: profileManager.currentProfile, + modalType: $modalType + ) + ExtraSection(currentProfile: profileManager.currentProfile) + DiagnosticsSection(currentProfile: profileManager.currentProfile) + UninstallVPNSection() + } + } } private var welcomeView: some View { - WelcomeView() + Text(L10n.Profile.Welcome.message) + .multilineTextAlignment(.center) + .themeInformativeText() } - + @ViewBuilder private func presentedModal(_ modalType: ModalType) -> some View { switch modalType { @@ -155,14 +157,6 @@ struct ProfileView: View { } } - private var loadingSection: some View { - Section( - header: Text(Unlocalized.VPN.vpn) - ) { - ProgressView() - } - } - private func loadProfileIfNeeded() { guard !isLoaded else { return