Skip to content

Commit 74f1bb8

Browse files
author
Fábio Oliveira
committed
Fixing #9
Quite a lot of changes in order to achieve this: - had to activate the navigation link programmatically in order to capture single and double tap - NavigationLink activated based on new view state for selected post tag - moved the RequestView to the ContentView so the selected post wouldn't reload the whole PostList and retriever the request - PostList now initialised with Listing object - setting the contentShape of the PostView in the list in order to capture hits in the whole area, otherwise it would land on the NavigationLink behind - adding two tap gestures in order of precedence - set the default frame for the new spinner to guarantee a minimum size while loading The only secondary effect from this solution I wasn't able to fix is that now cells don't get a selected state like before. I accept suggestions.
1 parent cab1e95 commit 74f1bb8

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

Reddit-macOS/ContentView.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ struct ContentView : View {
2222
var body: some View {
2323
NavigationView {
2424
/// Load the posts
25-
PostList(subreddit: state.subreddit, sortBy: state.sortBy)
26-
.frame(minWidth: 300)
25+
RequestView(Listing.self, Request {
26+
Url(API.subredditURL(state.subreddit, sortBy))
27+
Query(["raw_json":"1"])
28+
}) { listing in
29+
PostList(listing: listing, subreddit: self.state.subreddit, sortBy: self.state.sortBy)
30+
.frame(minWidth: 300)
31+
/// Spinner when loading
32+
SpinnerView()
33+
.frame(minWidth: 300, minHeight: 300)
34+
}
2735
Text("Select a post")
2836
.frame(maxWidth: .infinity, maxHeight: .infinity)
2937
}

Reddit-macOS/Views/PostList.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,36 @@ import SwiftUI
1010
import Request
1111

1212
struct PostList: View {
13+
let listing: Listing?
1314
let subreddit: String
1415
let sortBy: SortBy
1516

17+
@State private var selection: String? = nil
18+
1619
var body: some View {
1720
List {
1821
Section(header: Text("\(subreddit) | \(sortBy.rawValue)")) {
19-
/// Load posts from web and decode as `Listing`
20-
RequestView(Listing.self, Request {
21-
Url(API.subredditURL(subreddit, sortBy))
22-
Query(["raw_json":"1"])
23-
}) { listing in
24-
/// List of `PostView`s when loaded
25-
ForEach(listing != nil ? listing!.data.children.map { $0.data } : []) { post in
26-
NavigationLink(destination: PostDetailView(post: post)) {
27-
PostView(post: post)
22+
/// List of `PostView`s when loaded
23+
ForEach(listing != nil ? listing!.data.children.map { $0.data } : []) { post in
24+
VStack {
25+
NavigationLink(destination: PostDetailView(post: post), tag: post.id, selection: self.$selection) { EmptyView() }
26+
PostView(post: post)
2827
.tag(post.id)
2928
.padding(EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 0))
29+
.contentShape(Rectangle())
3030
/// Double-click to open a new window for the `PostDetailView`
3131
.onTapGesture(count: 2) {
32-
let controller = DetailWindowController(rootView: PostDetailView(post: post))
32+
let detailView = PostDetailView(post: post)
33+
34+
let controller = DetailWindowController(rootView: detailView)
3335
controller.window?.title = post.title
3436
controller.showWindow(nil)
35-
}
37+
}
38+
/// Adding after the double tap so that double tap takes precedence
39+
.onTapGesture(count: 1) {
40+
self.selection = post.id
3641
}
3742
}
38-
/// Spinner when loading
39-
SpinnerView()
4043
}
4144
}
4245
}

0 commit comments

Comments
 (0)