Skip to content

Commit

Permalink
Add update and end functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bndkt committed Sep 14, 2023
1 parent 90dd61c commit e95a657
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions example/SampleWidgetExtension/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,41 @@ public class ReactNativeWidgetExtensionModule: Module {
}
}
}

Function("updateActivity") { (quarter: Int, scoreLeft: Int, scoreRight: Int, bottomText: String) -> Void in
let logger = Logger()
logger.info("updateActivity()")

if #available(iOS 16.2, *) {
let future = Calendar.current.date(byAdding: .minute, value: (Int(15) ), to: Date())!
let contentState = SportsLiveActivityAttributes.ContentState(quarter: quarter, scoreLeft: scoreLeft, scoreRight: scoreRight, bottomText: bottomText)
let alertConfiguration = AlertConfiguration(title: "Score update", body: bottomText, sound: .default)
let updatedContent = ActivityContent(state: contentState, staleDate: nil)

Task {
for activity in Activity<SportsLiveActivityAttributes>.activities {
await activity.update(updatedContent, alertConfiguration: alertConfiguration)
logger.info("Updated the Live Activity: \(activity.id)")
}
}
}
}

Function("endActivity") { (quarter: Int, scoreLeft: Int, scoreRight: Int, bottomText: String) -> Void in
let logger = Logger()
logger.info("endActivity()")

if #available(iOS 16.2, *) {
let contentState = SportsLiveActivityAttributes.ContentState(quarter: quarter, scoreLeft: scoreLeft, scoreRight: scoreRight, bottomText: bottomText)
let finalContent = ActivityContent(state: contentState, staleDate: nil)

Task {
for activity in Activity<SportsLiveActivityAttributes>.activities {
await activity.end(finalContent, dismissalPolicy: .default)
logger.info("Ending the Live Activity: \(activity.id)")
}
}
}
}
}
}

0 comments on commit e95a657

Please sign in to comment.