2022-04-12 13:09:14 +00:00
|
|
|
//
|
2022-05-15 20:48:43 +00:00
|
|
|
// AddProfileMenu.swift
|
2022-04-12 13:09:14 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
2022-04-18 10:01:42 +00:00
|
|
|
// Created by Davide De Rosa on 4/18/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
//
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
2023-04-04 07:50:45 +00:00
|
|
|
import SwiftUI
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
struct AddProfileMenu: View {
|
|
|
|
enum ModalType: Identifiable {
|
|
|
|
case addProvider
|
|
|
|
|
|
|
|
case addHost(URL, Bool)
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
// XXX: alert ids
|
|
|
|
var id: Int {
|
|
|
|
switch self {
|
|
|
|
case .addProvider: return 1
|
|
|
|
|
|
|
|
case .addHost: return 2
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2022-05-15 20:48:43 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
@Binding private var modalType: ModalType?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
@Binding private var isHostFileImporterPresented: Bool
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
init(modalType: Binding<ModalType?>, isHostFileImporterPresented: Binding<Bool>) {
|
|
|
|
_modalType = modalType
|
|
|
|
_isHostFileImporterPresented = isHostFileImporterPresented
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
var body: some View {
|
|
|
|
Menu {
|
|
|
|
Button {
|
|
|
|
modalType = .addProvider
|
|
|
|
} label: {
|
|
|
|
Label(L10n.Global.Strings.provider, systemImage: themeProviderImage)
|
|
|
|
}
|
2022-10-13 06:53:50 +00:00
|
|
|
Button(action: presentHostFileImporter) {
|
2022-05-15 20:48:43 +00:00
|
|
|
Label(L10n.Menu.Contextual.AddProfile.fromFiles, systemImage: themeHostFilesImage)
|
|
|
|
}
|
2022-04-12 16:51:24 +00:00
|
|
|
// Button {
|
|
|
|
// // TODO: add profile from text
|
|
|
|
// } label: {
|
|
|
|
// Label(L10n.Organizer.Menus.AddProfile.fromText, systemImage: themeHostTextImage)
|
|
|
|
// }
|
2022-05-15 20:48:43 +00:00
|
|
|
if let urls = importedURLs, !urls.isEmpty {
|
|
|
|
Divider()
|
|
|
|
ForEach(urls, id: \.absoluteString, content: importedURLRow)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2022-05-15 20:48:43 +00:00
|
|
|
} label: {
|
|
|
|
themeAddMenuImage.asSystemImage
|
|
|
|
}.sheet(item: $modalType, content: presentedModal)
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
@ViewBuilder
|
|
|
|
private func presentedModal(_ modalType: ModalType) -> some View {
|
|
|
|
switch modalType {
|
|
|
|
case .addProvider:
|
|
|
|
NavigationView {
|
|
|
|
AddProviderView(
|
|
|
|
bindings: .init(
|
|
|
|
isPresented: isModalPresented
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}.themeGlobal()
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
case .addHost(let url, let deletingURLOnSuccess):
|
|
|
|
NavigationView {
|
|
|
|
AddHostView.NameView(
|
|
|
|
url: url,
|
|
|
|
deletingURLOnSuccess: deletingURLOnSuccess,
|
|
|
|
bindings: .init(
|
|
|
|
isPresented: isModalPresented
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}.themeGlobal()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2022-05-15 20:48:43 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
private var isModalPresented: Binding<Bool> {
|
|
|
|
.init {
|
|
|
|
modalType != nil
|
|
|
|
} set: {
|
|
|
|
if !$0 {
|
|
|
|
modalType = nil
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-15 20:48:43 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
private func importedURLRow(_ url: URL) -> some View {
|
|
|
|
Button(L10n.Menu.Contextual.AddProfile.imported(url.lastPathComponent)) {
|
|
|
|
presentAddHost(withURL: url, deletingURLOnSuccess: true)
|
2022-04-18 10:01:42 +00:00
|
|
|
}
|
2022-05-15 20:48:43 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
private var importedURLs: [URL]? {
|
|
|
|
do {
|
|
|
|
let url = FileManager.default.userURL(for: .documentDirectory, appending: nil)
|
|
|
|
let list = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
|
|
|
|
return list.filter {
|
|
|
|
VPNProtocolType.knownFileExtensions.contains($0.pathExtension)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
return nil
|
2022-04-18 10:01:42 +00:00
|
|
|
}
|
2022-05-15 20:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
extension AddProfileMenu {
|
|
|
|
private func presentAddProvider() {
|
|
|
|
modalType = .addProvider
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
private func presentAddHost(withURL url: URL, deletingURLOnSuccess: Bool) {
|
|
|
|
modalType = .addHost(url, deletingURLOnSuccess)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func presentHostFileImporter() {
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
// XXX: iOS bug, hack around crappy bug when dismissing by swiping down
|
|
|
|
//
|
|
|
|
// https://stackoverflow.com/questions/66965471/swiftui-fileimporter-modifier-not-updating-binding-when-dismissed-by-tapping
|
|
|
|
isHostFileImporterPresented = false
|
2022-10-13 06:53:50 +00:00
|
|
|
Task { @MainActor in
|
2022-05-15 20:48:43 +00:00
|
|
|
await Task.maybeWait(forMilliseconds: Constants.Delays.xxxPresentFileImporter)
|
|
|
|
isHostFileImporterPresented = true
|
2022-04-18 10:01:42 +00:00
|
|
|
}
|
2022-05-15 20:48:43 +00:00
|
|
|
// isHostFileImporterPresented = true
|
|
|
|
//
|
|
|
|
// // use this to test hardcoded bundle file
|
|
|
|
// let url = Bundle.main.url(forResource: "pia", withExtension: "ovpn")!
|
|
|
|
// importedProfileName = "pia.ovpn"
|
|
|
|
// modalType = .addHost(url, false)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|