2022-12-31 11:29:19 +00:00
|
|
|
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
|
2022-12-31 11:29:19 +00:00
|
|
|
case avatarPosition
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum AvatarPosition: String, CaseIterable {
|
|
|
|
case leading, top
|
|
|
|
|
|
|
|
public var description: LocalizedStringKey {
|
|
|
|
switch self {
|
|
|
|
case .leading:
|
|
|
|
return "Leading"
|
|
|
|
case .top:
|
|
|
|
return "Top"
|
|
|
|
}
|
|
|
|
}
|
2022-12-31 05:48:09 +00:00
|
|
|
}
|
2022-12-30 19:55:23 +00:00
|
|
|
|
2022-12-31 05:48:09 +00:00
|
|
|
@AppStorage("is_previously_set") var isSet: Bool = false
|
|
|
|
@AppStorage(ThemeKey.colorScheme.rawValue) public var colorScheme: String = "dark" {
|
|
|
|
didSet {
|
|
|
|
if colorScheme == "dark" {
|
|
|
|
setColor(set: DarkSet())
|
|
|
|
} else {
|
|
|
|
setColor(set: LightSet())
|
|
|
|
}
|
2022-12-30 19:55:23 +00:00
|
|
|
}
|
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
|
2022-12-31 11:29:19 +00:00
|
|
|
@AppStorage(ThemeKey.avatarPosition.rawValue) var rawAvatarPosition: String = AvatarPosition.top.rawValue
|
|
|
|
|
|
|
|
@Published public var avatarPosition: AvatarPosition = .top
|
|
|
|
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2022-12-31 05:48:09 +00:00
|
|
|
public init() {
|
|
|
|
if !isSet {
|
|
|
|
setColor(set: DarkSet())
|
|
|
|
isSet.toggle()
|
2022-12-30 19:55:23 +00:00
|
|
|
}
|
2022-12-31 11:29:19 +00:00
|
|
|
|
|
|
|
avatarPosition = AvatarPosition(rawValue: rawAvatarPosition) ?? .top
|
|
|
|
|
|
|
|
$avatarPosition
|
|
|
|
.dropFirst()
|
|
|
|
.map(\.rawValue)
|
|
|
|
.sink { [weak self] position in
|
|
|
|
self?.rawAvatarPosition = position
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
2022-12-31 05:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func setColor(set: ColorSet) {
|
|
|
|
self.tintColor = set.tintColor
|
|
|
|
self.primaryBackgroundColor = set.primaryBackgroundColor
|
|
|
|
self.secondaryBackgroundColor = set.secondaryBackgroundColor
|
|
|
|
self.labelColor = set.labelColor
|
|
|
|
}
|
2022-12-24 13:55:04 +00:00
|
|
|
}
|