Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tracking updates #64

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public actor LiveActivityManager: Sendable {
let start: (LiveActivityRequest.Start) async throws -> LiveActivityInfo
let update: (LiveActivityRequest.Update) async throws -> Bool
let end: (LiveActivityRequest.End) async throws -> Bool
let track: (String) -> Void

let pushToStartUpdates: ((@escaping @Sendable () async -> Void)) -> Void
let activityUpdates: (String, (@escaping @Sendable (LiveActivityInfo?) async -> Void)) -> Void
Expand Down Expand Up @@ -96,8 +97,20 @@ public actor LiveActivityManager: Sendable {
var update = false
let all = self.entries.values.compactMap { try? $0.list() }.joined()
for info in all {
do {
try track(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will result in multiple calls to track for the LA, but we handle spamming it on the SDK. We basically look for id/name combo and no-op if we are already watching for it

attributesType: info.attributesType,
activityID: info.id
)
} catch {
AirshipLogger.warn("Failed to track activity: \(error)")
}

if activityState[info.id] == nil {
await startWatchingActivityUpdates(info.id, attributesType: info.attributesType)
await startWatchingActivityUpdates(
info.id,
attributesType: info.attributesType
)
await updated(activityID: info.id, info: info, notifyOnChange: false)
update = true
}
Expand Down Expand Up @@ -135,6 +148,7 @@ public actor LiveActivityManager: Sendable {
})
}


public func start(_ request: LiveActivityRequest.Start) async throws -> LiveActivityInfo {
try await waitForSetup()
let result = try await findEntry(attributesType: request.attributesType).start(request)
Expand Down Expand Up @@ -202,6 +216,13 @@ public actor LiveActivityManager: Sendable {
}
return entry
}

private func track(attributesType: String, activityID: String) throws {
guard let entry = self.entries[attributesType] else {
throw AirshipErrors.error("Missing entry for attributesType \(attributesType)")
}
entry.track(activityID)
}
}


Expand All @@ -226,7 +247,7 @@ extension LiveActivityManager.Entry {
self.activityUpdates = { id, callback in
Task {
let contentTask = Task {
try? await Self.watchContentUpates(
try? await Self.watchContentUpdates(
type,
activityID: id,
attributesType: attributesType,
Expand Down Expand Up @@ -268,6 +289,30 @@ extension LiveActivityManager.Entry {

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

self.track = { activityID in
Self.track(
type,
activityID:activityID,
airshipNameExtractor: airshipNameExtractor
)
}
}

private static func track<T: ActivityAttributes>(
_ type: Activity<T>.Type,
activityID: String,
airshipNameExtractor: (@Sendable (T) -> String)?
) {
guard
let activity = type.activities.first(where: { $0.id == activityID })
else {
return
}

if let airshipName = airshipNameExtractor?(activity.attributes) {
Airship.channel.trackLiveActivity(activity, name: airshipName)
}
}

private static func updateActivity<T: ActivityAttributes>(
Expand Down Expand Up @@ -377,7 +422,7 @@ extension LiveActivityManager.Entry {
}
}

private static func watchContentUpates<T: ActivityAttributes>(
private static func watchContentUpdates<T: ActivityAttributes>(
_ type: Activity<T>.Type,
activityID: String,
attributesType: String,
Expand Down
Loading