Skip to content

Commit adabf8e

Browse files
committed
Flair in PostDetailView
1 parent 9f3be28 commit adabf8e

File tree

7 files changed

+63
-4
lines changed

7 files changed

+63
-4
lines changed

Reddit-watchOS WatchKit Extension/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct ContentView: View {
5353
struct ContentView_Previews: PreviewProvider {
5454
static var previews: some View {
5555
List([0, 1, 2], id: \.self) { item in
56-
PostView(post: Post(title: "Hello World", name: "hello-world", id: "hw", selftext: "This is some body content. Blah blah\nblah blah blah", selftext_html: nil, thumbnail: "blahblah", url: "", author: "me", subreddit: "swift", score: 1000, num_comments: 50, stickied: true, created_utc: Date().timeIntervalSince1970, preview: nil, replies: nil))
56+
PostView(post: Post(title: "Hello World", name: "hello-world", id: "hw", selftext: "This is some body content. Blah blah\nblah blah blah", selftext_html: nil, thumbnail: "blahblah", url: "", author: "me", subreddit: "swift", score: 1000, num_comments: 50, stickied: true, created_utc: Date().timeIntervalSince1970, preview: nil, link_flair_text: "Hello World", is_original_content: true, spoiler: false, replies: nil))
5757
}
5858
.listStyle(CarouselListStyle())
5959
}

Reddit-watchOS WatchKit Extension/Views/PostDetailView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct PostDetailView: View {
3737
#if DEBUG
3838
struct PostDetailView_Previews: PreviewProvider {
3939
static var previews: some View {
40-
PostDetailView(post: Post(title: "Hello World | This is secondary text", name: "hello-world", id: "hw", selftext: "This is some body content. Blah blah\nblah blah blah", selftext_html: nil, thumbnail: "blahblah", url: "", author: "me", subreddit: "swift", score: 1000, num_comments: 50, stickied: true, created_utc: Date().timeIntervalSince1970, preview: nil, replies: nil))
40+
PostDetailView(post: Post(title: "Hello World | This is secondary text", name: "hello-world", id: "hw", selftext: "This is some body content. Blah blah\nblah blah blah", selftext_html: nil, thumbnail: "blahblah", url: "", author: "me", subreddit: "swift", score: 1000, num_comments: 50, stickied: true, created_utc: Date().timeIntervalSince1970, preview: nil, link_flair_text: "Hello World", is_original_content: true, spoiler: false, replies: nil))
4141
}
4242
}
4343
#endif

Reddit-watchOS WatchKit Extension/Views/PostView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct PostView: View {
4242
#if DEBUG
4343
struct PostView_Previews: PreviewProvider {
4444
static var previews: some View {
45-
PostView(post: Post(title: "Hello World | This is secondary text", name: "hello-world", id: "hw", selftext: "This is some body content. Blah blah\nblah blah blah", selftext_html: nil, thumbnail: "blahblah", url: "", author: "me", subreddit: "swift", score: 1000, num_comments: 50, stickied: true, created_utc: Date().timeIntervalSince1970, preview: nil, replies: nil))
45+
PostView(post: Post(title: "Hello World | This is secondary text", name: "hello-world", id: "hw", selftext: "This is some body content. Blah blah\nblah blah blah", selftext_html: nil, thumbnail: "blahblah", url: "", author: "me", subreddit: "swift", score: 1000, num_comments: 50, stickied: true, created_utc: Date().timeIntervalSince1970, preview: nil, link_flair_text: "Hello World", is_original_content: true, spoiler: false, replies: nil))
4646
}
4747
}
4848
#endif

Shared/Models/Post.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ struct Post: Decodable, Identifiable {
2626
let created_utc: Double
2727
let preview: Preview?
2828

29+
let link_flair_text: String?
30+
let is_original_content: Bool
31+
let spoiler: Bool
32+
33+
var flairs: [String] {
34+
var res: [String] = []
35+
if link_flair_text != nil {
36+
res.append(link_flair_text!)
37+
}
38+
if is_original_content {
39+
res.append("OC")
40+
}
41+
if spoiler {
42+
res.append("Spoiler")
43+
}
44+
return res
45+
}
46+
2947
let replies: [Self]?
3048

3149
struct Preview: Decodable {

Shared/Views/FlairView.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// TagView.swift
3+
// Reddit
4+
//
5+
// Created by Carson Katri on 8/9/19.
6+
// Copyright © 2019 Carson Katri. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
struct FlairView: View {
12+
let flairs: [String]
13+
14+
func flair(_ name: String) -> some View {
15+
Text(name)
16+
.font(.caption)
17+
.foregroundColor(.primary)
18+
.padding(5)
19+
.background(Color.secondary.opacity(0.5))
20+
.cornerRadius(4)
21+
}
22+
23+
var body: some View {
24+
HStack {
25+
ForEach(flairs, id: \.self) {
26+
self.flair($0)
27+
}
28+
}
29+
}
30+
}
31+
32+
#if DEBUG
33+
struct FlairView_Previews: PreviewProvider {
34+
static var previews: some View {
35+
FlairView(flairs: ["Hello", "World"])
36+
}
37+
}
38+
#endif

Shared/Views/MetadataView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct MetadataView: View {
5151
#if DEBUG
5252
struct MetadataView_Previews: PreviewProvider {
5353
static var previews: some View {
54-
MetadataView(post: Post(title: "Hello World | This is secondary text", name: "hello-world", id: "hw", selftext: "This is some body content. Blah blah\nblah blah blah", selftext_html: nil, thumbnail: "blahblah", url: "", author: "me", subreddit: "swift", score: 1000, num_comments: 50, stickied: true, created_utc: Date().timeIntervalSince1970, preview: nil, replies: nil), spaced: true)
54+
MetadataView(post: Post(title: "Hello World | This is secondary text", name: "hello-world", id: "hw", selftext: "This is some body content. Blah blah\nblah blah blah", selftext_html: nil, thumbnail: "blahblah", url: "", author: "me", subreddit: "swift", score: 1000, num_comments: 50, stickied: true, created_utc: Date().timeIntervalSince1970, preview: nil, link_flair_text: "Hello World", is_original_content: true, spoiler: false, replies: nil), spaced: true)
5555
.font(.system(size: 10))
5656
}
5757
}

Shared/Views/PostDetailView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ struct PostDetailView: View {
5353
if post.selftext != "" {
5454
Text(post.selftext)
5555
}
56+
if post.flairs.count > 0 {
57+
FlairView(flairs: post.flairs)
58+
}
5659
MetadataView(post: post, spaced: true)
5760
CommentsView(post: post)
5861
}

0 commit comments

Comments
 (0)