Skip to content

Commit

Permalink
Refactor models
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Mar 24, 2018
1 parent 9028dfd commit d2c445e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Clock/Classes/BN0021.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Clock/Classes/BN0032.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Clock/Classes/BN0111.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Clock/Classes/ClockView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class ClockView: NSView {

// MARK: - Properties

class var modelName: String {
fatalError("Unimplemented")
}

var drawsLogo = false
var logoImage: NSImage?
var clockFrame: CGRect {
Expand Down
2 changes: 1 addition & 1 deletion Clock/Classes/MainView.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AppKit
import ScreenSaver

class MainView: ScreenSaverView {
final class MainView: ScreenSaverView {

// MARK: - Properties

Expand Down
24 changes: 12 additions & 12 deletions Clock/Classes/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
])
}
Expand Down

0 comments on commit d2c445e

Please sign in to comment.