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

144 lines
4.1 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
}
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 "Leading"
case .top:
return "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 "Circle"
case .rounded:
return "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 "All"
case .discret:
return "Only buttons"
case .none:
return "No buttons"
}
}
}
2023-01-17 10:36:01 +00:00
2023-01-07 16:44:25 +00:00
public enum StatusDisplayStyle: String, CaseIterable {
case large, compact
public var description: LocalizedStringKey {
switch self {
case .large:
return "Large"
case .compact:
return "Compact"
}
}
}
2023-01-17 10:36:01 +00:00
@AppStorage("is_previously_set") private var isSet: 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
@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
// If theme is never set before set the default store. This should only execute once after install.
2023-01-17 10:36:01 +00:00
2022-12-31 05:48:09 +00:00
if !isSet {
setColor(withName: .iceCubeDark)
isSet = true
}
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-01-17 10:36:01 +00:00
public static var allColorSet: [ColorSet] {
[
IceCubeDark(),
IceCubeLight(),
DesertDark(),
DesertLight(),
NemesisDark(),
2023-01-17 10:36:01 +00:00
NemesisLight(),
]
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
}