Skip to content

Commit a004d5f

Browse files
author
Fábio Oliveira
committed
Cleaned the PostList API a bit with a list of posts and a selected post id init parameters;
Using a computed property for the list and navigation link selections; Listing now has a computed property to return an array of Posts.
1 parent d55d1f0 commit a004d5f

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

Reddit-macOS/ContentView.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct ContentView : View {
1515
@State private var showSortSheet: Bool = false
1616
@State private var showSubredditSheet: Bool = false
1717

18-
@State private var selectedPost: String? = nil
18+
@State private var selectedPostId: String? = nil
1919

2020
@EnvironmentObject private var state: ContentViewState
2121

@@ -26,8 +26,13 @@ struct ContentView : View {
2626
Url(API.subredditURL(state.subreddit, sortBy))
2727
Query(["raw_json":"1"])
2828
}) { listing in
29-
PostList(listing: listing, subreddit: self.state.subreddit, sortBy: self.state.sortBy)
29+
if listing != nil {
30+
PostList(posts: listing!.posts, subreddit: self.state.subreddit, sortBy: self.state.sortBy, selectedPostId: self.$selectedPostId)
3031
.frame(minWidth: 300)
32+
}
33+
else {
34+
Text("Error while loading posts").frame(minWidth: 300, minHeight: 300)
35+
}
3136
/// Spinner when loading
3237
SpinnerView()
3338
.frame(minWidth: 300, minHeight: 300)

Reddit-macOS/Views/PostList.swift

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,64 @@ import SwiftUI
1010
import Request
1111

1212
struct PostList: View {
13-
let listing: Listing?
13+
let posts: [Post]
1414
let subreddit: String
1515
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+
}
1945

2046
var body: some View {
21-
List(selection: $selectedPostIds) {
47+
List(selection: selectedPostIds) {
2248
Section(header: Text("\(subreddit) | \(sortBy.rawValue)")) {
2349
/// List of `PostView`s when loaded
24-
ForEach(listing != nil ? listing!.data.children.map { $0.data } : []) { post in
50+
ForEach(posts) { post in
2551
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() }
2753
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-
}
4454
}
4555
.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+
}
4671
}
4772
}
4873
}

Shared/Models/Listing.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import Foundation
1111
/// Root of Reddit API response
1212
struct Listing: Decodable {
1313
let data: ListingData
14+
var posts: [Post] {
15+
return data.children.map { (postData) -> Post in
16+
postData.data
17+
}
18+
}
1419

1520
struct ListingData: Decodable {
1621
let children: [PostData]

0 commit comments

Comments
 (0)