2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// ProfileImporterModifier.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/3/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
|
|
|
|
|
|
|
|
struct ProfileImporterModifier: ViewModifier {
|
|
|
|
let profileManager: ProfileManager
|
|
|
|
|
|
|
|
let registry: Registry
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var isPresented: Bool
|
|
|
|
|
|
|
|
let errorHandler: ErrorHandler
|
|
|
|
|
|
|
|
@StateObject
|
|
|
|
private var importer = ProfileImporter()
|
|
|
|
|
|
|
|
func body(content: Content) -> some View {
|
|
|
|
content
|
|
|
|
.fileImporter(
|
|
|
|
isPresented: $isPresented,
|
|
|
|
allowedContentTypes: [.item],
|
|
|
|
allowsMultipleSelection: true,
|
2024-09-28 09:43:57 +00:00
|
|
|
onCompletion: handleResult
|
2024-09-23 13:02:26 +00:00
|
|
|
)
|
2024-09-30 15:22:16 +00:00
|
|
|
.onReceive(ImporterPipe.shared) {
|
|
|
|
handleResult(.success($0))
|
2024-09-28 09:43:57 +00:00
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
.alert(
|
2024-11-23 19:31:22 +00:00
|
|
|
Strings.Views.App.Toolbar.importProfile,
|
2024-09-23 13:02:26 +00:00
|
|
|
isPresented: $importer.isPresentingPassphrase,
|
|
|
|
presenting: importer.nextURL,
|
|
|
|
actions: actions,
|
|
|
|
message: message
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProfileImporterModifier {
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
func actions(for url: URL) -> some View {
|
|
|
|
SecureField(
|
|
|
|
Strings.Placeholders.secret,
|
|
|
|
text: $importer.currentPassphrase
|
|
|
|
)
|
2024-10-28 19:07:19 +00:00
|
|
|
Button(Strings.Alerts.Import.Passphrase.ok) {
|
2024-09-23 13:02:26 +00:00
|
|
|
Task {
|
|
|
|
try await importer.reImport(
|
|
|
|
url: url,
|
|
|
|
profileManager: profileManager,
|
2024-12-01 20:33:58 +00:00
|
|
|
importer: registry
|
2024-09-23 13:02:26 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-11-23 19:31:22 +00:00
|
|
|
Button(Strings.Global.Actions.cancel, role: .cancel) {
|
2024-09-23 13:02:26 +00:00
|
|
|
importer.cancelImport()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func message(for url: URL) -> some View {
|
2024-10-28 19:07:19 +00:00
|
|
|
Text(Strings.Alerts.Import.Passphrase.message(url.lastPathComponent))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-09-28 09:43:57 +00:00
|
|
|
func handleResult(_ result: Result<[URL], Error>) {
|
2024-09-23 13:02:26 +00:00
|
|
|
Task.detached {
|
|
|
|
do {
|
|
|
|
let urls = try result.get()
|
|
|
|
try await importer.tryImport(
|
|
|
|
urls: urls,
|
|
|
|
profileManager: profileManager,
|
2024-12-01 20:33:58 +00:00
|
|
|
importer: registry
|
2024-09-23 13:02:26 +00:00
|
|
|
)
|
|
|
|
} catch {
|
|
|
|
await errorHandler.handle(
|
|
|
|
error,
|
2024-11-23 19:31:22 +00:00
|
|
|
title: Strings.Views.App.Toolbar.importProfile,
|
2024-11-27 13:49:23 +00:00
|
|
|
message: Strings.Errors.App.import
|
2024-09-23 13:02:26 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|