From 2a326600ecc0aa3bc51479e8bb45501557986fe2 Mon Sep 17 00:00:00 2001 From: Brad Root Date: Sun, 30 Jan 2022 19:30:38 -0800 Subject: [PATCH] replace set current time notification with a completion handler --- Shared/Scene/Animations.swift | 8 ++++---- Shared/Scene/ClockController.swift | 23 ++++++++++------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/Shared/Scene/Animations.swift b/Shared/Scene/Animations.swift index aeeac7e..b85f2f5 100644 --- a/Shared/Scene/Animations.swift +++ b/Shared/Scene/Animations.swift @@ -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: @@ -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" @@ -176,7 +176,7 @@ class Animation { } - NotificationCenter.default.post(name: NSNotification.Name("SetCurrentTime"), object: nil, userInfo: ["time": timeString]) + timeSetCompletionHandler(timeString) return SKAction.group(actions) } diff --git a/Shared/Scene/ClockController.swift b/Shared/Scene/ClockController.swift index 5f36f44..9796965 100644 --- a/Shared/Scene/ClockController.swift +++ b/Shared/Scene/ClockController.swift @@ -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() { @@ -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() @@ -218,7 +207,7 @@ class ClockController { // MARK: - Animations - @objc func animationCompleted() { + private func animationCompleted() { timeSinceLastAnimation = 0 animationsCompleted += 1 if animationsCompleted == 48 { @@ -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)