Move code from Defaults to AppDefaults.

This commit is contained in:
Brent Simmons 2017-09-23 13:41:15 -07:00
parent 1ab282c23a
commit d3593683df
3 changed files with 77 additions and 75 deletions

View File

@ -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 = "<group>"; };
842E45DC1ED8C54B000A8B52 /* Browser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Browser.swift; path = Evergreen/Browser.swift; sourceTree = "<group>"; };
842E45DE1ED8C582000A8B52 /* Defaults.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Defaults.swift; path = Evergreen/Preferences/Defaults.swift; sourceTree = "<group>"; };
842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardDelegateProtocol.swift; sourceTree = "<group>"; };
842E45E41ED8C6B7000A8B52 /* MainWindowSplitView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainWindowSplitView.swift; sourceTree = "<group>"; };
842E45E61ED8C747000A8B52 /* DB5.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = DB5.plist; path = Evergreen/Resources/DB5.plist; sourceTree = "<group>"; };
@ -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 */,

View File

@ -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
}

View File

@ -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)!
}