2024-11-14 10:02:26 +00:00
|
|
|
//
|
2024-11-16 11:29:03 +00:00
|
|
|
// MigrateContentView+List.swift
|
2024-11-14 10:02:26 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 11/13/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/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import CommonLibrary
|
|
|
|
import SwiftUI
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
extension MigrateContentView {
|
|
|
|
struct ListView: View {
|
|
|
|
let step: MigrateViewStep
|
2024-11-14 14:11:25 +00:00
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
let profiles: [MigratableProfile]
|
|
|
|
|
|
|
|
@Binding
|
2024-11-14 14:11:25 +00:00
|
|
|
var statuses: [UUID: MigrationStatus]
|
2024-11-14 10:02:26 +00:00
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
@Binding
|
|
|
|
var isEditing: Bool
|
|
|
|
|
|
|
|
let onDelete: ([MigratableProfile]) -> Void
|
|
|
|
|
|
|
|
let performButton: () -> PerformButton
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var selection: Set<UUID> = []
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
var body: some View {
|
2024-11-17 13:02:40 +00:00
|
|
|
VStack {
|
|
|
|
messageView
|
|
|
|
profilesList
|
2024-11-16 11:29:03 +00:00
|
|
|
}
|
2024-11-16 14:07:47 +00:00
|
|
|
.themeNavigationDetail()
|
2024-11-16 11:29:03 +00:00
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
|
|
performButton()
|
|
|
|
.disabled(isEditing)
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
private extension MigrateContentView.ListView {
|
2024-11-17 13:02:40 +00:00
|
|
|
var isEmpty: Bool {
|
|
|
|
step.isReady && profiles.isEmpty
|
2024-11-16 14:07:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 13:02:40 +00:00
|
|
|
var messageView: some View {
|
2024-11-23 19:31:22 +00:00
|
|
|
Text(Strings.Views.Migration.Sections.Main.header(Strings.Unlocalized.appName))
|
2024-11-17 13:02:40 +00:00
|
|
|
.padding([.top, .leading, .trailing])
|
|
|
|
}
|
|
|
|
|
|
|
|
var profilesList: some View {
|
|
|
|
List {
|
|
|
|
Section {
|
|
|
|
ForEach(profiles, id: \.id) {
|
|
|
|
if isEditing {
|
|
|
|
EditableRowView(profile: $0, selection: $selection)
|
|
|
|
} else {
|
|
|
|
ControlView(
|
|
|
|
step: step,
|
|
|
|
profile: $0,
|
|
|
|
isIncluded: isIncludedBinding(for: $0.id),
|
|
|
|
status: statusBinding(for: $0.id)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} header: {
|
|
|
|
EditProfilesButton(isEditing: $isEditing, selection: $selection) {
|
|
|
|
onDelete(profiles.filter {
|
|
|
|
selection.contains($0.id)
|
|
|
|
})
|
|
|
|
// disable isEditing after confirmation
|
2024-11-16 11:29:03 +00:00
|
|
|
}
|
2024-11-17 13:02:40 +00:00
|
|
|
.textCase(.none)
|
2024-11-16 11:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-17 13:02:40 +00:00
|
|
|
.listStyle(.plain)
|
2024-11-16 14:07:47 +00:00
|
|
|
.disabled(!step.canSelect)
|
2024-11-23 19:31:22 +00:00
|
|
|
.themeEmpty(if: isEmpty, message: Strings.Views.Migration.noProfiles)
|
2024-11-16 11:29:03 +00:00
|
|
|
}
|
|
|
|
|
2024-11-15 00:47:52 +00:00
|
|
|
func isIncludedBinding(for profileId: UUID) -> Binding<Bool> {
|
|
|
|
Binding {
|
|
|
|
statuses[profileId] != .excluded
|
|
|
|
} set: {
|
|
|
|
if $0 {
|
|
|
|
statuses.removeValue(forKey: profileId)
|
2024-11-14 14:11:25 +00:00
|
|
|
} else {
|
2024-11-15 00:47:52 +00:00
|
|
|
statuses[profileId] = .excluded
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
|
2024-11-15 00:47:52 +00:00
|
|
|
func statusBinding(for profileId: UUID) -> Binding<MigrationStatus?> {
|
|
|
|
Binding {
|
|
|
|
statuses[profileId]
|
|
|
|
} set: {
|
|
|
|
if let newValue = $0 {
|
|
|
|
statuses[profileId] = newValue
|
|
|
|
} else {
|
|
|
|
statuses.removeValue(forKey: profileId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 14:07:47 +00:00
|
|
|
// MARK: - Subviews
|
|
|
|
|
|
|
|
private extension MigrateContentView.ListView {
|
|
|
|
struct EditProfilesButton: View {
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var isEditing: Bool
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var selection: Set<UUID>
|
|
|
|
|
|
|
|
let onDelete: () -> Void
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
HStack {
|
|
|
|
if isEditing {
|
2024-11-23 19:31:22 +00:00
|
|
|
Button(Strings.Global.Actions.cancel) {
|
2024-11-16 14:07:47 +00:00
|
|
|
isEditing = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Spacer()
|
2024-11-16 20:16:25 +00:00
|
|
|
Button(title, role: role) {
|
2024-11-16 14:07:47 +00:00
|
|
|
if isEditing {
|
|
|
|
if !selection.isEmpty {
|
|
|
|
onDelete()
|
|
|
|
} else {
|
|
|
|
isEditing = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
selection = []
|
|
|
|
isEditing = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.disabled(isEditing && selection.isEmpty)
|
|
|
|
}
|
|
|
|
.frame(height: 30)
|
|
|
|
}
|
2024-11-16 20:16:25 +00:00
|
|
|
|
|
|
|
var title: String {
|
2024-11-23 19:31:22 +00:00
|
|
|
isEditing ? Strings.Views.Migration.Items.discard : Strings.Global.Actions.edit
|
2024-11-16 20:16:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var role: ButtonRole? {
|
|
|
|
isEditing ? .destructive : nil
|
|
|
|
}
|
2024-11-16 14:07:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
private extension MigrateContentView.ListView {
|
|
|
|
struct EditableRowView: View {
|
|
|
|
let profile: MigratableProfile
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var selection: Set<UUID>
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Button {
|
|
|
|
if selection.contains(profile.id) {
|
|
|
|
selection.remove(profile.id)
|
|
|
|
} else {
|
|
|
|
selection.insert(profile.id)
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
CardView(profile: profile)
|
|
|
|
Spacer()
|
|
|
|
ThemeImage(selection.contains(profile.id) ? .selectionOn : .selectionOff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 00:47:52 +00:00
|
|
|
struct ControlView: View {
|
2024-11-16 11:29:03 +00:00
|
|
|
let step: MigrateViewStep
|
2024-11-15 00:47:52 +00:00
|
|
|
|
|
|
|
let profile: MigratableProfile
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var isIncluded: Bool
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var status: MigrationStatus?
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
switch step {
|
|
|
|
case .initial, .fetching, .fetched:
|
|
|
|
buttonView
|
|
|
|
|
|
|
|
default:
|
|
|
|
rowView
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var buttonView: some View {
|
|
|
|
Button {
|
|
|
|
if status == .excluded {
|
|
|
|
status = nil
|
|
|
|
} else {
|
|
|
|
status = .excluded
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
rowView
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var rowView: some View {
|
|
|
|
HStack {
|
|
|
|
CardView(profile: profile)
|
|
|
|
Spacer()
|
|
|
|
StatusView(isIncluded: status != .excluded, status: status)
|
|
|
|
}
|
|
|
|
.foregroundStyle(status.style)
|
2024-11-14 14:11:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
private extension MigrateContentView.ListView {
|
2024-11-14 14:11:25 +00:00
|
|
|
struct CardView: View {
|
|
|
|
let profile: MigratableProfile
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
Text(profile.name)
|
|
|
|
.font(.headline)
|
|
|
|
|
|
|
|
profile.lastUpdate.map {
|
|
|
|
Text($0.localizedDescription(style: .timestamp))
|
|
|
|
.font(.subheadline)
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
private extension MigrateContentView.ListView {
|
2024-11-14 14:11:25 +00:00
|
|
|
struct StatusView: View {
|
|
|
|
let isIncluded: Bool
|
|
|
|
|
|
|
|
let status: MigrationStatus?
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
if let status {
|
|
|
|
icon(forStatus: status)
|
|
|
|
} else if isIncluded {
|
|
|
|
ThemeImage(.marked)
|
|
|
|
}
|
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
func icon(forStatus status: MigrationStatus) -> some View {
|
|
|
|
switch status {
|
|
|
|
case .excluded:
|
2024-11-17 15:26:22 +00:00
|
|
|
Text(Strings.Unlocalized.longDash)
|
2024-11-14 10:02:26 +00:00
|
|
|
|
|
|
|
case .pending:
|
|
|
|
ProgressView()
|
|
|
|
|
2024-11-16 20:16:25 +00:00
|
|
|
case .done:
|
2024-11-14 10:02:26 +00:00
|
|
|
ThemeImage(.marked)
|
|
|
|
|
2024-11-14 14:11:25 +00:00
|
|
|
case .failed:
|
2024-11-14 10:02:26 +00:00
|
|
|
ThemeImage(.failure)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|