Last active
August 1, 2020 08:27
-
-
Save serhiybutz/7ab4e40aa17dbf264e2102a7aeca73cf to your computer and use it in GitHub Desktop.
This file contains 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 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