// // ToastConfiguration.swift // ToastConfiguration // // Created by Shadowfacts on 8/14/21. // Copyright © 2021 Shadowfacts. All rights reserved. // import UIKit import Pachyderm struct ToastConfiguration { var systemImageName: String? var titleFont: UIFont = .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: Client.Error, with title: String, in viewController: UIViewController, retryAction: @escaping (ToastView) -> Void) { self.init(title: title) self.subtitle = error.localizedDescription self.systemImageName = error.systemImageName self.actionTitle = "Retry" self.action = retryAction 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, delegate: viewController) viewController.present(reporter, animated: true) } } } fileprivate extension Client.Error { var systemImageName: String { switch type { case .networkError(_): return "wifi.exclamationmark" default: return "exclamationmark.triangle" } } } // todo: i don't like that this protocol conformance is accessible outside of this file extension UIViewController: IssueReporterViewControllerDelegate { func didDismissReporter() { self.dismiss(animated: true) } }