metatext-app-ios-iphone-ipad/ServiceLayer/Sources/ServiceLayer/Utilities/AppPreferences.swift

233 lines
6.6 KiB
Swift
Raw Normal View History

2020-10-15 09:44:01 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import CodableBloomFilter
import Foundation
2020-10-27 04:01:12 +01:00
import Mastodon
2020-10-15 09:44:01 +02:00
public struct AppPreferences {
private let userDefaults: UserDefaults
2020-10-23 00:16:06 +02:00
private let systemReduceMotion: () -> Bool
2021-03-06 03:25:18 +01:00
private let systemAutoplayVideos: () -> Bool
2020-10-15 09:44:01 +02:00
public init(environment: AppEnvironment) {
self.userDefaults = environment.userDefaults
2020-10-23 00:16:06 +02:00
self.systemReduceMotion = environment.reduceMotion
2021-03-06 03:25:18 +01:00
self.systemAutoplayVideos = environment.autoplayVideos
2020-10-15 09:44:01 +02:00
}
}
public extension AppPreferences {
enum ColorScheme: String, CaseIterable, Identifiable {
case system
case light
case dark
public var id: String { rawValue }
}
2021-01-31 02:43:48 +01:00
enum StatusWord: String, CaseIterable, Identifiable {
case toot
case post
public var id: String { rawValue }
}
2020-10-15 09:44:01 +02:00
enum AnimateAvatars: String, CaseIterable, Identifiable {
case everywhere
case profiles
case never
public var id: String { rawValue }
}
enum Autoplay: String, CaseIterable, Identifiable {
case always
case wifi
case never
public var id: String { rawValue }
}
2020-10-27 04:01:12 +01:00
enum PositionBehavior: String, CaseIterable, Identifiable {
2021-02-08 21:33:51 +01:00
case localRememberPosition
2020-10-27 04:01:12 +01:00
case newest
public var id: String { rawValue }
}
var colorScheme: ColorScheme {
get {
if let rawValue = self[.colorScheme] as String?,
let value = ColorScheme(rawValue: rawValue) {
return value
}
return .system
}
set { self[.colorScheme] = newValue.rawValue }
}
2021-01-31 02:43:48 +01:00
var statusWord: StatusWord {
get {
if let rawValue = self[.statusWord] as String?,
let value = StatusWord(rawValue: rawValue) {
return value
}
return .toot
}
set { self[.statusWord] = newValue.rawValue }
}
2020-10-15 09:44:01 +02:00
var animateAvatars: AnimateAvatars {
get {
if let rawValue = self[.animateAvatars] as String?,
let value = AnimateAvatars(rawValue: rawValue) {
return value
}
2021-03-06 03:25:18 +01:00
return systemReduceMotion() ? .never : .everywhere
2020-10-15 09:44:01 +02:00
}
set { self[.animateAvatars] = newValue.rawValue }
}
var animateHeaders: Bool {
2021-03-06 03:25:18 +01:00
get { self[.animateHeaders] ?? !systemReduceMotion() }
2020-10-15 09:44:01 +02:00
set { self[.animateHeaders] = newValue }
}
2021-02-22 08:10:34 +01:00
var animateCustomEmojis: Bool {
2021-03-06 03:25:18 +01:00
get { self[.animateCustomEmojis] ?? !systemReduceMotion() }
2021-02-22 08:10:34 +01:00
set { self[.animateCustomEmojis] = newValue }
}
2020-10-15 09:44:01 +02:00
var autoplayGIFs: Autoplay {
get {
if let rawValue = self[.autoplayGIFs] as String?,
let value = Autoplay(rawValue: rawValue) {
return value
}
2021-03-06 03:25:18 +01:00
return (!systemAutoplayVideos() || systemReduceMotion()) ? .never : .always
2020-10-15 09:44:01 +02:00
}
set { self[.autoplayGIFs] = newValue.rawValue }
}
var autoplayVideos: Autoplay {
get {
if let rawValue = self[.autoplayVideos] as String?,
let value = Autoplay(rawValue: rawValue) {
return value
}
2021-03-06 03:25:18 +01:00
return (!systemAutoplayVideos() || systemReduceMotion()) ? .never : .wifi
2020-10-15 09:44:01 +02:00
}
set { self[.autoplayVideos] = newValue.rawValue }
}
2020-10-23 00:16:06 +02:00
2020-10-27 04:01:12 +01:00
var homeTimelineBehavior: PositionBehavior {
get {
if let rawValue = self[.homeTimelineBehavior] as String?,
let value = PositionBehavior(rawValue: rawValue) {
return value
}
2021-02-08 21:33:51 +01:00
return .localRememberPosition
2020-10-27 04:01:12 +01:00
}
set { self[.homeTimelineBehavior] = newValue.rawValue }
}
2021-01-15 22:43:46 +01:00
var defaultEmojiSkinTone: SystemEmoji.SkinTone? {
get {
if let rawValue = self[.defaultEmojiSkinTone] as Int?,
let value = SystemEmoji.SkinTone(rawValue: rawValue) {
return value
}
return nil
}
set { self[.defaultEmojiSkinTone] = newValue?.rawValue }
}
2021-02-04 06:24:00 +01:00
var notificationSounds: Set<MastodonNotification.NotificationType> {
get {
Set((self[.notificationSounds] as [String]?)?.compactMap {
MastodonNotification.NotificationType(rawValue: $0)
} ?? MastodonNotification.NotificationType.allCasesExceptUnknown)
}
set { self[.notificationSounds] = newValue.map { $0.rawValue } }
}
2021-02-08 21:33:51 +01:00
func positionBehavior(timeline: Timeline) -> PositionBehavior {
switch timeline {
2020-10-27 04:01:12 +01:00
case .home:
return homeTimelineBehavior
2021-02-08 21:33:51 +01:00
default:
2021-02-08 20:07:59 +01:00
return .newest
2020-10-27 04:01:12 +01:00
}
}
var showReblogAndFavoriteCounts: Bool {
get { self[.showReblogAndFavoriteCounts] ?? false }
set { self[.showReblogAndFavoriteCounts] = newValue }
}
2021-01-31 20:23:33 +01:00
var requireDoubleTapToReblog: Bool {
get { self[.requireDoubleTapToReblog] ?? false }
set { self[.requireDoubleTapToReblog] = newValue }
}
var requireDoubleTapToFavorite: Bool {
get { self[.requireDoubleTapToFavorite] ?? false }
set { self[.requireDoubleTapToFavorite] = newValue }
}
2021-02-04 06:24:00 +01:00
var notificationPictures: Bool {
get { self[.notificationPictures] ?? true }
set { self[.notificationPictures] = newValue }
}
var notificationAccountName: Bool {
get { self[.notificationAccountName] ?? false }
set { self[.notificationAccountName] = newValue }
}
2021-02-06 09:07:23 +01:00
var openLinksInDefaultBrowser: Bool {
get { self[.openLinksInDefaultBrowser] ?? false }
set { self[.openLinksInDefaultBrowser] = newValue }
}
var useUniversalLinks: Bool {
get { self[.useUniversalLinks] ?? true }
set { self[.useUniversalLinks] = newValue }
}
2020-10-15 09:44:01 +02:00
}
private extension AppPreferences {
enum Item: String {
case colorScheme
2021-01-31 02:43:48 +01:00
case statusWord
2021-01-31 20:23:33 +01:00
case requireDoubleTapToReblog
case requireDoubleTapToFavorite
2020-10-15 09:44:01 +02:00
case animateAvatars
case animateHeaders
2021-02-22 08:10:34 +01:00
case animateCustomEmojis
2020-10-15 09:44:01 +02:00
case autoplayGIFs
case autoplayVideos
2020-10-27 04:01:12 +01:00
case homeTimelineBehavior
case notificationsTabBehavior
2021-01-15 22:43:46 +01:00
case defaultEmojiSkinTone
case showReblogAndFavoriteCounts
2021-02-04 06:24:00 +01:00
case notificationPictures
case notificationAccountName
case notificationSounds
2021-02-06 09:07:23 +01:00
case openLinksInDefaultBrowser
case useUniversalLinks
2020-10-15 09:44:01 +02:00
}
subscript<T>(index: Item) -> T? {
get { userDefaults.value(forKey: index.rawValue) as? T }
set { userDefaults.set(newValue, forKey: index.rawValue) }
}
}