NetNewsWire/Evergreen/AppDefaults.swift

197 lines
4.3 KiB
Swift
Raw Normal View History

2017-09-23 03:37:25 +02:00
//
// AppDefaults.swift
// Evergreen
//
// Created by Brent Simmons on 9/22/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import AppKit
2017-09-23 03:37:25 +02:00
enum FontSize: Int {
case small = 0
case medium = 1
case large = 2
case veryLarge = 3
}
2017-09-23 03:37:25 +02:00
final class AppDefaults {
static let shared = AppDefaults()
2017-09-24 21:24:44 +02:00
struct Key {
2017-09-23 03:37:25 +02:00
static let firstRunDate = "firstRunDate"
static let sidebarFontSize = "sidebarFontSize"
static let timelineFontSize = "timelineFontSize"
static let timelineSortDirection = "timelineSortDirection"
2017-09-23 03:37:25 +02:00
static let detailFontSize = "detailFontSize"
static let openInBrowserInBackground = "openInBrowserInBackground"
// Hidden prefs
static let showTitleOnMainWindow = "KafasisTitleMode"
2017-09-23 03:37:25 +02:00
}
2017-09-24 21:24:44 +02:00
private let smallestFontSizeRawValue = FontSize.small.rawValue
private let largestFontSizeRawValue = FontSize.veryLarge.rawValue
2017-09-23 21:17:14 +02:00
let isFirstRun: Bool
var openInBrowserInBackground: Bool {
2017-09-23 03:37:25 +02:00
get {
return bool(for: Key.openInBrowserInBackground)
2017-09-23 03:37:25 +02:00
}
set {
setBool(for: Key.openInBrowserInBackground, newValue)
2017-09-23 03:37:25 +02:00
}
}
var sidebarFontSize: FontSize {
2017-09-23 03:37:25 +02:00
get {
return fontSize(for: Key.sidebarFontSize)
2017-09-23 03:37:25 +02:00
}
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)
2017-09-23 03:37:25 +02:00
}
}
var showTitleOnMainWindow: Bool {
return bool(for: Key.showTitleOnMainWindow)
}
var timelineSortDirection: ComparisonResult {
get {
return sortDirection(for: Key.timelineSortDirection)
}
set {
setSortDirection(for: Key.timelineSortDirection, newValue)
}
}
2017-09-24 21:24:44 +02:00
private init() {
2017-09-23 03:37:25 +02:00
2017-09-24 21:24:44 +02:00
AppDefaults.registerDefaults()
let firstRunDate = UserDefaults.standard.object(forKey: Key.firstRunDate) as? Date
if firstRunDate == nil {
2017-09-23 21:17:14 +02:00
self.isFirstRun = true
self.firstRunDate = Date()
}
else {
self.isFirstRun = false
}
2017-09-23 03:37:25 +02:00
}
static func actualFontSize(for fontSize: FontSize) -> CGFloat {
switch fontSize {
case .small:
return NSFont.systemFontSize
case .medium:
return actualFontSize(for: .small) + 1.0
case .large:
return actualFontSize(for: .medium) + 4.0
case .veryLarge:
return actualFontSize(for: .large) + 8.0
}
}
}
2017-09-23 03:37:25 +02:00
private extension AppDefaults {
2017-09-23 03:37:25 +02:00
var firstRunDate: Date? {
get {
return date(for: Key.firstRunDate)
}
set {
2017-09-24 21:24:44 +02:00
setDate(for: Key.firstRunDate, newValue)
}
}
2017-09-23 03:37:25 +02:00
2017-09-24 21:24:44 +02:00
static func registerDefaults() {
let defaults = [Key.sidebarFontSize: FontSize.medium.rawValue, Key.timelineFontSize: FontSize.medium.rawValue, Key.detailFontSize: FontSize.medium.rawValue, Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue]
UserDefaults.standard.register(defaults: defaults)
2017-09-23 03:37:25 +02:00
}
func fontSize(for key: String) -> FontSize {
2017-09-23 03:37:25 +02:00
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)
}
2017-09-23 03:37:25 +02:00
func bool(for key: String) -> Bool {
return UserDefaults.standard.bool(forKey: key)
}
func setBool(for key: String, _ flag: Bool) {
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)
}
2017-09-23 03:37:25 +02:00
func date(for key: String) -> Date? {
return UserDefaults.standard.object(forKey: key) as? Date
}
2017-09-24 21:24:44 +02:00
func setDate(for key: String, _ date: Date?) {
2017-09-23 03:37:25 +02:00
UserDefaults.standard.set(date, forKey: key)
}
func sortDirection(for key:String) -> ComparisonResult {
let rawInt = int(for: key)
if rawInt == ComparisonResult.orderedAscending.rawValue {
return .orderedAscending
}
return .orderedDescending
}
func setSortDirection(for key: String, _ value: ComparisonResult) {
if value == .orderedAscending {
setInt(for: key, ComparisonResult.orderedAscending.rawValue)
}
else {
setInt(for: key, ComparisonResult.orderedDescending.rawValue)
}
}
2017-09-23 03:37:25 +02:00
}