IceCubesApp/Packages/DesignSystem/Sources/DesignSystem/Theme.swift

205 lines
6.2 KiB
Swift
Raw Normal View History

import Combine
2022-12-24 13:55:04 +00:00
import SwiftUI
public class Theme: ObservableObject {
2022-12-31 05:48:09 +00:00
enum ThemeKey: String {
case colorScheme, tint, label, primaryBackground, secondaryBackground
2023-01-07 16:44:25 +00:00
case avatarPosition, avatarShape, statusActionsDisplay, statusDisplayStyle
case selectedSet, selectedScheme
case followSystemColorSchme
case displayFullUsernameTimeline
case lineSpacing
}
2023-01-30 06:27:06 +00:00
public enum FontState: Int, CaseIterable {
case system
case openDyslexic
case hyperLegible
case SFRounded
2023-01-30 06:27:06 +00:00
case custom
@MainActor
public var title: LocalizedStringKey {
switch self {
case .system:
return "settings.display.font.system"
case .openDyslexic:
return "Open Dyslexic"
case .hyperLegible:
return "Hyper Legible"
case .SFRounded:
2023-02-21 06:23:42 +00:00
return "SF Rounded"
2023-01-30 06:27:06 +00:00
case .custom:
return "settings.display.font.custom"
}
}
2023-01-30 06:27:06 +00:00
}
2023-01-17 10:36:01 +00:00
public enum AvatarPosition: String, CaseIterable {
case leading, top
2023-01-17 10:36:01 +00:00
public var description: LocalizedStringKey {
switch self {
case .leading:
return "enum.avatar-position.leading"
case .top:
return "enum.avatar-position.top"
2022-12-31 05:48:09 +00:00
}
}
2022-12-31 05:48:09 +00:00
}
public enum AvatarShape: String, CaseIterable {
case circle, rounded
public var description: LocalizedStringKey {
switch self {
case .circle:
return "enum.avatar-shape.circle"
case .rounded:
return "enum.avatar-shape.rounded"
}
}
}
2023-01-17 10:36:01 +00:00
2023-01-06 16:14:34 +00:00
public enum StatusActionsDisplay: String, CaseIterable {
case full, discret, none
public var description: LocalizedStringKey {
switch self {
case .full:
return "enum.status-actions-display.all"
2023-01-06 16:14:34 +00:00
case .discret:
return "enum.status-actions-display.only-buttons"
2023-01-06 16:14:34 +00:00
case .none:
return "enum.status-actions-display.no-buttons"
2023-01-06 16:14:34 +00:00
}
}
}
2023-01-17 10:36:01 +00:00
2023-01-07 16:44:25 +00:00
public enum StatusDisplayStyle: String, CaseIterable {
2023-02-22 06:26:32 +00:00
case large, medium, compact
2023-01-07 16:44:25 +00:00
public var description: LocalizedStringKey {
switch self {
case .large:
return "enum.status-display-style.large"
2023-02-22 06:26:32 +00:00
case .medium:
return "enum.status-display-style.medium"
2023-01-07 16:44:25 +00:00
case .compact:
return "enum.status-display-style.compact"
2023-01-07 16:44:25 +00:00
}
}
}
2023-02-21 06:23:42 +00:00
2023-02-21 06:37:16 +00:00
private var _cachedChoosenFont: UIFont?
public var chosenFont: UIFont? {
get {
2023-02-21 06:37:16 +00:00
if let _cachedChoosenFont {
return _cachedChoosenFont
}
guard let chosenFontData,
let font = try? NSKeyedUnarchiver.unarchivedObject(ofClass: UIFont.self, from: chosenFontData) else { return nil }
2023-02-21 06:37:16 +00:00
_cachedChoosenFont = font
return font
}
set {
if let font = newValue,
let data = try? NSKeyedArchiver.archivedData(withRootObject: font, requiringSecureCoding: false)
{
chosenFontData = data
} else {
chosenFontData = nil
}
2023-02-21 06:37:16 +00:00
_cachedChoosenFont = nil
}
}
2023-01-17 10:36:01 +00:00
@AppStorage("is_previously_set") public var isThemePreviouslySet: Bool = false
@AppStorage(ThemeKey.selectedScheme.rawValue) public var selectedScheme: ColorScheme = .dark
2022-12-31 05:48:09 +00:00
@AppStorage(ThemeKey.tint.rawValue) public var tintColor: Color = .black
@AppStorage(ThemeKey.primaryBackground.rawValue) public var primaryBackgroundColor: Color = .white
@AppStorage(ThemeKey.secondaryBackground.rawValue) public var secondaryBackgroundColor: Color = .gray
@AppStorage(ThemeKey.label.rawValue) public var labelColor: Color = .black
@AppStorage(ThemeKey.avatarPosition.rawValue) var rawAvatarPosition: String = AvatarPosition.top.rawValue
@AppStorage(ThemeKey.avatarShape.rawValue) var rawAvatarShape: String = AvatarShape.rounded.rawValue
@AppStorage(ThemeKey.selectedSet.rawValue) var storedSet: ColorSetName = .iceCubeDark
2023-01-06 16:14:34 +00:00
@AppStorage(ThemeKey.statusActionsDisplay.rawValue) public var statusActionsDisplay: StatusActionsDisplay = .full
2023-01-07 16:44:25 +00:00
@AppStorage(ThemeKey.statusDisplayStyle.rawValue) public var statusDisplayStyle: StatusDisplayStyle = .large
@AppStorage(ThemeKey.followSystemColorSchme.rawValue) public var followSystemColorScheme: Bool = true
@AppStorage(ThemeKey.displayFullUsernameTimeline.rawValue) public var displayFullUsername: Bool = true
@AppStorage(ThemeKey.lineSpacing.rawValue) public var lineSpacing: Double = 0.8
@AppStorage("font_size_scale") public var fontSizeScale: Double = 1
@AppStorage("chosen_font") public private(set) var chosenFontData: Data?
@Published public var avatarPosition: AvatarPosition = .top
@Published public var avatarShape: AvatarShape = .rounded
@Published public var selectedSet: ColorSetName = .iceCubeDark
private var cancellables = Set<AnyCancellable>()
2023-01-17 10:36:01 +00:00
public static let shared = Theme()
2023-01-17 10:36:01 +00:00
private init() {
selectedSet = storedSet
2023-01-17 10:36:01 +00:00
avatarPosition = AvatarPosition(rawValue: rawAvatarPosition) ?? .top
avatarShape = AvatarShape(rawValue: rawAvatarShape) ?? .rounded
$avatarPosition
.dropFirst()
.map(\.rawValue)
.sink { [weak self] position in
self?.rawAvatarPosition = position
}
.store(in: &cancellables)
$avatarShape
.dropFirst()
.map(\.rawValue)
.sink { [weak self] shape in
self?.rawAvatarShape = shape
}
.store(in: &cancellables)
2023-01-17 10:36:01 +00:00
// Workaround, since @AppStorage can't be directly observed
$selectedSet
.dropFirst()
.sink { [weak self] colorSetName in
self?.setColor(withName: colorSetName)
}
.store(in: &cancellables)
2023-09-12 13:55:11 +00:00
#if os(visionOS)
selectedSet = .iceCubeDark
#endif
}
2023-01-17 10:36:01 +00:00
public static var allColorSet: [ColorSet] {
[
IceCubeDark(),
IceCubeLight(),
2023-01-21 17:40:35 +00:00
IceCubeNeonDark(),
IceCubeNeonLight(),
DesertDark(),
DesertLight(),
NemesisDark(),
2023-01-17 10:36:01 +00:00
NemesisLight(),
2023-01-19 10:58:38 +00:00
MediumLight(),
MediumDark(),
2023-08-30 06:02:38 +00:00
ConstellationLight(),
ConstellationDark(),
]
2022-12-31 05:48:09 +00:00
}
2023-01-17 10:36:01 +00:00
public func setColor(withName name: ColorSetName) {
let colorSet = Theme.allColorSet.filter { $0.name == name }.first ?? IceCubeDark()
2023-01-17 10:36:01 +00:00
selectedScheme = colorSet.scheme
tintColor = colorSet.tintColor
primaryBackgroundColor = colorSet.primaryBackgroundColor
secondaryBackgroundColor = colorSet.secondaryBackgroundColor
labelColor = colorSet.labelColor
storedSet = name
2022-12-31 05:48:09 +00:00
}
2022-12-24 13:55:04 +00:00
}