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
|
2023-04-04 07:50:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 06:53:50 +00:00
|
|
|
@StateObject private var intentsManager = IntentsManager()
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-19 08:10:06 +00:00
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-18 14:42:17 +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
|
|
|
|
2022-04-18 14:42:17 +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
|
2022-04-21 13:56:26 +00:00
|
|
|
}.toolbar {
|
2022-04-19 08:10:06 +00:00
|
|
|
themeCloseItem(presentationMode: presentationMode)
|
|
|
|
}.sheet(item: $modalType, content: presentedModal)
|
2022-10-13 06:53:50 +00:00
|
|
|
.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
|
|
|
|
2022-04-12 16:51:24 +00:00
|
|
|
.navigationTitle(Unlocalized.Other.siri)
|
2022-04-21 13:56:26 +00:00
|
|
|
.themeSecondaryView()
|
2022-04-12 13:09:14 +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 ShortcutsView {
|
|
|
|
var shortcutsSection: some View {
|
2022-05-01 17:43:33 +00:00
|
|
|
Section {
|
2022-04-18 14:42:17 +00:00
|
|
|
ForEach(relevantShortcuts, content: rowView)
|
2022-05-01 17:43:33 +00:00
|
|
|
} header: {
|
|
|
|
Text(L10n.Shortcuts.Edit.Sections.All.header)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var relevantShortcuts: [Shortcut] {
|
2022-04-18 14:42:17 +00:00
|
|
|
intentsManager.shortcuts.values.filter {
|
|
|
|
$0.isRelevant(to: target)
|
|
|
|
}.sorted()
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var addSection: some View {
|
2022-05-01 17:43:33 +00:00
|
|
|
Section {
|
2022-04-12 13:09:14 +00:00
|
|
|
NavigationLink(isActive: $isNavigationPresented) {
|
|
|
|
AddView(
|
2022-04-18 14:42:17 +00:00
|
|
|
target: target,
|
2022-04-12 13:09:14 +00:00
|
|
|
pendingShortcut: delegatingPendingShortcut
|
|
|
|
)
|
|
|
|
} label: {
|
|
|
|
Text(L10n.Shortcuts.Edit.Items.AddShortcut.caption)
|
|
|
|
}
|
2022-05-01 17:43:33 +00:00
|
|
|
} footer: {
|
|
|
|
Text(L10n.Shortcuts.Edit.Sections.Add.footer)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
2023-07-03 14:54:43 +00:00
|
|
|
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
|
|
|
|
2023-07-03 14:54:43 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var delegatingPendingShortcut: Binding<INShortcut?> {
|
2022-04-12 13:09:14 +00:00
|
|
|
.init {
|
|
|
|
pendingShortcut
|
|
|
|
} set: {
|
|
|
|
guard let pendingShortcut = $0 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
presentAddShortcut(pendingShortcut)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 14:42:17 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
|
|
|
|
// 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
|