diff --git a/Evergreen.xcodeproj/project.pbxproj b/Evergreen.xcodeproj/project.pbxproj index 1c2264e66..5c98b3da0 100644 --- a/Evergreen.xcodeproj/project.pbxproj +++ b/Evergreen.xcodeproj/project.pbxproj @@ -9,7 +9,6 @@ /* Begin PBXBuildFile section */ 842E45CE1ED8C308000A8B52 /* AppNotifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45CD1ED8C308000A8B52 /* AppNotifications.swift */; }; 842E45DD1ED8C54B000A8B52 /* Browser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45DC1ED8C54B000A8B52 /* Browser.swift */; }; - 842E45DF1ED8C582000A8B52 /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45DE1ED8C582000A8B52 /* Defaults.swift */; }; 842E45E31ED8C681000A8B52 /* KeyboardDelegateProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */; }; 842E45E51ED8C6B7000A8B52 /* MainWindowSplitView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45E41ED8C6B7000A8B52 /* MainWindowSplitView.swift */; }; 842E45E71ED8C747000A8B52 /* DB5.plist in Resources */ = {isa = PBXBuildFile; fileRef = 842E45E61ED8C747000A8B52 /* DB5.plist */; }; @@ -374,7 +373,6 @@ /* Begin PBXFileReference section */ 842E45CD1ED8C308000A8B52 /* AppNotifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppNotifications.swift; path = Evergreen/AppNotifications.swift; sourceTree = ""; }; 842E45DC1ED8C54B000A8B52 /* Browser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Browser.swift; path = Evergreen/Browser.swift; sourceTree = ""; }; - 842E45DE1ED8C582000A8B52 /* Defaults.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Defaults.swift; path = Evergreen/Preferences/Defaults.swift; sourceTree = ""; }; 842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardDelegateProtocol.swift; sourceTree = ""; }; 842E45E41ED8C6B7000A8B52 /* MainWindowSplitView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainWindowSplitView.swift; sourceTree = ""; }; 842E45E61ED8C747000A8B52 /* DB5.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = DB5.plist; path = Evergreen/Resources/DB5.plist; sourceTree = ""; }; @@ -475,7 +473,6 @@ isa = PBXGroup; children = ( 849A97A41ED9F94D007D329B /* Preferences.storyboard */, - 842E45DE1ED8C582000A8B52 /* Defaults.swift */, 849A97841ED9ECCD007D329B /* PreferencesWindowController.swift */, ); name = Preferences; @@ -1161,7 +1158,6 @@ 849A97981ED9EFAA007D329B /* Node-Extensions.swift in Sources */, 849A97531ED9EAC0007D329B /* AddFeedController.swift in Sources */, 849A97831ED9EC63007D329B /* StatusBarView.swift in Sources */, - 842E45DF1ED8C582000A8B52 /* Defaults.swift in Sources */, 849A97431ED9EAA9007D329B /* AddFolderWindowController.swift in Sources */, 849A97921ED9EF65007D329B /* IndeterminateProgressWindowController.swift in Sources */, 849A97801ED9EC42007D329B /* DetailViewController.swift in Sources */, diff --git a/Evergreen/AppDefaults.swift b/Evergreen/AppDefaults.swift index 0a5f64044..a45b1f21f 100644 --- a/Evergreen/AppDefaults.swift +++ b/Evergreen/AppDefaults.swift @@ -8,6 +8,13 @@ import Foundation +enum FontSize: Int { + case small = 0 + case medium = 1 + case large = 2 + case veryLarge = 3 +} + final class AppDefaults { static let shared = AppDefaults() @@ -25,14 +32,6 @@ final class AppDefaults { let isFirstRun: Bool - var firstRunDate: Date? { - get { - return date(for: Key.firstRunDate) - } - set { - setDate(for: key.firstRunDate, date) - } - } var openInBrowserInBackground: Bool { get { return bool(for: Key.openInBrowserInBackground) @@ -42,6 +41,33 @@ final class AppDefaults { } } + var sidebarFontSize: FontSize { + get { + return fontSize(for: Key.sidebarFontSize) + } + set { + setFontSize(for: Key.sidebarFontSize, newValue) + } + } + + var timelineFontSize: FontSize { + get { + return fontSize(for: Key.timelineFontSize) + } + set { + setFontSize(for: Key.timelineFontSize, newValue) + } + } + + var detailFontSize: FontSize { + get { + return fontSize(for: Key.detailFontSize) + } + set { + setFontSize(for: Key.detailFontSize, newValue) + } + } + init() { registerDefaults() @@ -53,17 +79,46 @@ final class AppDefaults { else { self.isFirstRun = false } - } - - func registerDefaults() { - - - } } private extension AppDefaults { + var firstRunDate: Date? { + get { + return date(for: Key.firstRunDate) + } + set { + setDate(for: key.firstRunDate, date) + } + } + + func registerDefaults() { + + let defaults = [Key.sidebarFontSize: FontSize.medium.rawValue, Key.timelineFontSize: FontSize.medium.rawValue, Key.detailFontSize: FontSize.medium.rawValue] + + UserDefaults.standard.register(defaults: defaults) + } + + func fontSize(for key: String) -> FontSize { + + static let smallestFontSizeRawValue = FontSize.small.rawValue + static let largestFontSizeRawValue = FontSize.veryLarge.rawValue + + var rawFontSize = int(for: key) + if rawFontSize < smallestFontSizeRawValue { + rawFontSize = smallestFontSizeRawValue + } + if rawFontSize > largestFontSizeRawValue { + rawFontSize = largestFontSizeRawValue + } + return FontSize(rawValue: rawFontSize)! + } + + func setFontSize(for key: String, _ fontSize: FontSize) { + setInt(for: key, fontSize.rawValue) + } + func bool(for key: String) -> Bool { return UserDefaults.standard.bool(forKey: key) } @@ -72,6 +127,14 @@ private extension AppDefaults { UserDefaults.standard.set(flag, forKey: key) } + func int(for key: String) -> Int { + return UserDefaults.standard.integer(forKey: key) + } + + func setInt(for key: String, _ x: Int) { + UserDefaults.standard.set(x, forKey: key) + } + func date(for key: String) -> Date? { return UserDefaults.standard.object(forKey: key) as? Date } diff --git a/Evergreen/Preferences/Defaults.swift b/Evergreen/Preferences/Defaults.swift deleted file mode 100644 index 1bb6999df..000000000 --- a/Evergreen/Preferences/Defaults.swift +++ /dev/null @@ -1,57 +0,0 @@ -// -// Defaults.swift -// Evergreen -// -// Created by Brent Simmons on 2/20/16. -// Copyright © 2016 Ranchero Software, LLC. All rights reserved. -// - -import Foundation - -final class AppDefaults { - - - -} - -extension AppDefaultsKey { - - static let sidebarFontSizeKVO = "values." + sidebarFontSize - static let timelineFontSizeKVO = "values." + timelineFontSize - static let detailFontSizeKVO = "values." + detailFontSize -} - -enum FontSize: Int { - case small = 0 - case medium = 1 - case large = 2 - case veryLarge = 3 -} - -private let smallestFontSizeRawValue = FontSize.small.rawValue -private let largestFontSizeRawValue = FontSize.veryLarge.rawValue - -func registerDefaults() { - - let defaults = [AppDefaultsKey.sidebarFontSize: FontSize.medium.rawValue, AppDefaultsKey.timelineFontSize: FontSize.medium.rawValue, AppDefaultsKey.detailFontSize: FontSize.medium.rawValue] - - UserDefaults.standard.register(defaults: defaults) -} - -func timelineFontSize() -> FontSize { - - return fontSizeForKey(TimelineFontSizeKey) -} - -private func fontSizeForKey(_ key: String) -> FontSize { - - var rawFontSize = UserDefaults.standard.integer(forKey: key) - if rawFontSize < smallestFontSizeRawValue { - rawFontSize = smallestFontSizeRawValue - } - if rawFontSize > largestFontSizeRawValue { - rawFontSize = largestFontSizeRawValue - } - return FontSize(rawValue: rawFontSize)! -} -