|
| 1 | +// |
| 2 | +// File.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by bro on 2022/11/22. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | +import Combine |
| 10 | + |
| 11 | +fileprivate func safetyAccessUI(_ closure: @escaping () -> Void) { |
| 12 | + if Thread.isMainThread { |
| 13 | + closure() |
| 14 | + } else { |
| 15 | + DispatchQueue.main.async { |
| 16 | + closure() |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +@available(iOS 13.0, *) |
| 22 | +extension StackKitCompatible where Base: UIView { |
| 23 | + |
| 24 | + @discardableResult |
| 25 | + public func receive<Value>( |
| 26 | + publisher: Published<Value>.Publisher, |
| 27 | + storeIn cancellables: inout Set<AnyCancellable>, |
| 28 | + sink receiveValue: @escaping ((Base, Published<Value>.Publisher.Output) -> Void) |
| 29 | + ) -> Self |
| 30 | + { |
| 31 | + let v = self.view |
| 32 | + publisher.sink { [weak v] output in |
| 33 | + guard let v else { return } |
| 34 | + receiveValue(v, output) |
| 35 | + }.store(in: &cancellables) |
| 36 | + return self |
| 37 | + } |
| 38 | + |
| 39 | + @discardableResult |
| 40 | + public func receive( |
| 41 | + isHidden publisher: Published<Bool>.Publisher, |
| 42 | + storeIn cancellables: inout Set<AnyCancellable> |
| 43 | + ) -> Self |
| 44 | + { |
| 45 | + receive(publisher: publisher, storeIn: &cancellables) { view, output in |
| 46 | + safetyAccessUI { |
| 47 | + view.isHidden = output |
| 48 | + } |
| 49 | + } |
| 50 | + return self |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +/** |
| 56 | + 这里由于设计逻辑,会有个问题 |
| 57 | + |
| 58 | + 系统提供的 receive(on: DispatchQueue.main) 虽然也可以 |
| 59 | + ⚠️ 但是:DispatchQueue 是个调度器 |
| 60 | + 任务添加后需要等到下一个 loop cycle 才会执行 |
| 61 | + 这样就会导致一个问题: |
| 62 | + ❌ 在主线程中修改值,并触发 `container.setNeedsLayout()` 的时候, |
| 63 | + `setNeedsLayout` 会先执行,而 `publisher` 会将任务派发到下一个 loop cycle (也就是 setNeedsLayout 和 receive 先后执行的问题) |
| 64 | + */ |
| 65 | + |
| 66 | +@available(iOS 13.0, *) |
| 67 | +extension StackKitCompatible where Base: UILabel { |
| 68 | + |
| 69 | + @discardableResult |
| 70 | + public func receive( |
| 71 | + text publisher: Published<String>.Publisher, |
| 72 | + storeIn cancellables: inout Set<AnyCancellable> |
| 73 | + ) -> Self |
| 74 | + { |
| 75 | + receive(publisher: publisher, storeIn: &cancellables) { view, output in |
| 76 | + safetyAccessUI { |
| 77 | + view.text = output |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + @discardableResult |
| 84 | + public func receive( |
| 85 | + text publisher: Published<String?>.Publisher, |
| 86 | + storeIn cancellables: inout Set<AnyCancellable> |
| 87 | + ) -> Self |
| 88 | + { |
| 89 | + receive(publisher: publisher, storeIn: &cancellables) { view, output in |
| 90 | + safetyAccessUI { |
| 91 | + view.text = output |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @discardableResult |
| 97 | + public func receive( |
| 98 | + attributedText publisher: Published<NSAttributedString>.Publisher, |
| 99 | + storeIn cancellables: inout Set<AnyCancellable> |
| 100 | + ) -> Self |
| 101 | + { |
| 102 | + receive(publisher: publisher, storeIn: &cancellables) { view, output in |
| 103 | + safetyAccessUI { |
| 104 | + view.attributedText = output |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @discardableResult |
| 110 | + public func receive( |
| 111 | + attributedText publisher: Published<NSAttributedString?>.Publisher, |
| 112 | + storeIn cancellables: inout Set<AnyCancellable> |
| 113 | + ) -> Self |
| 114 | + { |
| 115 | + receive(publisher: publisher, storeIn: &cancellables) { view, output in |
| 116 | + safetyAccessUI { |
| 117 | + view.attributedText = output |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments