-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSearchableActivityConvertable.swift
More file actions
88 lines (72 loc) · 3.17 KB
/
Copy pathSearchableActivityConvertable.swift
File metadata and controls
88 lines (72 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import UIKit
import CoreSpotlight
import UniformTypeIdentifiers
/// Custom NSUSerActivity types for the WPiOS. Primarily used for navigation points.
///
public enum WPActivityType: String {
case siteList = "org.wordpress.mysites"
case siteDetails = "org.wordpress.mysites.details"
case reader = "org.wordpress.reader"
case me = "org.wordpress.me"
case appSettings = "org.wordpress.me.appsettings"
case notificationSettings = "org.wordpress.me.notificationsettings"
case support = "org.wordpress.me.support"
case notifications = "org.wordpress.notifications"
}
/// NSUserActivity userInfo keys
///
public enum WPActivityUserInfoKeys: String {
case siteId = "siteid"
}
@objc public protocol SearchableActivityConvertable {
/// Type name used to uniquly indentify this activity.
///
@objc var activityType: String { get }
/// Activity title to be displayed in spotlight search.
///
@objc var activityTitle: String { get }
// MARK: Optional Vars
/// A set of localized keywords that can help users find the activity in search results.
///
@objc optional var activityKeywords: Set<String>? { get }
/// The date after which the activity is no longer eligible for indexing. If not set,
/// the expiration date will default to one week from the current date.
///
@objc optional var activityExpirationDate: Date? { get }
/// A dictionary containing state information related to this indexed activity.
///
@objc optional var activityUserInfo: [String: String]? { get }
/// Activity description
///
@objc optional var activityDescription: String? { get }
}
public extension SearchableActivityConvertable where Self: UIViewController {
func registerUserActivity() {
let activity = NSUserActivity(activityType: activityType)
activity.title = activityTitle
if let keywords = activityKeywords as? Set<String>, !keywords.isEmpty {
activity.keywords = keywords
}
if let expirationDate = activityExpirationDate {
activity.expirationDate = expirationDate
} else {
let oneWeekFromNow = Calendar.current.date(byAdding: .weekOfYear, value: 1, to: Date())
activity.expirationDate = oneWeekFromNow
}
if let activityUserInfo {
activity.userInfo = activityUserInfo
activity.requiredUserInfoKeys = Set([WPActivityUserInfoKeys.siteId.rawValue])
}
if let activityDescription {
let contentAttributeSet = CSSearchableItemAttributeSet(itemContentType: UTType.text.identifier)
contentAttributeSet.contentDescription = activityDescription
contentAttributeSet.contentCreationDate = nil // Set this to nil so it doesn't display in spotlight
activity.contentAttributeSet = contentAttributeSet
}
activity.isEligibleForSearch = true
activity.isEligibleForHandoff = false
// Set the UIViewController's userActivity property, which is defined in UIResponder. Doing this allows
// UIKit to automagically manage this user activity (e.g. making it current when needed)
userActivity = activity
}
}