import UIKit class CustomViewController: UIViewController { private lazy var leftNavigationAction: () -> Void = { self.navigationController?.popViewController(animated: true) } var viewModel: VM! { didSet { viewModelDidLoad() } } lazy var loadingView: LoadingView = { return LoadingView() }() lazy var errorView: ErrorView = { return ErrorView() }() override func viewDidLoad() { super.viewDidLoad() // Create the view model if needed if (viewModel == nil) { viewModel = VM() } // setTheme() } public func showContentView() { dropViews([errorView, loadingView]) } public func showLoadingView() { dropViews([errorView]) addViewIfNeeded(loadingView) } public func showErrorView(error: String = .defaultError) { dropViews([loadingView]) addViewIfNeeded(errorView) errorView.title = error } private func dropViews(_ views: [UIView]) { views.forEach { view in view.removeFromSuperview() } } private func addViewIfNeeded(_ subview: UIView) { if (!view.subviews.contains(subview)) { view.addSubviewAndFill(subview) view.bringSubviewToFront(subview) } } func viewModelDidLoad() { // Empty } @objc private func leftAction() { leftNavigationAction() } func setLeftNavigationButton(_ icon: UIImage, action: (() -> Void)? = nil) { // Attach left action // Or fallback with a pop if let leftAction = action { leftNavigationAction = leftAction } else { leftNavigationAction = { self.navigationController?.popViewController(animated: true) } } navigationItem.leftBarButtonItem = UIBarButtonItem( image: .back, style: .plain, target: self, action: #selector(CustomViewController.leftAction) ) } func showAlert(title: String, address: String? = nil , errorMsg: String? = nil, description: String? = nil){ let showAlert = UIAlertController(title: title, message: errorMsg, preferredStyle: .alert) if let address = address { let imageView = UIImageView(frame: CGRect(x: 10, y: 50, width: 250, height: 250)) imageView.image = generateQRCode(from: address) imageView.layer.magnificationFilter = CALayerContentsFilter.nearest showAlert.view.addSubview(imageView) let height = NSLayoutConstraint(item: showAlert.view as Any, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 400) let width = NSLayoutConstraint(item: showAlert.view as Any, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:250) showAlert.view.addConstraint(height) showAlert.view.addConstraint(width) showAlert.addAction(UIAlertAction(title: "Copy Address", style: .default, handler: { action in UIPasteboard.general.string = address debugPrint("✅✅ Created Invoice ✅✅", address) })) } if let description = description { showAlert.message = description } showAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in })) self.present(showAlert, animated: true, completion: nil) } func generateQRCode(from string: String) -> UIImage? { let data = string.data(using: String.Encoding.ascii) if let QRFilter = CIFilter(name: "CIQRCodeGenerator") { QRFilter.setValue(data, forKey: "inputMessage") guard let QRImage = QRFilter.outputImage else {return nil} return UIImage(ciImage: QRImage) } return nil } }