2019-04-15 22:03:05 +02:00
|
|
|
//
|
|
|
|
// AppDefaults.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 9/22/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
struct AppDefaults {
|
|
|
|
|
2019-11-04 18:35:45 +01:00
|
|
|
static var shared: UserDefaults = {
|
2019-10-25 19:56:28 +02:00
|
|
|
let appIdentifierPrefix = Bundle.main.object(forInfoDictionaryKey: "AppIdentifierPrefix") as! String
|
|
|
|
let suiteName = "\(appIdentifierPrefix)group.\(Bundle.main.bundleIdentifier!)"
|
2019-10-15 16:11:18 +02:00
|
|
|
return UserDefaults.init(suiteName: suiteName)!
|
2019-11-04 18:35:45 +01:00
|
|
|
}()
|
2019-10-15 16:11:18 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
struct Key {
|
2019-09-17 00:09:49 +02:00
|
|
|
static let lastImageCacheFlushDate = "lastImageCacheFlushDate"
|
2019-04-15 22:03:05 +02:00
|
|
|
static let firstRunDate = "firstRunDate"
|
2019-09-09 00:41:00 +02:00
|
|
|
static let timelineGroupByFeed = "timelineGroupByFeed"
|
|
|
|
static let timelineNumberOfLines = "timelineNumberOfLines"
|
2019-04-15 22:03:05 +02:00
|
|
|
static let timelineSortDirection = "timelineSortDirection"
|
2019-10-10 22:13:20 +02:00
|
|
|
static let displayUndoAvailableTip = "displayUndoAvailableTip"
|
2019-04-15 22:03:05 +02:00
|
|
|
static let refreshInterval = "refreshInterval"
|
2019-04-26 22:24:39 +02:00
|
|
|
static let lastRefresh = "lastRefresh"
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static let isFirstRun: Bool = {
|
2019-09-22 20:09:06 +02:00
|
|
|
if let _ = AppDefaults.shared.object(forKey: Key.firstRunDate) as? Date {
|
2019-04-15 22:03:05 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
firstRunDate = Date()
|
|
|
|
return true
|
|
|
|
}()
|
|
|
|
|
2019-09-17 00:09:49 +02:00
|
|
|
static var lastImageCacheFlushDate: Date? {
|
|
|
|
get {
|
|
|
|
return date(for: Key.lastImageCacheFlushDate)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
setDate(for: Key.lastImageCacheFlushDate, newValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
static var refreshInterval: RefreshInterval {
|
|
|
|
get {
|
2019-09-22 20:09:06 +02:00
|
|
|
let rawValue = AppDefaults.shared.integer(forKey: Key.refreshInterval)
|
2019-04-15 22:03:05 +02:00
|
|
|
return RefreshInterval(rawValue: rawValue) ?? RefreshInterval.everyHour
|
|
|
|
}
|
|
|
|
set {
|
2019-09-22 20:09:06 +02:00
|
|
|
AppDefaults.shared.set(newValue.rawValue, forKey: Key.refreshInterval)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 00:41:00 +02:00
|
|
|
|
|
|
|
static var timelineGroupByFeed: Bool {
|
|
|
|
get {
|
|
|
|
return bool(for: Key.timelineGroupByFeed)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
setBool(for: Key.timelineGroupByFeed, newValue)
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
static var timelineSortDirection: ComparisonResult {
|
|
|
|
get {
|
|
|
|
return sortDirection(for: Key.timelineSortDirection)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
setSortDirection(for: Key.timelineSortDirection, newValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 22:13:20 +02:00
|
|
|
static var displayUndoAvailableTip: Bool {
|
2019-10-08 02:48:58 +02:00
|
|
|
get {
|
2019-10-10 22:13:20 +02:00
|
|
|
return bool(for: Key.displayUndoAvailableTip)
|
2019-10-08 02:48:58 +02:00
|
|
|
}
|
|
|
|
set {
|
2019-10-10 22:13:20 +02:00
|
|
|
setBool(for: Key.displayUndoAvailableTip, newValue)
|
2019-10-08 02:48:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 22:24:39 +02:00
|
|
|
static var lastRefresh: Date? {
|
|
|
|
get {
|
|
|
|
return date(for: Key.lastRefresh)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
setDate(for: Key.lastRefresh, newValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:29:00 +02:00
|
|
|
static var timelineNumberOfLines: Int {
|
|
|
|
get {
|
|
|
|
return int(for: Key.timelineNumberOfLines)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
setInt(for: Key.timelineNumberOfLines, newValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
static func registerDefaults() {
|
2019-09-17 00:09:49 +02:00
|
|
|
let defaults: [String : Any] = [Key.lastImageCacheFlushDate: Date(),
|
|
|
|
Key.refreshInterval: RefreshInterval.everyHour.rawValue,
|
2019-09-09 00:41:00 +02:00
|
|
|
Key.timelineGroupByFeed: false,
|
2019-10-23 20:33:22 +02:00
|
|
|
Key.timelineNumberOfLines: 2,
|
2019-10-08 02:48:58 +02:00
|
|
|
Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue,
|
2019-10-10 22:13:20 +02:00
|
|
|
Key.displayUndoAvailableTip: true]
|
2019-09-22 20:09:06 +02:00
|
|
|
AppDefaults.shared.register(defaults: defaults)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension AppDefaults {
|
|
|
|
|
|
|
|
static var firstRunDate: Date? {
|
|
|
|
get {
|
|
|
|
return date(for: Key.firstRunDate)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
setDate(for: Key.firstRunDate, newValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func bool(for key: String) -> Bool {
|
2019-09-22 20:09:06 +02:00
|
|
|
return AppDefaults.shared.bool(forKey: key)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func setBool(for key: String, _ flag: Bool) {
|
2019-09-22 20:09:06 +02:00
|
|
|
AppDefaults.shared.set(flag, forKey: key)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func int(for key: String) -> Int {
|
2019-09-22 20:09:06 +02:00
|
|
|
return AppDefaults.shared.integer(forKey: key)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func setInt(for key: String, _ x: Int) {
|
2019-09-22 20:09:06 +02:00
|
|
|
AppDefaults.shared.set(x, forKey: key)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func date(for key: String) -> Date? {
|
2019-09-22 20:09:06 +02:00
|
|
|
return AppDefaults.shared.object(forKey: key) as? Date
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func setDate(for key: String, _ date: Date?) {
|
2019-09-22 20:09:06 +02:00
|
|
|
AppDefaults.shared.set(date, forKey: key)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func sortDirection(for key:String) -> ComparisonResult {
|
|
|
|
let rawInt = int(for: key)
|
|
|
|
if rawInt == ComparisonResult.orderedAscending.rawValue {
|
|
|
|
return .orderedAscending
|
|
|
|
}
|
|
|
|
return .orderedDescending
|
|
|
|
}
|
|
|
|
|
|
|
|
static func setSortDirection(for key: String, _ value: ComparisonResult) {
|
|
|
|
if value == .orderedAscending {
|
|
|
|
setInt(for: key, ComparisonResult.orderedAscending.rawValue)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setInt(for: key, ComparisonResult.orderedDescending.rawValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|