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
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
// FIXME: #878, show CloudKit progress
|
2024-11-14 10:02:26 +00:00
|
|
|
|
|
|
|
struct MigrateView: View {
|
|
|
|
enum Style {
|
2024-11-16 11:29:03 +00:00
|
|
|
case list
|
2024-11-14 10:02:26 +00:00
|
|
|
|
|
|
|
case table
|
|
|
|
}
|
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var migrationManager: MigrationManager
|
|
|
|
|
2024-11-17 17:21:37 +00:00
|
|
|
@EnvironmentObject
|
|
|
|
private var iapManager: IAPManager
|
|
|
|
|
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
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
@State
|
|
|
|
private var isEditing = false
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var isDeleting = false
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var profilesPendingDeletion: [MigratableProfile]?
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
@StateObject
|
|
|
|
private var errorHandler: ErrorHandler = .default()
|
|
|
|
|
|
|
|
var body: some View {
|
2024-11-16 11:29:03 +00:00
|
|
|
debugChanges()
|
|
|
|
return MigrateContentView(
|
|
|
|
style: style,
|
|
|
|
step: model.step,
|
|
|
|
profiles: model.visibleProfiles,
|
|
|
|
statuses: $model.statuses,
|
|
|
|
isEditing: $isEditing,
|
|
|
|
onDelete: onDelete,
|
|
|
|
performButton: performButton
|
|
|
|
)
|
2024-11-17 13:02:40 +00:00
|
|
|
.themeProgress(if: !model.step.isReady)
|
2024-11-16 20:16:25 +00:00
|
|
|
.themeAnimation(on: model, category: .profiles)
|
2024-11-16 11:29:03 +00:00
|
|
|
.themeConfirmation(
|
|
|
|
isPresented: $isDeleting,
|
2024-11-16 20:16:25 +00:00
|
|
|
title: Strings.Views.Migrate.Items.discard,
|
2024-11-16 11:29:03 +00:00
|
|
|
message: messageForDeletion,
|
|
|
|
isDestructive: true,
|
|
|
|
action: confirmPendingDeletion
|
2024-11-15 15:33:26 +00:00
|
|
|
)
|
2024-11-14 10:02:26 +00:00
|
|
|
.navigationTitle(title)
|
|
|
|
.task {
|
|
|
|
await fetch()
|
|
|
|
}
|
|
|
|
.withErrorHandler(errorHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension MigrateView {
|
|
|
|
var title: String {
|
|
|
|
Strings.Views.Migrate.title
|
|
|
|
}
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
var messageForDeletion: String? {
|
|
|
|
profilesPendingDeletion.map {
|
|
|
|
let nameList = $0
|
|
|
|
.map(\.name)
|
|
|
|
.joined(separator: "\n")
|
|
|
|
|
|
|
|
return Strings.Views.Migrate.Alerts.Delete.message(nameList)
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
func performButton() -> some View {
|
|
|
|
MigrateButton(step: model.step) {
|
|
|
|
Task {
|
|
|
|
await perform(at: model.step)
|
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-16 11:29:03 +00:00
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
private extension MigrateView {
|
|
|
|
func onDelete(_ profiles: [MigratableProfile]) {
|
|
|
|
profilesPendingDeletion = profiles
|
|
|
|
isDeleting = true
|
2024-11-14 14:11:25 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
func perform(at step: MigrateViewStep) async {
|
2024-11-14 14:11:25 +00:00
|
|
|
switch step {
|
2024-11-16 11:29:03 +00:00
|
|
|
case .fetched(let profiles):
|
|
|
|
await migrate(profiles)
|
2024-11-14 14:11:25 +00:00
|
|
|
|
2024-11-16 20:16:25 +00:00
|
|
|
case .migrated:
|
2024-11-14 14:11:25 +00:00
|
|
|
dismiss()
|
|
|
|
|
|
|
|
default:
|
2024-11-16 11:29:03 +00:00
|
|
|
assertionFailure("No action allowed at step \(step), why is button enabled?")
|
2024-11-14 14:11:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2024-11-16 11:29:03 +00:00
|
|
|
pp_log(.App.migration, .notice, "Fetch migratable profiles...")
|
2024-11-14 14:11:25 +00:00
|
|
|
let migratable = try await migrationManager.fetchMigratableProfiles()
|
|
|
|
let knownIDs = Set(profileManager.headers.map(\.id))
|
|
|
|
model.profiles = migratable.filter {
|
|
|
|
!knownIDs.contains($0.id)
|
|
|
|
}
|
2024-11-16 11:29:03 +00:00
|
|
|
model.step = .fetched(model.profiles)
|
2024-11-14 10:02:26 +00:00
|
|
|
} catch {
|
|
|
|
pp_log(.App.migration, .error, "Unable to fetch migratable profiles: \(error)")
|
2024-11-17 14:28:31 +00:00
|
|
|
errorHandler.handle(error, title: title) {
|
|
|
|
dismiss()
|
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
func migrate(_ allProfiles: [MigratableProfile]) async {
|
|
|
|
guard case .fetched = model.step else {
|
|
|
|
assertionFailure("Must call fetch() and succeed, why is button enabled?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let profiles = allProfiles.filter {
|
|
|
|
model.statuses[$0.id] != .excluded
|
2024-11-14 14:11:25 +00:00
|
|
|
}
|
2024-11-16 11:29:03 +00:00
|
|
|
guard !profiles.isEmpty else {
|
|
|
|
assertionFailure("Nothing to migrate, why is button enabled?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let previousStep = model.step
|
|
|
|
model.step = .migrating
|
2024-11-14 10:02:26 +00:00
|
|
|
do {
|
2024-11-16 11:29:03 +00:00
|
|
|
pp_log(.App.migration, .notice, "Migrate \(profiles.count) profiles...")
|
|
|
|
let profiles = try await migrationManager.migratedProfiles(profiles) {
|
2024-11-16 20:16:25 +00:00
|
|
|
guard $1 != .done else {
|
|
|
|
return
|
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
model.statuses[$0] = $1
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
2024-11-16 20:16:25 +00:00
|
|
|
pp_log(.App.migration, .notice, "Mapped \(profiles.count) profiles to the new format, saving...")
|
|
|
|
await migrationManager.importProfiles(profiles, into: profileManager) {
|
|
|
|
model.statuses[$0] = $1
|
|
|
|
}
|
|
|
|
let migrated = profiles.filter {
|
|
|
|
model.statuses[$0.id] == .done
|
|
|
|
}
|
|
|
|
pp_log(.App.migration, .notice, "Migrated \(migrated.count) profiles")
|
2024-11-17 17:21:37 +00:00
|
|
|
|
|
|
|
if !iapManager.isRestricted {
|
|
|
|
do {
|
|
|
|
try await migrationManager.deleteMigratableProfiles(withIds: Set(migrated.map(\.id)))
|
|
|
|
pp_log(.App.migration, .notice, "Discarded \(migrated.count) migrated profiles from old store")
|
|
|
|
} catch {
|
|
|
|
pp_log(.App.migration, .error, "Unable to discard migrated profiles: \(error)")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pp_log(.App.migration, .notice, "Restricted build, do not discard migrated profiles")
|
2024-11-17 10:40:49 +00:00
|
|
|
}
|
2024-11-17 17:21:37 +00:00
|
|
|
|
2024-11-16 20:16:25 +00:00
|
|
|
model.step = .migrated(migrated)
|
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-16 11:29:03 +00:00
|
|
|
model.step = previousStep
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
func confirmPendingDeletion() {
|
|
|
|
guard let profilesPendingDeletion else {
|
|
|
|
isEditing = false
|
|
|
|
assertionFailure("No profiles pending deletion?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let deletedIds = Set(profilesPendingDeletion.map(\.id))
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
try await migrationManager.deleteMigratableProfiles(withIds: deletedIds)
|
|
|
|
withAnimation {
|
|
|
|
model.profiles.removeAll {
|
|
|
|
deletedIds.contains($0.id)
|
|
|
|
}
|
|
|
|
model.step = .fetched(model.profiles)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
pp_log(.App.migration, .error, "Unable to delete migratable profiles \(deletedIds): \(error)")
|
|
|
|
}
|
|
|
|
isEditing = false
|
|
|
|
}
|
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|