-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// Tasks.swift | ||
// DailyPic | ||
// | ||
// Created by Paul Zenker on 26.11.24. | ||
// | ||
|
||
import SwiftUI | ||
import Combine | ||
|
||
class DelayedTaskManager: ObservableObject { | ||
// Stores the current countdown | ||
@Published var timeRemaining: TimeInterval = 0 | ||
|
||
// Stores the cancellable task | ||
private var taskCancellable: Task<Void, Never>? | ||
|
||
// Private countdown timer | ||
private var countdownTimer: Timer? | ||
|
||
func scheduleTask(delay: TimeInterval, task: @escaping () async -> Void) { | ||
// Cancel any existing task | ||
taskCancellable?.cancel() | ||
countdownTimer?.invalidate() | ||
|
||
// Reset time remaining | ||
timeRemaining = delay | ||
|
||
// Start countdown timer | ||
countdownTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] timer in | ||
guard let self = self else { return } | ||
|
||
self.timeRemaining -= 1 | ||
|
||
if self.timeRemaining <= 0 { | ||
timer.invalidate() | ||
} | ||
} | ||
|
||
// Create a new task with delay | ||
taskCancellable = Task { | ||
do { | ||
// Wait for the specified delay | ||
try await Task.sleep(for: .seconds(delay)) | ||
|
||
// Execute the actual task | ||
await task() | ||
} catch is CancellationError { | ||
print("Task was cancelled") | ||
} catch { | ||
print("Task failed: \(error)") | ||
} | ||
} | ||
} | ||
|
||
func cancelTask() { | ||
taskCancellable?.cancel() | ||
countdownTimer?.invalidate() | ||
timeRemaining = 0 | ||
} | ||
} | ||
|
||
struct DelayedTaskView: View { | ||
@StateObject private var taskManager = DelayedTaskManager() | ||
|
||
func startTask() { | ||
taskManager.scheduleTask(delay: 300) { | ||
try? await ImageManager.shared.downloadImage(of: Date()) | ||
} | ||
} | ||
var body: some View { | ||
VStack { | ||
// Countdown label | ||
Text(timeString(from: taskManager.timeRemaining)) | ||
.font(.largeTitle) | ||
.padding() | ||
|
||
// // Start task button | ||
// Button("Start 5 Minute Delayed Task") { | ||
// taskManager.scheduleTask(delay: 300) { | ||
// // Example task: downloading an image | ||
// try? await ImageManager.shared.downloadImage(of: Date()) | ||
// } | ||
// } | ||
|
||
// Cancel button | ||
Button("Cancel Task") { | ||
taskManager.cancelTask() | ||
} | ||
.disabled(taskManager.timeRemaining == 0) | ||
} | ||
} | ||
|
||
// Helper to format time | ||
func timeString(from timeInterval: TimeInterval) -> String { | ||
let minutes = Int(timeInterval) / 60 | ||
let seconds = Int(timeInterval) % 60 | ||
return String(format: "%02d:%02d", minutes, seconds) | ||
} | ||
} |
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