2024-11-14 10:02:26 +00:00
|
|
|
//
|
|
|
|
// MigrateView.swift
|
|
|
|
// 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 CommonUtils
|
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
// FIXME: ###, migrations UI
|
|
|
|
|
|
|
|
struct MigrateView: View {
|
|
|
|
enum Style {
|
|
|
|
case section
|
|
|
|
|
|
|
|
case table
|
|
|
|
}
|
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var migrationManager: MigrationManager
|
|
|
|
|
2024-11-14 14:11:25 +00:00
|
|
|
@Environment(\.dismiss)
|
|
|
|
private var dismiss
|
2024-11-14 10:02:26 +00:00
|
|
|
|
2024-11-14 14:11:25 +00:00
|
|
|
let style: Style
|
2024-11-14 10:02:26 +00:00
|
|
|
|
2024-11-14 14:11:25 +00:00
|
|
|
@ObservedObject
|
|
|
|
var profileManager: ProfileManager
|
2024-11-14 10:02:26 +00:00
|
|
|
|
|
|
|
@State
|
2024-11-14 14:11:25 +00:00
|
|
|
private var model = Model()
|
2024-11-14 10:02:26 +00:00
|
|
|
|
|
|
|
@StateObject
|
|
|
|
private var errorHandler: ErrorHandler = .default()
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Form {
|
2024-11-14 14:11:25 +00:00
|
|
|
ContentView(
|
2024-11-14 10:02:26 +00:00
|
|
|
style: style,
|
2024-11-14 14:11:25 +00:00
|
|
|
step: model.step,
|
|
|
|
profiles: model.visibleProfiles,
|
|
|
|
statuses: $model.statuses
|
2024-11-14 10:02:26 +00:00
|
|
|
)
|
2024-11-14 14:11:25 +00:00
|
|
|
.disabled(model.step != .fetched)
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
.themeForm()
|
2024-11-15 15:33:26 +00:00
|
|
|
.themeProgress(
|
|
|
|
if: model.step == .fetching,
|
|
|
|
isEmpty: model.profiles.isEmpty,
|
|
|
|
emptyMessage: "Nothing to migrate"
|
|
|
|
)
|
2024-11-14 14:11:25 +00:00
|
|
|
.themeAnimation(on: model, category: .profiles)
|
2024-11-14 10:02:26 +00:00
|
|
|
.navigationTitle(title)
|
|
|
|
.toolbar(content: toolbarContent)
|
|
|
|
.task {
|
|
|
|
await fetch()
|
|
|
|
}
|
|
|
|
.withErrorHandler(errorHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension MigrateView {
|
|
|
|
var title: String {
|
|
|
|
Strings.Views.Migrate.title
|
|
|
|
}
|
|
|
|
|
|
|
|
func toolbarContent() -> some ToolbarContent {
|
|
|
|
ToolbarItem(placement: .confirmationAction) {
|
2024-11-14 14:11:25 +00:00
|
|
|
Button(itemTitle(at: model.step)) {
|
2024-11-14 10:02:26 +00:00
|
|
|
Task {
|
2024-11-14 14:11:25 +00:00
|
|
|
await itemPerform(at: model.step)
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
.disabled(!itemEnabled(at: model.step))
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension MigrateView {
|
2024-11-14 14:11:25 +00:00
|
|
|
func itemTitle(at step: Model.Step) -> String {
|
|
|
|
switch step {
|
|
|
|
case .initial, .fetching, .fetched:
|
|
|
|
return "Proceed"
|
|
|
|
|
|
|
|
case .migrating, .migrated:
|
|
|
|
return "Import"
|
|
|
|
|
|
|
|
case .importing, .imported:
|
|
|
|
return "Done"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func itemEnabled(at step: Model.Step) -> Bool {
|
|
|
|
switch step {
|
|
|
|
case .initial, .fetching, .migrating, .importing:
|
|
|
|
return false
|
|
|
|
|
|
|
|
case .fetched:
|
|
|
|
return !model.profiles.isEmpty
|
|
|
|
|
|
|
|
case .migrated(let profiles):
|
|
|
|
return !profiles.isEmpty
|
|
|
|
|
|
|
|
case .imported:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func itemPerform(at step: Model.Step) async {
|
|
|
|
switch step {
|
|
|
|
case .fetched:
|
|
|
|
await migrate()
|
|
|
|
|
|
|
|
case .migrated(let profiles):
|
|
|
|
await save(profiles)
|
|
|
|
|
|
|
|
case .imported:
|
|
|
|
dismiss()
|
|
|
|
|
|
|
|
default:
|
|
|
|
fatalError("No action allowed at step \(step)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
func fetch() async {
|
2024-11-14 14:11:25 +00:00
|
|
|
guard model.step == .initial else {
|
|
|
|
return
|
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
do {
|
2024-11-14 14:11:25 +00:00
|
|
|
model.step = .fetching
|
|
|
|
let migratable = try await migrationManager.fetchMigratableProfiles()
|
|
|
|
let knownIDs = Set(profileManager.headers.map(\.id))
|
|
|
|
model.profiles = migratable.filter {
|
|
|
|
!knownIDs.contains($0.id)
|
|
|
|
}
|
|
|
|
model.step = .fetched
|
2024-11-14 10:02:26 +00:00
|
|
|
} catch {
|
|
|
|
pp_log(.App.migration, .error, "Unable to fetch migratable profiles: \(error)")
|
|
|
|
errorHandler.handle(error, title: title)
|
2024-11-14 14:11:25 +00:00
|
|
|
model.step = .initial
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrate() async {
|
2024-11-14 14:11:25 +00:00
|
|
|
guard model.step == .fetched else {
|
|
|
|
fatalError("Must call fetch() and succeed")
|
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
do {
|
2024-11-14 14:11:25 +00:00
|
|
|
model.step = .migrating
|
|
|
|
let profiles = try await migrationManager.migrateProfiles(model.profiles, selection: model.selection) {
|
|
|
|
model.statuses[$0] = $1
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
model.step = .migrated(profiles)
|
2024-11-14 10:02:26 +00:00
|
|
|
} catch {
|
|
|
|
pp_log(.App.migration, .error, "Unable to migrate profiles: \(error)")
|
|
|
|
errorHandler.handle(error, title: title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 14:11:25 +00:00
|
|
|
func save(_ profiles: [Profile]) async {
|
|
|
|
guard case .migrated(let profiles) = model.step, !profiles.isEmpty else {
|
|
|
|
fatalError("Must call migrate() and succeed with non-empty profiles")
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
model.step = .importing
|
|
|
|
model.excludeFailed()
|
|
|
|
await migrationManager.importProfiles(profiles, into: profileManager) {
|
|
|
|
model.statuses[$0] = $1
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
model.step = .imported
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
}
|