Gemini/Gemini-iOS/Intents/UserActivities.swift

55 lines
1.4 KiB
Swift

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