Javier Cicchelli 9bcdaa697b [Setup] Basic project structure (#1)
This PR contains all the work related to setting up this project as required to implement the [Assignment](https://repo.rock-n-code.com/rock-n-code/deep-linking-assignment/wiki/Assignment) on top, as intended.

To summarise this work:
- [x] created a new **Xcode** project;
- [x] cloned the `Wikipedia` app and inserted it into the **Xcode** project;
- [x] created the `Locations` app and also, its `Libraries` package;
- [x] created the `Shared` package to share dependencies between the apps;
- [x] added a `Makefile` file and implemented some **environment** and **help** commands.

Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Reviewed-on: rock-n-code/deep-linking-assignment#1
2023-04-08 18:37:13 +00:00

53 lines
1.6 KiB
Swift

import Foundation
@objc(WMFPeriodicWorker) public protocol PeriodicWorker: NSObjectProtocol {
func doPeriodicWork(_ completion: @escaping () -> Void)
}
@objc(WMFPeriodicWorkerController) public class PeriodicWorkerController: WorkerController {
let interval: TimeInterval
let initialDelay: TimeInterval
let leeway: TimeInterval
lazy var workTimer: RepeatingTimer = {
assert(Thread.isMainThread)
return RepeatingTimer(interval, afterDelay: initialDelay, leeway: leeway) { [weak self] in
self?.doPeriodicWork()
}
}()
@objc(initWithInterval:initialDelay:leeway:) public required init(_ interval: TimeInterval, initialDelay: TimeInterval, leeway: TimeInterval) {
self.interval = interval
self.initialDelay = initialDelay
self.leeway = leeway
}
var workers = [PeriodicWorker]()
@objc public func add(_ worker: PeriodicWorker) {
workers.append(worker)
}
@objc public func start() {
workTimer.resume()
}
@objc public func stop() {
workTimer.pause()
}
@objc public func doPeriodicWork(_ completion: (() -> Void)? = nil) {
let identifier = UUID().uuidString
delegate?.workerControllerWillStart(self, workWithIdentifier: identifier)
workers.asyncForEach({ (worker, completion) in
worker.doPeriodicWork(completion)
}) { [weak self] () in
completion?()
guard let strongSelf = self else {
return
}
strongSelf.delegate?.workerControllerDidEnd(strongSelf, workWithIdentifier: identifier)
}
}
}