From d2c445e4d69a1d87993aafb6d470e3908e2afc49 Mon Sep 17 00:00:00 2001 From: Sam Soffes Date: Sat, 24 Mar 2018 09:59:27 -0700 Subject: [PATCH] Refactor models --- Clock/Classes/BN0021.swift | 4 ++++ Clock/Classes/BN0032.swift | 4 ++++ Clock/Classes/BN0111.swift | 4 ++++ Clock/Classes/ClockView.swift | 4 ++++ Clock/Classes/MainView.swift | 2 +- Clock/Classes/Preferences.swift | 24 ++++++++++++------------ 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Clock/Classes/BN0021.swift b/Clock/Classes/BN0021.swift index c96ed70..11371f4 100644 --- a/Clock/Classes/BN0021.swift +++ b/Clock/Classes/BN0021.swift @@ -72,6 +72,10 @@ final class BN0021: ClockView { // MARK: - ClockView + override class var modelName: String { + return "BN0021" + } + override var styleName: String { set { style = Style(rawValue: newValue) ?? Style.default diff --git a/Clock/Classes/BN0032.swift b/Clock/Classes/BN0032.swift index 4bcd582..e49fd63 100644 --- a/Clock/Classes/BN0032.swift +++ b/Clock/Classes/BN0032.swift @@ -67,6 +67,10 @@ final class BN0032: ClockView { // MARK: - ClockView + override class var modelName: String { + return "BN0032" + } + override var styleName: String { set { style = Style(rawValue: newValue) ?? Style.default diff --git a/Clock/Classes/BN0111.swift b/Clock/Classes/BN0111.swift index 2907d70..1bf0b3a 100644 --- a/Clock/Classes/BN0111.swift +++ b/Clock/Classes/BN0111.swift @@ -87,6 +87,10 @@ final class BN0111: ClockView { // MARK: - ClockView + override class var modelName: String { + return "BN0111" + } + override var styleName: String { set { style = Style(rawValue: newValue) ?? Style.default diff --git a/Clock/Classes/ClockView.swift b/Clock/Classes/ClockView.swift index 10d2e4e..8300671 100644 --- a/Clock/Classes/ClockView.swift +++ b/Clock/Classes/ClockView.swift @@ -5,6 +5,10 @@ class ClockView: NSView { // MARK: - Properties + class var modelName: String { + fatalError("Unimplemented") + } + var drawsLogo = false var logoImage: NSImage? var clockFrame: CGRect { diff --git a/Clock/Classes/MainView.swift b/Clock/Classes/MainView.swift index 3c868b2..034c73c 100644 --- a/Clock/Classes/MainView.swift +++ b/Clock/Classes/MainView.swift @@ -1,7 +1,7 @@ import AppKit import ScreenSaver -class MainView: ScreenSaverView { +final class MainView: ScreenSaverView { // MARK: - Properties diff --git a/Clock/Classes/Preferences.swift b/Clock/Classes/Preferences.swift index d03a181..6cb44e3 100644 --- a/Clock/Classes/Preferences.swift +++ b/Clock/Classes/Preferences.swift @@ -6,14 +6,6 @@ extension Notification.Name { static let ModelDidChange = Notification.Name(rawValue: "SAMClockModelDidChangeNotification") } -let models: [String: ClockView.Type] = [ - "BN0021": BN0021.self, - "BN0032": BN0032.self, - "BN0111": BN0111.self -] - -let defaultModel = BN0032.self - final class Preferences: NSObject { // MARK: - Types @@ -32,13 +24,21 @@ final class Preferences: NSObject { private let defaults: UserDefaults = ScreenSaverDefaults(forModuleWithName: Bundle(for: Preferences.self).bundleIdentifier!)! + private let models = [ + BN0021.self, + BN0032.self, + BN0111.self + ] + + private let defaultModel = BN0032.self + var model: ClockView.Type { - return models[modelName] ?? defaultModel + return models.first { $0.modelName == modelName } ?? defaultModel } @objc var modelName: String { get { - return defaults.string(forKey: DefaultsKey.model.key) ?? "BN0032" + return defaults.string(forKey: DefaultsKey.model.key) ?? defaultModel.modelName } set { @@ -81,8 +81,8 @@ final class Preferences: NSObject { override init() { defaults.register(defaults: [ - DefaultsKey.model.key: "BN0032", - DefaultsKey.style.key: "BKBKG", + DefaultsKey.model.key: defaultModel.modelName, + DefaultsKey.style.key: defaultModel.Style.default.rawValue, DefaultsKey.logo.key: false ]) }