loading and error handling
This commit is contained in:
1 parent
edde7ddb0c
commit
e932b82515
4 files changed
+68
-4
No files matched your search
@@ -43,13 +43,13 @@ class DownloadManager: ObservableObject {
|
||||
DispatchQueue.main.async {
|
||||
PosterBoardManager.shared.selectedTendies.append(newURL)
|
||||
Haptic.shared.notify(.success)
|
||||
UIApplication.shared.dismissAlert(animated: false)
|
||||
UIApplication.shared.dismissAlert(animated: true)
|
||||
UIApplication.shared.alert(title: NSLocalizedString("Successfully downloaded wallpaper!", comment: ""), body: NSLocalizedString("Your downloaded .tendies will be on the Home page.", comment: "after successfully downloading tendies"))
|
||||
}
|
||||
} catch {
|
||||
DispatchQueue.main.async {
|
||||
Haptic.shared.notify(.error)
|
||||
UIApplication.shared.dismissAlert(animated: false)
|
||||
UIApplication.shared.dismissAlert(animated: true)
|
||||
UIApplication.shared.alert(title: NSLocalizedString("Could not download wallpaper!", comment: ""), body: error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3777,6 +3777,9 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Failed to load explore page." : {
|
||||
|
||||
},
|
||||
"Fallback Shortcut Creator" : {
|
||||
"localizations" : {
|
||||
@@ -7219,6 +7222,9 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Retry" : {
|
||||
|
||||
},
|
||||
"Select Tendies" : {
|
||||
"localizations" : {
|
||||
@@ -7839,6 +7845,9 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Successfully downloaded wallpaper!" : {
|
||||
|
||||
},
|
||||
"Successfully imported %@" : {
|
||||
"localizations" : {
|
||||
@@ -9957,6 +9966,9 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"Your downloaded .tendies will be on the Home page." : {
|
||||
"comment" : "after successfully downloading tendies"
|
||||
},
|
||||
"Your PosterBoard will refresh automatically." : {
|
||||
"localizations" : {
|
||||
"ar" : {
|
||||
|
||||
@@ -11,21 +11,50 @@ import WebKit
|
||||
|
||||
struct WallpaperWebView: UIViewRepresentable {
|
||||
let url: URL
|
||||
@Binding var isLoading: Bool
|
||||
@Binding var error: Error?
|
||||
|
||||
init(_ url: URL) {
|
||||
init(_ url: URL, isLoading: Binding<Bool>, error: Binding<Error?>) {
|
||||
self.url = url
|
||||
self._isLoading = isLoading
|
||||
self._error = error
|
||||
}
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(self)
|
||||
}
|
||||
func makeUIView(context: Context) -> some UIView {
|
||||
let configuration = WKWebViewConfiguration()
|
||||
configuration.setURLSchemeHandler(AssetsSchemeHandler(), forURLScheme: "pocketposter")
|
||||
let webView = WKWebView(frame: .zero, configuration: configuration)
|
||||
webView.navigationDelegate = context.coordinator
|
||||
webView.load(URLRequest(url: url))
|
||||
|
||||
return webView
|
||||
}
|
||||
|
||||
func updateUIView(_ uiView: UIViewType, context: Context) {}
|
||||
|
||||
class Coordinator: NSObject, WKNavigationDelegate {
|
||||
var parent: WallpaperWebView
|
||||
init(_ parent: WallpaperWebView) {
|
||||
self.parent = parent
|
||||
}
|
||||
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
|
||||
parent.isLoading = true
|
||||
}
|
||||
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
||||
parent.isLoading = false
|
||||
}
|
||||
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
|
||||
return
|
||||
let nsError = error as NSError
|
||||
if nsError.code == NSURLErrorCancelled { return }
|
||||
print("loading error: \(error)")
|
||||
parent.isLoading = false
|
||||
parent.error = error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,30 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ExploreView: View {
|
||||
@State private var isLoading = true
|
||||
@State private var error: Error? = nil
|
||||
|
||||
var body: some View {
|
||||
WallpaperWebView(URL(string: "https://cowabun.ga/wallpapers?pocketposter=true")!)
|
||||
ZStack {
|
||||
if let error = error {
|
||||
VStack {
|
||||
Text(error.localizedDescription)
|
||||
.foregroundStyle(.red)
|
||||
Button(action: {
|
||||
isLoading = true
|
||||
self.error = nil
|
||||
}) {
|
||||
Text("Retry")
|
||||
}
|
||||
}
|
||||
} else if let url = URL(string: "https://cowabun.ga/wallpapers?pocketposter=true") {
|
||||
WallpaperWebView(url, isLoading: $isLoading, error: $error)
|
||||
if isLoading {
|
||||
ProgressView()
|
||||
}
|
||||
} else {
|
||||
Text("Failed to load explore page.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user