Skip to content

Commit

Permalink
Use attributesType instead of typeReferenceId
Browse files Browse the repository at this point in the history
  • Loading branch information
rlepinski committed Sep 30, 2024
1 parent c51a37e commit 0ffc429
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
8 changes: 4 additions & 4 deletions ios/AirshipFrameworkProxy/LiveActivity/LiveActivityInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import ActivityKit

public struct LiveActivityInfo: Codable, Sendable, Equatable, Hashable {
public var id: String
public var typeReferenceID: String
public var attributesType: String
public var state: LiveActivityState
public var content: LiveActivityContent
public var attributes: AirshipJSON

enum CodingKeys: String, CodingKey {
case id
case typeReferenceID = "typeReferenceId"
case attributesType
case state
case content
case attributes
Expand All @@ -36,10 +36,10 @@ extension LiveActivityInfo {
@available(iOS 16.1, *)
init<T: ActivityAttributes>(
activity: Activity<T>,
typeReferenceID: String
attributesType: String
) throws {
self.id = activity.id
self.typeReferenceID = typeReferenceID
self.attributesType = attributesType
self.attributes = try AirshipJSON.wrap(activity.attributes)
self.state = Self.state(state: activity.activityState)
self.content = if #available(iOS 16.2, *) {
Expand Down
46 changes: 23 additions & 23 deletions ios/AirshipFrameworkProxy/LiveActivity/LiveActivityManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public actor LiveActivityManager: Sendable {

public func register<T: ActivityAttributes>(
forType type: Activity<T>.Type,
typeReferenceID: String,
attributesType: String = String(describing: T.self),
airshipNameExtractor: (@Sendable (T) -> String)?
) async {
self.entries[typeReferenceID] = Entry(
self.entries[attributesType] = Entry(
forType: type,
typeReferenceID: typeReferenceID,
attributesType: attributesType,
airshipNameExtractor: airshipNameExtractor
)
await restorer.restore(forType: type)
Expand Down Expand Up @@ -97,7 +97,7 @@ public actor LiveActivityManager: Sendable {
let all = self.entries.values.compactMap { try? $0.list() }.joined()
for info in all {
if activityState[info.id] == nil {
await startWatchingActivityUpdates(info.id, typeReferenceID: info.typeReferenceID)
await startWatchingActivityUpdates(info.id, attributesType: info.attributesType)
await updated(activityID: info.id, info: info, notifyOnChange: false)
update = true
}
Expand Down Expand Up @@ -128,40 +128,40 @@ public actor LiveActivityManager: Sendable {
await AirshipProxyEventEmitter.shared.addEvent(LiveActivitiesUpdatedEvent(activities))
}

private func startWatchingActivityUpdates(_ activityID: String, typeReferenceID: String) async {
let entry = try? await self.findEntry(typeReferenceID: typeReferenceID)
private func startWatchingActivityUpdates(_ activityID: String, attributesType: String) async {
let entry = try? await self.findEntry(attributesType: attributesType)
entry?.activityUpdates(activityID, { [weak self] info in
await self?.updated(activityID: activityID, info: info, notifyOnChange: true)
})
}

public func create(_ request: LiveActivityRequest.Create) async throws -> LiveActivityInfo {
let result = try await findEntry(typeReferenceID: request.typeReferenceID).create(request)
let result = try await findEntry(attributesType: request.attributesType).create(request)
if #unavailable(iOS 17.2) {
await self.checkForActivities()
}
return result
}

public func update(_ request: LiveActivityRequest.Update) async throws -> Void {
try await findEntry(typeReferenceID: request.typeReferenceID).update(request)
try await findEntry(attributesType: request.attributesType).update(request)
}

public func end(_ request: LiveActivityRequest.End) async throws -> Void {
try await findEntry(typeReferenceID: request.typeReferenceID).end(request)
try await findEntry(attributesType: request.attributesType).end(request)
}

public func list(_ request: LiveActivityRequest.List) async throws -> [LiveActivityInfo] {
return try await findEntry(typeReferenceID: request.typeReferenceID).list()
return try await findEntry(attributesType: request.attributesType).list()
}

private func findEntry(typeReferenceID: String) async throws -> Entry {
private func findEntry(attributesType: String) async throws -> Entry {
guard await self.setupCalled else {
throw AirshipErrors.error("Setup not called")
}
await setupTask?.value
guard let entry = self.entries[typeReferenceID] else {
throw AirshipErrors.error("Missing entry for typeReferenceID \(typeReferenceID)")
guard let entry = self.entries[attributesType] else {
throw AirshipErrors.error("Missing entry for attributesType \(attributesType)")
}
return entry
}
Expand All @@ -172,7 +172,7 @@ public actor LiveActivityManager: Sendable {
extension LiveActivityManager.Entry {
init<T: ActivityAttributes>(
forType type: Activity<T>.Type,
typeReferenceID: String,
attributesType: String,
airshipNameExtractor: (@Sendable (T) -> String)?
) {

Expand All @@ -192,7 +192,7 @@ extension LiveActivityManager.Entry {
try? await Self.watchContentUpates(
type,
activityID: id,
typeReferenceID: typeReferenceID,
attributesType: attributesType,
onUpdate: callback
)
}
Expand All @@ -201,7 +201,7 @@ extension LiveActivityManager.Entry {
try? await Self.watchStatusUpdates(
type,
activityID: id,
typeReferenceID: typeReferenceID,
attributesType: attributesType,
onUpdate: callback
)
contentTask.cancel()
Expand All @@ -215,7 +215,7 @@ extension LiveActivityManager.Entry {

self.list = {
return try type.activities.map { activity in
try LiveActivityInfo(activity: activity, typeReferenceID: typeReferenceID)
try LiveActivityInfo(activity: activity, attributesType: attributesType)
}
}

Expand All @@ -229,7 +229,7 @@ extension LiveActivityManager.Entry {
Airship.channel.trackLiveActivity(activity, name: airshipName)
}

return try LiveActivityInfo(activity: activity, typeReferenceID: typeReferenceID)
return try LiveActivityInfo(activity: activity, attributesType: attributesType)
}
}

Expand Down Expand Up @@ -317,7 +317,7 @@ extension LiveActivityManager.Entry {
private static func watchStatusUpdates<T: ActivityAttributes>(
_ type: Activity<T>.Type,
activityID: String,
typeReferenceID: String,
attributesType: String,
onUpdate: @escaping @Sendable (LiveActivityInfo?) async -> Void
) async throws {
guard let activity = type.activities.first(where: { $0.id == activityID }) else {
Expand All @@ -330,7 +330,7 @@ extension LiveActivityManager.Entry {
return
}

if let info = try? LiveActivityInfo(activity: updated, typeReferenceID: typeReferenceID) {
if let info = try? LiveActivityInfo(activity: updated, attributesType: attributesType) {
await onUpdate(info)
}
}
Expand All @@ -339,7 +339,7 @@ extension LiveActivityManager.Entry {
private static func watchContentUpates<T: ActivityAttributes>(
_ type: Activity<T>.Type,
activityID: String,
typeReferenceID: String,
attributesType: String,
onUpdate: @escaping @Sendable (LiveActivityInfo?) async -> Void
) async throws {
guard let activity = type.activities.first(where: { $0.id == activityID }) else {
Expand All @@ -353,7 +353,7 @@ extension LiveActivityManager.Entry {
break
}

if let info = try? LiveActivityInfo(activity: updated, typeReferenceID: typeReferenceID) {
if let info = try? LiveActivityInfo(activity: updated, attributesType: attributesType) {
await onUpdate(info)
}
}
Expand All @@ -364,7 +364,7 @@ extension LiveActivityManager.Entry {
break
}

if let info = try? LiveActivityInfo(activity: updated, typeReferenceID: typeReferenceID) {
if let info = try? LiveActivityInfo(activity: updated, attributesType: attributesType) {
await onUpdate(info)
}
}
Expand Down
34 changes: 16 additions & 18 deletions ios/AirshipFrameworkProxy/LiveActivity/LiveActivityRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,81 @@ import AirshipCore
public struct LiveActivityRequest: Sendable, Equatable {

public struct List: Sendable, Equatable, Codable {
public var typeReferenceID: String
public var attributesType: String

enum CodingKeys: String, CodingKey {
case typeReferenceID = "typeReferenceId"
}

public init(typeReferenceID: String) {
self.typeReferenceID = typeReferenceID
public init(attributesType: String) {
self.attributesType = attributesType
}
}

public struct Update: Sendable, Equatable, Codable {
public var activityID: String
public var typeReferenceID: String
public var attributesType: String
public var content: LiveActivityContent


enum CodingKeys: String, CodingKey {
case activityID = "activityId"
case typeReferenceID = "typeReferenceId"
case attributesType
case content
}


public init(
activityID: String,
typeReferenceID: String,
attributesType: String,
content: LiveActivityContent
) {
self.activityID = activityID
self.typeReferenceID = typeReferenceID
self.attributesType = attributesType
self.content = content
}
}

public struct End: Sendable, Equatable, Codable {
public var activityID: String
public var typeReferenceID: String
public var attributesType: String
public var content: LiveActivityContent?
public var dismissalPolicy: DismissalPolicy?

enum CodingKeys: String, CodingKey {
case activityID = "activityId"
case typeReferenceID = "typeReferenceId"
case attributesType
case content
case dismissalPolicy
}

public init(
activityID: String,
typeReferenceID: String,
attributesType: String,
content: LiveActivityContent? = nil,
dismissalPolicy: DismissalPolicy? = nil
) {
self.activityID = activityID
self.typeReferenceID = typeReferenceID
self.attributesType = attributesType
self.content = content
self.dismissalPolicy = dismissalPolicy
}
}

public struct Create: Sendable, Equatable, Codable {
public var typeReferenceID: String
public var attributesType: String
public var content: LiveActivityContent
public var attributes: AirshipJSON

enum CodingKeys: String, CodingKey {
case typeReferenceID = "typeReferenceId"
case attributesType
case content
case attributes
}

public init(
typeReferenceID: String,
attributesType: String,
content: LiveActivityContent,
attributes: AirshipJSON
) {
self.typeReferenceID = typeReferenceID
self.attributesType = attributesType
self.content = content
self.attributes = attributes
}
Expand Down

0 comments on commit 0ffc429

Please sign in to comment.