Skip to content

Commit ae70b02

Browse files
committed
NSE: refactor
1 parent cc62e59 commit ae70b02

File tree

1 file changed

+60
-55
lines changed

1 file changed

+60
-55
lines changed

notification-extension/ITBNotificationServiceExtension.swift

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import UserNotifications
88
var contentHandler: ((UNNotificationContent) -> Void)?
99
var bestAttemptContent: UNMutableNotificationContent?
1010

11-
@objc override open func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
11+
@objc override open func didReceive(_ request: UNNotificationRequest,
12+
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
1213
self.contentHandler = contentHandler
1314
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
1415

@@ -28,8 +29,8 @@ import UserNotifications
2829
// MARK: - Private
2930

3031
private func retrieveAttachment(from content: UNNotificationContent) {
31-
guard let itblDictionary = content.userInfo[JsonKey.Payload.metadata] as? [AnyHashable: Any],
32-
let attachmentUrlString = itblDictionary[JsonKey.Payload.attachmentUrl] as? String,
32+
guard let metadata = content.userInfo[JsonKey.Payload.metadata] as? [AnyHashable: Any],
33+
let attachmentUrlString = metadata[JsonKey.Payload.attachmentUrl] as? String,
3334
let url = URL(string: attachmentUrlString) else {
3435
attachmentRetrievalFinished = true
3536
return
@@ -48,7 +49,8 @@ import UserNotifications
4849
return
4950
}
5051

51-
let attachmentId = UUID().uuidString + ITBNotificationServiceExtension.getAttachmentIdSuffix(response: response, responseUrl: responseUrl)
52+
let attachmentId = UUID().uuidString + ITBNotificationServiceExtension.getAttachmentIdSuffix(response: response,
53+
responseUrl: responseUrl)
5254
let tempFileUrl = FileManager.default.temporaryDirectory.appendingPathComponent(attachmentId)
5355

5456
var attachment: UNNotificationAttachment?
@@ -82,14 +84,15 @@ import UserNotifications
8284
return
8385
}
8486

85-
guard let itblPayload = content.userInfo[JsonKey.Payload.metadata] as? [AnyHashable: Any],
86-
let messageId = itblPayload[JsonKey.Payload.messageId] as? String else {
87+
guard let metadata = content.userInfo[JsonKey.Payload.metadata] as? [AnyHashable: Any],
88+
let messageId = metadata[JsonKey.Payload.messageId] as? String else {
8789
setCategoryId(id: "")
8890
return
8991
}
9092

9193
messageCategory = UNNotificationCategory(identifier: messageId,
92-
actions: getNotificationActions(payload: itblPayload, content: content),
94+
actions: getNotificationActions(metadata: metadata,
95+
content: content),
9396
intentIdentifiers: [],
9497
options: [])
9598

@@ -116,54 +119,50 @@ import UserNotifications
116119
}
117120
}
118121

