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

176 lines
4.9 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.
// 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
2022-06-23 21:31:01 +00:00
import PassepartoutLibrary
2022-04-12 13:09:14 +00:00
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
@Environment(\.presentationMode) private var presentationMode
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?
init(target: Profile) {
2022-04-12 13:09:14 +00:00
intentsManager = .shared
self.target = target
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)
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
}
.navigationTitle(Unlocalized.Other.siri)
.themeSecondaryView()
2022-04-12 13:09:14 +00:00
}
private 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
}
}
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 {
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
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)
}
}
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
}
}