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

181 lines
4.7 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// ShortcutsView.swift
// Passepartout
//
// Created by Davide De Rosa on 2/8/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-04-12 13:09:14 +00:00
import Intents
2022-06-23 21:31:01 +00:00
import PassepartoutLibrary
import SwiftUI
2022-04-12 13:09:14 +00:00
struct ShortcutsView: View {
enum ModalType: Identifiable {
case edit(shortcut: Shortcut)
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
case add(shortcut: INShortcut)
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 .edit: return 1
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
case .add: return 2
}
}
}
@StateObject private var intentsManager = IntentsManager()
2023-03-17 20:55:47 +00:00
@Environment(\.presentationMode) private var presentationMode
2023-03-17 20:55:47 +00:00
private let target: Profile
2022-04-12 13:09:14 +00:00
@State private var modalType: ModalType?
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@State private var isNavigationPresented = false
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@State private var pendingShortcut: INShortcut?
2023-03-17 20:55:47 +00:00
init(target: Profile) {
self.target = target
2022-04-12 13:09:14 +00:00
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var body: some View {
List {
if !intentsManager.shortcuts.isEmpty {
shortcutsSection
}
addSection
}.toolbar {
themeCloseItem(presentationMode: presentationMode)
}.sheet(item: $modalType, content: presentedModal)
.themeAnimation(on: intentsManager.isReloadingShortcuts)
2023-03-17 20:55:47 +00:00
2022-04-21 09:44:49 +00:00
// IntentsUI
.onReceive(intentsManager.shouldDismissIntentView) { _ in
2022-04-12 13:09:14 +00:00
modalType = nil
}
2023-03-17 20:55:47 +00:00
.navigationTitle(Unlocalized.Other.siri)
.themeSecondaryView()
2022-04-12 13:09:14 +00:00
}
}
// MARK: -
2023-03-17 20:55:47 +00:00
private extension ShortcutsView {
var shortcutsSection: some View {
Section {
ForEach(relevantShortcuts, content: rowView)
} header: {
Text(L10n.Shortcuts.Edit.Sections.All.header)
2022-04-12 13:09:14 +00:00
}
}
2023-03-17 20:55:47 +00:00
var relevantShortcuts: [Shortcut] {
intentsManager.shortcuts.values.filter {
$0.isRelevant(to: target)
}.sorted()
}
2023-03-17 20:55:47 +00:00
var addSection: some View {
Section {
2022-04-12 13:09:14 +00:00
NavigationLink(isActive: $isNavigationPresented) {
AddView(
target: target,
2022-04-12 13:09:14 +00:00
pendingShortcut: delegatingPendingShortcut
)
} label: {
Text(L10n.Shortcuts.Edit.Items.AddShortcut.caption)
}
} footer: {
Text(L10n.Shortcuts.Edit.Sections.Add.footer)
2022-04-12 13:09:14 +00:00
}
}
@ViewBuilder
func presentedModal(_ modalType: ModalType) -> some View {
2022-04-12 13:09:14 +00:00
switch modalType {
case .edit(let shortcut):
IntentEditView(
shortcut: shortcut,
delegate: intentsManager
)
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
case .add(let shortcut):
IntentAddView(
shortcut: shortcut,
delegate: intentsManager
)
}
}
2023-03-17 20:55:47 +00:00
func rowView(forShortcut vs: Shortcut) -> some View {
2022-04-12 13:09:14 +00:00
Button {
presentEditShortcut(vs)
} label: {
Label(vs.native.invocationPhrase, systemImage: themeShortcutsImage)
}
}
var delegatingPendingShortcut: Binding<INShortcut?> {
2022-04-12 13:09:14 +00:00
.init {
pendingShortcut
} set: {
guard let pendingShortcut = $0 else {
return
}
presentAddShortcut(pendingShortcut)
}
}
}
private extension Shortcut {
func isRelevant(to profile: Profile) -> Bool {
guard let intent = native.shortcut.intent else {
return true
}
if let connectIntent = intent as? ConnectVPNIntent {
return connectIntent.profileId == profile.id.uuidString
}
if let moveToIntent = intent as? MoveToLocationIntent {
return moveToIntent.profileId == profile.id.uuidString
}
return true
}
}
// MARK: -
private extension ShortcutsView {
func presentEditShortcut(_ shortcut: Shortcut) {
modalType = .edit(shortcut: shortcut)
}
func presentAddShortcut(_ shortcut: INShortcut) {
isNavigationPresented = false
modalType = .add(shortcut: shortcut)
}
}
2023-12-16 19:58:54 +00:00
#endif