forked from redmilk/Safari-Page-Casher-Documents-Editor
-
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.
First part of background capabilities implementation
- Loading branch information
Showing
8 changed files
with
190 additions
and
11 deletions.
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
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
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
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
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
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,56 @@ | ||
// | ||
// SessionCleaner.swift | ||
// AirPrint | ||
// | ||
// Created by Danyl Timofeyev on 20.01.2022. | ||
// | ||
|
||
import Foundation | ||
import BackgroundTasks | ||
/// fire debug: | ||
/// e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.surf.devip"] | ||
class BackgroundScheduler { | ||
|
||
static let shared = BackgroundScheduler() | ||
|
||
var taskProcess: VoidClosure? | ||
var shouldRepeating: Bool = true | ||
|
||
private var taskIdentifierFromPlist: String! | ||
|
||
func register(taskIdentifier: String = "com.surf.devip") { | ||
taskIdentifierFromPlist = taskIdentifier | ||
BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifier, using: nil) { (task) in | ||
self.handleAppRefreshTask(task: task as! BGAppRefreshTask) | ||
} | ||
} | ||
|
||
func handleAppRefreshTask(task: BGAppRefreshTask) { | ||
task.expirationHandler = { | ||
task.setTaskCompleted(success: false) | ||
} | ||
/// task process | ||
//UserDefaults.standard.set("\(Date().timeIntervalSince1970)", forKey: "123") | ||
taskProcess?() | ||
/// | ||
task.setTaskCompleted(success: true) | ||
if shouldRepeating { | ||
scheduleBackgroundFetch() | ||
} | ||
} | ||
|
||
func scheduleBackgroundFetch(in seconds: Double = 60) { | ||
let task = BGAppRefreshTaskRequest(identifier: taskIdentifierFromPlist) | ||
//task.earliestBeginDate = Date(timeIntervalSinceNow: seconds) | ||
do { | ||
try BGTaskScheduler.shared.submit(task) | ||
} catch { | ||
Logger.log("Unable to submit task: \(error.localizedDescription)", type: .error) | ||
Logger.logError(error) | ||
} | ||
} | ||
|
||
func cancelPendingTask() { | ||
BGTaskScheduler.shared.cancelAllTaskRequests() | ||
} | ||
} |
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,62 @@ | ||
// | ||
// BackgroundTimer.swift | ||
// AirPrint | ||
// | ||
// Created by Danyl Timofeyev on 19.01.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
class BackgroundTimer { | ||
|
||
let timeInterval: TimeInterval | ||
|
||
init(timeInterval: TimeInterval) { | ||
self.timeInterval = timeInterval | ||
} | ||
|
||
private lazy var timer: DispatchSourceTimer = { | ||
let t = DispatchSource.makeTimerSource() | ||
t.schedule(deadline: .now() + self.timeInterval, repeating: self.timeInterval) | ||
t.setEventHandler(handler: { [weak self] in | ||
self?.eventHandler?() | ||
}) | ||
return t | ||
}() | ||
|
||
var eventHandler: (() -> Void)? | ||
|
||
private enum State { | ||
case suspended | ||
case resumed | ||
} | ||
|
||
private var state: State = .suspended | ||
|
||
deinit { | ||
timer.setEventHandler {} | ||
timer.cancel() | ||
/* | ||
If the timer is suspended, calling cancel without resuming | ||
triggers a crash. This is documented here https://forums.developer.apple.com/thread/15902 | ||
*/ | ||
resume() | ||
eventHandler = nil | ||
} | ||
|
||
func resume() { | ||
if state == .resumed { | ||
return | ||
} | ||
state = .resumed | ||
timer.resume() | ||
} | ||
|
||
func suspend() { | ||
if state == .suspended { | ||
return | ||
} | ||
state = .suspended | ||
timer.suspend() | ||
} | ||
} |
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,36 @@ | ||
// | ||
// TaskBackgroundKeeper.swift | ||
// AirPrint | ||
// | ||
// Created by Danyl Timofeyev on 19.01.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
class TaskBackgroundKeeper { | ||
private var backgroundTaskId: UIBackgroundTaskIdentifier? | ||
private var taskName: String = "task-that-should-continue-runing-in-background" | ||
private var queue = DispatchQueue.global(qos: .background) | ||
|
||
func executeAfterDelay(delay: TimeInterval, completion: @escaping (() -> Void)) { | ||
backgroundTaskId = UIApplication.shared.beginBackgroundTask( | ||
withName: taskName, | ||
expirationHandler: { [weak self] in | ||
if let taskId = self?.backgroundTaskId { | ||
UIApplication.shared.endBackgroundTask(taskId) | ||
} | ||
}) | ||
let startTime = Date() | ||
queue.async { | ||
while Date().timeIntervalSince(startTime) < delay { | ||
Thread.sleep(forTimeInterval: 1) //0.01) | ||
} | ||
DispatchQueue.main.async { [weak self] in | ||
completion() | ||
if let taskId = self?.backgroundTaskId { | ||
UIApplication.shared.endBackgroundTask(taskId) | ||
} | ||
} | ||
} | ||
} | ||
} |