Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created February 8, 2022 21:03
Show Gist options
  • Save christianselig/eca9fa149a917fbf8eaf6f5028469aeb to your computer and use it in GitHub Desktop.
Save christianselig/eca9fa149a917fbf8eaf6f5028469aeb to your computer and use it in GitHub Desktop.
import UIKit
import SwiftUI
// 🐧 Manager Singleton
class SomeThemeManager: ObservableObject {
static var shared = SomeThemeManager()
@Published var currentTheme = SomeTheme.normal
}
// 🎨 Theme
struct SomeTheme {
let mainColor: UIColor
static let normal = SomeTheme(mainColor: UIColor(hexcode: "0000FF"))
static let emerald = SomeTheme(mainColor: UIColor(hexcode: "31AD00"))
}
// UIKit 🦕
class ViewController2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let hostingController = UIHostingController(rootView: FunTimeView())
addChild(hostingController)
hostingController.view.frame = view.bounds
view.addSubview(hostingController.view)
// Simulate a button press
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
SomeThemeManager.shared.currentTheme = .emerald
}
}
}
// SwiftUI 👶
struct FunTimeView: View {
@StateObject private var manager: SomeThemeManager = .shared
var body: some View {
Text("Hi there!")
.foregroundColor(Color(manager.currentTheme.mainColor))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment