Created
July 29, 2022 10:05
-
-
Save alicanbatur/2b8cff7782892dc8ea90f4fe712d0183 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LiveActivityHelper.swift | |
// LiveActivity | |
// | |
// Created by Ali Can Batur on 29.07.2022. | |
// | |
import Foundation | |
import ActivityKit | |
class LiveActivityHelper { | |
var statusActivity: Activity<StatusAttribute>? | |
func start() { | |
// We can check if activity is enabled. | |
guard ActivityAuthorizationInfo().areActivitiesEnabled else { | |
print("Activities are not enabled!") | |
return | |
} | |
// Initializing the models. | |
let statusAttribute = StatusAttribute(buildId: "123456789") | |
let initialStatus = StatusAttribute.ProcessStatus(status: .pending, | |
estimatedCompletionTime: Date().addingTimeInterval(60)) | |
// Key point here! | |
// Now we tell iOS that there is a new activity started! | |
do { | |
statusActivity = try Activity<StatusAttribute>.request( | |
attributes: statusAttribute, | |
contentState: initialStatus, | |
pushType: nil) | |
guard let statusActivity else { | |
print("Error: Could not initialize activity.") | |
return | |
} | |
print("Build with ID: \(statusActivity.id) is now pending.") | |
} catch (let error) { | |
print("Error: \(error.localizedDescription)") | |
} | |
} | |
// Now I will update the current activity. | |
func update() { | |
Task { | |
let updatedCICDStatus = StatusAttribute.ProcessStatus(status: .inProgress, | |
estimatedCompletionTime: Date().addingTimeInterval(30)) | |
guard let statusActivity else { | |
return | |
} | |
await statusActivity.update(using: updatedCICDStatus) | |
} | |
} | |
func end(with status: Status) { | |
Task { | |
let updatedCICDStatus = StatusAttribute.ProcessStatus(status: status, estimatedCompletionTime: Date()) | |
guard let statusActivity else { | |
return | |
} | |
await statusActivity.end(using: updatedCICDStatus, dismissalPolicy: .default) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment