@@ -27,11 +27,35 @@ import UIKit
27
27
Snitch is a handy library to access useful information
28
28
about your application from the Home Screen.
29
29
30
- To snitch the information just call static function `snitch(options:)`
30
+ To snitch the information just call `snitch` function
31
31
of this structure and it will add Home Screen quick action menu
32
32
with the information according to the passed options.
33
33
*/
34
34
public struct Snitch {
35
+ public let options : [ Option ]
36
+
37
+ /**
38
+ An array with the `UIApplicationShortcutItem` instances.
39
+
40
+ This array contains shortcut items prepared to be displayed on the Home Screen.
41
+ Use this variable if you already have shortcut items in your app.
42
+ */
43
+ public var shortcutItems : [ UIApplicationShortcutItem ] {
44
+ return options. compactMap { option in
45
+ guard let data = option. fetch ( ) else { return nil }
46
+
47
+ let title : String = " \( option. title) : \( data) "
48
+ let icon : UIApplicationShortcutIcon ? = option. icon
49
+
50
+ return . init(
51
+ type: option. rawValue,
52
+ localizedTitle: title,
53
+ localizedSubtitle: nil ,
54
+ icon: icon
55
+ )
56
+ }
57
+ }
58
+
35
59
/**
36
60
Function to tell the `Snitch` to snitch the data according to the passed options.
37
61
@@ -40,29 +64,18 @@ public struct Snitch {
40
64
because during the transition to a background state is a good time
41
65
to update any dynamic quick actions, so the system will execute
42
66
code inside the function right before user returns to the Home Screen.
67
+ */
68
+ public func snitch( ) {
69
+ UIApplication . shared. shortcutItems = self . shortcutItems
70
+ }
71
+
72
+ /**
73
+ Creates an instance of the `Snitch` structure.
43
74
44
75
- Parameters:
45
- - options: Options that you want to be shown on Home Screen quick action menu .
76
+ - options: Options that you want to show in Quick Action Menu .
46
77
*/
47
- public static func snitch( options: [ Option ] = [ . size, . version] ) {
48
- var shortcutItems : [ UIApplicationShortcutItem ] = . init( )
49
-
50
- for option in options {
51
- if let data = option. fetch ( ) {
52
- let title : String = " \( option. title) : \( data) "
53
- let icon : UIApplicationShortcutIcon = option. icon
54
-
55
- shortcutItems. append (
56
- . init(
57
- type: option. rawValue,
58
- localizedTitle: title,
59
- localizedSubtitle: nil ,
60
- icon: icon
61
- )
62
- )
63
- }
64
- }
65
-
66
- UIApplication . shared. shortcutItems = shortcutItems
78
+ public init ( options: [ Option ] = [ . size, . version] ) {
79
+ self . options = options
67
80
}
68
81
}
0 commit comments