Last active
July 31, 2020 04:39
-
-
Save serhiybutz/8c1a180392b6ccbd7f47d452cdd2f641 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 | |
fileprivate var associatedKey = "ThemerTargetActionStorage" | |
final class TargetActionStorage { | |
private var targetActions: [TargetAction] = [] | |
static func setup(for target: Any) -> TargetActionStorage { | |
let storage = TargetActionStorage() | |
objc_setAssociatedObject( | |
target, | |
&associatedKey, | |
storage, | |
.OBJC_ASSOCIATION_RETAIN_NONATOMIC | |
) | |
return storage | |
} | |
static func get(for target: Any) -> TargetActionStorage? { | |
return objc_getAssociatedObject(target, &associatedKey) | |
.map { $0 as! TargetActionStorage } | |
} | |
func register<Target: AnyObject>( | |
target: Target, | |
action: @escaping (Target) -> (MyTheme) -> (), | |
initialTheme: MyTheme) | |
{ | |
let ta = AnyTargetActionWrapper(target: target, action: action) | |
targetActions.append(ta) | |
ta.applyTheme(initialTheme) | |
} | |
func applyTheme(_ theme: MyTheme) { | |
targetActions.forEach { | |
$0.applyTheme(theme) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment