Skip to content

Commit

Permalink
Replace active modules count with a description (#915)
Browse files Browse the repository at this point in the history
To get access to modules, try to avoid full Profile objects. Instead,
replace the coupled ProfileHeader occurrences with a new intermediary
ProfilePreview everywhere.

This way, a ProfileProcessor can inject the localized modules
descriptions from above with the preview() method.
  • Loading branch information
keeshux authored Nov 22, 2024
1 parent a2f2464 commit 4b4fca8
Show file tree
Hide file tree
Showing 30 changed files with 229 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private extension InstalledProfileView {
ProfileContextMenu(
profileManager: profileManager,
tunnel: tunnel,
header: (profile ?? .mock).header(),
preview: .init(profile ?? .mock),
interactiveManager: interactiveManager,
errorHandler: errorHandler,
isInstalledProfile: true,
Expand Down Expand Up @@ -324,7 +324,7 @@ private struct ContentView: View {
style: .full,
profileManager: .mock,
tunnel: .mock,
header: Profile.mock.header(),
preview: .init(.mock),
interactiveManager: InteractiveManager(),
errorHandler: .default(),
nextProfileId: .constant(nil),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
//

import CommonLibrary
import PassepartoutKit
import SwiftUI

struct ProfileCardView: View {
Expand All @@ -36,21 +35,22 @@ struct ProfileCardView: View {

let style: Style

let header: ProfileHeader
let preview: ProfilePreview

var body: some View {
switch style {
case .compact:
Text(header.name)
Text(preview.name)
.themeTruncating()
.frame(maxWidth: .infinity, alignment: .leading)

case .full:
VStack(alignment: .leading) {
Text(header.name)
Text(preview.name)
.font(.headline)
.themeTruncating()
Text(Strings.Views.Profiles.Rows.modules(header.modules.count))

Text(preview.subtitle ?? Strings.Views.Profiles.Rows.noModules)
.font(.subheadline)
.foregroundStyle(.secondary)
}
Expand All @@ -66,13 +66,13 @@ struct ProfileCardView: View {
Section {
ProfileCardView(
style: .compact,
header: Profile.mock.header()
preview: .init(.mock)
)
}
Section {
ProfileCardView(
style: .full,
header: Profile.mock.header()
preview: .init(.mock)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private struct ContainerModifier: ViewModifier {
profileManager.search(byName: $0)
}
.themeAnimation(on: profileManager.isReady, category: .profiles)
.themeAnimation(on: profileManager.headers, category: .profiles)
.themeAnimation(on: profileManager.previews, category: .profiles)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct ProfileContextMenu: View, Routable {

let tunnel: ExtendedTunnel

let header: ProfileHeader
let preview: ProfilePreview

let interactiveManager: InteractiveManager

Expand All @@ -60,7 +60,7 @@ struct ProfileContextMenu: View, Routable {
@MainActor
private extension ProfileContextMenu {
var profile: Profile? {
profileManager.profile(withId: header.id)
profileManager.profile(withId: preview.id)
}

var tunnelToggleButton: some View {
Expand Down Expand Up @@ -111,7 +111,7 @@ private extension ProfileContextMenu {

var profileEditButton: some View {
Button {
flow?.onEditProfile(header)
flow?.onEditProfile(preview)
} label: {
ThemeImageLabel(Strings.Global.edit.withTrailingDots, .profileEdit)
}
Expand All @@ -120,7 +120,7 @@ private extension ProfileContextMenu {
var profileDuplicateButton: some View {
ProfileDuplicateButton(
profileManager: profileManager,
header: header,
preview: preview,
errorHandler: errorHandler
) {
ThemeImageLabel(Strings.Global.duplicate, .contextDuplicate)
Expand All @@ -130,7 +130,7 @@ private extension ProfileContextMenu {
var profileRemoveButton: some View {
ProfileRemoveButton(
profileManager: profileManager,
header: header
preview: preview
) {
ThemeImageLabel(Strings.Global.remove, .contextRemove)
}
Expand All @@ -143,7 +143,7 @@ private extension ProfileContextMenu {
ProfileContextMenu(
profileManager: .mock,
tunnel: .mock,
header: Profile.mock.header(),
preview: .init(.mock),
interactiveManager: InteractiveManager(),
errorHandler: .default(),
isInstalledProfile: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@

import CommonLibrary
import CommonUtils
import PassepartoutKit
import SwiftUI

struct ProfileDuplicateButton<Label>: View where Label: View {
let profileManager: ProfileManager

let header: ProfileHeader
let preview: ProfilePreview

let errorHandler: ErrorHandler

Expand All @@ -41,12 +40,12 @@ struct ProfileDuplicateButton<Label>: View where Label: View {
Button {
Task {
do {
try await profileManager.duplicate(profileWithId: header.id)
try await profileManager.duplicate(profileWithId: preview.id)
} catch {
errorHandler.handle(
error,
title: Strings.Global.duplicate,
message: Strings.Views.Profiles.Errors.duplicate(header.name)
message: Strings.Views.Profiles.Errors.duplicate(preview.name)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Foundation
import PassepartoutKit

struct ProfileFlow {
let onEditProfile: (ProfileHeader) -> Void
let onEditProfile: (ProfilePreview) -> Void

let onEditProviderEntity: (Profile) -> Void

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct ProfileGridView: View, Routable, TunnelInstallationProviding {
.unanimated()
}
LazyVGrid(columns: columns) {
ForEach(allHeaders, content: profileView)
ForEach(allPreviews, content: profileView)
.onDelete { offsets in
Task {
await profileManager.removeProfiles(at: offsets)
Expand All @@ -88,8 +88,8 @@ struct ProfileGridView: View, Routable, TunnelInstallationProviding {
// MARK: - Subviews

private extension ProfileGridView {
var allHeaders: [ProfileHeader] {
profileManager.headers
var allPreviews: [ProfilePreview] {
profileManager.previews
}

func headerView(scrollProxy: ScrollViewProxy) -> some View {
Expand All @@ -108,7 +108,7 @@ private extension ProfileGridView {
ProfileContextMenu(
profileManager: profileManager,
tunnel: tunnel,
header: $0.header(),
preview: .init($0),
interactiveManager: interactiveManager,
errorHandler: errorHandler,
isInstalledProfile: true,
Expand All @@ -118,31 +118,31 @@ private extension ProfileGridView {
}
}

func profileView(for header: ProfileHeader) -> some View {
func profileView(for preview: ProfilePreview) -> some View {
ProfileRowView(
style: .full,
profileManager: profileManager,
tunnel: tunnel,
header: header,
preview: preview,
interactiveManager: interactiveManager,
errorHandler: errorHandler,
nextProfileId: $nextProfileId,
withMarker: true,
flow: flow
)
.themeGridCell(isSelected: header.id == nextProfileId ?? currentProfile?.id)
.themeGridCell(isSelected: preview.id == nextProfileId ?? currentProfile?.id)
.contextMenu {
ProfileContextMenu(
profileManager: profileManager,
tunnel: tunnel,
header: header,
preview: preview,
interactiveManager: interactiveManager,
errorHandler: errorHandler,
isInstalledProfile: false,
flow: flow
)
}
.id(header.id)
.id(preview.id)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//

import PassepartoutKit
import CommonLibrary
import SwiftUI

struct ProfileInfoButton: View {
let header: ProfileHeader
let preview: ProfilePreview

let onEdit: (ProfileHeader) -> Void
let onEdit: (ProfilePreview) -> Void

var body: some View {
Button {
onEdit(header)
onEdit(preview)
} label: {
ThemeImage(.info)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct ProfileListView: View, Routable, TunnelInstallationProviding {
.unanimated()
}
Group {
ForEach(allHeaders, content: profileView)
ForEach(allPreviews, content: profileView)
.onDelete { offsets in
Task {
await profileManager.removeProfiles(at: offsets)
Expand All @@ -78,8 +78,8 @@ struct ProfileListView: View, Routable, TunnelInstallationProviding {
}

private extension ProfileListView {
var allHeaders: [ProfileHeader] {
profileManager.headers
var allPreviews: [ProfilePreview] {
profileManager.previews
}

func headerView(scrollProxy: ScrollViewProxy) -> some View {
Expand All @@ -98,7 +98,7 @@ private extension ProfileListView {
ProfileContextMenu(
profileManager: profileManager,
tunnel: tunnel,
header: $0.header(),
preview: .init($0),
interactiveManager: interactiveManager,
errorHandler: errorHandler,
isInstalledProfile: true,
Expand All @@ -108,12 +108,12 @@ private extension ProfileListView {
}
}

func profileView(for header: ProfileHeader) -> some View {
func profileView(for preview: ProfilePreview) -> some View {
ProfileRowView(
style: cardStyle,
profileManager: profileManager,
tunnel: tunnel,
header: header,
preview: preview,
interactiveManager: interactiveManager,
errorHandler: errorHandler,
nextProfileId: $nextProfileId,
Expand All @@ -124,14 +124,14 @@ private extension ProfileListView {
ProfileContextMenu(
profileManager: profileManager,
tunnel: tunnel,
header: header,
preview: preview,
interactiveManager: interactiveManager,
errorHandler: errorHandler,
isInstalledProfile: false,
flow: flow
)
}
.id(header.id)
.id(preview.id)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@
//

import CommonLibrary
import PassepartoutKit
import SwiftUI

struct ProfileRemoveButton<Label>: View where Label: View {
let profileManager: ProfileManager

let header: ProfileHeader
let preview: ProfilePreview

let label: () -> Label

var body: some View {
Button(role: .destructive) {
Task {
await profileManager.remove(withId: header.id)
await profileManager.remove(withId: preview.id)
}
} label: {
label()
Expand Down
Loading

0 comments on commit 4b4fca8

Please sign in to comment.