2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// OrganizerView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/6/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
|
|
|
|
|
|
|
struct OrganizerView: View {
|
2023-03-17 15:49:35 +00:00
|
|
|
enum ModalType: Identifiable {
|
|
|
|
case interactiveAccount(profile: Profile)
|
|
|
|
|
|
|
|
// XXX: alert ids
|
|
|
|
var id: Int {
|
|
|
|
switch self {
|
|
|
|
case .interactiveAccount: return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
enum AlertType: Identifiable {
|
|
|
|
case subscribeReddit
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-20 06:29:14 +00:00
|
|
|
case error(String, String)
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
// XXX: alert ids
|
|
|
|
var id: Int {
|
|
|
|
switch self {
|
|
|
|
case .subscribeReddit: return 1
|
|
|
|
|
|
|
|
case .error: return 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
@State private var addProfileModalType: AddProfileMenu.ModalType?
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-03-17 15:49:35 +00:00
|
|
|
@State private var modalType: ModalType?
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@State private var alertType: AlertType?
|
|
|
|
|
|
|
|
@State private var isHostFileImporterPresented = false
|
|
|
|
|
2022-06-15 18:53:37 +00:00
|
|
|
@AppStorage(AppPreference.didHandleSubreddit.key) private var didHandleSubreddit = false
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let hostFileTypes = Constants.URLs.filetypes
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let redditURL = Constants.URLs.subreddit
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var body: some View {
|
|
|
|
debugChanges()
|
|
|
|
return ZStack {
|
2022-05-01 11:12:12 +00:00
|
|
|
hiddenSceneView
|
2023-03-17 15:49:35 +00:00
|
|
|
ProfilesList(modalType: $modalType)
|
2022-04-21 13:56:26 +00:00
|
|
|
}.toolbar {
|
|
|
|
ToolbarItem(placement: .primaryAction) {
|
2022-05-15 20:48:43 +00:00
|
|
|
AddProfileMenu(
|
|
|
|
modalType: $addProfileModalType,
|
2022-04-21 13:56:26 +00:00
|
|
|
isHostFileImporterPresented: $isHostFileImporterPresented
|
|
|
|
)
|
|
|
|
}
|
2022-05-15 20:48:43 +00:00
|
|
|
ToolbarItem(placement: .navigation) {
|
2022-05-16 09:06:02 +00:00
|
|
|
if themeIdiom == .phone {
|
2022-08-27 20:42:52 +00:00
|
|
|
SettingsButton()
|
2022-05-16 09:06:02 +00:00
|
|
|
}
|
2022-04-21 13:56:26 +00:00
|
|
|
}
|
2023-03-17 15:49:35 +00:00
|
|
|
}.sheet(item: $modalType, content: presentedModal)
|
|
|
|
.alert(item: $alertType, content: presentedAlert)
|
2022-05-03 10:32:03 +00:00
|
|
|
.fileImporter(
|
2022-04-12 13:09:14 +00:00
|
|
|
isPresented: $isHostFileImporterPresented,
|
|
|
|
allowedContentTypes: hostFileTypes,
|
|
|
|
allowsMultipleSelection: false,
|
|
|
|
onCompletion: onHostFileImporterResult
|
|
|
|
).onOpenURL(perform: onOpenURL)
|
2022-05-03 10:32:03 +00:00
|
|
|
.themePrimaryView()
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-20 06:29:14 +00:00
|
|
|
// VPN configuration error publisher (no need to observe VPNManager)
|
2022-08-28 07:19:15 +00:00
|
|
|
.onReceive(VPNManager.shared.configurationError) {
|
2022-05-20 06:29:14 +00:00
|
|
|
alertType = .error($0.profile.header.name, $0.error.localizedAppDescription)
|
|
|
|
}
|
2022-05-01 11:12:12 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-01 11:12:12 +00:00
|
|
|
private var hiddenSceneView: some View {
|
|
|
|
SceneView(
|
|
|
|
alertType: $alertType,
|
|
|
|
didHandleSubreddit: $didHandleSubreddit
|
|
|
|
)
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension OrganizerView {
|
2022-05-15 20:48:43 +00:00
|
|
|
private func onHostFileImporterResult(_ result: Result<[URL], Error>) {
|
|
|
|
switch result {
|
|
|
|
case .success(let urls):
|
|
|
|
guard let url = urls.first else {
|
|
|
|
assertionFailure("Empty URLs from file importer?")
|
|
|
|
return
|
|
|
|
}
|
2022-10-13 06:53:50 +00:00
|
|
|
Task { @MainActor in
|
2022-05-20 14:34:38 +00:00
|
|
|
await Task.maybeWait(forMilliseconds: Constants.Delays.xxxPresentFileImporter)
|
|
|
|
addProfileModalType = .addHost(url, false)
|
|
|
|
}
|
2022-04-18 10:01:42 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
case .failure(let error):
|
|
|
|
alertType = .error(
|
|
|
|
L10n.Menu.Contextual.AddProfile.fromFiles,
|
2022-05-20 06:29:14 +00:00
|
|
|
error.localizedDescription
|
2022-05-15 20:48:43 +00:00
|
|
|
)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-15 20:48:43 +00:00
|
|
|
private func onOpenURL(_ url: URL) {
|
|
|
|
addProfileModalType = .addHost(url, false)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 15:49:35 +00:00
|
|
|
@ViewBuilder
|
|
|
|
private func presentedModal(_ modalType: ModalType) -> some View {
|
|
|
|
switch modalType {
|
|
|
|
case .interactiveAccount(let profile):
|
|
|
|
NavigationView {
|
|
|
|
InteractiveConnectionView(profile: profile)
|
|
|
|
}.themeGlobal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private func presentedAlert(_ alertType: AlertType) -> Alert {
|
|
|
|
switch alertType {
|
|
|
|
case .subscribeReddit:
|
|
|
|
return Alert(
|
|
|
|
title: Text(Unlocalized.Social.reddit),
|
|
|
|
message: Text(L10n.Organizer.Alerts.Reddit.message),
|
|
|
|
primaryButton: .default(Text(L10n.Organizer.Alerts.Reddit.Buttons.subscribe)) {
|
|
|
|
didHandleSubreddit = true
|
2023-04-04 07:50:45 +00:00
|
|
|
URL.open(redditURL)
|
2022-04-12 13:09:14 +00:00
|
|
|
},
|
2022-09-25 06:35:52 +00:00
|
|
|
secondaryButton: .cancel(Text(L10n.Global.Alerts.Buttons.never)) {
|
2022-04-12 13:09:14 +00:00
|
|
|
didHandleSubreddit = true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-05-20 06:29:14 +00:00
|
|
|
case .error(let title, let errorDescription):
|
2022-04-12 13:09:14 +00:00
|
|
|
return Alert(
|
|
|
|
title: Text(title),
|
2022-05-20 06:29:14 +00:00
|
|
|
message: Text(errorDescription),
|
2022-04-12 13:09:14 +00:00
|
|
|
dismissButton: .cancel(Text(L10n.Global.Strings.ok))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension OrganizerView {
|
|
|
|
private func presentSubscribeReddit() {
|
|
|
|
alertType = .subscribeReddit
|
|
|
|
}
|
|
|
|
}
|