2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// OrganizerView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/6/22.
|
2024-01-14 13:34:21 +00:00
|
|
|
// Copyright (c) 2024 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/>.
|
|
|
|
//
|
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
#if !os(tvOS)
|
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-04-12 13:09:14 +00:00
|
|
|
// XXX: alert ids
|
|
|
|
var id: Int {
|
|
|
|
switch self {
|
|
|
|
case .subscribeReddit: return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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?
|
|
|
|
|
2023-07-03 14:41:49 +00:00
|
|
|
@State private var isAlertPresented = false
|
|
|
|
|
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)
|
2023-07-03 14:41:49 +00:00
|
|
|
.alert(
|
|
|
|
Unlocalized.appName,
|
|
|
|
isPresented: $isAlertPresented,
|
|
|
|
presenting: alertType,
|
|
|
|
actions: alertActions,
|
|
|
|
message: alertMessage
|
2023-12-16 19:58:54 +00:00
|
|
|
)
|
|
|
|
.fileImporter(
|
2022-04-12 13:09:14 +00:00
|
|
|
isPresented: $isHostFileImporterPresented,
|
|
|
|
allowedContentTypes: hostFileTypes,
|
|
|
|
allowsMultipleSelection: false,
|
|
|
|
onCompletion: onHostFileImporterResult
|
2023-12-16 19:58:54 +00:00
|
|
|
)
|
|
|
|
.onOpenURL(perform: onOpenURL)
|
2022-05-03 10:32:03 +00:00
|
|
|
.themePrimaryView()
|
2022-05-01 11:12:12 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension OrganizerView {
|
|
|
|
var hiddenSceneView: some View {
|
2022-05-01 11:12:12 +00:00
|
|
|
SceneView(
|
2023-07-03 14:41:49 +00:00
|
|
|
isAlertPresented: $isAlertPresented,
|
2022-05-01 11:12:12 +00:00
|
|
|
alertType: $alertType,
|
|
|
|
didHandleSubreddit: $didHandleSubreddit
|
|
|
|
)
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-03-17 15:49:35 +00:00
|
|
|
@ViewBuilder
|
2023-07-03 14:54:43 +00:00
|
|
|
func presentedModal(_ modalType: ModalType) -> some View {
|
2023-03-17 15:49:35 +00:00
|
|
|
switch modalType {
|
|
|
|
case .interactiveAccount(let profile):
|
|
|
|
NavigationView {
|
|
|
|
InteractiveConnectionView(profile: profile)
|
|
|
|
}.themeGlobal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
func alertActions(_ alertType: AlertType) -> some View {
|
2022-04-12 13:09:14 +00:00
|
|
|
switch alertType {
|
|
|
|
case .subscribeReddit:
|
2023-07-03 14:41:49 +00:00
|
|
|
return Group {
|
|
|
|
Button(L10n.Organizer.Alerts.Reddit.Buttons.subscribe) {
|
2022-04-12 13:09:14 +00:00
|
|
|
didHandleSubreddit = true
|
2023-04-04 07:50:45 +00:00
|
|
|
URL.open(redditURL)
|
2023-07-03 14:41:49 +00:00
|
|
|
}
|
|
|
|
Button(role: .cancel) {
|
2022-04-12 13:09:14 +00:00
|
|
|
didHandleSubreddit = true
|
2023-07-03 14:41:49 +00:00
|
|
|
} label: {
|
|
|
|
Text(L10n.Global.Alerts.Buttons.never)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:41:49 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
func alertMessage(_ alertType: AlertType) -> some View {
|
2023-07-03 14:41:49 +00:00
|
|
|
switch alertType {
|
|
|
|
case .subscribeReddit:
|
|
|
|
return Text(L10n.Organizer.Alerts.Reddit.message)
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension OrganizerView {
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
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
|
|
|
|
}
|
|
|
|
Task {
|
|
|
|
await Task.maybeWait(forMilliseconds: Constants.Delays.xxxPresentFileImporter)
|
|
|
|
addProfileModalType = .addHost(url, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
ErrorHandler.shared.handle(error, title: L10n.Menu.Contextual.AddProfile.fromFiles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func onOpenURL(_ url: URL) {
|
|
|
|
addProfileModalType = .addHost(url, false)
|
|
|
|
}
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
#endif
|