-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate background update to use BGTaskScheduler (#26)
- Loading branch information
Showing
4 changed files
with
150 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// BackgroundFetchOp.swift | ||
// ampplayer | ||
// | ||
// Created by Aleksi Sitomaniemi on 15.10.2023. | ||
// Copyright © 2023 Aleksi Sitomaniemi. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import NotificationCenter | ||
import Alamofire | ||
|
||
class RefershLatestOperation: Operation { | ||
private var _executing = false | ||
private var _finished = false | ||
|
||
override private(set) var isExecuting: Bool { | ||
get { | ||
return _executing | ||
} | ||
set { | ||
willChangeValue(forKey: "isExecuting") | ||
_executing = newValue | ||
didChangeValue(forKey: "isExecuting") | ||
} | ||
} | ||
|
||
override private(set) var isFinished: Bool { | ||
get { | ||
return _finished | ||
} | ||
set { | ||
willChangeValue(forKey: "isFinished") | ||
_finished = newValue | ||
didChangeValue(forKey: "isFinished") | ||
} | ||
} | ||
|
||
override func start() { | ||
if isCancelled { | ||
isFinished = true | ||
return | ||
} | ||
|
||
isExecuting = true | ||
main() // Call your main method to perform the task | ||
} | ||
|
||
override func main() { | ||
// Send a REST request to refresh app contents | ||
log.debug("") | ||
let req = RESTRoutes.latestId | ||
AF.request(req).validate().responseString { resp in | ||
switch resp.result { | ||
case .failure: | ||
self.cancel() | ||
case .success(let str): | ||
guard let collectionSize = Int(str) else { | ||
self.completeOperation() | ||
return | ||
} | ||
log.info("Collection Size: \(collectionSize)") | ||
self.updateCollectionSize(size: collectionSize) | ||
self.completeOperation() | ||
} | ||
} | ||
} | ||
|
||
private func completeOperation() { | ||
isExecuting = false | ||
isFinished = true | ||
} | ||
|
||
func updateCollectionSize(size: Int) { | ||
log.debug("") | ||
settings.collectionSize = size | ||
let prevSize = settings.prevCollectionSize | ||
|
||
// Only fire the request once per a given collectionSize/diff | ||
if prevSize < size && settings.badgeCount < Constants.maxBadgeValue { | ||
let fmt = "Radio_Notification".l13n() | ||
let content = UNMutableNotificationContent() | ||
content.body = String.init(format: fmt, "\(settings.badgeCount)") | ||
content.categoryIdentifier = "newmodules" | ||
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 0.1, repeats: false) | ||
let req = UNNotificationRequest.init(identifier: "newmodules-usernotif", content: content, trigger: trigger) | ||
UNUserNotificationCenter.current().add(req, withCompletionHandler: nil) | ||
settings.prevCollectionSize = settings.collectionSize | ||
} | ||
} | ||
} |