|
| 1 | +// |
| 2 | +// ContinuedProcessingManager.swift |
| 3 | +// StikJIT |
| 4 | +// |
| 5 | +// Created by se2crid on 18/12/25. |
| 6 | +// |
| 7 | + |
| 8 | +import BackgroundTasks |
| 9 | +import Foundation |
| 10 | + |
| 11 | +final class ContinuedProcessingManager { |
| 12 | + static let shared = ContinuedProcessingManager() |
| 13 | + private let handler: ContinuedProcessingHandling |
| 14 | + |
| 15 | + private init() { |
| 16 | + if #available(iOS 26.0, *) { |
| 17 | + handler = ModernContinuedProcessingHandler() |
| 18 | + } else { |
| 19 | + handler = NoopContinuedProcessingHandler() |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + var isSupported: Bool { handler.isSupported } |
| 24 | + |
| 25 | + func configureIfNeeded() { |
| 26 | + handler.configureIfNeeded() |
| 27 | + } |
| 28 | + |
| 29 | + func cancelPendingTasks() { |
| 30 | + handler.cancelPendingTasks() |
| 31 | + } |
| 32 | + |
| 33 | + func begin(title: String, subtitle: String) { |
| 34 | + handler.begin(title: title, subtitle: subtitle) |
| 35 | + } |
| 36 | + |
| 37 | + func updateProgress(_ fraction: Double) { |
| 38 | + handler.updateProgress(fraction) |
| 39 | + } |
| 40 | + |
| 41 | + func finish(success: Bool) { |
| 42 | + handler.finish(success: success) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +private protocol ContinuedProcessingHandling: AnyObject { |
| 47 | + var isSupported: Bool { get } |
| 48 | + func configureIfNeeded() |
| 49 | + func cancelPendingTasks() |
| 50 | + func begin(title: String, subtitle: String) |
| 51 | + func updateProgress(_ fraction: Double) |
| 52 | + func finish(success: Bool) |
| 53 | +} |
| 54 | + |
| 55 | +private final class NoopContinuedProcessingHandler: ContinuedProcessingHandling { |
| 56 | + var isSupported: Bool { false } |
| 57 | + func configureIfNeeded() {} |
| 58 | + func cancelPendingTasks() {} |
| 59 | + func begin(title: String, subtitle: String) {} |
| 60 | + func updateProgress(_ fraction: Double) {} |
| 61 | + func finish(success: Bool) {} |
| 62 | +} |
| 63 | + |
| 64 | +@available(iOS 26.0, *) |
| 65 | +private final class ModernContinuedProcessingHandler: ContinuedProcessingHandling { |
| 66 | + private let scheduler = BGTaskScheduler.shared |
| 67 | + private let taskIdentifier: String |
| 68 | + private var didRegister = false |
| 69 | + private var activeTask: BGContinuedProcessingTask? |
| 70 | + private let queue = DispatchQueue( |
| 71 | + label: "com.stikdebug.continuedProcessing", |
| 72 | + qos: .utility) |
| 73 | + private var pendingMetadata: (title: String, subtitle: String)? |
| 74 | + |
| 75 | + init() { |
| 76 | + let bundleID = Bundle.main.bundleIdentifier ?? "com.stik.sj" |
| 77 | + taskIdentifier = "\(bundleID).continuedProcessingTask.script" |
| 78 | + } |
| 79 | + |
| 80 | + var isSupported: Bool { true } |
| 81 | + |
| 82 | + func configureIfNeeded() { |
| 83 | + guard !didRegister else { return } |
| 84 | + scheduler.register(forTaskWithIdentifier: taskIdentifier, using: nil) { [weak self] task in |
| 85 | + guard let continuedTask = task as? BGContinuedProcessingTask else { |
| 86 | + task.setTaskCompleted(success: false) |
| 87 | + return |
| 88 | + } |
| 89 | + self?.handle(task: continuedTask) |
| 90 | + } |
| 91 | + didRegister = true |
| 92 | + } |
| 93 | + |
| 94 | + func begin(title: String, subtitle: String) { |
| 95 | + guard UserDefaults.standard.bool(forKey: UserDefaults.Keys.enableContinuedProcessing) else { |
| 96 | + return |
| 97 | + } |
| 98 | + configureIfNeeded() |
| 99 | + var reserved = false |
| 100 | + queue.sync { |
| 101 | + if activeTask == nil && pendingMetadata == nil { |
| 102 | + pendingMetadata = (title: title, subtitle: subtitle) |
| 103 | + reserved = true |
| 104 | + } |
| 105 | + } |
| 106 | + guard reserved else { return } |
| 107 | + // Clear any stale request that might block new submissions. |
| 108 | + scheduler.cancel(taskRequestWithIdentifier: taskIdentifier) |
| 109 | + let request = BGContinuedProcessingTaskRequest( |
| 110 | + identifier: taskIdentifier, |
| 111 | + title: title, |
| 112 | + subtitle: subtitle) |
| 113 | + request.strategy = .queue |
| 114 | + do { |
| 115 | + try scheduler.submit(request) |
| 116 | + LogManager.shared.addInfoLog("Requested continued processing: \(title)") |
| 117 | + } catch { |
| 118 | + LogManager.shared.addWarningLog( |
| 119 | + "Unable to request continued processing: \(error.localizedDescription)") |
| 120 | + queue.async { [weak self] in |
| 121 | + self?.pendingMetadata = nil |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + func cancelPendingTasks() { |
| 127 | + queue.async { [weak self] in |
| 128 | + guard let self else { return } |
| 129 | + if let task = activeTask { |
| 130 | + task.setTaskCompleted(success: false) |
| 131 | + activeTask = nil |
| 132 | + } |
| 133 | + pendingMetadata = nil |
| 134 | + scheduler.cancel(taskRequestWithIdentifier: taskIdentifier) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + func updateProgress(_ fraction: Double) { |
| 139 | + queue.async { [weak self] in |
| 140 | + guard let task = self?.activeTask else { return } |
| 141 | + let clamped = max(0.0, min(1.0, fraction)) |
| 142 | + task.progress.totalUnitCount = max(task.progress.totalUnitCount, 100) |
| 143 | + task.progress.completedUnitCount = Int64(Double(task.progress.totalUnitCount) * clamped) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + func finish(success: Bool) { |
| 148 | + queue.async { [weak self] in |
| 149 | + guard let self else { return } |
| 150 | + if let task = self.activeTask { |
| 151 | + task.progress.completedUnitCount = task.progress.totalUnitCount |
| 152 | + task.setTaskCompleted(success: success) |
| 153 | + self.activeTask = nil |
| 154 | + } else if pendingMetadata != nil { |
| 155 | + scheduler.cancel(taskRequestWithIdentifier: taskIdentifier) |
| 156 | + } |
| 157 | + pendingMetadata = nil |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private func handle(task: BGContinuedProcessingTask) { |
| 162 | + queue.async { [weak self] in |
| 163 | + guard let self else { return } |
| 164 | + activeTask = task |
| 165 | + if let metadata = pendingMetadata { |
| 166 | + task.updateTitle(metadata.title, subtitle: metadata.subtitle) |
| 167 | + } |
| 168 | + if task.progress.totalUnitCount == 0 { |
| 169 | + task.progress.totalUnitCount = 100 |
| 170 | + } |
| 171 | + task.progress.completedUnitCount = 1 |
| 172 | + task.expirationHandler = { [weak self] in |
| 173 | + self?.handleExpiration() |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private func handleExpiration() { |
| 179 | + LogManager.shared.addWarningLog("Continued processing expired early") |
| 180 | + finish(success: false) |
| 181 | + } |
| 182 | +} |
0 commit comments