119-
private func createNotificationActionButton(buttonDictionary: [AnyHashable: Any]) -> UNNotificationAction? {
120-
guard let identifier = buttonDictionary[JsonKey.ActionButton.identifier] as? String else { return nil }
121-
guard let title = buttonDictionary[JsonKey.ActionButton.title] as? String else { return nil }
122-
123-
let buttonType = getButtonType(buttonDictionary: buttonDictionary)
124-
125-
var openApp = true
122+
private func getNotificationActions(metadata: [AnyHashable: Any], content: UNNotificationContent) -> [UNNotificationAction] {
123+
var actionButtons: [[AnyHashable: Any]] = []
126124

127-
if let openAppFromDict = buttonDictionary[JsonKey.ActionButton.openApp] as? NSNumber {
128-
openApp = openAppFromDict.boolValue
125+
if let actionButtonsFromMetadata = metadata[JsonKey.Payload.actionButtons] as? [[AnyHashable: Any]] {
126+
actionButtons = actionButtonsFromMetadata
127+
} else {
128+
#if DEBUG
129+
if let actionButtonsFromUserInfo = content.userInfo[JsonKey.Payload.actionButtons] as? [[AnyHashable: Any]] {
130+
actionButtons = actionButtonsFromUserInfo
131+
}
132+
#endif
129133
}
130134

131-
var requiresUnlock = false
132-
133-
if let requiresUnlockFromDict = buttonDictionary[JsonKey.ActionButton.requiresUnlock] as? NSNumber {
134-
requiresUnlock = requiresUnlockFromDict.boolValue
135-
}
135+
return actionButtons.compactMap { createNotificationActionButton(info: $0) }
136+
}
137+
138+
private func createNotificationActionButton(info: [AnyHashable: Any]) -> UNNotificationAction? {
139+
guard let identifier = info[JsonKey.ActionButton.identifier] as? String else { return nil }
140+
guard let title = info[JsonKey.ActionButton.title] as? String else { return nil }
136141

137-
var actionOptions: UNNotificationActionOptions = []
142+
let buttonType = getButtonType(info: info)
143+
let openApp = getBoolValue(info[JsonKey.ActionButton.openApp]) ?? true
144+
let requiresUnlock = getBoolValue(info[JsonKey.ActionButton.requiresUnlock]) ?? false
138145

139-
if buttonType == IterableButtonTypeDestructive {
140-
actionOptions.insert(.destructive)
141-
}
146+
let options = getActionButtonOptions(buttonType: buttonType,
147+
openApp: openApp,
148+
requiresUnlock: requiresUnlock)
142149

143-
if openApp {
144-
actionOptions.insert(.foreground)
150+
guard buttonType == IterableButtonTypeTextInput else {
151+
return UNNotificationAction(identifier: identifier, title: title, options: options)
145152
}
146153

147-
if requiresUnlock || openApp {
148-
actionOptions.insert(.authenticationRequired)
149-
}
154+
let inputTitle = info[JsonKey.ActionButton.inputTitle] as? String ?? ""
155+
let inputPlaceholder = info[JsonKey.ActionButton.inputPlaceholder] as? String ?? ""
150156

151-
if buttonType == IterableButtonTypeTextInput {
152-
let inputTitle = buttonDictionary[JsonKey.ActionButton.inputTitle] as? String ?? ""
153-
let inputPlaceholder = buttonDictionary[JsonKey.ActionButton.inputPlaceholder] as? String ?? ""
154-
155-
return UNTextInputNotificationAction(identifier: identifier,
156-
title: title,
157-
options: actionOptions,
158-
textInputButtonTitle: inputTitle,
159-
textInputPlaceholder: inputPlaceholder)
160-
} else {
161-
return UNNotificationAction(identifier: identifier, title: title, options: actionOptions)
162-
}
157+
return UNTextInputNotificationAction(identifier: identifier,
158+
title: title,
159+
options: options,
160+
textInputButtonTitle: inputTitle,
161+
textInputPlaceholder: inputPlaceholder)
163162
}
164163

165-
private func getButtonType(buttonDictionary: [AnyHashable: Any]) -> String {
166-
if let buttonType = buttonDictionary[JsonKey.ActionButton.buttonType] as? String {
164+
private func getButtonType(info: [AnyHashable: Any]) -> String {
165+
if let buttonType = info[JsonKey.ActionButton.buttonType] as? String {
167166
if buttonType == IterableButtonTypeTextInput || buttonType == IterableButtonTypeDestructive {
168167
return buttonType
169168
}
@@ -172,20 +171,26 @@ import UserNotifications
172171
return IterableButtonTypeDefault
173172
}
174173

175-
private func getNotificationActions(payload: [AnyHashable: Any], content: UNNotificationContent) -> [UNNotificationAction] {
176-
var actionButtons: [[AnyHashable: Any]] = []
174+
private func getBoolValue(_ value: Any?) -> Bool? {
175+
return (value as? NSNumber)?.boolValue
176+
}
177+
178+
private func getActionButtonOptions(buttonType: String, openApp: Bool, requiresUnlock: Bool) -> UNNotificationActionOptions {
179+
var options: UNNotificationActionOptions = []
177180

178-
if let actionButtonsFromItblPayload = payload[JsonKey.Payload.actionButtons] as? [[AnyHashable: Any]] {
179-
actionButtons = actionButtonsFromItblPayload
180-
} else {
181-
#if DEBUG
182-
if let actionButtonsFromUserInfo = content.userInfo[JsonKey.Payload.actionButtons] as? [[AnyHashable: Any]] {
183-
actionButtons = actionButtonsFromUserInfo
184-
}
185-
#endif
181+
if buttonType == IterableButtonTypeDestructive {
182+
options.insert(.destructive)
183+
}
184+
185+
if openApp {
186+
options.insert(.foreground)
187+
}
188+
189+
if requiresUnlock || openApp {
190+
options.insert(.authenticationRequired)
186191
}
187192

188-
return actionButtons.compactMap { createNotificationActionButton(buttonDictionary: $0) }
193+
return options
189194
}
190195

191196
private func checkPushCreationCompletion() {

0 commit comments

Comments
 (0)