From 9c81f0b183cf09fdff7476af3716238d84b042c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Bu=CC=88nz?= Date: Wed, 8 May 2019 17:44:20 +0200 Subject: [PATCH] Notification Static * collect Notifications in an extension * update NSNotification to Notification * adopt Notifications to new static lets --- CriticalMass/ChatManager.swift | 2 +- CriticalMass/LocationManager.swift | 4 ++-- CriticalMass/MapViewController.swift | 6 +++--- CriticalMass/MemoryDataStore.swift | 4 ++-- CriticalMass/Notification+Static.swift | 6 +++++- CriticalMass/Preferences.swift | 2 +- CriticalMassTests/MemoryDataStoreTests.swift | 8 ++++---- 7 files changed, 18 insertions(+), 14 deletions(-) diff --git a/CriticalMass/ChatManager.swift b/CriticalMass/ChatManager.swift index 9fc58d5d..c718963b 100644 --- a/CriticalMass/ChatManager.swift +++ b/CriticalMass/ChatManager.swift @@ -24,7 +24,7 @@ class ChatManager { init(requestManager: RequestManager) { self.requestManager = requestManager - NotificationCenter.default.addObserver(self, selector: #selector(didReceiveMessages(notification:)), name: NSNotification.Name("chatMessagesReceived"), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(didReceiveMessages(notification:)), name: Notification.chatMessagesReceived, object: nil) } @objc private func didReceiveMessages(notification: Notification) { diff --git a/CriticalMass/LocationManager.swift b/CriticalMass/LocationManager.swift index 62d9de38..bc6fec5b 100644 --- a/CriticalMass/LocationManager.swift +++ b/CriticalMass/LocationManager.swift @@ -40,7 +40,7 @@ class LocationManager: NSObject, CLLocationManagerDelegate, LocationProvider { } if let location = currentLocation { didSetInitialLocation = true - NotificationCenter.default.post(name: NSNotification.Name("initialGpsDataReceived"), object: location) + NotificationCenter.default.post(name: Notification.initialGpsDataReceived, object: location) } } get { @@ -95,6 +95,6 @@ class LocationManager: NSObject, CLLocationManagerDelegate, LocationProvider { } func locationManager(_: CLLocationManager, didChangeAuthorization _: CLAuthorizationStatus) { - NotificationCenter.default.post(name: NSNotification.Name("gpsStateChanged"), object: type(of: self).accessPermission) + NotificationCenter.default.post(name: Notification.gpsStateChanged, object: type(of: self).accessPermission) } } diff --git a/CriticalMass/MapViewController.swift b/CriticalMass/MapViewController.swift index 54b4c758..8f347113 100644 --- a/CriticalMass/MapViewController.swift +++ b/CriticalMass/MapViewController.swift @@ -106,9 +106,9 @@ class MapViewController: UIViewController { } private func configureNotifications() { - NotificationCenter.default.addObserver(self, selector: #selector(positionsDidChange(notification:)), name: NSNotification.Name("positionOthersChanged"), object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(didReceiveInitialLocation(notification:)), name: NSNotification.Name("initialGpsDataReceived"), object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(updateGPSDisabledOverlayVisibility), name: NSNotification.Name("gpsStateChanged"), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(positionsDidChange(notification:)), name: Notification.positionOthersChanged, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(didReceiveInitialLocation(notification:)), name: Notification.initialGpsDataReceived, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(updateGPSDisabledOverlayVisibility), name: Notification.gpsStateChanged, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: Notification.themeDidChange, object: nil) } diff --git a/CriticalMass/MemoryDataStore.swift b/CriticalMass/MemoryDataStore.swift index 50caa084..06f1c486 100644 --- a/CriticalMass/MemoryDataStore.swift +++ b/CriticalMass/MemoryDataStore.swift @@ -11,10 +11,10 @@ class MemoryDataStore: DataStore { private var lastKnownResponse: ApiResponse? { didSet { if oldValue?.locations != lastKnownResponse?.locations { - NotificationCenter.default.post(name: NSNotification.Name("positionOthersChanged"), object: lastKnownResponse) + NotificationCenter.default.post(name: Notification.positionOthersChanged, object: lastKnownResponse) } if oldValue?.chatMessages != lastKnownResponse?.chatMessages { - NotificationCenter.default.post(name: NSNotification.Name("chatMessagesReceived"), object: lastKnownResponse) + NotificationCenter.default.post(name: Notification.chatMessagesReceived, object: lastKnownResponse) } } } diff --git a/CriticalMass/Notification+Static.swift b/CriticalMass/Notification+Static.swift index f90076e9..343d7ccb 100644 --- a/CriticalMass/Notification+Static.swift +++ b/CriticalMass/Notification+Static.swift @@ -1,5 +1,5 @@ // -// NSNotification+Static.swift +// Notification+Static.swift // CriticalMaps // // Created by Malte Bünz on 19.04.19. @@ -10,4 +10,8 @@ import Foundation extension Notification { static let themeDidChange = Notification.Name(rawValue: "themeDidChange") + static let initialGpsDataReceived = Notification.Name(rawValue: "initialGpsDataReceived") + static let gpsStateChanged = Notification.Name(rawValue: "gpsStateChanged") + static let positionOthersChanged = Notification.Name(rawValue: "positionOthersChanged") + static let chatMessagesReceived = Notification.Name(rawValue: "chatMessagesReceived") } diff --git a/CriticalMass/Preferences.swift b/CriticalMass/Preferences.swift index f44adb67..be10b6f3 100644 --- a/CriticalMass/Preferences.swift +++ b/CriticalMass/Preferences.swift @@ -14,7 +14,7 @@ class Preferences { } set { UserDefaults.standard.set(newValue, forKey: #function) - NotificationCenter.default.post(name: NSNotification.Name("gpsStateChanged"), object: newValue) + NotificationCenter.default.post(name: Notification.gpsStateChanged, object: newValue) } } diff --git a/CriticalMassTests/MemoryDataStoreTests.swift b/CriticalMassTests/MemoryDataStoreTests.swift index 49c00a66..5a9b951f 100644 --- a/CriticalMassTests/MemoryDataStoreTests.swift +++ b/CriticalMassTests/MemoryDataStoreTests.swift @@ -12,7 +12,7 @@ class MemoryDataStoreTests: XCTestCase { func testNotificationAfterStoringLocations() { let store = MemoryDataStore() let expectedObject = ApiResponse(locations: ["a": Location(longitude: 100, latitude: 100, timestamp: 100, name: "hello", color: "world")], chatMessages: [:]) - let notificationName = NSNotification.Name("positionOthersChanged") + let notificationName = Notification.positionOthersChanged let exp = expectation(forNotification: notificationName, object: nil) { (notification) -> Bool in notification.object as AnyObject as! ApiResponse == expectedObject } @@ -24,7 +24,7 @@ class MemoryDataStoreTests: XCTestCase { func testNotificationAfterStoringChatMessage() { let store = MemoryDataStore() let expectedObject = ApiResponse(locations: [:], chatMessages: ["b": ChatMessage(message: "Hello", timestamp: 1000)]) - let notificationName = NSNotification.Name("positionOthersChanged") + let notificationName = Notification.positionOthersChanged let exp = expectation(forNotification: notificationName, object: nil) { (notification) -> Bool in notification.object as AnyObject as! ApiResponse == expectedObject } @@ -36,7 +36,7 @@ class MemoryDataStoreTests: XCTestCase { func testNoNoDoublicatedNotificationAfterStoringOldLocations() { let store = MemoryDataStore() let expectedObject = ApiResponse(locations: ["a": Location(longitude: 100, latitude: 100, timestamp: 100, name: "hello", color: "world")], chatMessages: [:]) - let notificationName = NSNotification.Name("positionOthersChanged") + let notificationName = Notification.positionOthersChanged var notificationCount = 0 let exp = expectation(forNotification: notificationName, object: nil) { (notification) -> Bool in notificationCount += 1 @@ -54,7 +54,7 @@ class MemoryDataStoreTests: XCTestCase { let store = MemoryDataStore() let expectedObject = ApiResponse(locations: [:], chatMessages: ["b": ChatMessage(message: "Hello", timestamp: 1000)]) var notificationCount = 0 - let notificationName = NSNotification.Name("positionOthersChanged") + let notificationName = Notification.positionOthersChanged let exp = expectation(forNotification: notificationName, object: nil) { (notification) -> Bool in notificationCount += 1 return notification.object as AnyObject as! ApiResponse == expectedObject