Repo created
This commit is contained in:
parent
4af19165ec
commit
68073add76
12458 changed files with 12350765 additions and 2 deletions
|
|
@ -0,0 +1,57 @@
|
|||
@objc(MWMBackgroundFetchScheduler)
|
||||
final class BackgroundFetchScheduler: NSObject {
|
||||
typealias FetchResultHandler = (UIBackgroundFetchResult) -> Void
|
||||
|
||||
private let completionHandler: FetchResultHandler
|
||||
private let tasks: [BackgroundFetchTask]
|
||||
private var tasksLeft: Int
|
||||
private var bestResultSoFar = UIBackgroundFetchResult.noData
|
||||
|
||||
@objc init(tasks: [BackgroundFetchTask], completionHandler: @escaping FetchResultHandler) {
|
||||
self.tasks = tasks
|
||||
self.completionHandler = completionHandler
|
||||
tasksLeft = tasks.count
|
||||
super.init()
|
||||
}
|
||||
|
||||
@objc func run() {
|
||||
fullfillFrameworkRequirements()
|
||||
|
||||
let completionHandler: FetchResultHandler = { [weak self] result in
|
||||
self?.finishTask(result: result)
|
||||
}
|
||||
|
||||
tasks.forEach { $0.start(completion: completionHandler) }
|
||||
}
|
||||
|
||||
private func fullfillFrameworkRequirements() {
|
||||
minFrameworkTypeRequired().create()
|
||||
}
|
||||
|
||||
private func minFrameworkTypeRequired() -> BackgroundFetchTaskFrameworkType {
|
||||
return tasks.reduce(.none) { max($0, $1.frameworkType) }
|
||||
}
|
||||
|
||||
private func finishTask(result: UIBackgroundFetchResult) {
|
||||
updateFetchResult(result)
|
||||
tasksLeft -= 1
|
||||
if tasksLeft <= 0 {
|
||||
completionHandler(bestResultSoFar)
|
||||
}
|
||||
}
|
||||
|
||||
private func updateFetchResult(_ result: UIBackgroundFetchResult) {
|
||||
if resultPriority(bestResultSoFar) < resultPriority(result) {
|
||||
bestResultSoFar = result
|
||||
}
|
||||
}
|
||||
|
||||
private func resultPriority(_ result: UIBackgroundFetchResult) -> Int {
|
||||
switch result {
|
||||
case .newData: return 3
|
||||
case .noData: return 1
|
||||
case .failed: return 2
|
||||
@unknown default: fatalError("Unexpected case in UIBackgroundFetchResult switch")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
@objc class BackgroundFetchTask: NSObject {
|
||||
var frameworkType: BackgroundFetchTaskFrameworkType { return .none }
|
||||
|
||||
private var backgroundTaskIdentifier = UIBackgroundTaskIdentifier.invalid
|
||||
|
||||
private var completionHandler: BackgroundFetchScheduler.FetchResultHandler?
|
||||
|
||||
func start(completion: @escaping BackgroundFetchScheduler.FetchResultHandler) {
|
||||
completionHandler = completion
|
||||
backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName:description,
|
||||
expirationHandler: {
|
||||
self.finish(.failed)
|
||||
})
|
||||
if backgroundTaskIdentifier != UIBackgroundTaskIdentifier.invalid { fire() }
|
||||
}
|
||||
|
||||
fileprivate func fire() {
|
||||
finish(.failed)
|
||||
}
|
||||
|
||||
fileprivate func finish(_ result: UIBackgroundFetchResult) {
|
||||
guard backgroundTaskIdentifier != UIBackgroundTaskIdentifier.invalid else { return }
|
||||
UIApplication.shared.endBackgroundTask(UIBackgroundTaskIdentifier(rawValue: backgroundTaskIdentifier.rawValue))
|
||||
backgroundTaskIdentifier = UIBackgroundTaskIdentifier.invalid
|
||||
completionHandler?(result)
|
||||
}
|
||||
}
|
||||
|
||||
@objc(MWMBackgroundEditsUpload)
|
||||
final class BackgroundEditsUpload: BackgroundFetchTask {
|
||||
override fileprivate func fire() {
|
||||
MWMEditorHelper.uploadEdits(self.finish)
|
||||
}
|
||||
|
||||
override var description: String {
|
||||
return "Edits upload"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
@objc enum BackgroundFetchTaskFrameworkType: Int {
|
||||
case none
|
||||
case full
|
||||
|
||||
func create() {
|
||||
switch self {
|
||||
case .none: return
|
||||
case .full: FrameworkHelper.createFramework()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BackgroundFetchTaskFrameworkType: Equatable {
|
||||
static func ==(lhs: BackgroundFetchTaskFrameworkType, rhs: BackgroundFetchTaskFrameworkType) -> Bool {
|
||||
return lhs.rawValue == rhs.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
extension BackgroundFetchTaskFrameworkType: Comparable {
|
||||
static func <(lhs: BackgroundFetchTaskFrameworkType, rhs: BackgroundFetchTaskFrameworkType) -> Bool {
|
||||
return lhs.rawValue < rhs.rawValue
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue