Make AppDefaults a static struct instead of an object with a shared instance.
This commit is contained in:
parent
2279d10ca3
commit
94da6fc7de
|
@ -9,16 +9,13 @@
|
||||||
import AppKit
|
import AppKit
|
||||||
|
|
||||||
enum FontSize: Int {
|
enum FontSize: Int {
|
||||||
|
|
||||||
case small = 0
|
case small = 0
|
||||||
case medium = 1
|
case medium = 1
|
||||||
case large = 2
|
case large = 2
|
||||||
case veryLarge = 3
|
case veryLarge = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
final class AppDefaults {
|
struct AppDefaults {
|
||||||
|
|
||||||
static let shared = AppDefaults()
|
|
||||||
|
|
||||||
struct Key {
|
struct Key {
|
||||||
static let firstRunDate = "firstRunDate"
|
static let firstRunDate = "firstRunDate"
|
||||||
|
@ -33,12 +30,18 @@ final class AppDefaults {
|
||||||
static let showTitleOnMainWindow = "KafasisTitleMode"
|
static let showTitleOnMainWindow = "KafasisTitleMode"
|
||||||
}
|
}
|
||||||
|
|
||||||
private let smallestFontSizeRawValue = FontSize.small.rawValue
|
private static let smallestFontSizeRawValue = FontSize.small.rawValue
|
||||||
private let largestFontSizeRawValue = FontSize.veryLarge.rawValue
|
private static let largestFontSizeRawValue = FontSize.veryLarge.rawValue
|
||||||
|
|
||||||
let isFirstRun: Bool
|
static let isFirstRun: Bool = {
|
||||||
|
if let _ = UserDefaults.standard.object(forKey: Key.firstRunDate) as? Date {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
firstRunDate = Date()
|
||||||
|
return true
|
||||||
|
}()
|
||||||
|
|
||||||
var openInBrowserInBackground: Bool {
|
static var openInBrowserInBackground: Bool {
|
||||||
get {
|
get {
|
||||||
return bool(for: Key.openInBrowserInBackground)
|
return bool(for: Key.openInBrowserInBackground)
|
||||||
}
|
}
|
||||||
|
@ -47,7 +50,7 @@ final class AppDefaults {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var sidebarFontSize: FontSize {
|
static var sidebarFontSize: FontSize {
|
||||||
get {
|
get {
|
||||||
return fontSize(for: Key.sidebarFontSize)
|
return fontSize(for: Key.sidebarFontSize)
|
||||||
}
|
}
|
||||||
|
@ -56,7 +59,7 @@ final class AppDefaults {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var timelineFontSize: FontSize {
|
static var timelineFontSize: FontSize {
|
||||||
get {
|
get {
|
||||||
return fontSize(for: Key.timelineFontSize)
|
return fontSize(for: Key.timelineFontSize)
|
||||||
}
|
}
|
||||||
|
@ -65,7 +68,7 @@ final class AppDefaults {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var detailFontSize: FontSize {
|
static var detailFontSize: FontSize {
|
||||||
get {
|
get {
|
||||||
return fontSize(for: Key.detailFontSize)
|
return fontSize(for: Key.detailFontSize)
|
||||||
}
|
}
|
||||||
|
@ -74,11 +77,11 @@ final class AppDefaults {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var showTitleOnMainWindow: Bool {
|
static var showTitleOnMainWindow: Bool {
|
||||||
return bool(for: Key.showTitleOnMainWindow)
|
return bool(for: Key.showTitleOnMainWindow)
|
||||||
}
|
}
|
||||||
|
|
||||||
var timelineSortDirection: ComparisonResult {
|
static var timelineSortDirection: ComparisonResult {
|
||||||
get {
|
get {
|
||||||
return sortDirection(for: Key.timelineSortDirection)
|
return sortDirection(for: Key.timelineSortDirection)
|
||||||
}
|
}
|
||||||
|
@ -87,7 +90,7 @@ final class AppDefaults {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var mainWindowWidths: [Int]? {
|
static var mainWindowWidths: [Int]? {
|
||||||
get {
|
get {
|
||||||
return UserDefaults.standard.object(forKey: Key.mainWindowWidths) as? [Int]
|
return UserDefaults.standard.object(forKey: Key.mainWindowWidths) as? [Int]
|
||||||
}
|
}
|
||||||
|
@ -95,19 +98,11 @@ final class AppDefaults {
|
||||||
UserDefaults.standard.set(newValue, forKey: Key.mainWindowWidths)
|
UserDefaults.standard.set(newValue, forKey: Key.mainWindowWidths)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private init() {
|
|
||||||
|
|
||||||
AppDefaults.registerDefaults()
|
static func registerDefaults() {
|
||||||
|
let defaults: [String : Any] = [Key.sidebarFontSize: FontSize.medium.rawValue, Key.timelineFontSize: FontSize.medium.rawValue, Key.detailFontSize: FontSize.medium.rawValue, Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue, "NSScrollViewShouldScrollUnderTitlebar": false]
|
||||||
|
|
||||||
let firstRunDate = UserDefaults.standard.object(forKey: Key.firstRunDate) as? Date
|
UserDefaults.standard.register(defaults: defaults)
|
||||||
if firstRunDate == nil {
|
|
||||||
self.isFirstRun = true
|
|
||||||
self.firstRunDate = Date()
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
self.isFirstRun = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static func actualFontSize(for fontSize: FontSize) -> CGFloat {
|
static func actualFontSize(for fontSize: FontSize) -> CGFloat {
|
||||||
|
@ -127,7 +122,7 @@ final class AppDefaults {
|
||||||
|
|
||||||
private extension AppDefaults {
|
private extension AppDefaults {
|
||||||
|
|
||||||
var firstRunDate: Date? {
|
static var firstRunDate: Date? {
|
||||||
get {
|
get {
|
||||||
return date(for: Key.firstRunDate)
|
return date(for: Key.firstRunDate)
|
||||||
}
|
}
|
||||||
|
@ -136,14 +131,7 @@ private extension AppDefaults {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func registerDefaults() {
|
static func fontSize(for key: String) -> FontSize {
|
||||||
|
|
||||||
let defaults: [String : Any] = [Key.sidebarFontSize: FontSize.medium.rawValue, Key.timelineFontSize: FontSize.medium.rawValue, Key.detailFontSize: FontSize.medium.rawValue, Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue, "NSScrollViewShouldScrollUnderTitlebar": false]
|
|
||||||
|
|
||||||
UserDefaults.standard.register(defaults: defaults)
|
|
||||||
}
|
|
||||||
|
|
||||||
func fontSize(for key: String) -> FontSize {
|
|
||||||
|
|
||||||
// Punted till after 1.0.
|
// Punted till after 1.0.
|
||||||
return .medium
|
return .medium
|
||||||
|
@ -158,35 +146,35 @@ private extension AppDefaults {
|
||||||
// return FontSize(rawValue: rawFontSize)!
|
// return FontSize(rawValue: rawFontSize)!
|
||||||
}
|
}
|
||||||
|
|
||||||
func setFontSize(for key: String, _ fontSize: FontSize) {
|
static func setFontSize(for key: String, _ fontSize: FontSize) {
|
||||||
setInt(for: key, fontSize.rawValue)
|
setInt(for: key, fontSize.rawValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
func bool(for key: String) -> Bool {
|
static func bool(for key: String) -> Bool {
|
||||||
return UserDefaults.standard.bool(forKey: key)
|
return UserDefaults.standard.bool(forKey: key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setBool(for key: String, _ flag: Bool) {
|
static func setBool(for key: String, _ flag: Bool) {
|
||||||
UserDefaults.standard.set(flag, forKey: key)
|
UserDefaults.standard.set(flag, forKey: key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func int(for key: String) -> Int {
|
static func int(for key: String) -> Int {
|
||||||
return UserDefaults.standard.integer(forKey: key)
|
return UserDefaults.standard.integer(forKey: key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setInt(for key: String, _ x: Int) {
|
static func setInt(for key: String, _ x: Int) {
|
||||||
UserDefaults.standard.set(x, forKey: key)
|
UserDefaults.standard.set(x, forKey: key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func date(for key: String) -> Date? {
|
static func date(for key: String) -> Date? {
|
||||||
return UserDefaults.standard.object(forKey: key) as? Date
|
return UserDefaults.standard.object(forKey: key) as? Date
|
||||||
}
|
}
|
||||||
|
|
||||||
func setDate(for key: String, _ date: Date?) {
|
static func setDate(for key: String, _ date: Date?) {
|
||||||
UserDefaults.standard.set(date, forKey: key)
|
UserDefaults.standard.set(date, forKey: key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sortDirection(for key:String) -> ComparisonResult {
|
static func sortDirection(for key:String) -> ComparisonResult {
|
||||||
|
|
||||||
let rawInt = int(for: key)
|
let rawInt = int(for: key)
|
||||||
if rawInt == ComparisonResult.orderedAscending.rawValue {
|
if rawInt == ComparisonResult.orderedAscending.rawValue {
|
||||||
|
@ -195,7 +183,7 @@ private extension AppDefaults {
|
||||||
return .orderedDescending
|
return .orderedDescending
|
||||||
}
|
}
|
||||||
|
|
||||||
func setSortDirection(for key: String, _ value: ComparisonResult) {
|
static func setSortDirection(for key: String, _ value: ComparisonResult) {
|
||||||
|
|
||||||
if value == .orderedAscending {
|
if value == .orderedAscending {
|
||||||
setInt(for: key, ComparisonResult.orderedAscending.rawValue)
|
setInt(for: key, ComparisonResult.orderedAscending.rawValue)
|
||||||
|
|
|
@ -128,7 +128,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations,
|
||||||
|
|
||||||
appName = (Bundle.main.infoDictionary!["CFBundleExecutable"]! as! String)
|
appName = (Bundle.main.infoDictionary!["CFBundleExecutable"]! as! String)
|
||||||
|
|
||||||
let isFirstRun = AppDefaults.shared.isFirstRun
|
AppDefaults.registerDefaults()
|
||||||
|
let isFirstRun = AppDefaults.isFirstRun
|
||||||
if isFirstRun {
|
if isFirstRun {
|
||||||
logDebugMessage("Is first run.")
|
logDebugMessage("Is first run.")
|
||||||
}
|
}
|
||||||
|
@ -492,12 +493,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations,
|
||||||
|
|
||||||
@IBAction func sortByOldestArticleOnTop(_ sender: Any?) {
|
@IBAction func sortByOldestArticleOnTop(_ sender: Any?) {
|
||||||
|
|
||||||
AppDefaults.shared.timelineSortDirection = .orderedAscending
|
AppDefaults.timelineSortDirection = .orderedAscending
|
||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func sortByNewestArticleOnTop(_ sender: Any?) {
|
@IBAction func sortByNewestArticleOnTop(_ sender: Any?) {
|
||||||
|
|
||||||
AppDefaults.shared.timelineSortDirection = .orderedDescending
|
AppDefaults.timelineSortDirection = .orderedDescending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -524,7 +525,7 @@ private extension AppDelegate {
|
||||||
|
|
||||||
func updateSortMenuItems() {
|
func updateSortMenuItems() {
|
||||||
|
|
||||||
let sortByNewestOnTop = AppDefaults.shared.timelineSortDirection == .orderedDescending
|
let sortByNewestOnTop = AppDefaults.timelineSortDirection == .orderedDescending
|
||||||
sortByNewestArticleOnTopMenuItem.state = sortByNewestOnTop ? .on : .off
|
sortByNewestArticleOnTopMenuItem.state = sortByNewestOnTop ? .on : .off
|
||||||
sortByOldestArticleOnTopMenuItem.state = sortByNewestOnTop ? .off : .on
|
sortByOldestArticleOnTopMenuItem.state = sortByNewestOnTop ? .off : .on
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ struct Browser {
|
||||||
static func open(_ urlString: String) {
|
static func open(_ urlString: String) {
|
||||||
|
|
||||||
// Opens according to prefs.
|
// Opens according to prefs.
|
||||||
open(urlString, inBackground: AppDefaults.shared.openInBrowserInBackground)
|
open(urlString, inBackground: AppDefaults.openInBrowserInBackground)
|
||||||
}
|
}
|
||||||
|
|
||||||
static func open(_ urlString: String, inBackground: Bool) {
|
static func open(_ urlString: String, inBackground: Bool) {
|
||||||
|
|
|
@ -39,12 +39,12 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
|
||||||
|
|
||||||
sharingServicePickerDelegate = SharingServicePickerDelegate(self.window)
|
sharingServicePickerDelegate = SharingServicePickerDelegate(self.window)
|
||||||
|
|
||||||
if !AppDefaults.shared.showTitleOnMainWindow {
|
if !AppDefaults.showTitleOnMainWindow {
|
||||||
window?.titleVisibility = .hidden
|
window?.titleVisibility = .hidden
|
||||||
}
|
}
|
||||||
|
|
||||||
window?.setFrameUsingName(windowAutosaveName, force: true)
|
window?.setFrameUsingName(windowAutosaveName, force: true)
|
||||||
if AppDefaults.shared.isFirstRun && !MainWindowController.didPositionWindowOnFirstRun {
|
if AppDefaults.isFirstRun && !MainWindowController.didPositionWindowOnFirstRun {
|
||||||
|
|
||||||
if let window = window {
|
if let window = window {
|
||||||
let point = NSPoint(x: 128, y: 64)
|
let point = NSPoint(x: 128, y: 64)
|
||||||
|
@ -592,14 +592,14 @@ private extension MainWindowController {
|
||||||
}
|
}
|
||||||
|
|
||||||
let widths = splitView.arrangedSubviews.map{ Int(floor($0.frame.width)) }
|
let widths = splitView.arrangedSubviews.map{ Int(floor($0.frame.width)) }
|
||||||
AppDefaults.shared.mainWindowWidths = widths
|
AppDefaults.mainWindowWidths = widths
|
||||||
}
|
}
|
||||||
|
|
||||||
func restoreSplitViewState() {
|
func restoreSplitViewState() {
|
||||||
|
|
||||||
// TODO: Update this for multiple windows.
|
// TODO: Update this for multiple windows.
|
||||||
|
|
||||||
guard let splitView = splitViewController?.splitView, let widths = AppDefaults.shared.mainWindowWidths, widths.count == 3, let window = window else {
|
guard let splitView = splitViewController?.splitView, let widths = AppDefaults.mainWindowWidths, widths.count == 3, let window = window else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ import RSCore
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
|
|
||||||
sidebarCellAppearance = SidebarCellAppearance(theme: appDelegate.currentTheme, fontSize: AppDefaults.shared.sidebarFontSize)
|
sidebarCellAppearance = SidebarCellAppearance(theme: appDelegate.currentTheme, fontSize: AppDefaults.sidebarFontSize)
|
||||||
|
|
||||||
outlineView.dataSource = dataSource
|
outlineView.dataSource = dataSource
|
||||||
outlineView.setDraggingSourceOperationMask(.move, forLocal: true)
|
outlineView.setDraggingSourceOperationMask(.move, forLocal: true)
|
||||||
|
|
|
@ -62,14 +62,14 @@ class TimelineViewController: NSViewController, UndoableCommandRunner {
|
||||||
private var didRegisterForNotifications = false
|
private var didRegisterForNotifications = false
|
||||||
static let fetchAndMergeArticlesQueue = CoalescingQueue(name: "Fetch and Merge Articles", interval: 0.5)
|
static let fetchAndMergeArticlesQueue = CoalescingQueue(name: "Fetch and Merge Articles", interval: 0.5)
|
||||||
|
|
||||||
private var sortDirection = AppDefaults.shared.timelineSortDirection {
|
private var sortDirection = AppDefaults.timelineSortDirection {
|
||||||
didSet {
|
didSet {
|
||||||
if sortDirection != oldValue {
|
if sortDirection != oldValue {
|
||||||
sortDirectionDidChange()
|
sortDirectionDidChange()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private var fontSize: FontSize = AppDefaults.shared.timelineFontSize {
|
private var fontSize: FontSize = AppDefaults.timelineFontSize {
|
||||||
didSet {
|
didSet {
|
||||||
if fontSize != oldValue {
|
if fontSize != oldValue {
|
||||||
fontSizeDidChange()
|
fontSizeDidChange()
|
||||||
|
@ -429,8 +429,8 @@ class TimelineViewController: NSViewController, UndoableCommandRunner {
|
||||||
|
|
||||||
@objc func userDefaultsDidChange(_ note: Notification) {
|
@objc func userDefaultsDidChange(_ note: Notification) {
|
||||||
|
|
||||||
self.fontSize = AppDefaults.shared.timelineFontSize
|
self.fontSize = AppDefaults.timelineFontSize
|
||||||
self.sortDirection = AppDefaults.shared.timelineSortDirection
|
self.sortDirection = AppDefaults.timelineSortDirection
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Reloading Data
|
// MARK: - Reloading Data
|
||||||
|
|
Loading…
Reference in New Issue