Skip to content

Commit 5470075

Browse files
authored
Make comment header tappable and fix WebView styling (#322)
1 parent 4006c18 commit 5470075

File tree

5 files changed

+55
-21
lines changed

5 files changed

+55
-21
lines changed

ios/HackerNews/Comments/CommentsHeader.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ struct CommentsHeader: View {
1111
let state: CommentsHeaderState
1212
let likePost: () -> Void
1313
let toggleBody: () -> Void
14+
let onTitleTap: () -> Void
1415

1516
var body: some View {
1617
VStack(alignment: .leading) {
17-
// title
18-
Text(state.story.title)
19-
.font(.ibmPlexMono(.bold, size: 16))
20-
.frame(maxWidth: .infinity, alignment: .leading)
18+
// title - wrap in Button
19+
Button(action: onTitleTap) {
20+
Text(state.story.title)
21+
.font(.ibmPlexMono(.bold, size: 16))
22+
.frame(maxWidth: .infinity, alignment: .leading)
23+
}
24+
.buttonStyle(.plain)
2125

2226
// actions
2327
HStack {
@@ -81,6 +85,7 @@ struct CommentsHeader: View {
8185
CommentsHeader(
8286
state: CommentsHeaderState(story: PreviewHelpers.makeFakeStory()),
8387
likePost: {},
84-
toggleBody: {}
88+
toggleBody: {},
89+
onTitleTap: {}
8590
)
8691
}

ios/HackerNews/Comments/CommentsScreen.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import SwiftUI
1010

1111
struct CommentsScreen: View {
1212

13-
@ObservedObject var model: CommentsViewModel
13+
@State var model: CommentsViewModel
1414

1515
var body: some View {
1616
ScrollView {
@@ -31,6 +31,9 @@ struct CommentsScreen: View {
3131
},
3232
toggleBody: {
3333
model.toggleHeaderBody()
34+
},
35+
onTitleTap: {
36+
model.goToWebsite()
3437
}
3538
)
3639

@@ -109,7 +112,7 @@ struct CommentsScreen: View {
109112
PreviewHelpers.makeFakeComment(),
110113
PreviewHelpers.makeFakeComment(),
111114
]
112-
let viewModel = CommentsViewModel(
115+
@Previewable @State var viewModel = CommentsViewModel(
113116
story: PreviewHelpers.makeFakeStory(kids: comments.map { $0.id }),
114117
auth: .loggedIn,
115118
navigation: { _ in }

ios/HackerNews/Comments/CommentsViewModel.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ extension CommentFormData {
7777
enum CommentsDestination {
7878
case back
7979
case login
80+
case website(url: URL)
8081
}
8182

8283
@MainActor
83-
class CommentsViewModel: ObservableObject {
84+
@Observable
85+
class CommentsViewModel {
8486

85-
@Published var state: CommentsUiState
87+
var state: CommentsUiState
8688

8789
private let story: Story
8890
private let navigation: (_ to: CommentsDestination) -> Void
@@ -196,6 +198,11 @@ class CommentsViewModel: ObservableObject {
196198
func goToLogin() {
197199
navigation(.login)
198200
}
201+
202+
func goToWebsite() {
203+
guard let url = story.makeUrl() else { return }
204+
navigation(.website(url: url))
205+
}
199206

200207
func updateComment(text: String) {
201208
state.postCommentState?.text = text

ios/HackerNews/HNApp.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import Reaper
99
import Sentry
10-
import SwiftUI
1110
import SwiftData
11+
import SwiftUI
1212

1313
@main
1414
struct Hacker_NewsApp: App {
@@ -17,12 +17,13 @@ struct Hacker_NewsApp: App {
1717
init() {
1818
UINavigationBar.appearance().backgroundColor = .clear
1919
UICollectionView.appearance().backgroundColor = .clear
20-
20+
2121
EMGReaper.sharedInstance().start(withAPIKey: "f77fb081-cfc2-4d15-acb5-18bad59c9376")
22-
22+
2323
SentrySDK.start { options in
24-
options.dsn = "https://118cff4b239bd3e0ede8fd74aad9bf8f@o497846.ingest.sentry.io/4506027753668608"
25-
options.enableTracing = true
24+
options.dsn =
25+
"https://118cff4b239bd3e0ede8fd74aad9bf8f@o497846.ingest.sentry.io/4506027753668608"
26+
options.enableTracing = true
2627
}
2728
}
2829

@@ -38,17 +39,18 @@ struct Hacker_NewsApp: App {
3839
.navigationDestination(for: AppViewModel.AppNavigation.self) { appNavigation in
3940
switch appNavigation {
4041
case .webLink(let url, let title):
41-
WebView(url: url)
42+
WebViewContainer(url: url, title: title)
4243
.ignoresSafeArea()
43-
.navigationTitle(title)
44-
.navigationBarTitleDisplayMode(.inline)
4544
case .storyComments(let story):
46-
let commentModel = CommentsViewModel(story: story, auth: appModel.authState) { destination in
45+
let commentModel = CommentsViewModel(story: story, auth: appModel.authState) {
46+
destination in
4747
switch destination {
4848
case .back:
4949
appModel.backPressed()
5050
case .login:
5151
appModel.gotoLogin()
52+
case let .website(url):
53+
appModel.openLink(url: url)
5254
}
5355
}
5456
CommentsScreen(model: commentModel)

ios/HackerNews/Web/WebView.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,30 @@ import Foundation
99
import SwiftUI
1010
import WebKit
1111

12+
struct WebViewContainer: View {
13+
let url: URL
14+
let title: String
15+
16+
var body: some View {
17+
WebView(url: url)
18+
.navigationBarTitleDisplayMode(.inline)
19+
.navigationTitle(title)
20+
.toolbarBackground(.visible, for: .navigationBar)
21+
.toolbarBackground(Color(UIColor.systemBackground), for: .navigationBar)
22+
}
23+
}
24+
1225
struct WebView: UIViewRepresentable {
1326
let url: URL
14-
27+
1528
func makeUIView(context: Context) -> WKWebView {
16-
return WKWebView()
29+
let configuration = WKWebViewConfiguration()
30+
let webView = WKWebView(frame: .zero, configuration: configuration)
31+
webView.backgroundColor = .systemBackground
32+
webView.isOpaque = false
33+
return webView
1734
}
18-
35+
1936
func updateUIView(_ webView: WKWebView, context: Context) {
2037
let request = URLRequest(url: url)
2138
webView.load(request)

0 commit comments

Comments
 (0)