Design system improvements (#45)

* Improve DesignSystem

Apply Theme to all connected windows

* Only use UIKit when available
This commit is contained in:
David Walter 2023-01-08 19:49:49 +01:00 committed by GitHub
parent 6ccd27b2e5
commit c304b3eefe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 13 deletions

View File

@ -33,14 +33,12 @@ struct IceCubesApp: App {
var body: some Scene {
WindowGroup {
appView
.tint(theme.tintColor)
.applyTheme(theme)
.onAppear {
setNewClientsInEnv(client: appAccountsManager.currentClient)
setBarsColor(color: theme.primaryBackgroundColor)
setupRevenueCat()
refreshPushSubs()
}
.preferredColorScheme(theme.selectedScheme == ColorScheme.dark ? .dark : .light)
.environmentObject(appAccountsManager)
.environmentObject(appAccountsManager.currentClient)
.environmentObject(quickLook)
@ -52,18 +50,15 @@ struct IceCubesApp: App {
.environmentObject(PushNotificationsService.shared)
.quickLookPreview($quickLook.url, in: quickLook.urls)
}
.onChange(of: scenePhase, perform: { scenePhase in
.onChange(of: scenePhase) { scenePhase in
handleScenePhase(scenePhase: scenePhase)
})
}
.onChange(of: appAccountsManager.currentClient) { newClient in
setNewClientsInEnv(client: newClient)
if newClient.isAuth {
watcher.watch(streams: [.user, .direct])
}
}
.onChange(of: theme.primaryBackgroundColor) { newValue in
setBarsColor(color: newValue)
}
}
@ViewBuilder
@ -143,11 +138,6 @@ struct IceCubesApp: App {
}
}
private func setBarsColor(color: Color) {
UINavigationBar.appearance().isTranslucent = true
UINavigationBar.appearance().barTintColor = UIColor(color)
}
private func setupRevenueCat() {
Purchases.logLevel = .error
Purchases.configure(withAPIKey: "appl_JXmiRckOzXXTsHKitQiicXCvMQi")

View File

@ -0,0 +1,68 @@
import SwiftUI
#if canImport(UIKit)
import UIKit
#endif
public extension View {
func applyTheme(_ theme: Theme) -> some View {
modifier(ThemeApplier(theme: theme))
}
}
struct ThemeApplier: ViewModifier {
@ObservedObject var theme: Theme
func body(content: Content) -> some View {
content
.tint(theme.tintColor)
.preferredColorScheme(theme.selectedScheme == ColorScheme.dark ? .dark : .light)
#if canImport(UIKit)
.onAppear {
setWindowTint(theme.tintColor)
setWindowUserInterfaceStyle(theme.selectedScheme)
setBarsColor(theme.primaryBackgroundColor)
}
.onChange(of: theme.tintColor) { newValue in
setWindowTint(newValue)
}
.onChange(of: theme.selectedScheme) { newValue in
setWindowUserInterfaceStyle(newValue)
}
.onChange(of: theme.primaryBackgroundColor) { newValue in
setBarsColor(newValue)
}
#endif
}
#if canImport(UIKit)
private func setWindowUserInterfaceStyle(_ colorScheme: ColorScheme) {
allWindows()
.forEach {
switch colorScheme {
case .dark:
$0.overrideUserInterfaceStyle = .dark
case .light:
$0.overrideUserInterfaceStyle = .light
}
}
}
private func setWindowTint(_ color: Color) {
allWindows()
.forEach {
$0.tintColor = UIColor(color)
}
}
private func setBarsColor(_ color: Color) {
UINavigationBar.appearance().isTranslucent = true
UINavigationBar.appearance().barTintColor = UIColor(color)
}
private func allWindows() -> [UIWindow] {
UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
}
#endif
}