Skip to content

Commit 4671d0a

Browse files
committed
Move APNSLiveActivityNotificationEvent back to a struct.
1 parent 3d78f52 commit 4671d0a

File tree

4 files changed

+34
-90
lines changed

4 files changed

+34
-90
lines changed

Sources/APNSCore/LiveActivity/APNSLiveActivityNotification.swift

+7-26
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,7 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
4949
/// Event type e.g. update
5050
public var event: APNSLiveActivityNotificationEvent {
5151
get {
52-
switch self.aps.event {
53-
case "end":
54-
return APNSLiveActivityNotificationEventEnd()
55-
case "update":
56-
return APNSLiveActivityNotificationEventUpdate()
57-
default:
58-
guard let attributesType = self.aps.attributesType,
59-
let state = self.aps.attributesContent,
60-
let alert = self.aps.alert
61-
else {
62-
// Default to update
63-
return APNSLiveActivityNotificationEventUpdate()
64-
}
65-
66-
return APNSLiveActivityNotificationEventStart(
67-
attributes: .init(type: attributesType, state: state),
68-
alert: alert
69-
)
70-
}
52+
return APNSLiveActivityNotificationEvent(rawValue: self.aps.event)
7153
}
7254

7355
set {
@@ -136,7 +118,8 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
136118
priority: APNSPriority,
137119
appID: String,
138120
contentState: ContentState,
139-
event: any APNSLiveActivityNotificationEvent,
121+
event: APNSLiveActivityNotificationEvent,
122+
startOptions: APNSLiveActivityNotificationEventStartOptions<ContentState>? = nil,
140123
timestamp: Int,
141124
dismissalDate: APNSLiveActivityDismissalDate = .none,
142125
apnsID: UUID? = nil
@@ -147,6 +130,7 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
147130
topic: appID + ".push-type.liveactivity",
148131
contentState: contentState,
149132
event: event,
133+
startOptions: startOptions,
150134
timestamp: timestamp,
151135
dismissalDate: dismissalDate
152136
)
@@ -173,18 +157,15 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
173157
topic: String,
174158
apnsID: UUID? = nil,
175159
contentState: ContentState,
176-
event: any APNSLiveActivityNotificationEvent,
160+
event: APNSLiveActivityNotificationEvent,
161+
startOptions: APNSLiveActivityNotificationEventStartOptions<ContentState>? = nil,
177162
timestamp: Int,
178163
dismissalDate: APNSLiveActivityDismissalDate = .none
179164
) {
180-
var attributes: APNSLiveActivityNotificationEventStart<ContentState>.Attributes?
181-
if let event = event as? APNSLiveActivityNotificationEventStart<ContentState> {
182-
attributes = event.attributes
183-
}
184-
185165
self.aps = APNSLiveActivityNotificationAPSStorage(
186166
timestamp: timestamp,
187167
event: event,
168+
startOptions: startOptions,
188169
contentState: contentState,
189170
dismissalDate: dismissalDate.dismissal
190171
)

Sources/APNSCore/LiveActivity/APNSLiveActivityNotificationAPSStorage.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ struct APNSLiveActivityNotificationAPSStorage<ContentState: Encodable>: Encodabl
3333

3434
init(
3535
timestamp: Int,
36-
event: any APNSLiveActivityNotificationEvent,
36+
event: APNSLiveActivityNotificationEvent,
37+
startOptions: APNSLiveActivityNotificationEventStartOptions<ContentState>?,
3738
contentState: ContentState,
3839
dismissalDate: Int?
3940
) {
@@ -42,10 +43,10 @@ struct APNSLiveActivityNotificationAPSStorage<ContentState: Encodable>: Encodabl
4243
self.dismissalDate = dismissalDate
4344
self.event = event.rawValue
4445

45-
if let event = event as? APNSLiveActivityNotificationEventStart<ContentState> {
46-
self.attributesType = event.attributes.type
47-
self.attributesContent = event.attributes.state
48-
self.alert = event.alert
46+
if let startOptions {
47+
self.attributesType = startOptions.attributeType
48+
self.attributesContent = startOptions.attributes
49+
self.alert = startOptions.alert
4950
}
5051
}
5152
}

Sources/APNSCore/LiveActivity/APNSLiveActivityNotificationEvent.swift

+19-54
Original file line numberDiff line numberDiff line change
@@ -12,65 +12,30 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
public protocol APNSLiveActivityNotificationEvent: Encodable {
16-
var rawValue: String { get }
17-
}
18-
19-
public struct APNSLiveActivityNotificationEventUpdate: APNSLiveActivityNotificationEvent {
20-
public let rawValue = "update"
21-
}
22-
23-
public struct APNSLiveActivityNotificationEventEnd: APNSLiveActivityNotificationEvent {
24-
public let rawValue = "end"
25-
}
2615

27-
public protocol APNSLiveActivityNotificationEventStartStateProtocol: Encodable
28-
{
29-
associatedtype State: Encodable
30-
}
31-
32-
public struct APNSLiveActivityNotificationEventStart<State: Encodable>:
33-
APNSLiveActivityNotificationEvent, APNSLiveActivityNotificationEventStartStateProtocol
34-
{
35-
public struct Attributes: Encodable {
36-
public let type: String
37-
public let state: State
16+
public struct APNSLiveActivityNotificationEvent: Encodable {
17+
/// The underlying raw value that is send to APNs.
18+
@usableFromInline
19+
internal let rawValue: String
3820

39-
public init(type: String, state: State) {
40-
self.type = type
41-
self.state = state
42-
}
43-
}
21+
/// Specifies that live activity should be updated
22+
public static let update = Self(rawValue: "update")
4423

45-
public let rawValue = "start"
46-
public let attributes: Attributes
47-
public let alert: APNSAlertNotificationContent
24+
/// Specifies that live activity should be ended
25+
public static let end = Self(rawValue: "end")
4826

49-
public init(attributes: Attributes, alert: APNSAlertNotificationContent) {
50-
self.attributes = attributes
51-
self.alert = alert
52-
}
27+
/// The underlying raw value that is send to APNs.
28+
public static let start = Self(rawValue: "start")
5329
}
5430

55-
extension APNSLiveActivityNotificationEvent where Self == APNSLiveActivityNotificationEventUpdate {
56-
public static var update: APNSLiveActivityNotificationEventUpdate {
57-
APNSLiveActivityNotificationEventUpdate()
58-
}
59-
}
60-
61-
extension APNSLiveActivityNotificationEvent where Self == APNSLiveActivityNotificationEventEnd {
62-
public static var end: APNSLiveActivityNotificationEventEnd {
63-
APNSLiveActivityNotificationEventEnd()
64-
}
65-
}
31+
public struct APNSLiveActivityNotificationEventStartOptions<State: Encodable> {
32+
var attributeType: String
33+
var attributes: State
34+
var alert: APNSAlertNotificationContent
6635

67-
extension APNSLiveActivityNotificationEvent
68-
where Self: APNSLiveActivityNotificationEventStartStateProtocol {
69-
public static func start(type: String, state: State, alert: APNSAlertNotificationContent)
70-
-> APNSLiveActivityNotificationEventStart<
71-
State
72-
>
73-
{
74-
.init(attributes: .init(type: type, state: state), alert: alert)
75-
}
36+
public init(attributeType: String, attributes: State, alert: APNSAlertNotificationContent) {
37+
self.attributeType = attributeType
38+
self.attributes = attributes
39+
self.alert = alert
40+
}
7641
}

Tests/APNSTests/LiveActivity/APNSLiveActivityNotificationTests.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@ final class APNSLiveActivityNotificationTests: XCTestCase {
5151
priority: .immediately,
5252
appID: "test.app.id",
5353
contentState: State(),
54-
// Need the fully qualified name here
55-
event: APNSLiveActivityNotificationEventStart(
56-
attributes: .init(type: "State", state: State()),
57-
alert: .init(title: .raw("Update"))
58-
),
54+
event: .start,
55+
startOptions: .init(attributeType: "State", attributes: State(), alert: .init(title: .raw("Update"))),
5956
timestamp: 1_672_680_658)
6057

6158
let encoder = JSONEncoder()

0 commit comments

Comments
 (0)