Last active
July 27, 2019 13:09
-
-
Save magi82/fb9036c28898eb947655f97986313289 to your computer and use it in GitHub Desktop.
Hookable.swift
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
protocol Hookable { | |
associatedtype HookModelType | |
var hook: PublishRelay<HookModelType> { get } | |
} | |
private var hookKey: String = "hook" | |
extension Hookable { | |
var hook: PublishRelay<HookModelType> { | |
get { | |
if let value = objc_getAssociatedObject(self, &hookKey) as? PublishRelay<HookModelType> { | |
return value | |
} | |
let hook: PublishRelay<HookModelType> = PublishRelay() | |
objc_setAssociatedObject(self, | |
&hookKey, | |
hook, | |
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) | |
return hook | |
} | |
} | |
} |
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
protocol HookModelProtocol { | |
associatedtype HookModel | |
} |
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
enum HookUserCase: HookModelProtocol { | |
enum HookModel { | |
case article(Article) | |
case user(User) | |
} | |
struct Article { | |
let name: String | |
} | |
struct User { | |
let id: Int | |
} | |
} |
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
class ViewController: UIViewController, Hookable { | |
typealias HookModelType = HookUserCase.HookModel | |
var disposeBag: DisposeBag = .init() | |
deinit { | |
debugPrint("DEINIT: ViewController") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
hook.asObservable() | |
.map { hook -> HookUserCase.Article? in | |
guard case HookUserCase.HookModel.article(let value) = hook else { return nil } | |
return value | |
} | |
.filter { $0 != nil } | |
.debug("article") | |
.subscribe() | |
.disposed(by: disposeBag) | |
hook.asObservable() | |
.map { hook -> HookUserCase.User? in | |
guard case HookUserCase.HookModel.user(let value) = hook else { return nil } | |
return value | |
} | |
.filter { $0 != nil } | |
.debug("user") | |
.subscribe() | |
.disposed(by: disposeBag) | |
hook.accept(HookModelType.article(HookUserCase.Article.init(name: "test"))) | |
hook.accept(HookModelType.user(.init(id: 20))) | |
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { | |
UIApplication.shared.keyWindow?.rootViewController = nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment