-
-
Save chriswitko/ea348a537054f3c88d7ec2d0d48d4cf0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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