Gemini/Gemini-iOS/Intents/UserActivities.swift

55 lines
1.4 KiB
Swift
Raw Normal View History

2021-09-29 01:03:37 +00:00
//
// UserActivities.swift
// Gemini-iOS
//
// Created by Shadowfacts on 9/28/21.
//
import Foundation
import BrowserCore
2021-10-02 15:47:01 +00:00
private let browseType = "space.vaccor.Gemini.activity.browse"
private let homepageType = "space.vaccor.Gemini.activity.homepage"
private let encoder = PropertyListEncoder()
private let decoder = PropertyListDecoder()
2021-09-29 01:03:37 +00:00
extension NSUserActivity {
2021-10-02 15:47:01 +00:00
static func homepage() -> NSUserActivity {
return NSUserActivity(activityType: homepageType)
}
2021-09-29 01:03:37 +00:00
convenience init(geminiURL url: URL) {
2021-10-02 15:47:01 +00:00
self.init(activityType: browseType)
2021-09-29 01:03:37 +00:00
self.userInfo = [
"url": url,
]
}
convenience init(navigationManager manager: NavigationManager) {
2021-10-02 15:47:01 +00:00
self.init(activityType: browseType)
2021-09-29 01:03:37 +00:00
self.userInfo = [
"url": manager.currentURL,
]
if let data = try? encoder.encode(manager) {
self.userInfo!["manager"] = data
}
2021-09-29 01:03:37 +00:00
}
var navigationManager: NavigationManager? {
2021-10-02 15:47:01 +00:00
guard activityType == browseType,
let data = userInfo?["manager"] as? Data,
let manager = try? decoder.decode(NavigationManager.self, from: data) else { return nil }
2021-09-29 01:03:37 +00:00
return manager
}
var geminiURL: URL? {
2021-10-02 15:47:01 +00:00
guard activityType == browseType,
2021-09-29 01:03:37 +00:00
let url = userInfo?["url"] as? URL else { return nil }
return url
}
2021-10-02 15:47:01 +00:00
var isHomepage: Bool {
return activityType == homepageType
}
2021-09-29 01:03:37 +00:00
}