Skip to content

Commit 4e7f244

Browse files
authored
Merge pull request #20 from v2er-app/bugfix/safari-view-ui-mode
Fix: Safari view UI mode and Settings page external links
2 parents 447c8be + b0333a1 commit 4e7f244

File tree

2 files changed

+73
-35
lines changed

2 files changed

+73
-35
lines changed

V2er/General/Extentions.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,43 @@ extension URL {
277277
// MARK: - Safari View
278278
struct SafariView: UIViewControllerRepresentable {
279279
let url: URL
280+
@Environment(\.colorScheme) var colorScheme
280281

281282
func makeUIViewController(context: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
282-
return SFSafariViewController(url: url)
283+
let safariVC = SFSafariViewController(url: url)
284+
updateAppearance(safariVC)
285+
return safariVC
283286
}
284287

285288
func updateUIViewController(_ uiViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<SafariView>) {
289+
updateAppearance(uiViewController)
290+
}
291+
292+
private func updateAppearance(_ safariVC: SFSafariViewController) {
293+
// Get the actual interface style from the root view controller
294+
let actualStyle = V2erApp.rootViewController?.overrideUserInterfaceStyle ?? .unspecified
295+
296+
// Apply the appropriate style to Safari view
297+
if actualStyle != .unspecified {
298+
// User has explicitly set light or dark mode
299+
safariVC.overrideUserInterfaceStyle = actualStyle
300+
} else {
301+
// Following system setting - use the current colorScheme
302+
safariVC.overrideUserInterfaceStyle = colorScheme == .dark ? .dark : .light
303+
}
304+
305+
// Set tint colors based on the effective style
306+
let effectiveStyle = safariVC.overrideUserInterfaceStyle == .dark ||
307+
(safariVC.overrideUserInterfaceStyle == .unspecified && colorScheme == .dark)
308+
309+
if effectiveStyle {
310+
// Dark mode colors
311+
safariVC.preferredControlTintColor = UIColor.systemBlue
312+
safariVC.preferredBarTintColor = UIColor.systemBackground
313+
} else {
314+
// Light mode colors
315+
safariVC.preferredControlTintColor = UIColor.systemBlue
316+
safariVC.preferredBarTintColor = UIColor.systemBackground
317+
}
286318
}
287319
}

V2er/View/Settings/SettingsPage.swift

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,67 @@
99
import SwiftUI
1010
import SafariServices
1111

12+
// Wrapper to make URL Identifiable for sheet presentation
13+
struct IdentifiableURL: Identifiable {
14+
let id = UUID()
15+
let url: URL
16+
}
17+
1218
struct SettingsPage: View {
1319
@Environment(\.dismiss) var dismiss
1420
@State private var showingAlert = false
1521
@State var logingOut: Bool = false
16-
22+
@State private var safariURL: IdentifiableURL?
23+
1724
var body: some View {
1825
formView
1926
.navBar("设置")
27+
.sheet(item: $safariURL) { item in
28+
SafariView(url: item.url)
29+
}
2030
}
21-
31+
2232
@ViewBuilder
2333
private var formView: some View {
2434
ScrollView {
2535
VStack(spacing: 0) {
2636
SectionItemView("外观设置", showDivider: false)
2737
.padding(.top, 8)
2838
.to { AppearanceSettingView() }
29-
39+
3040
SectionItemView("通用设置")
3141
.to { OtherSettingsView() }
3242

43+
Button {
44+
if let url = URL(string: "https://github.com/v2er-app/iOS/issues") {
45+
safariURL = IdentifiableURL(url: url)
46+
}
47+
} label: {
3348
SectionItemView("问题反馈")
3449
.padding(.top, 8)
35-
.to {
36-
SafariWebView(url: "https://github.com/v2er-app/iOS/issues")
37-
}
50+
}
51+
52+
Button {
53+
if let url = URL(string: "https://www.v2ex.com/help") {
54+
safariURL = IdentifiableURL(url: url)
55+
}
56+
} label: {
3857
SectionItemView("V2EX帮助")
39-
.to {
40-
WebBrowserView(url: "https://www.v2ex.com/help")
41-
}
42-
SectionItemView("源码开放")
43-
.to {
44-
WebBrowserView(url: "https://github.com/v2er-app")
45-
}
58+
}
4659

60+
Button {
61+
if let url = URL(string: "https://github.com/v2er-app") {
62+
safariURL = IdentifiableURL(url: url)
63+
}
64+
} label: {
65+
SectionItemView("源码开放")
66+
}
67+
68+
Button {
69+
if let url = URL(string: "https://v2er.app") {
70+
safariURL = IdentifiableURL(url: url)
71+
}
72+
} label: {
4773
SectionView("关于") {
4874
HStack {
4975
Text("版本1.0.0")
@@ -55,9 +81,7 @@ struct SettingsPage: View {
5581
.padding(.trailing, 16)
5682
}
5783
}
58-
.to {
59-
WebBrowserView(url: "https://v2er.app")
60-
}
84+
}
6185

6286
Button {
6387
// "https://github.com/v2er-app".openURL()
@@ -108,24 +132,6 @@ struct SettingsPage: View {
108132
}
109133
}
110134

111-
struct SafariWebView: View {
112-
let url: String
113-
@State private var showingSafari = false
114-
115-
var body: some View {
116-
Color.clear
117-
.onAppear {
118-
if let url = URL(string: url) {
119-
showingSafari = true
120-
}
121-
}
122-
.sheet(isPresented: $showingSafari) {
123-
if let url = URL(string: url) {
124-
SafariView(url: url)
125-
}
126-
}
127-
}
128-
}
129135

130136
struct SettingsPage_Previews: PreviewProvider {
131137
static var previews: some View {

0 commit comments

Comments
 (0)