Skip to content

Commit bde3c30

Browse files
committed
Update Combine README
1 parent 35abfba commit bde3c30

3 files changed

Lines changed: 46 additions & 15 deletions

File tree

Demo/Demo/Preview1ViewController.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class Preview1ViewController: UIViewController {
2121
@Published var brief = "The best way to use HStack and VStack in UIKit, and also supports Spacer and Divider."
2222

2323
var cancellables: Set<AnyCancellable> = []
24+
25+
deinit {
26+
// ⚠️ important
27+
// cleanup at deinit
28+
cancellables.forEach({ $0.cancel() })
29+
}
2430

2531
override func viewDidLoad() {
2632
super.viewDidLoad()
@@ -51,30 +57,24 @@ class Preview1ViewController: UIViewController {
5157
container.addContent {
5258
logoView.stack.size(80)
5359
VStackView(alignment: .left, distribution: .spacing(6)) {
54-
nameLabel.stack
60+
nameLabel
61+
.stack
5562
.receive(text: $name, storeIn: &cancellables)
5663

57-
briefLabel.stack.maxWidth(220)
64+
briefLabel
65+
.stack.maxWidth(220)
5866
.receive(text: $brief, storeIn: &cancellables)
5967
}
6068
}
6169

6270
self.view.addSubview(container)
6371

72+
// update combine value and refresh layout container
6473
DispatchQueue.main.asyncAfter(wallDeadline: .now() + 2) {
65-
print("done")
66-
self.name = "StackKit design by iWECon"
67-
self.brief = "Yes!!!"
74+
self.name = "Designed by iWECon"
75+
self.brief = "Version: 1.2.3"
6876
self.container.setNeedsLayout()
6977
}
70-
71-
DispatchQueue.global().asyncAfter(wallDeadline: .now() + 5) {
72-
self.name = "StackKit design by iWECon1"
73-
self.brief = "Yes!!!2"
74-
DispatchQueue.main.async {
75-
self.container.setNeedsLayout()
76-
}
77-
}
7878
}
7979

8080
override func viewDidLayoutSubviews() {

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
☝️ A layout library similar to SwiftUI.
44

5+
🔥 Support `Combine` (available iOS 13.0+)
6+
57
⚠️ If you have any questions or new ideas, you can bring me a PR at any time.
68

79

@@ -23,6 +25,8 @@ HStackView(alignment: .center, distribution: .spacing(14), padding: UIEdgeInsets
2325
![Demo](Demo/preview2.png)
2426

2527

28+
⚠️ If you have any question, please see the demo in `Demo/Demo.xcodeproj` or submit issue.
29+
2630
💡 There are no more examples, you can help to complete/optimize.
2731

2832

@@ -85,7 +89,6 @@ VStackView {
8589
}
8690
```
8791

88-
8992
### Change and update
9093

9194
```swift
@@ -96,6 +99,34 @@ briefLabel.text = "Bump version to 1.2.2"
9699
stackContainer.setNeedsLayout() // or .sizeToFit()
97100
```
98101

102+
### Combine
103+
104+
```swift
105+
106+
import Combine
107+
108+
// ...
109+
@Published var name: String = "StackKit"
110+
111+
var cancellables = Set<AnyCancellable>()
112+
113+
// ...
114+
HStackView {
115+
UILabel()
116+
.stack
117+
.then {
118+
$0.font = .systemFont(ofSize: 16)
119+
$0.textColor = .systemPink
120+
}
121+
.receive(text: $name, storeIn: &cancellables)
122+
}
123+
124+
// update name
125+
self.name = "StackKit version 1.2.3"
126+
// update stackView
127+
stackView.setNeedsLayout()
128+
```
129+
99130
# 🤔
100131

101132
I'm not very good at writing documents. If you have the advantages in this regard, please submit PR to me. Thank you very much.

Sources/StackKit/UIView+StackKit/UIView+Combine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ extension StackKitCompatible where Base: UIView {
5151
}
5252
}
5353

54-
5554
/**
5655
这里由于设计逻辑,会有个问题
5756

@@ -61,6 +60,7 @@ extension StackKitCompatible where Base: UIView {
6160
这样就会导致一个问题:
6261
❌ 在主线程中修改值,并触发 `container.setNeedsLayout()` 的时候,
6362
`setNeedsLayout` 会先执行,而 `publisher` 会将任务派发到下一个 loop cycle (也就是 setNeedsLayout 和 receive 先后执行的问题)
63+
所以这里采用 `safetyAccessUI` 来处理线程问题
6464
*/
6565

6666
@available(iOS 13.0, *)

0 commit comments

Comments
 (0)