Skip to content

Instantly share code, notes, and snippets.

@serhiybutz
Last active August 1, 2020 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serhiybutz/7ab4e40aa17dbf264e2102a7aeca73cf to your computer and use it in GitHub Desktop.
Save serhiybutz/7ab4e40aa17dbf264e2102a7aeca73cf to your computer and use it in GitHub Desktop.
import Foundation
final class ThemeManager {
private var themables = NSHashTable<Themable>
.weakObjects()
var theme: MyTheme {
didSet {
guard theme != oldValue else { return; }
apply()
}
}
private static var instance: ThemeManager?
static var shared: ThemeManager {
if instance == nil {
instance = ThemeManager(defaultTheme: .light)
}
return instance!
}
private init(defaultTheme: MyTheme) {
self.theme = defaultTheme
}
func register(_ themable: Themable) {
themables.add(themable)
themable.applyTheme(theme)
}
private func apply() {
themables.allObjects.forEach {
$0.applyTheme(theme)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment