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
struct MyThemeSettings { | |
let appBgColor: UIColor | |
let highlightedBgColor: UIColor | |
let textColor: UIColor | |
} | |
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
let lightThemeModel = MyThemeModel( | |
appBgColor: .white, | |
textColor: .black) | |
let darkThemeModel = MyThemeModel( | |
appBgColor: .darkGray, | |
textColor: .white) |
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
enum MyTheme { | |
case light | |
case dark | |
var settings: MyThemeSettings { | |
switch self { | |
case .light: return MyThemeSettings.lightTheme | |
case .dark: return MyThemeSettings.darkTheme | |
} | |
} | |
} |
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
@objc protocol Themable { | |
func applyTheme(_ theme: MyTheme) | |
} |
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() |
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
@objc enum MyTheme: Int { | |
case light | |
case dark | |
var settings: MyThemeSettings { | |
switch self { | |
case .light: return MyThemeSettings.lightTheme | |
case .dark: return MyThemeSettings.darkTheme | |
} | |
} | |
} |
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 UIKit | |
// MARK: - Highlighted Label Class | |
class HighlightedLabel: UILabel { | |
override func didMoveToWindow() { | |
// Subscribe to theme notifications | |
ThemeManager.shared.register(self) | |
super.didMoveToWindow() | |
} | |
} |
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 | |
fileprivate var associatedKey = "ThemerTargetActionStorage" | |
final class TargetActionStorage { | |
private var targetActions: [TargetAction] = [] | |
static func setup(for target: Any) -> TargetActionStorage { | |
let storage = TargetActionStorage() | |
objc_setAssociatedObject( |
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
protocol TargetAction { | |
func applyTheme(_ theme: MyTheme) | |
} | |
struct AnyTargetActionWrapper<T: AnyObject>: TargetAction { | |
weak var target: T? | |
let action: (T) -> (MyTheme) -> () | |
func applyTheme(_ theme: MyTheme) -> () { | |
if let t = target { |
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 targetActionStorages = NSHashTable<TargetActionStorage> | |
.weakObjects() | |
var theme: MyTheme { | |
didSet { | |
guard theme != oldValue else { return; } | |
apply() |
OlderNewer