// // ToastConfiguration.swift // ToastConfiguration // // Created by Shadowfacts on 8/14/21. // Copyright © 2021 Shadowfacts. All rights reserved. // import UIKit import Pachyderm import Sentry struct ToastConfiguration { var systemImageName: String? var titleFont: UIFont = UIFontMetrics(forTextStyle: .body).scaledFont(for: .boldSystemFont(ofSize: 14)) var title: String var subtitle: String? var actionTitle: String? var action: ((ToastView) -> Void)? var longPressAction: ((ToastView) -> Void)? var edgeSpacing: CGFloat = 8 var edge: Edge = .automatic var dismissOnScroll = true var dismissAutomaticallyAfter: TimeInterval? = nil init(title: String) { self.title = title } enum Edge: Equatable { case top case bottom /// Determines edge based on the current device. Bottom on iPhone, top on iPad/Mac. case automatic } } extension ToastConfiguration { init(from error: Error, with title: String, in viewController: UIViewController, retryAction: ((ToastView) -> Void)?) { self.init(title: title) // localizedDescription is statically dispatched, so we need to call it after the downcast if let error = error as? Pachyderm.Client.Error { self.subtitle = error.localizedDescription self.systemImageName = error.systemImageName self.longPressAction = { [unowned viewController] toast in toast.dismissToast(animated: true) let text = """ \(title): \(error.requestMethod.name) \(error.requestEndpoint) \(error.type) """ let reporter = IssueReporterViewController.create(reportText: text, dismiss: { [unowned viewController] in viewController.dismiss(animated: true) }) viewController.present(reporter, animated: true) } // TODO: this is a bizarre place to do this, but code path covers basically all errors switch error.type { case .invalidRequest, .invalidResponse, .invalidModel(_), .mastodonError(_): SentrySDK.capture(error: error) { scope in scope.setFingerprint([String(describing: error)]) let crumb = Breadcrumb(level: .error, category: "error") crumb.message = title crumb.data = [ "request_method": error.requestMethod.name, "request_endpoint": error.requestEndpoint.description, ] switch error.type { case .invalidRequest: crumb.data!["error_type"] = "invalid_request" case .invalidResponse: crumb.data!["error_type"] = "invalid_response" case .invalidModel(let error): crumb.data!["error_type"] = "invalid_model" crumb.data!["underlying_error"] = String(describing: error) case .mastodonError(let error): crumb.data!["error_type"] = "mastodon_error" crumb.data!["underlying_error"] = error default: break } scope.add(crumb) } default: break } } else { self.subtitle = error.localizedDescription self.systemImageName = "exclamationmark.triangle" } if let retryAction = retryAction { self.actionTitle = "Retry" self.action = retryAction } } init(from error: Error, with title: String, in viewController: UIViewController, retryAction: @escaping @MainActor (ToastView) async -> Void) { self.init(from: error, with: title, in: viewController) { toast in Task { await retryAction(toast) } } } } fileprivate extension Pachyderm.Client.Error { var systemImageName: String { switch type { case .networkError(_): return "wifi.exclamationmark" default: return "exclamationmark.triangle" } } }