2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// ShortcutsView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/8/22.
|
|
|
|
// Copyright (c) 2022 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import Intents
|
|
|
|
import PassepartoutCore
|
|
|
|
|
|
|
|
struct ShortcutsView: View {
|
|
|
|
enum ModalType: Identifiable {
|
|
|
|
case edit(shortcut: Shortcut)
|
|
|
|
|
|
|
|
case add(shortcut: INShortcut)
|
|
|
|
|
|
|
|
// XXX: alert ids
|
|
|
|
var id: Int {
|
|
|
|
switch self {
|
|
|
|
case .edit: return 1
|
|
|
|
|
|
|
|
case .add: return 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ObservedObject private var intentsManager: IntentsManager
|
2022-04-18 14:42:17 +00:00
|
|
|
|
2022-04-19 08:10:06 +00:00
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
@State private var isNavigationPresented = false
|
|
|
|
|
|
|
|
@State private var pendingShortcut: INShortcut?
|
|
|
|
|
2022-04-18 14:42:17 +00:00
|
|
|
init(target: Profile) {
|
2022-04-12 13:09:14 +00:00
|
|
|
intentsManager = .shared
|
2022-04-18 14:42:17 +00:00
|
|
|
self.target = target
|
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-04-21 09:44:49 +00:00
|
|
|
|
|
|
|
// reloading
|
2022-04-12 13:09:14 +00:00
|
|
|
.onAppear {
|
|
|
|
intentsManager.reloadShortcuts()
|
2022-04-23 10:08:24 +00:00
|
|
|
}.themeAnimation(on: intentsManager.isReloadingShortcuts)
|
2022-04-21 09:44:49 +00:00
|
|
|
|
|
|
|
// IntentsUI
|
|
|
|
.onReceive(intentsManager.shouldDismissIntentView) { _ in
|
2022-04-12 13:09:14 +00:00
|
|
|
modalType = nil
|
|
|
|
}
|
2022-04-21 13:56:26 +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
|
|
|
}
|
|
|
|
|
|
|
|
private var shortcutsSection: some View {
|
|
|
|
Section(
|
|
|
|
header: Text(L10n.Shortcuts.Edit.Sections.All.header)
|
|
|
|
) {
|
2022-04-18 14:42:17 +00:00
|
|
|
ForEach(relevantShortcuts, content: rowView)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 14:42:17 +00:00
|
|
|
private var relevantShortcuts: [Shortcut] {
|
|
|
|
intentsManager.shortcuts.values.filter {
|
|
|
|
$0.isRelevant(to: target)
|
|
|
|
}.sorted()
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private var addSection: some View {
|
2022-04-18 10:01:42 +00:00
|
|
|
Section(
|
2022-04-12 16:51:24 +00:00
|
|
|
footer: Text(L10n.Shortcuts.Edit.Sections.Add.footer)
|
2022-04-18 10:01:42 +00:00
|
|
|
) {
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private func presentedModal(_ modalType: ModalType) -> some View {
|
|
|
|
switch modalType {
|
|
|
|
case .edit(let shortcut):
|
|
|
|
IntentEditView(
|
|
|
|
shortcut: shortcut,
|
|
|
|
delegate: intentsManager
|
|
|
|
)
|
|
|
|
|
|
|
|
case .add(let shortcut):
|
|
|
|
IntentAddView(
|
|
|
|
shortcut: shortcut,
|
|
|
|
delegate: intentsManager
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func rowView(forShortcut vs: Shortcut) -> some View {
|
|
|
|
Button {
|
|
|
|
presentEditShortcut(vs)
|
|
|
|
} label: {
|
|
|
|
Label(vs.native.invocationPhrase, systemImage: themeShortcutsImage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var delegatingPendingShortcut: Binding<INShortcut?> {
|
|
|
|
.init {
|
|
|
|
pendingShortcut
|
|
|
|
} set: {
|
|
|
|
guard let pendingShortcut = $0 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
presentAddShortcut(pendingShortcut)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func presentEditShortcut(_ shortcut: Shortcut) {
|
|
|
|
modalType = .edit(shortcut: shortcut)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func presentAddShortcut(_ shortcut: INShortcut) {
|
|
|
|
isNavigationPresented = false
|
|
|
|
modalType = .add(shortcut: shortcut)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|