Tusker/Tusker/Shortcuts/AppShortcutItems.swift

70 lines
2.2 KiB
Swift

//
// AppShortcutItems.swift
// Tusker
//
// Created by Shadowfacts on 9/19/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import UIKit
enum AppShortcutItem: String, CaseIterable {
case showHomeTimeline
case showNotifications
case composePost
var title: String {
switch self {
case .showHomeTimeline:
return NSLocalizedString("Home", comment: "home app shortcut title")
case .showNotifications:
return NSLocalizedString("Notifications", comment: "notifications app shortcut title")
case .composePost:
return NSLocalizedString("Compose Post", comment: "compose post app shortcut title")
}
}
var icon: UIApplicationShortcutIcon {
switch self {
case .showHomeTimeline:
return .init(type: .home)
case .showNotifications:
return .init(systemImageName: "bell.fill")
case .composePost:
return .init(type: .compose)
}
}
@MainActor
func handle() {
let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first!
let window = scene.windows.first { $0.isKeyWindow }!
guard let root = window.rootViewController as? TuskerRootViewController else {
return
}
switch self {
case .showHomeTimeline:
root.select(route: .timelines, animated: false)
case .showNotifications:
root.select(route: .notifications, animated: false)
case .composePost:
root.compose(editing: nil, animated: false, isDucked: false)
}
}
}
extension AppShortcutItem {
static func createItems(for application: UIApplication) {
application.shortcutItems = allCases.map {
return UIApplicationShortcutItem(type: $0.rawValue, localizedTitle: $0.title, localizedSubtitle: nil, icon: $0.icon, userInfo: nil)
}
}
@MainActor
static func handle(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
guard let type = AppShortcutItem(rawValue: shortcutItem.type) else { return false }
type.handle()
return true
}
}