iOS/Sources/Shared/Notifications/UNNotificationContent+Additions.swift
Michal Šrůtek 7de47c6e62
static var to static let (#2642)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

- `static let` plays nicely with modern Swift concurrency (async/await)
- `static let` makes it impossible to mutate the values - causing
undefined behavior in some cases
2024-03-25 11:57:48 +01:00

46 lines
1.5 KiB
Swift

import ObjectMapper
import UserNotifications
public extension UNNotificationContent {
private static let separator: String = "@duplicate_identifier-"
static func uncombinedAction(from identifier: String) -> String {
if identifier.contains(separator), let substring = identifier.components(separatedBy: separator).first {
return substring
} else {
return identifier
}
}
static func combinedAction(base: String, appended: String) -> String {
[base, appended].joined(separator: String(separator))
}
var userInfoActionConfigs: [MobileAppConfigPushCategory.Action] {
let actions = userInfo["actions"] as? [[String: Any]] ?? []
do {
return try Mapper<MobileAppConfigPushCategory.Action>()
.mapArray(JSONArray: actions)
.reduce(into: []) { result, original in
var trailing = (2...).lazy.map(String.init(describing:)).makeIterator()
var action = original
while result.contains(where: { $0.identifier == action.identifier }) {
action.identifier = Self.combinedAction(base: original.identifier, appended: trailing.next()!)
}
result.append(action)
}
} catch {
return []
}
}
var userInfoActions: [UNNotificationAction] {
userInfoActionConfigs
.map(NotificationAction.init(action:))
.map(\.action)
}
}