From 504bcbdbd2c1645b6da9c4218b60af54f7d4af1e Mon Sep 17 00:00:00 2001 From: Davide Date: Sun, 6 Oct 2024 19:19:16 +0200 Subject: [PATCH] Refactor and move entities around (#698) Do some housekeeping in AppLibrary/AppUI. --- ....swift => ProfileManager+Extensions.swift} | 16 +++------ .../AppUI/Business/ProfileProcessor.swift | 31 +++++++++++++++++ .../AppUI/Business/Tunnel+Extensions.swift | 5 +-- .../ModuleTypeProviding.swift | 0 .../App => Domain}/ProfilesLayout.swift | 2 +- .../AppUI/Domain/TunnelInstallation.swift | 33 +++++++++++++++++++ .../AppUI/Views/App/ProfileGridView.swift | 8 +++-- .../AppUI/Views/App/ProfileListView.swift | 8 +++-- .../Extensions/InteractiveViewProviding.swift | 33 +++++++++++++++++++ .../Extensions}/ModuleViewProviding.swift | 7 ---- .../Extensions}/TunnelContextProviding.swift | 0 .../TunnelInstallationProviding.swift | 6 ---- .../AppUI/Views/Modules/OpenVPNView.swift | 2 +- 13 files changed, 117 insertions(+), 34 deletions(-) rename Passepartout/Library/Sources/AppUI/Business/{ProfileManagerProviding.swift => ProfileManager+Extensions.swift} (76%) create mode 100644 Passepartout/Library/Sources/AppUI/Business/ProfileProcessor.swift rename Passepartout/Library/Sources/AppUI/{Business => Domain}/ModuleTypeProviding.swift (100%) rename Passepartout/Library/Sources/AppUI/{Views/App => Domain}/ProfilesLayout.swift (92%) create mode 100644 Passepartout/Library/Sources/AppUI/Domain/TunnelInstallation.swift create mode 100644 Passepartout/Library/Sources/AppUI/Views/Extensions/InteractiveViewProviding.swift rename Passepartout/Library/Sources/AppUI/{Business => Views/Extensions}/ModuleViewProviding.swift (85%) rename Passepartout/Library/Sources/AppUI/{Business => Views/Extensions}/TunnelContextProviding.swift (100%) rename Passepartout/Library/Sources/AppUI/{Business => Views/Extensions}/TunnelInstallationProviding.swift (95%) diff --git a/Passepartout/Library/Sources/AppUI/Business/ProfileManagerProviding.swift b/Passepartout/Library/Sources/AppUI/Business/ProfileManager+Extensions.swift similarity index 76% rename from Passepartout/Library/Sources/AppUI/Business/ProfileManagerProviding.swift rename to Passepartout/Library/Sources/AppUI/Business/ProfileManager+Extensions.swift index d10c7050..65fcbdeb 100644 --- a/Passepartout/Library/Sources/AppUI/Business/ProfileManagerProviding.swift +++ b/Passepartout/Library/Sources/AppUI/Business/ProfileManager+Extensions.swift @@ -1,5 +1,5 @@ // -// ProfileManagerProviding.swift +// ProfileManager+Extensions.swift // Passepartout // // Created by Davide De Rosa on 9/3/24. @@ -27,22 +27,16 @@ import AppLibrary import Foundation import PassepartoutKit -protocol ProfileManagerProviding { - var profileManager: ProfileManager { get } -} - @MainActor -extension ProfileManagerProviding { - func removeProfiles(at offsets: IndexSet) { - let idsToRemove = profileManager.headers +extension ProfileManager { + func removeProfiles(at offsets: IndexSet) async { + let idsToRemove = headers .enumerated() .filter { offsets.contains($0.offset) } .map(\.element.id) - Task { - await profileManager.remove(withIds: idsToRemove) - } + await remove(withIds: idsToRemove) } } diff --git a/Passepartout/Library/Sources/AppUI/Business/ProfileProcessor.swift b/Passepartout/Library/Sources/AppUI/Business/ProfileProcessor.swift new file mode 100644 index 00000000..2001fe50 --- /dev/null +++ b/Passepartout/Library/Sources/AppUI/Business/ProfileProcessor.swift @@ -0,0 +1,31 @@ +// +// ProfileProcessor.swift +// Passepartout +// +// Created by Davide De Rosa on 10/6/24. +// Copyright (c) 2024 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 Foundation +import PassepartoutKit + +protocol ProfileProcessor { + func processedProfile(_ profile: Profile) throws -> Profile +} diff --git a/Passepartout/Library/Sources/AppUI/Business/Tunnel+Extensions.swift b/Passepartout/Library/Sources/AppUI/Business/Tunnel+Extensions.swift index fe99182c..12380c61 100644 --- a/Passepartout/Library/Sources/AppUI/Business/Tunnel+Extensions.swift +++ b/Passepartout/Library/Sources/AppUI/Business/Tunnel+Extensions.swift @@ -27,10 +27,7 @@ import CommonLibrary import Foundation import PassepartoutKit -protocol ProfileProcessor { - func processedProfile(_ profile: Profile) throws -> Profile -} - +@MainActor extension Tunnel { func install(_ profile: Profile, processor: ProfileProcessor) async throws { let newProfile = try processor.processedProfile(profile) diff --git a/Passepartout/Library/Sources/AppUI/Business/ModuleTypeProviding.swift b/Passepartout/Library/Sources/AppUI/Domain/ModuleTypeProviding.swift similarity index 100% rename from Passepartout/Library/Sources/AppUI/Business/ModuleTypeProviding.swift rename to Passepartout/Library/Sources/AppUI/Domain/ModuleTypeProviding.swift diff --git a/Passepartout/Library/Sources/AppUI/Views/App/ProfilesLayout.swift b/Passepartout/Library/Sources/AppUI/Domain/ProfilesLayout.swift similarity index 92% rename from Passepartout/Library/Sources/AppUI/Views/App/ProfilesLayout.swift rename to Passepartout/Library/Sources/AppUI/Domain/ProfilesLayout.swift index b84bdb74..4128e783 100644 --- a/Passepartout/Library/Sources/AppUI/Views/App/ProfilesLayout.swift +++ b/Passepartout/Library/Sources/AppUI/Domain/ProfilesLayout.swift @@ -25,7 +25,7 @@ import Foundation -public enum ProfilesLayout: String, RawRepresentable, CaseIterable, Codable { +enum ProfilesLayout: String, RawRepresentable, CaseIterable, Codable { case list case grid diff --git a/Passepartout/Library/Sources/AppUI/Domain/TunnelInstallation.swift b/Passepartout/Library/Sources/AppUI/Domain/TunnelInstallation.swift new file mode 100644 index 00000000..db38ec1c --- /dev/null +++ b/Passepartout/Library/Sources/AppUI/Domain/TunnelInstallation.swift @@ -0,0 +1,33 @@ +// +// TunnelInstallation.swift +// Passepartout +// +// Created by Davide De Rosa on 10/6/24. +// Copyright (c) 2024 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 Foundation +import PassepartoutKit + +struct TunnelInstallation { + let header: ProfileHeader + + let onDemand: Bool +} diff --git a/Passepartout/Library/Sources/AppUI/Views/App/ProfileGridView.swift b/Passepartout/Library/Sources/AppUI/Views/App/ProfileGridView.swift index 7b35afaf..f8fde3db 100644 --- a/Passepartout/Library/Sources/AppUI/Views/App/ProfileGridView.swift +++ b/Passepartout/Library/Sources/AppUI/Views/App/ProfileGridView.swift @@ -28,7 +28,7 @@ import PassepartoutKit import SwiftUI import UtilsLibrary -struct ProfileGridView: View, ProfileManagerProviding, TunnelInstallationProviding { +struct ProfileGridView: View, TunnelInstallationProviding { @Environment(\.isSearching) private var isSearching @@ -61,7 +61,11 @@ struct ProfileGridView: View, ProfileManagerProviding, TunnelInstallationProvidi } LazyVGrid(columns: columns) { ForEach(allHeaders, content: profileView) - .onDelete(perform: removeProfiles) + .onDelete { offsets in + Task { + await profileManager.removeProfiles(at: offsets) + } + } } .themeGridHeader(title: Strings.Views.Profiles.Folders.default) } diff --git a/Passepartout/Library/Sources/AppUI/Views/App/ProfileListView.swift b/Passepartout/Library/Sources/AppUI/Views/App/ProfileListView.swift index 40243696..f09cbd5f 100644 --- a/Passepartout/Library/Sources/AppUI/Views/App/ProfileListView.swift +++ b/Passepartout/Library/Sources/AppUI/Views/App/ProfileListView.swift @@ -28,7 +28,7 @@ import PassepartoutKit import SwiftUI import UtilsLibrary -struct ProfileListView: View, ProfileManagerProviding, TunnelInstallationProviding { +struct ProfileListView: View, TunnelInstallationProviding { @Environment(\.horizontalSizeClass) private var hsClass @@ -63,7 +63,11 @@ struct ProfileListView: View, ProfileManagerProviding, TunnelInstallationProvidi } Group { ForEach(allHeaders, content: profileView) - .onDelete(perform: removeProfiles) + .onDelete { offsets in + Task { + await profileManager.removeProfiles(at: offsets) + } + } } .themeSection(header: Strings.Views.Profiles.Folders.default) } diff --git a/Passepartout/Library/Sources/AppUI/Views/Extensions/InteractiveViewProviding.swift b/Passepartout/Library/Sources/AppUI/Views/Extensions/InteractiveViewProviding.swift new file mode 100644 index 00000000..b79ca17d --- /dev/null +++ b/Passepartout/Library/Sources/AppUI/Views/Extensions/InteractiveViewProviding.swift @@ -0,0 +1,33 @@ +// +// InteractiveViewProviding.swift +// Passepartout +// +// Created by Davide De Rosa on 10/6/24. +// Copyright (c) 2024 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 + +protocol InteractiveViewProviding { + associatedtype InteractiveContent: View + + @MainActor + func interactiveView(with editor: ProfileEditor) -> InteractiveContent +} diff --git a/Passepartout/Library/Sources/AppUI/Business/ModuleViewProviding.swift b/Passepartout/Library/Sources/AppUI/Views/Extensions/ModuleViewProviding.swift similarity index 85% rename from Passepartout/Library/Sources/AppUI/Business/ModuleViewProviding.swift rename to Passepartout/Library/Sources/AppUI/Views/Extensions/ModuleViewProviding.swift index e4eb3278..b64fdc91 100644 --- a/Passepartout/Library/Sources/AppUI/Business/ModuleViewProviding.swift +++ b/Passepartout/Library/Sources/AppUI/Views/Extensions/ModuleViewProviding.swift @@ -31,10 +31,3 @@ protocol ModuleViewProviding { @MainActor func moduleView(with editor: ProfileEditor) -> Content } - -protocol InteractiveViewProviding { - associatedtype InteractiveContent: View - - @MainActor - func interactiveView(with editor: ProfileEditor) -> InteractiveContent -} diff --git a/Passepartout/Library/Sources/AppUI/Business/TunnelContextProviding.swift b/Passepartout/Library/Sources/AppUI/Views/Extensions/TunnelContextProviding.swift similarity index 100% rename from Passepartout/Library/Sources/AppUI/Business/TunnelContextProviding.swift rename to Passepartout/Library/Sources/AppUI/Views/Extensions/TunnelContextProviding.swift diff --git a/Passepartout/Library/Sources/AppUI/Business/TunnelInstallationProviding.swift b/Passepartout/Library/Sources/AppUI/Views/Extensions/TunnelInstallationProviding.swift similarity index 95% rename from Passepartout/Library/Sources/AppUI/Business/TunnelInstallationProviding.swift rename to Passepartout/Library/Sources/AppUI/Views/Extensions/TunnelInstallationProviding.swift index aa1db65a..8bbe8677 100644 --- a/Passepartout/Library/Sources/AppUI/Business/TunnelInstallationProviding.swift +++ b/Passepartout/Library/Sources/AppUI/Views/Extensions/TunnelInstallationProviding.swift @@ -33,12 +33,6 @@ protocol TunnelInstallationProviding { var tunnel: Tunnel { get } } -struct TunnelInstallation { - let header: ProfileHeader - - let onDemand: Bool -} - @MainActor extension TunnelInstallationProviding { var installation: TunnelInstallation? { diff --git a/Passepartout/Library/Sources/AppUI/Views/Modules/OpenVPNView.swift b/Passepartout/Library/Sources/AppUI/Views/Modules/OpenVPNView.swift index fc641ac6..5e5a34bd 100644 --- a/Passepartout/Library/Sources/AppUI/Views/Modules/OpenVPNView.swift +++ b/Passepartout/Library/Sources/AppUI/Views/Modules/OpenVPNView.swift @@ -34,7 +34,7 @@ extension OpenVPNModule.Builder: ModuleViewProviding { extension OpenVPNModule.Builder: InteractiveViewProviding { func interactiveView(with editor: ProfileEditor) -> some View { - let draft: Binding = editor.binding(forModule: self) + let draft = editor.binding(forModule: self) return OpenVPNView.CredentialsView( isInteractive: draft.isInteractive,