Tusker/Tusker/Shortcuts/AppShortcutItems.swift

68 lines
2.1 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)
}
}
func handle() {
let tab: MainTabBarViewController.Tab
switch self {
case .showHomeTimeline:
tab = .timelines
case .showNotifications:
tab = .notifications
case .composePost:
tab = .compose
}
let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first!
let window = scene.windows.first { $0.isKeyWindow }!
let controller = window.rootViewController as! MainTabBarViewController
controller.select(tab: tab)
}
}
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)
}
}
static func handle(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
guard let type = AppShortcutItem(rawValue: shortcutItem.type) else { return false }
type.handle()
return true
}
}