From 54aa8e20a3a65bef579ee0339f92eb97e923c05d Mon Sep 17 00:00:00 2001 From: leminlimez <59540996+leminlimez@users.noreply.github.com> Date: Wed, 18 Jun 2025 18:47:27 -0400 Subject: [PATCH] improve error codes --- Pocket Poster/Error Codes/ApplyError.swift | 30 ++++++++++++++++++++++ Pocket Poster/Views/ContentView.swift | 23 ++++++++++++----- 2 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 Pocket Poster/Error Codes/ApplyError.swift diff --git a/Pocket Poster/Error Codes/ApplyError.swift b/Pocket Poster/Error Codes/ApplyError.swift new file mode 100644 index 0000000..a42ef51 --- /dev/null +++ b/Pocket Poster/Error Codes/ApplyError.swift @@ -0,0 +1,30 @@ +// +// ApplyError.swift +// Pocket Poster +// +// Created by lemin on 6/18/25. +// + +import Foundation + +enum ApplyError: LocalizedError { + // Throw when the app hash is incorrect + case wrongAppHash + + // Throw when the collections need to be reset + case collectionsNeedsReset + + // Throw in all other cases + case unexpected(info: String) + + public var errorDescription: String? { + switch self { + case .wrongAppHash: + return "Your app hash is incorrect. Please set it again." + case .collectionsNeedsReset: + return "The folder is improperly set up. Please tap \"Reset Collections\" and try again." + case .unexpected(let info): + return info + } + } +} diff --git a/Pocket Poster/Views/ContentView.swift b/Pocket Poster/Views/ContentView.swift index adc9e83..11035aa 100644 --- a/Pocket Poster/Views/ContentView.swift +++ b/Pocket Poster/Views/ContentView.swift @@ -75,8 +75,8 @@ struct ContentView: View { try pbManager.applyTendies(appHash: pbHash) SymHandler.cleanup() // just to be extra sure try? FileManager.default.removeItem(at: pbManager.getTendiesStoreURL()) - Haptic.shared.notify(.success) UIApplication.shared.dismissAlert(animated: false) + Haptic.shared.notify(.success) DispatchQueue.main.asyncAfter(deadline: .now() + 0.35, execute: { pbManager.selectedTendies.removeAll() UIApplication.shared.confirmAlert(title: "Success!", body: "The PosterBoard app will now open. Please close it from the app switcher.", onOK: { @@ -87,13 +87,12 @@ struct ContentView: View { } }, noCancel: true) }) + } catch CocoaError.fileWriteUnknown { + presentError(ApplyError.wrongAppHash) + } catch CocoaError.fileWriteFileExists { + presentError(ApplyError.collectionsNeedsReset) } catch { - Haptic.shared.notify(.error) - SymHandler.cleanup() - UIApplication.shared.dismissAlert(animated: false) - DispatchQueue.main.asyncAfter(deadline: .now() + 0.35, execute: { - UIApplication.shared.alert(body: error.localizedDescription) - }) + presentError(ApplyError.unexpected(info: error.localizedDescription)) } } }) { @@ -173,6 +172,16 @@ struct ContentView: View { pbManager.selectedTendies.remove(atOffsets: offsets) } + func presentError(_ error: ApplyError) { + SymHandler.cleanup() + UIApplication.shared.dismissAlert(animated: false) + DispatchQueue.main.asyncAfter(deadline: .now() + 0.35, execute: { + lastError = error.localizedDescription + Haptic.shared.notify(.error) + showErrorAlert = true + }) + } + init() { // Fix file picker let fixMethod = class_getInstanceMethod(UIDocumentPickerViewController.self, #selector(UIDocumentPickerViewController.fix_init(forOpeningContentTypes:asCopy:)))!