Last active
October 26, 2019 03:30
-
-
Save tera-ny/2bc1c9a3012cd1bce15b3648f95dea53 to your computer and use it in GitHub Desktop.
Scheduler
This file contains hidden or 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
import UIKit | |
class HomeViewController: UIViewController { | |
private let uniqueKey: String = "hkjladjgaldkjakjl" | |
... | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewDidLoad() | |
Scheduler.shared.monitors.updateValue( self, forKey: uniqueKey) | |
} | |
override func viewWillDisappear(_ animated: Bool) { | |
super.viewDidLoad() | |
Scheduler.shared.monitors.removeValue(forKey: String) | |
} | |
} | |
extension HomeViewController: ScheduleMonitor { | |
func update(dateOfHHmmFormat: String) { | |
... | |
} | |
} |
This file contains hidden or 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
protocol ScheduleMonitor { | |
func update(dateOfHHmmFormat: String) | |
} |
This file contains hidden or 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
class Scheduler { | |
var monitors: [String: ScheduleMonitor] | |
private let formatter: DateFormatter = { | |
let _HHmmFormatter = DateFormatter() | |
formatter.dateformat = "HH:mm" | |
return formatter | |
}() | |
static let shared = Scheduler() | |
init(monitors: [String:ScheduleMonitor] = [:]) { | |
self.monitors = monitors | |
} | |
@objc private func updateCurrentTime() { | |
let date = Date() | |
let currentTimeText = _HHmmFormatter.string(date: date) | |
self.monitors.foreach { monitor in | |
monitor.update(dateOfHHmmFormat: currentTimeText) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment