passepartout-apple/Passepartout/App/Views/OrganizerView+AddMenu.swift

107 lines
3.8 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// OrganizerView+AddMenu.swift
2022-04-12 13:09:14 +00:00
// Passepartout
//
// Created by Davide De Rosa on 4/18/22.
2022-04-12 13:09:14 +00:00
// Copyright (c) 2022 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 SwiftUI
import PassepartoutCore
extension OrganizerView {
struct AddMenu: View {
@Binding private var modalType: ModalType?
2022-04-12 13:09:14 +00:00
@Binding private var isHostFileImporterPresented: Bool
2022-04-12 13:09:14 +00:00
init(modalType: Binding<ModalType?>, isHostFileImporterPresented: Binding<Bool>) {
_modalType = modalType
_isHostFileImporterPresented = isHostFileImporterPresented
2022-04-12 13:09:14 +00:00
}
// FIXME: l10n, shorten menu captions
2022-04-12 13:09:14 +00:00
var body: some View {
Menu {
2022-04-12 13:09:14 +00:00
Button {
modalType = .addProvider
2022-04-12 13:09:14 +00:00
} label: {
Label(L10n.Organizer.Items.AddProvider.caption, systemImage: themeProviderImage)
}
Button {
presentHostFileImporter()
} label: {
Label(L10n.Organizer.Items.AddHost.caption, systemImage: themeHostImage)
}
if let urls = importedURLs, !urls.isEmpty {
2022-04-12 13:09:14 +00:00
Divider()
ForEach(urls, id: \.absoluteString, content: importedURLRow)
2022-04-12 13:09:14 +00:00
}
} label: {
themeAddMenuImage.asSystemImage
2022-04-12 13:09:14 +00:00
}
}
private func importedURLRow(_ url: URL) -> some View {
Button(L10n.Organizer.Menus.AddProfile.imported(url.lastPathComponent)) {
presentAddHost(withURL: url, deletingURLOnSuccess: true)
}
}
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
}
}
private func presentAddProvider() {
modalType = .addProvider
}
2022-04-12 13:09:14 +00:00
private func presentAddHost(withURL url: URL, deletingURLOnSuccess: Bool) {
modalType = .addHost(url, deletingURLOnSuccess)
}
2022-04-12 13:09:14 +00:00
private func presentHostFileImporter() {
2022-04-12 13:09:14 +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
Task {
await Task.maybeWait(forMilliseconds: Constants.Delays.xxxPresentFileImporter)
isHostFileImporterPresented = true
}
// isHostFileImporterPresented = true
2022-04-12 13:09:14 +00:00
// // 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
}
}