NetNewsWire/Evergreen/AppDefaults.swift

146 lines
3.0 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 Foundation
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 detailFontSize = "detailFontSize"
static let openInBrowserInBackground = "openInBrowserInBackground"
}
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
}
}
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
}
}
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]
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)
}
}