Tusker/Tusker/Preferences/Preferences.swift

66 lines
1.8 KiB
Swift

//
// Preferences.swift
// Tusker
//
// Created by Shadowfacts on 8/28/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
import Pachyderm
class Preferences: Codable {
private(set) static var shared: Preferences = load()
private static var documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
private static var archiveURL = Preferences.documentsDirectory.appendingPathComponent("preferences").appendingPathExtension("plist")
static func save() {
let encoder = PropertyListEncoder()
let data = try? encoder.encode(shared)
try? data?.write(to: archiveURL, options: .noFileProtection)
}
static func load() -> Preferences {
let decoder = PropertyListDecoder()
if let data = try? Data(contentsOf: archiveURL),
let preferences = try? decoder.decode(Preferences.self, from: data) {
return preferences
}
return Preferences()
}
private init() {}
// MARK: - Appearance
var showRepliesInProfiles = false
var avatarStyle = AvatarStyle.roundRect
var hideCustomEmojiInUsernames = false
var tabs: [Tab: Int] = [.home: 0, .notifications: 1, .local: -1, .federated: 2, .myProfile: 3, .preferences: 4]
var defaultPostVisibility = Status.Visibility.public
var automaticallySaveDrafts = true
// MARK: - Advanced
var silentActions: [String: Permission] = [:]
// MARK: - Utility Methods
func tabIndex(_ tab: Tab) -> Int {
if let index = tabs[tab] {
return index
} else {
tabs[tab] = -1
return -1
}
}
}
extension Preferences {
enum Permission: String, Codable {
case undecided, accepted, rejected
}
}