Skip to content

Commit 51ad44a

Browse files
committed
Changed the way Snitch works with the shortcut items
1 parent 1cb0116 commit 51ad44a

File tree

5 files changed

+40
-27
lines changed

5 files changed

+40
-27
lines changed

Sources/Snitch/Common/Option.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public enum Option {
6969
return fetcher.title
7070
}
7171

72-
public var icon: UIApplicationShortcutIcon {
73-
return fetcher.icon.shortcutIcon
72+
public var icon: UIApplicationShortcutIcon? {
73+
return fetcher.icon?.shortcutIcon
7474
}
7575

7676
public var rawValue: String {

Sources/Snitch/Fetchers/Base/Fetcher.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public protocol Fetcher {
4141
Icon of the fetcher that will be shown in the
4242
Home Screen quick action menu.
4343
*/
44-
var icon: Icon { get }
44+
var icon: Icon? { get }
4545

4646
/**
4747
Function that is called by the `Snitch` to

Sources/Snitch/Fetchers/SizeFetcher.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct SizeFetcher: Fetcher {
2828
return NSLocalizedString("title.size", bundle: .module, comment: "")
2929
}
3030

31-
var icon: Icon {
31+
var icon: Icon? {
3232
return .init(name: "shippingbox", kind: .system)
3333
}
3434

Sources/Snitch/Fetchers/VersionFetcher.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct VersionFetcher: Fetcher {
2828
return NSLocalizedString("title.version", bundle: .module, comment: "")
2929
}
3030

31-
var icon: Icon {
31+
var icon: Icon? {
3232
return .init(name: "tag", kind: .system)
3333
}
3434

Sources/Snitch/Snitch.swift

+35-22
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,35 @@ import UIKit
2727
Snitch is a handy library to access useful information
2828
about your application from the Home Screen.
2929

30-
To snitch the information just call static function `snitch(options:)`
30+
To snitch the information just call `snitch` function
3131
of this structure and it will add Home Screen quick action menu
3232
with the information according to the passed options.
3333
*/
3434
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+
3559
/**
3660
Function to tell the `Snitch` to snitch the data according to the passed options.
3761

@@ -40,29 +64,18 @@ public struct Snitch {
4064
because during the transition to a background state is a good time
4165
to update any dynamic quick actions, so the system will execute
4266
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.
4374

4475
- 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.
4677
*/
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
6780
}
6881
}

0 commit comments

Comments
 (0)