2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// ProfileRowView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/5/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 <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
2024-11-03 12:16:13 +00:00
|
|
|
import CommonLibrary
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-09-23 13:02:26 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
2024-11-01 08:47:50 +00:00
|
|
|
struct ProfileRowView: View, Routable {
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var theme: Theme
|
|
|
|
|
|
|
|
let style: ProfileCardView.Style
|
|
|
|
|
2024-10-03 22:39:41 +00:00
|
|
|
@ObservedObject
|
|
|
|
var profileManager: ProfileManager
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-10 18:39:43 +00:00
|
|
|
let tunnel: ExtendedTunnel
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
let header: ProfileHeader
|
|
|
|
|
|
|
|
let interactiveManager: InteractiveManager
|
|
|
|
|
|
|
|
let errorHandler: ErrorHandler
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var nextProfileId: Profile.ID?
|
|
|
|
|
|
|
|
let withMarker: Bool
|
|
|
|
|
2024-10-23 15:17:20 +00:00
|
|
|
var flow: ProfileFlow?
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
HStack {
|
2024-10-04 00:50:05 +00:00
|
|
|
Group {
|
|
|
|
if withMarker {
|
|
|
|
markerView
|
|
|
|
}
|
|
|
|
cardView
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
Spacer()
|
2024-10-04 00:50:05 +00:00
|
|
|
HStack(spacing: 10.0) {
|
2024-11-10 19:54:00 +00:00
|
|
|
ProfileAttributesView(
|
|
|
|
attributes: attributes,
|
|
|
|
isRemoteImportingEnabled: profileManager.isRemoteImportingEnabled
|
|
|
|
)
|
2024-10-23 15:17:20 +00:00
|
|
|
ProfileInfoButton(header: header) {
|
|
|
|
flow?.onEditProfile($0)
|
|
|
|
}
|
2024-10-03 22:39:41 +00:00
|
|
|
}
|
2024-10-04 00:50:05 +00:00
|
|
|
.imageScale(.large)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 18:39:43 +00:00
|
|
|
// MARK: - Subviews (observing)
|
|
|
|
|
|
|
|
private struct MarkerView: View {
|
|
|
|
let headerId: Profile.ID
|
|
|
|
|
|
|
|
let nextProfileId: Profile.ID?
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var tunnel: ExtendedTunnel
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ThemeImage(headerId == nextProfileId ? .pending : statusImage)
|
|
|
|
.opaque(headerId == nextProfileId || headerId == tunnel.currentProfile?.id)
|
|
|
|
.frame(width: 24.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
var statusImage: Theme.ImageName {
|
|
|
|
switch tunnel.connectionStatus {
|
|
|
|
case .active:
|
|
|
|
return .marked
|
|
|
|
|
|
|
|
case .activating, .deactivating:
|
|
|
|
return .pending
|
|
|
|
|
|
|
|
case .inactive:
|
|
|
|
return .sleeping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-03 22:39:41 +00:00
|
|
|
|
2024-11-03 22:42:17 +00:00
|
|
|
private extension ProfileRowView {
|
2024-09-23 13:02:26 +00:00
|
|
|
var markerView: some View {
|
2024-11-10 18:39:43 +00:00
|
|
|
MarkerView(
|
|
|
|
headerId: header.id,
|
|
|
|
nextProfileId: nextProfileId,
|
|
|
|
tunnel: tunnel
|
|
|
|
)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cardView: some View {
|
|
|
|
TunnelToggleButton(
|
|
|
|
tunnel: tunnel,
|
|
|
|
profile: profileManager.profile(withId: header.id),
|
|
|
|
nextProfileId: $nextProfileId,
|
|
|
|
interactiveManager: interactiveManager,
|
2024-10-23 15:17:20 +00:00
|
|
|
errorHandler: errorHandler,
|
2024-11-18 16:43:01 +00:00
|
|
|
onProviderEntityRequired: {
|
|
|
|
flow?.onEditProviderEntity($0)
|
|
|
|
},
|
|
|
|
onPurchaseRequired: {
|
|
|
|
flow?.onPurchaseRequired($0)
|
|
|
|
},
|
2024-10-23 15:17:20 +00:00
|
|
|
label: { _ in
|
|
|
|
ProfileCardView(
|
|
|
|
style: style,
|
|
|
|
header: header
|
|
|
|
)
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
.contentShape(.rect)
|
|
|
|
}
|
|
|
|
)
|
2024-11-02 07:41:32 +00:00
|
|
|
.foregroundStyle(.primary)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-11-03 22:42:17 +00:00
|
|
|
|
2024-11-10 19:54:00 +00:00
|
|
|
var attributes: [ProfileAttributesView.Attribute] {
|
|
|
|
if isTV {
|
|
|
|
return [.tv]
|
|
|
|
} else if isShared {
|
|
|
|
return [.shared]
|
2024-11-03 22:42:17 +00:00
|
|
|
}
|
2024-11-10 19:54:00 +00:00
|
|
|
return []
|
2024-11-03 22:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var isShared: Bool {
|
|
|
|
profileManager.isRemotelyShared(profileWithId: header.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
var isTV: Bool {
|
|
|
|
isShared && profileManager.isAvailableForTV(profileWithId: header.id)
|
|
|
|
}
|
2024-11-10 19:54:00 +00:00
|
|
|
}
|
2024-11-03 22:42:17 +00:00
|
|
|
|
2024-11-10 19:54:00 +00:00
|
|
|
// MARK: - Previews
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
let profile: Profile = .mock
|
|
|
|
let profileManager: ProfileManager = .mock
|
|
|
|
|
|
|
|
return Form {
|
|
|
|
ProfileRowView(
|
|
|
|
style: .compact,
|
|
|
|
profileManager: profileManager,
|
|
|
|
tunnel: .mock,
|
|
|
|
header: profile.header(),
|
|
|
|
interactiveManager: InteractiveManager(),
|
|
|
|
errorHandler: .default(),
|
|
|
|
nextProfileId: .constant(nil),
|
|
|
|
withMarker: true
|
|
|
|
)
|
2024-11-03 22:42:17 +00:00
|
|
|
}
|
2024-11-10 19:54:00 +00:00
|
|
|
.task {
|
|
|
|
do {
|
|
|
|
try await profileManager.observeRemote(true)
|
|
|
|
try await profileManager.save(profile, force: true, remotelyShared: true)
|
|
|
|
} catch {
|
|
|
|
fatalError(error.localizedDescription)
|
|
|
|
}
|
2024-11-03 22:42:17 +00:00
|
|
|
}
|
2024-11-10 19:54:00 +00:00
|
|
|
.themeForm()
|
|
|
|
.withMockEnvironment()
|
2024-11-03 22:42:17 +00:00
|
|
|
}
|