Skip to content

Commit 7cf8ad8

Browse files
committed
fix refreash problem
1 parent f833886 commit 7cf8ad8

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ struct EditInfoView: View {
2323
@State var urlContent = ""
2424
@State var selectedTab = 1
2525

26+
@State var isStopLoadingWeb = false
2627
// webarchive
2728
@State var savingDataTrigger = false
29+
2830

2931
var body: some View {
3032
VStack {
@@ -141,14 +143,16 @@ struct EditInfoView: View {
141143
WebUIViewWithSave(
142144
urlStr: url.absoluteString,
143145
savingDataTrigger: $savingDataTrigger,
144-
savingData: $info.webArchive
146+
savingData: $info.webArchive,
147+
isStop: $isStopLoadingWeb
145148
)
146149
.tabItem { Label("网页", systemImage: "circle") }
147150
.tag(3)
148151
}
149152
}
150153
.onChange(of: info.url) { oldValue, newValue in
151154
tabSwitch()
155+
isStopLoadingWeb = false
152156
}
153157
.onAppear {
154158
tabSwitch()

SwiftPamphletApp/ViewComponet/ViewComponet.swift

+21-14
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ struct WebUIViewWithSave: NSViewRepresentable {
197197

198198
@Binding var savingDataTrigger: Bool
199199
@Binding var savingData: Data?
200+
201+
@Binding var isStop: Bool
200202

201203
func makeCoordinator() -> Coordinator {
202204
Coordinator()
@@ -209,20 +211,6 @@ struct WebUIViewWithSave: NSViewRepresentable {
209211
}
210212

211213
func updateNSView(_ nsView: NSViewType, context: Context) {
212-
if savingData != nil {
213-
if let data = savingData {
214-
nsView.load(data, mimeType: "application/x-webarchive", characterEncodingName: "utf-8", baseURL: getDocumentsDirectory())
215-
}
216-
} else if urlStr.isEmpty {
217-
let host = URL(string: baseURLStr)?.host ?? ""
218-
nsView.loadHTMLString(html, baseURL: URL(string: "https://\(host)"))
219-
} else {
220-
if let url = URL(string: urlStr) {
221-
let r = URLRequest(url: url)
222-
nsView.load(r)
223-
}
224-
}
225-
226214
if savingDataTrigger == true {
227215
nsView.createWebArchiveData { result in
228216
do {
@@ -234,6 +222,25 @@ struct WebUIViewWithSave: NSViewRepresentable {
234222
}
235223
savingDataTrigger = false
236224
}
225+
226+
if isStop == true {
227+
return
228+
}
229+
230+
if savingData != nil {
231+
if let data = savingData {
232+
nsView.load(data, mimeType: "application/x-webarchive", characterEncodingName: "utf-8", baseURL: getDocumentsDirectory())
233+
isStop = true
234+
}
235+
} else {
236+
if let url = URL(string: urlStr) {
237+
let r = URLRequest(url: url)
238+
nsView.load(r)
239+
isStop = true
240+
}
241+
}
242+
243+
237244
}
238245

239246
class Coordinator: NSObject, WKNavigationDelegate {

0 commit comments

Comments
 (0)