@@ -8,7 +8,7 @@ import SwiftUI
8
8
9
9
/// A container that presents items of variable heights arranged in a grid.
10
10
@available ( iOS 13 , OSX 10 . 15 , tvOS 13 , visionOS 1 , watchOS 6 , * )
11
- public struct WaterfallGrid < Data, ID, Content> : View where Data : RandomAccessCollection , Content : View , ID : Hashable {
11
+ public struct WaterfallGrid < Data, ID, Content> : View where Data : RandomAccessCollection , Content : View , ID : Hashable & Sendable {
12
12
13
13
@Environment ( \. gridStyle) private var style
14
14
@Environment ( \. scrollOptions) private var scrollOptions
@@ -20,7 +20,7 @@ public struct WaterfallGrid<Data, ID, Content>: View where Data : RandomAccessCo
20
20
@State private var loaded = false
21
21
@State private var gridHeight : CGFloat = 0
22
22
23
- @State private var alignmentGuides = [ AnyHashable : CGPoint] ( ) {
23
+ @State private var alignmentGuides = [ AnyHashableAndSendable : CGPoint] ( ) {
24
24
didSet { loaded = !oldValue. isEmpty }
25
25
}
26
26
@@ -29,12 +29,12 @@ public struct WaterfallGrid<Data, ID, Content>: View where Data : RandomAccessCo
29
29
GeometryReader { geometry in
30
30
self . grid ( in: geometry)
31
31
. onPreferenceChange ( ElementPreferenceKey . self, perform: { preferences in
32
- DispatchQueue . global ( qos : . userInteractive ) . async {
33
- let ( alignmentGuides, gridHeight) = self . alignmentsAndGridHeight ( columns: self . style. columns,
34
- spacing: self . style. spacing,
35
- scrollDirection: self . scrollOptions. direction,
36
- preferences: preferences)
37
- DispatchQueue . main . async {
32
+ Task . detached ( priority : . userInitiated ) {
33
+ let ( alignmentGuides, gridHeight) = await self . alignmentsAndGridHeight ( columns: self . style. columns,
34
+ spacing: self . style. spacing,
35
+ scrollDirection: self . scrollOptions. direction,
36
+ preferences: preferences)
37
+ await MainActor . run {
38
38
self . alignmentGuides = alignmentGuides
39
39
self . gridHeight = gridHeight
40
40
}
@@ -55,20 +55,20 @@ public struct WaterfallGrid<Data, ID, Content>: View where Data : RandomAccessCo
55
55
self . content ( element)
56
56
. frame ( width: self . scrollOptions. direction == . vertical ? columnWidth : nil ,
57
57
height: self . scrollOptions. direction == . horizontal ? columnWidth : nil )
58
- . background ( PreferenceSetter ( id: element [ keyPath: self . dataId] ) )
59
- . alignmentGuide ( . top, computeValue: { _ in self . alignmentGuides [ element [ keyPath: self . dataId] ] ? . y ?? 0 } )
60
- . alignmentGuide ( . leading, computeValue: { _ in self . alignmentGuides [ element [ keyPath: self . dataId] ] ? . x ?? 0 } )
61
- . opacity ( self . alignmentGuides [ element [ keyPath: self . dataId] ] != nil ? 1 : 0 )
58
+ . background ( PreferenceSetter ( id: AnyHashableAndSendable ( element [ keyPath: self . dataId] ) ) )
59
+ . alignmentGuide ( . top, computeValue: { _ in self . alignmentGuides [ AnyHashableAndSendable ( element [ keyPath: self . dataId] ) ] ? . y ?? 0 } )
60
+ . alignmentGuide ( . leading, computeValue: { _ in self . alignmentGuides [ AnyHashableAndSendable ( element [ keyPath: self . dataId] ) ] ? . x ?? 0 } )
61
+ . opacity ( self . alignmentGuides [ AnyHashableAndSendable ( element [ keyPath: self . dataId] ) ] != nil ? 1 : 0 )
62
62
}
63
63
}
64
64
. animation ( self . loaded ? self . style. animation : nil , value: UUID ( ) )
65
65
}
66
66
67
67
// MARK: - Helpers
68
68
69
- func alignmentsAndGridHeight( columns: Int , spacing: CGFloat , scrollDirection: Axis . Set , preferences: [ ElementPreferenceData ] ) -> ( [ AnyHashable : CGPoint ] , CGFloat ) {
69
+ func alignmentsAndGridHeight( columns: Int , spacing: CGFloat , scrollDirection: Axis . Set , preferences: [ ElementPreferenceData ] ) -> ( [ AnyHashableAndSendable : CGPoint ] , CGFloat ) {
70
70
var heights = Array ( repeating: CGFloat ( 0 ) , count: columns)
71
- var alignmentGuides = [ AnyHashable : CGPoint] ( )
71
+ var alignmentGuides = [ AnyHashableAndSendable : CGPoint] ( )
72
72
73
73
preferences. forEach { preference in
74
74
if let minValue = heights. min ( ) , let indexMin = heights. firstIndex ( of: minValue) {
0 commit comments