@@ -10,39 +10,64 @@ import SwiftUI
10
10
import Request
11
11
12
12
struct PostList : View {
13
- let listing : Listing ?
13
+ let posts : [ Post ]
14
14
let subreddit : String
15
15
let sortBy : SortBy
16
-
17
- @State private var selection : String ? = nil
18
- @State private var selectedPostIds : Set < String > = Set ( )
16
+
17
+ @Binding var selectedPostId : String ?
18
+
19
+ private var selectedPostIds : Binding < Set < String > > {
20
+ Binding (
21
+ get: {
22
+ if let selectedPostId = self . selectedPostId {
23
+ return Set ( arrayLiteral: selectedPostId)
24
+ }
25
+ else {
26
+ return Set ( )
27
+ }
28
+ } ,
29
+ set: {
30
+ self . selectedPostId = $0. first
31
+ }
32
+ )
33
+ }
34
+
35
+ private var selectedNavigationLink : Binding < String ? > {
36
+ Binding < String ? > (
37
+ get: {
38
+ return self . selectedPostId
39
+ } ,
40
+ set: { selectedPostId in
41
+ // Absorbing any change that NavigationLink does to its selection property
42
+ }
43
+ )
44
+ }
19
45
20
46
var body : some View {
21
- List ( selection: $ selectedPostIds) {
47
+ List ( selection: selectedPostIds) {
22
48
Section ( header: Text ( " \( subreddit) | \( sortBy. rawValue) " ) ) {
23
49
/// List of `PostView`s when loaded
24
- ForEach ( listing != nil ? listing! . data . children . map { $0 . data } : [ ] ) { post in
50
+ ForEach ( posts ) { post in
25
51
VStack {
26
- NavigationLink ( destination: PostDetailView ( post: post) , tag: post. id, selection: self . $selection ) { EmptyView ( ) }
52
+ NavigationLink ( destination: PostDetailView ( post: post) , tag: post. id, selection: self . selectedNavigationLink ) { EmptyView ( ) }
27
53
PostView ( post: post)
28
- . tag ( post. id)
29
- . padding ( EdgeInsets ( top: 5 , leading: 0 , bottom: 5 , trailing: 0 ) )
30
- . contentShape ( Rectangle ( ) )
31
- /// Double-click to open a new window for the `PostDetailView`
32
- . onTapGesture ( count: 2 ) {
33
- let detailView = PostDetailView ( post: post)
34
-
35
- let controller = DetailWindowController ( rootView: detailView)
36
- controller. window? . title = post. title
37
- controller. showWindow ( nil )
38
- }
39
- /// Adding after the double tap so that double tap takes precedence
40
- . onTapGesture ( count: 1 ) {
41
- self . selection = post. id
42
- self . selectedPostIds = Set ( arrayLiteral: post. id)
43
- }
44
54
}
45
55
. tag ( post. id)
56
+ . padding ( EdgeInsets ( top: 5 , leading: 0 , bottom: 5 , trailing: 0 ) )
57
+ . contentShape ( Rectangle ( ) )
58
+
59
+ /// Double-click to open a new window for the `PostDetailView`
60
+ . onTapGesture ( count: 2 ) {
61
+ let detailView = PostDetailView ( post: post)
62
+
63
+ let controller = DetailWindowController ( rootView: detailView)
64
+ controller. window? . title = post. title
65
+ controller. showWindow ( nil )
66
+ }
67
+ /// Adding after the double tap so that double tap takes precedence
68
+ . onTapGesture ( count: 1 ) {
69
+ self . selectedPostId = post. id
70
+ }
46
71
}
47
72
}
48
73
}
0 commit comments