@craft-native/ios 0.0.254 → 0.0.272
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -6
- package/package.json +1 -1
- package/templates/CraftApp.swift +55 -7
- package/templates/CraftHealth.swift +38 -0
- package/templates/CraftHybrid.swift +50 -7
- package/templates/CraftNativeActions.swift +109 -22
- package/templates/CraftNativeFlatList.swift +187 -18
- package/templates/CraftNativeMutation.swift +3 -1
- package/templates/CraftNativeScreen.swift +466 -24
|
@@ -49,6 +49,8 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
|
|
|
49
49
|
|
|
50
50
|
private(set) var representedIdentity: String?
|
|
51
51
|
private(set) var hostedView: UIView?
|
|
52
|
+
/// Top, bottom, leading and trailing: the row's own margins inset it.
|
|
53
|
+
private var hostedConstraints: [NSLayoutConstraint] = []
|
|
52
54
|
var onRecycle: ((String) -> Void)?
|
|
53
55
|
override func prepareForReuse() {
|
|
54
56
|
super.prepareForReuse()
|
|
@@ -63,32 +65,66 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
|
|
|
63
65
|
onRecycle = nil
|
|
64
66
|
}
|
|
65
67
|
|
|
68
|
+
/// Whether the row view this cell is meant to show is in it. UIKit
|
|
69
|
+
/// can prepare a second cell for a row (a prefetch, an update), and
|
|
70
|
+
/// the row's one view then moves there: the first cell keeps its
|
|
71
|
+
/// height and draws nothing.
|
|
72
|
+
var showsHostedView: Bool { hostedView.map { $0.superview === contentView } ?? true }
|
|
73
|
+
|
|
66
74
|
func host(_ view: UIView, identity: String) {
|
|
67
75
|
if let shown = representedIdentity, shown != identity { recycle() }
|
|
68
76
|
representedIdentity = identity
|
|
69
|
-
|
|
70
|
-
|
|
77
|
+
// The same view, already here. A view that left for another cell
|
|
78
|
+
// and is asked for again is put back below.
|
|
79
|
+
guard hostedView !== view || view.superview !== contentView else { return applyHostedMargins() }
|
|
80
|
+
// Only a view still in this cell is taken out of it: one that has
|
|
81
|
+
// since moved to another cell is that cell's now, and pulling it
|
|
82
|
+
// from there blanked a row on screen.
|
|
83
|
+
if let shown = hostedView, shown !== view, shown.superview === contentView { shown.removeFromSuperview() }
|
|
71
84
|
hostedView = view
|
|
72
85
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
73
86
|
contentView.addSubview(view)
|
|
74
|
-
|
|
87
|
+
hostedConstraints = [
|
|
75
88
|
view.topAnchor.constraint(equalTo: contentView.topAnchor),
|
|
76
|
-
|
|
89
|
+
contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
77
90
|
view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
|
78
|
-
|
|
79
|
-
]
|
|
91
|
+
contentView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
92
|
+
]
|
|
93
|
+
applyHostedMargins()
|
|
94
|
+
NSLayoutConstraint.activate(hostedConstraints)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// A row's margins (`mx-4` on a grouped row) inset it in its cell, as
|
|
98
|
+
/// a column inset it: pinned edge to edge, HQ's Discover results ran
|
|
99
|
+
/// to the screen's sides. Read again on every pass, since a style
|
|
100
|
+
/// update changes the row in place.
|
|
101
|
+
private func applyHostedMargins() {
|
|
102
|
+
guard let view = hostedView, view.superview === contentView, hostedConstraints.count == 4 else { return }
|
|
103
|
+
let margins = view.craftLayoutMargins
|
|
104
|
+
let constants = [margins.top, margins.bottom, margins.left, margins.right]
|
|
105
|
+
for (constraint, constant) in zip(hostedConstraints, constants) where constraint.constant != constant {
|
|
106
|
+
constraint.constant = constant
|
|
107
|
+
}
|
|
80
108
|
}
|
|
81
109
|
|
|
82
110
|
func releaseHostedView() {
|
|
83
111
|
recycle()
|
|
84
|
-
hostedView
|
|
112
|
+
if let shown = hostedView, shown.superview === contentView { shown.removeFromSuperview() }
|
|
85
113
|
hostedView = nil
|
|
86
114
|
}
|
|
87
115
|
|
|
116
|
+
/// Puts this cell's row view back when another cell took it.
|
|
117
|
+
func reclaimHostedView() {
|
|
118
|
+
guard let identity = representedIdentity, let view = hostedView, view.superview !== contentView else { return }
|
|
119
|
+
host(view, identity: identity)
|
|
120
|
+
}
|
|
121
|
+
|
|
88
122
|
override func preferredLayoutAttributesFitting(
|
|
89
123
|
_ layoutAttributes: UICollectionViewLayoutAttributes
|
|
90
124
|
) -> UICollectionViewLayoutAttributes {
|
|
91
125
|
guard let hostedView = hostedView else { return layoutAttributes }
|
|
126
|
+
applyHostedMargins()
|
|
127
|
+
let margins = hostedView.craftLayoutMargins
|
|
92
128
|
let attributes = layoutAttributes.copy() as! UICollectionViewLayoutAttributes
|
|
93
129
|
// A cell has no collectionView property; the list is its nearest
|
|
94
130
|
// collection view ancestor.
|
|
@@ -104,24 +140,26 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
|
|
|
104
140
|
let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout,
|
|
105
141
|
flowLayout.scrollDirection == .horizontal {
|
|
106
142
|
let height = fittingExtent ?? layoutAttributes.size.height
|
|
143
|
+
let inner = max(0, height - margins.top - margins.bottom)
|
|
107
144
|
let measured = CraftNativeFlexLayout.measure(
|
|
108
145
|
hostedView,
|
|
109
146
|
width: nil,
|
|
110
|
-
height:
|
|
147
|
+
height: inner,
|
|
111
148
|
maxWidth: CraftNativeFlexLayout.unbounded,
|
|
112
|
-
maxHeight:
|
|
149
|
+
maxHeight: inner
|
|
113
150
|
)
|
|
114
|
-
attributes.size = CGSize(width: max(1, ceil(measured.width)), height: max(1, height))
|
|
151
|
+
attributes.size = CGSize(width: max(1, ceil(measured.width + margins.left + margins.right)), height: max(1, height))
|
|
115
152
|
} else {
|
|
116
153
|
let width = fittingExtent ?? layoutAttributes.size.width
|
|
154
|
+
let inner = max(0, width - margins.left - margins.right)
|
|
117
155
|
let measured = CraftNativeFlexLayout.measure(
|
|
118
156
|
hostedView,
|
|
119
|
-
width:
|
|
157
|
+
width: inner,
|
|
120
158
|
height: nil,
|
|
121
|
-
maxWidth:
|
|
159
|
+
maxWidth: inner,
|
|
122
160
|
maxHeight: CraftNativeFlexLayout.unbounded
|
|
123
161
|
)
|
|
124
|
-
attributes.size = CGSize(width: max(1, width), height: max(1, ceil(measured.height)))
|
|
162
|
+
attributes.size = CGSize(width: max(1, width), height: max(1, ceil(measured.height + margins.top + margins.bottom)))
|
|
125
163
|
}
|
|
126
164
|
return attributes
|
|
127
165
|
}
|
|
@@ -167,6 +205,14 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
|
|
|
167
205
|
private var keyboardShouldPersistTaps = "never"
|
|
168
206
|
private var scrollTargetKey: String?
|
|
169
207
|
private var pendingScrollTarget: (id: String, animated: Bool, attempts: Int)?
|
|
208
|
+
/// A target that ends at the list's end, held to the true bottom while
|
|
209
|
+
/// the rows near it measure themselves. Rows self-size from a 44pt
|
|
210
|
+
/// estimate, so an offset worked out before they are measured stops short
|
|
211
|
+
/// of tall rows (multi-paragraph replies, cards). Cleared by a drag.
|
|
212
|
+
private var endSettlePasses: Int?
|
|
213
|
+
private var endSettleGeneration = 0
|
|
214
|
+
private var isAnimatingToEnd = false
|
|
215
|
+
private var isMeasuringEnd = false
|
|
170
216
|
|
|
171
217
|
init() {
|
|
172
218
|
let layout = CraftNativeFlatListLayout()
|
|
@@ -279,10 +325,22 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
|
|
|
279
325
|
override func layoutSubviews() {
|
|
280
326
|
updateItemEstimate()
|
|
281
327
|
super.layoutSubviews()
|
|
328
|
+
reclaimVisibleRows()
|
|
282
329
|
craftApplyCornerRadius()
|
|
283
330
|
scrollToPendingTarget()
|
|
284
331
|
}
|
|
285
332
|
|
|
333
|
+
/// A row on screen whose view went to a cell UIKit prepared for the same
|
|
334
|
+
/// row and then did not show gets it back, rather than staying blank.
|
|
335
|
+
private func reclaimVisibleRows() {
|
|
336
|
+
for case let cell as Cell in visibleCells where !cell.showsHostedView { cell.reclaimHostedView() }
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/// Every row on screen draws its own view (simulator-hosted XCTest).
|
|
340
|
+
var visibleRowsShowTheirViews: Bool {
|
|
341
|
+
visibleCells.allSatisfy { ($0 as? Cell)?.showsHostedView ?? true }
|
|
342
|
+
}
|
|
343
|
+
|
|
286
344
|
/// Scroll to a virtualized row identified by its native testID, id, or key.
|
|
287
345
|
/// The row need not be mounted yet; the data snapshot is enough to find its
|
|
288
346
|
/// index, and UIKit will mount it as part of the scroll.
|
|
@@ -310,6 +368,7 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
|
|
|
310
368
|
guard key != scrollTargetKey else { return }
|
|
311
369
|
scrollTargetKey = key
|
|
312
370
|
pendingScrollTarget = (id, animated, 0)
|
|
371
|
+
cancelEndSettle()
|
|
313
372
|
scrollToPendingTarget()
|
|
314
373
|
}
|
|
315
374
|
|
|
@@ -509,6 +568,7 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
|
|
|
509
568
|
pendingThemeRefresh = false
|
|
510
569
|
scrollTargetKey = nil
|
|
511
570
|
pendingScrollTarget = nil
|
|
571
|
+
cancelEndSettle()
|
|
512
572
|
}
|
|
513
573
|
|
|
514
574
|
/// Releases callbacks and UIKit ownership that belong to a permanently
|
|
@@ -540,15 +600,28 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
|
|
|
540
600
|
}
|
|
541
601
|
|
|
542
602
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
603
|
+
// The end is measured by jumping there and back in one pass, before
|
|
604
|
+
// anything draws: the screen hears only where the scroll goes.
|
|
605
|
+
guard !isMeasuringEnd else { return }
|
|
543
606
|
onScrollEvent?(scrollView)
|
|
544
607
|
onLayoutChanged?()
|
|
545
608
|
evaluateEndReached()
|
|
546
609
|
}
|
|
547
610
|
|
|
548
611
|
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
|
|
612
|
+
// The finger has the list now: stop holding it to its end.
|
|
613
|
+
cancelEndSettle()
|
|
549
614
|
onScrollBeginDrag?(scrollView)
|
|
550
615
|
}
|
|
551
616
|
|
|
617
|
+
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
|
|
618
|
+
guard isAnimatingToEnd else { return }
|
|
619
|
+
isAnimatingToEnd = false
|
|
620
|
+
// Rows passed on the way measured themselves and moved the end.
|
|
621
|
+
settleAtEnd()
|
|
622
|
+
scheduleEndSettle()
|
|
623
|
+
}
|
|
624
|
+
|
|
552
625
|
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
|
|
553
626
|
onScrollEndDrag?(scrollView)
|
|
554
627
|
}
|
|
@@ -607,12 +680,16 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
|
|
|
607
680
|
|
|
608
681
|
private func scrollToPendingTarget() {
|
|
609
682
|
guard let target = pendingScrollTarget else { return }
|
|
683
|
+
// A snapshot that is landing, or queued behind the one landing, can
|
|
684
|
+
// add rows before the target or move it: an index read now is stale
|
|
685
|
+
// (it scrolled to the old end). finishApplyingSnapshot() aims again
|
|
686
|
+
// once the rows are in, and so does a list not laid out yet.
|
|
687
|
+
guard !isApplyingSnapshot, pendingApply == nil, bounds.width > 0, bounds.height > 0 else { return }
|
|
610
688
|
guard let index = targetIndex(for: target.id), index < numberOfItems(inSection: 0) else {
|
|
689
|
+
// No row has this id once the list is idle: try a few more
|
|
690
|
+
// passes, then let it go.
|
|
611
691
|
if target.attempts >= 4 {
|
|
612
|
-
|
|
613
|
-
// for finishApplyingSnapshot(), but discard an invalid target
|
|
614
|
-
// once the list is otherwise idle.
|
|
615
|
-
if !isApplyingSnapshot { pendingScrollTarget = nil }
|
|
692
|
+
pendingScrollTarget = nil
|
|
616
693
|
return
|
|
617
694
|
}
|
|
618
695
|
pendingScrollTarget = (target.id, target.animated, target.attempts + 1)
|
|
@@ -620,8 +697,100 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
|
|
|
620
697
|
return
|
|
621
698
|
}
|
|
622
699
|
pendingScrollTarget = nil
|
|
700
|
+
let indexPath = IndexPath(item: index, section: 0)
|
|
701
|
+
let identity = diffableDataSource.itemIdentifier(for: indexPath) ?? items[index].identity
|
|
702
|
+
if index == numberOfItems(inSection: 0) - 1 {
|
|
703
|
+
scrollToEnd(animated: target.animated)
|
|
704
|
+
return
|
|
705
|
+
}
|
|
623
706
|
let position: UICollectionView.ScrollPosition = flowLayout.scrollDirection == .horizontal ? .left : .top
|
|
624
|
-
scrollToItem(at:
|
|
707
|
+
scrollToItem(at: indexPath, at: position, animated: target.animated)
|
|
708
|
+
guard !target.animated else { return }
|
|
709
|
+
// A row near the end cannot come to the top: the list stops at its
|
|
710
|
+
// end, which is only true once the rows there are measured.
|
|
711
|
+
layoutIfNeeded()
|
|
712
|
+
if rowReachesPastEnd(identity) { scrollToEnd(animated: false) }
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/// The offset that shows the end of the content, along the scroll axis.
|
|
716
|
+
private var endOffset: CGPoint {
|
|
717
|
+
if flowLayout.scrollDirection == .horizontal {
|
|
718
|
+
return CGPoint(
|
|
719
|
+
x: max(-adjustedContentInset.left, contentSize.width + adjustedContentInset.right - bounds.width),
|
|
720
|
+
y: contentOffset.y
|
|
721
|
+
)
|
|
722
|
+
}
|
|
723
|
+
return CGPoint(
|
|
724
|
+
x: contentOffset.x,
|
|
725
|
+
y: max(-adjustedContentInset.top, contentSize.height + adjustedContentInset.bottom - bounds.height)
|
|
726
|
+
)
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/// Whether the row's leading edge lies past where the list can scroll to.
|
|
730
|
+
private func rowReachesPastEnd(_ identity: String) -> Bool {
|
|
731
|
+
guard let indexPath = diffableDataSource.indexPath(for: identity),
|
|
732
|
+
let frame = layoutAttributesForItem(at: indexPath)?.frame else { return false }
|
|
733
|
+
if flowLayout.scrollDirection == .horizontal {
|
|
734
|
+
return frame.minX - adjustedContentInset.left >= endOffset.x - 0.5
|
|
735
|
+
}
|
|
736
|
+
return frame.minY - adjustedContentInset.top >= endOffset.y - 0.5
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
private func scrollToEnd(animated: Bool) {
|
|
740
|
+
endSettlePasses = 2
|
|
741
|
+
endSettleGeneration += 1
|
|
742
|
+
if animated {
|
|
743
|
+
// Measure the rows at the end where they will be, then animate
|
|
744
|
+
// there from where the list was, so the animation lands on the
|
|
745
|
+
// true end rather than on an estimate.
|
|
746
|
+
let start = contentOffset
|
|
747
|
+
isMeasuringEnd = true
|
|
748
|
+
settleAtEnd()
|
|
749
|
+
let end = contentOffset
|
|
750
|
+
setContentOffset(start, animated: false)
|
|
751
|
+
isMeasuringEnd = false
|
|
752
|
+
if abs(end.x - start.x) > 0.5 || abs(end.y - start.y) > 0.5 {
|
|
753
|
+
isAnimatingToEnd = true
|
|
754
|
+
setContentOffset(end, animated: true)
|
|
755
|
+
}
|
|
756
|
+
} else {
|
|
757
|
+
settleAtEnd()
|
|
758
|
+
}
|
|
759
|
+
scheduleEndSettle()
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/// Moves to the end until it stops moving: each move shows rows that
|
|
763
|
+
/// measure themselves and change the content size.
|
|
764
|
+
private func settleAtEnd() {
|
|
765
|
+
guard endSettlePasses != nil, !isDragging, !isTracking else { return cancelEndSettle() }
|
|
766
|
+
for _ in 0..<6 {
|
|
767
|
+
layoutIfNeeded()
|
|
768
|
+
let end = endOffset
|
|
769
|
+
guard abs(end.x - contentOffset.x) > 0.5 || abs(end.y - contentOffset.y) > 0.5 else { break }
|
|
770
|
+
setContentOffset(end, animated: false)
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/// Checks the end again on the next runloop passes, while late rows and
|
|
775
|
+
/// the content size settle.
|
|
776
|
+
private func scheduleEndSettle() {
|
|
777
|
+
let generation = endSettleGeneration
|
|
778
|
+
DispatchQueue.main.async { [weak self] in
|
|
779
|
+
guard let self, self.endSettleGeneration == generation, let passes = self.endSettlePasses,
|
|
780
|
+
// An animation on its way there settles, and checks
|
|
781
|
+
// again, when it ends.
|
|
782
|
+
!self.isAnimatingToEnd else { return }
|
|
783
|
+
if passes <= 0 { return self.cancelEndSettle() }
|
|
784
|
+
self.endSettlePasses = passes - 1
|
|
785
|
+
if !self.isApplyingSnapshot, self.pendingApply == nil { self.settleAtEnd() }
|
|
786
|
+
self.scheduleEndSettle()
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
private func cancelEndSettle() {
|
|
791
|
+
endSettlePasses = nil
|
|
792
|
+
endSettleGeneration += 1
|
|
793
|
+
isAnimatingToEnd = false
|
|
625
794
|
}
|
|
626
795
|
|
|
627
796
|
private func clamped(_ offset: CGPoint) -> CGPoint {
|
|
@@ -339,11 +339,13 @@ final class CraftNativeMutationDocument {
|
|
|
339
339
|
/// `Svg`). Only View, SafeAreaView and ScrollView used to be here, so a
|
|
340
340
|
/// drawing revealed after the first paint (an `:if` around an `<svg>`)
|
|
341
341
|
/// had its shapes refused as INVALID_PARENT, and the whole screen was
|
|
342
|
-
/// drawn again; Android's host already took the same list.
|
|
342
|
+
/// drawn again; Android's host already took the same list. A shape holds
|
|
343
|
+
/// its `Animate` (SMIL keyframes: HQ's moving exercise figures).
|
|
343
344
|
static let containerTypes: Set<String> = [
|
|
344
345
|
"View", "SafeAreaView", "ScrollView", "KeyboardAvoidingView", "Modal", "Pressable",
|
|
345
346
|
"TouchableOpacity", "TouchableHighlight", "ImageBackground", "FlatList", "SectionList",
|
|
346
347
|
"Fragment", "Svg", "G", "Map",
|
|
348
|
+
"Path", "Line", "Rect", "Circle", "Ellipse", "Polyline", "Polygon",
|
|
347
349
|
]
|
|
348
350
|
private static let containerTypeKeys = Set(containerTypes.map { $0.lowercased() })
|
|
349
351
|
|