Early WIP of theming support

This commit is contained in:
Thomas Ricouard 2022-12-24 14:55:04 +01:00
parent 70ee6e0d27
commit 5e24c1ee58
9 changed files with 181 additions and 5 deletions

View file

@ -8,15 +8,18 @@ class AppAccountsManager: ObservableObject {
oauthToken: currentAccount.oauthToken) oauthToken: currentAccount.oauthToken)
} }
} }
@Published var availableAccount: [AppAccount]
@Published var currentClient: Client @Published var currentClient: Client
init() { init() {
var defaultAccount = AppAccount(server: IceCubesApp.defaultServer, oauthToken: nil) var defaultAccount = AppAccount(server: IceCubesApp.defaultServer, oauthToken: nil)
do { do {
let keychainAccounts = try AppAccount.retrieveAll() let keychainAccounts = try AppAccount.retrieveAll()
availableAccount = keychainAccounts
defaultAccount = keychainAccounts.last ?? defaultAccount defaultAccount = keychainAccounts.last ?? defaultAccount
} catch {} } catch {}
currentAccount = defaultAccount currentAccount = defaultAccount
availableAccount = [defaultAccount]
currentClient = .init(server: defaultAccount.server, oauthToken: defaultAccount.oauthToken) currentClient = .init(server: defaultAccount.server, oauthToken: defaultAccount.oauthToken)
} }

View file

@ -3,6 +3,7 @@ import Timeline
import Network import Network
import KeychainSwift import KeychainSwift
import Env import Env
import DesignSystem
@main @main
struct IceCubesApp: App { struct IceCubesApp: App {
@ -15,6 +16,8 @@ struct IceCubesApp: App {
@StateObject private var appAccountsManager = AppAccountsManager() @StateObject private var appAccountsManager = AppAccountsManager()
@StateObject private var currentAccount = CurrentAccount() @StateObject private var currentAccount = CurrentAccount()
@StateObject private var quickLook = QuickLook() @StateObject private var quickLook = QuickLook()
@StateObject private var theme = Theme()
@State private var selectedTab: Tab = .timeline @State private var selectedTab: Tab = .timeline
@State private var popToRootTab: Tab = .other @State private var popToRootTab: Tab = .other
@ -60,7 +63,7 @@ struct IceCubesApp: App {
} }
.tag(Tab.settings) .tag(Tab.settings)
} }
.tint(.brand) .tint(theme.tintColor)
.onChange(of: appAccountsManager.currentClient) { newClient in .onChange(of: appAccountsManager.currentClient) { newClient in
currentAccount.setClient(client: newClient) currentAccount.setClient(client: newClient)
} }
@ -71,6 +74,7 @@ struct IceCubesApp: App {
.environmentObject(appAccountsManager.currentClient) .environmentObject(appAccountsManager.currentClient)
.environmentObject(quickLook) .environmentObject(quickLook)
.environmentObject(currentAccount) .environmentObject(currentAccount)
.environmentObject(theme)
.quickLookPreview($quickLook.url, in: quickLook.urls) .quickLookPreview($quickLook.url, in: quickLook.urls)
} }
} }

View file

@ -4,11 +4,13 @@ import Env
import Network import Network
import Account import Account
import Models import Models
import DesignSystem
struct SettingsTabs: View { struct SettingsTabs: View {
@Environment(\.openURL) private var openURL @Environment(\.openURL) private var openURL
@EnvironmentObject private var client: Client @EnvironmentObject private var client: Client
@EnvironmentObject private var appAccountsManager: AppAccountsManager @EnvironmentObject private var appAccountsManager: AppAccountsManager
@EnvironmentObject private var theme: Theme
@State private var signInInProgress = false @State private var signInInProgress = false
@State private var accountData: Account? @State private var accountData: Account?
@ -20,6 +22,7 @@ struct SettingsTabs: View {
Form { Form {
appSection appSection
accountSection accountSection
themeSection
instanceSection instanceSection
} }
.onOpenURL(perform: { url in .onOpenURL(perform: { url in
@ -59,6 +62,19 @@ struct SettingsTabs: View {
} }
} }
private var themeSection: some View {
Section("Theme") {
ColorPicker("Tint color", selection: $theme.tintColor)
Button {
theme.tintColor = .brand
theme.labelColor = .label
} label: {
Text("Restore default")
}
}
}
@ViewBuilder @ViewBuilder
private var instanceSection: some View { private var instanceSection: some View {
if let instanceData { if let instanceData {

View file

@ -4,4 +4,39 @@ extension Color {
public static var brand: Color { public static var brand: Color {
Color("brand", bundle: .module) Color("brand", bundle: .module)
} }
public static var primaryBackground: Color {
Color("primaryBackground", bundle: .module)
}
public static var secondaryBackground: Color {
Color("secondaryBackground", bundle: .module)
}
public static var label: Color {
Color("label", bundle: .module)
}
}
extension Color: RawRepresentable {
public init?(rawValue: Int) {
let red = Double((rawValue & 0xFF0000) >> 16) / 0xFF
let green = Double((rawValue & 0x00FF00) >> 8) / 0xFF
let blue = Double(rawValue & 0x0000FF) / 0xFF
self = Color(red: red, green: green, blue: blue)
}
public var rawValue: Int {
guard let coreImageColor = coreImageColor else {
return 0
}
let red = Int(coreImageColor.red * 255 + 0.5)
let green = Int(coreImageColor.green * 255 + 0.5)
let blue = Int(coreImageColor.blue * 255 + 0.5)
return (red << 16) | (green << 8) | blue
}
private var coreImageColor: CIColor? {
return CIColor(color: .init(self))
}
} }

View file

@ -5,9 +5,9 @@
"color-space" : "srgb", "color-space" : "srgb",
"components" : { "components" : {
"alpha" : "1.000", "alpha" : "1.000",
"blue" : "255", "blue" : "1.000",
"green" : "90", "green" : "0.353",
"red" : "89" "red" : "0.349"
} }
}, },
"idiom" : "universal" "idiom" : "universal"

View file

@ -0,0 +1,28 @@
{
"colors" : [
{
"color" : {
"platform" : "universal",
"reference" : "labelColor"
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"platform" : "universal",
"reference" : "labelColor"
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View file

@ -0,0 +1,38 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0x22",
"green" : "0x1B",
"red" : "0x19"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0x22",
"green" : "0x1B",
"red" : "0x19"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View file

@ -0,0 +1,38 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0x37",
"green" : "0x2C",
"red" : "0x28"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0x37",
"green" : "0x2C",
"red" : "0x28"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View file

@ -0,0 +1,14 @@
import SwiftUI
public class Theme: ObservableObject {
enum ThemeKey: String {
case tint, label, primaryBackground, secondaryBackground
}
@AppStorage(ThemeKey.tint.rawValue) public var tintColor: Color = .brand
@AppStorage(ThemeKey.primaryBackground.rawValue) public var primaryBackgroundColor: Color = .primaryBackground
@AppStorage(ThemeKey.secondaryBackground.rawValue) public var secondaryBackgroundColor: Color = .secondaryBackground
@AppStorage(ThemeKey.label.rawValue) public var labelColor: Color = .label
public init() { }
}