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

178 lines
5.0 KiB
Swift
Raw Normal View History

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
import SwiftUI
2022-04-12 13:09:14 +00:00
struct OrganizerView: View {
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
}
}
}
@State private var addProfileModalType: AddProfileMenu.ModalType?
2022-04-12 13:09:14 +00:00
@State private var modalType: ModalType?
@State private var isAlertPresented = false
2022-04-12 13:09:14 +00:00
@State private var alertType: AlertType?
@State private var isHostFileImporterPresented = false
@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 {
hiddenSceneView
ProfilesList(modalType: $modalType)
}.toolbar {
ToolbarItem(placement: .primaryAction) {
AddProfileMenu(
modalType: $addProfileModalType,
isHostFileImporterPresented: $isHostFileImporterPresented
)
}
ToolbarItem(placement: .navigation) {
if themeIdiom == .phone {
SettingsButton()
}
}
}.sheet(item: $modalType, content: presentedModal)
.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)
.themePrimaryView()
}
}
// MARK: -
2023-03-17 20:55:47 +00:00
private extension OrganizerView {
var hiddenSceneView: some View {
SceneView(
isAlertPresented: $isAlertPresented,
alertType: $alertType,
didHandleSubreddit: $didHandleSubreddit
)
}
2022-04-12 13:09:14 +00:00
@ViewBuilder
func presentedModal(_ modalType: ModalType) -> some View {
switch modalType {
case .interactiveAccount(let profile):
NavigationView {
InteractiveConnectionView(profile: profile)
}.themeGlobal()
}
}
func alertActions(_ alertType: AlertType) -> some View {
2022-04-12 13:09:14 +00:00
switch alertType {
case .subscribeReddit:
return Group {
Button(L10n.Organizer.Alerts.Reddit.Buttons.subscribe) {
2022-04-12 13:09:14 +00:00
didHandleSubreddit = true
URL.open(redditURL)
}
Button(role: .cancel) {
2022-04-12 13:09:14 +00:00
didHandleSubreddit = true
} label: {
Text(L10n.Global.Alerts.Buttons.never)
2022-04-12 13:09:14 +00:00
}
}
2022-04-12 13:09:14 +00:00
}
}
func alertMessage(_ alertType: AlertType) -> some View {
switch alertType {
case .subscribeReddit:
return Text(L10n.Organizer.Alerts.Reddit.message)
}
2022-04-12 13:09:14 +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