Skip to content

Commit

Permalink
replace set current time notification with a completion handler
Browse files Browse the repository at this point in the history
  • Loading branch information
amiantos committed Jan 31, 2022
1 parent 37c8123 commit 2a32660
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
8 changes: 4 additions & 4 deletions Shared/Scene/Animations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class Animation {
animationType = animation
}

public func actions(clocks: [ClockNode], clusters: [ClusterNode], completionHandler: @escaping () -> Void) -> SKAction {
public func actions(clocks: [ClockNode], clusters: [ClusterNode], completionHandler: @escaping () -> Void, timeSetCompletionHandler: @escaping (String) -> Void) -> SKAction {

switch animationType {
case .spinBothHands:
return spinBothHands(clocks: clocks, completionHandler: completionHandler)
case .currentTimePrint:
return currentTimePrint(clusters: clusters, completionHandler: completionHandler)
return currentTimePrint(clusters: clusters, completionHandler: completionHandler, timeSetCompletionHandler: timeSetCompletionHandler)
case .currentTimeClock:
return currentTimeClock(clocks: clocks, completionHandler: completionHandler)
case .wait:
Expand Down Expand Up @@ -152,7 +152,7 @@ class Animation {
return SKAction.group(actions)
}

private func currentTimePrint(clusters: [ClusterNode], completionHandler: @escaping () -> Void) -> SKAction {
private func currentTimePrint(clusters: [ClusterNode], completionHandler: @escaping () -> Void, timeSetCompletionHandler: @escaping (String) -> Void) -> SKAction {
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hhmm"
Expand All @@ -176,7 +176,7 @@ class Animation {

}

NotificationCenter.default.post(name: NSNotification.Name("SetCurrentTime"), object: nil, userInfo: ["time": timeString])
timeSetCompletionHandler(timeString)

return SKAction.group(actions)
}
Expand Down
23 changes: 10 additions & 13 deletions Shared/Scene/ClockController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class ClockController {
Log.useEmoji = true

updateInterval = TimeInterval(1)

NotificationCenter.default.addObserver(self, selector: #selector(setCurrentTime(_:)), name: NSNotification.Name("SetCurrentTime"), object: nil)
}

public func start() {
Expand All @@ -69,15 +67,6 @@ class ClockController {

// MARK: - Timer

@objc func setCurrentTime(_ notification: NSNotification) {
if let time = notification.userInfo?["time"] as? String {
if time != lastTimeDisplayed {
Log.debug("New displayed time: \(time)")
lastTimeDisplayed = time
}
}
}

@objc func updateTime() {
let date = Date()
let dateFormatter = DateFormatter()
Expand Down Expand Up @@ -218,7 +207,7 @@ class ClockController {

// MARK: - Animations

@objc func animationCompleted() {
private func animationCompleted() {
timeSinceLastAnimation = 0
animationsCompleted += 1
if animationsCompleted == 48 {
Expand All @@ -234,8 +223,16 @@ class ClockController {
}
}


private func setCurrentTime(time: String) {
if time != lastTimeDisplayed {
Log.debug("New displayed time: \(time)")
lastTimeDisplayed = time
}
}

private func run(_ animation: Animation) {
let actionGroup = animation.actions(clocks: clocks, clusters: clusters, completionHandler: animationCompleted)
let actionGroup = animation.actions(clocks: clocks, clusters: clusters, completionHandler: animationCompleted, timeSetCompletionHandler: setCurrentTime)
isAnimating = true
timeSinceLastAnimation = 0
scene?.run(actionGroup)
Expand Down

0 comments on commit 2a32660

Please sign in to comment.