Skip to content

Commit

Permalink
fix: indexError
Browse files Browse the repository at this point in the history
  • Loading branch information
KuramaSyu committed Nov 26, 2024
1 parent cf0d68f commit 3dc5729
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
100 changes: 100 additions & 0 deletions DailyPic/Components/Tasks.swift
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)
}
}
2 changes: 1 addition & 1 deletion utils/imageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class ImageManager: ObservableObject {
}

func shuffleIndex() {
if config?.toggles.shuffle_favorites_only ?? false && !favoriteImages.isEmpty {
if config?.toggles.shuffle_favorites_only ?? false && !favoriteImages.isEmpty{
let image = favoriteImages.shuffled()[0]

// Search for the index of this image in the images array
Expand Down

0 comments on commit 3dc5729

Please sign in to comment.