From e932b82515a284b2385ee378440f4187bc3ed1d5 Mon Sep 17 00:00:00 2001 From: leminlimez Date: Fri, 11 Jul 2025 11:15:52 -0400 Subject: [PATCH] loading and error handling --- .../Controllers/DownloadManager.swift | 4 +-- Pocket Poster/Localizable.xcstrings | 12 +++++++ .../Views/Accessory Views/WebView.swift | 31 ++++++++++++++++++- .../Views/NavigatorViews/ExploreView.swift | 25 ++++++++++++++- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/Pocket Poster/Controllers/DownloadManager.swift b/Pocket Poster/Controllers/DownloadManager.swift index 9b53338..1071b48 100644 --- a/Pocket Poster/Controllers/DownloadManager.swift +++ b/Pocket Poster/Controllers/DownloadManager.swift @@ -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) } } diff --git a/Pocket Poster/Localizable.xcstrings b/Pocket Poster/Localizable.xcstrings index f1359c0..6b1c1dd 100644 --- a/Pocket Poster/Localizable.xcstrings +++ b/Pocket Poster/Localizable.xcstrings @@ -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" : { diff --git a/Pocket Poster/Views/Accessory Views/WebView.swift b/Pocket Poster/Views/Accessory Views/WebView.swift index a403cab..28e4b28 100644 --- a/Pocket Poster/Views/Accessory Views/WebView.swift +++ b/Pocket Poster/Views/Accessory Views/WebView.swift @@ -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, error: Binding) { 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 + } + } } diff --git a/Pocket Poster/Views/NavigatorViews/ExploreView.swift b/Pocket Poster/Views/NavigatorViews/ExploreView.swift index 6f5f33f..b891fc9 100644 --- a/Pocket Poster/Views/NavigatorViews/ExploreView.swift +++ b/Pocket Poster/Views/NavigatorViews/ExploreView.swift @@ -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.") + } + } } }