// // UIExtension.swift // GMERemittance // // Created by Sujal on 12/11/17. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved. // import Foundation import UIKit import MBProgressHUD import NVActivityIndicatorView #if DEBUG import FLEX #endif // MARK: - Navigation extension UIViewController { func setupPicturedNavBar(sideMenuAction: Selector? = nil) { self.navigationController?.isNavigationBarHidden = false self.navigationController?.navigationBar.isTranslucent = false self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow") if let selector = sideMenuAction { let leftButton = UIBarButtonItem( image: UIImage(named: "new_menu")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: selector ) self.navigationItem.leftBarButtonItem = leftButton } else { self.navigationItem.leftBarButtonItem = nil } self.navigationController?.navigationBar.barTintColor = .white let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 125, height: 30)) imageView.contentMode = .scaleAspectFit let image = UIImage(named: "nav_logo")?.withRenderingMode(.alwaysOriginal) imageView.image = image self.navigationItem.titleView = imageView self.setupNavigationShadow(isUse: false) } func setupNormalNavigation(color: UIColor = .themeWhite) { self.navigationController?.isNavigationBarHidden = false self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "ic_back_button")?.withRenderingMode(.alwaysOriginal) self.navigationController?.navigationBar.isTranslucent = false self.navigationController?.navigationBar .backIndicatorTransitionMaskImage = UIImage(named: "ic_back_button")?.withRenderingMode(.alwaysOriginal) self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont.sanfrancisco(.medium, size: 17) ] self.navigationController?.navigationBar.barTintColor = color self.setupNavigationShadow(isUse: false) } func setupNavigationShadow(isUse: Bool = true) { navigationController?.navigationBar.setValue(!isUse, forKey: "hidesShadow") } } // MARK: - Alert extension UIViewController { enum AlertType { case success case error case normal } func alert( type: AlertType = .normal, message: String?, title: String? = nil, rightButtomTitle: String = "ok_text".localized(), okAction: (() -> Void)? = nil ) { let settedTitle: String switch type { case .normal: settedTitle = title == nil ? "alert_text".localized() : title! case .error: settedTitle = title == nil ? "error_text".localized() : title! case .success: settedTitle = title == nil ? "success_text".localized() : title! } gmeAlert( type: type, title: settedTitle, message: message, rightButtonTitle: rightButtomTitle, leftButtonTitle: nil, rightButtonAction: okAction, leftButtonAction: nil ) } func alertWithOkCancel( type: AlertType = .normal, message: String?, title: String? = nil, okTitle: String = "ok_text".localized(), cancelTitle: String = "cancel_text".localized(), okAction: (() -> Void)? = nil, cancelAction: (() -> Void)? = nil ) { let settedTitle: String switch type { case .normal: settedTitle = title == nil ? "alert_text".localized() : title! case .error: settedTitle = title == nil ? "error_text".localized() : title! case .success: settedTitle = title == nil ? "success_text".localized() : title! } gmeAlert( type: type, title: settedTitle, message: message, rightButtonTitle: okTitle, leftButtonTitle: cancelTitle, rightButtonAction: okAction, leftButtonAction: cancelAction ) } func alertWithOk( type: AlertType = .normal, message: String?, title: String? = nil, okTitle: String = "ok_text".localized(), okAction: (() -> Void)? = nil ) { let settedTitle: String switch type { case .normal: settedTitle = title == nil ? "alert_text".localized() : title! case .error: settedTitle = title == nil ? "error_text".localized() : title! case .success: settedTitle = title == nil ? "success_text".localized() : title! } gmeAlert( type: type, title: settedTitle, message: message, rightButtonTitle: okTitle, leftButtonTitle: nil, rightButtonAction: okAction, leftButtonAction: nil ) } private func getAlert( type: AlertType, message: String?, title: String?, style: UIAlertController.Style? = .alert ) -> UIAlertController { let customerWalletNumber = GMEDB.shared.user.string(.referralCode) ?? "" let customerTitle: String switch type { case .error: customerTitle = "\(title ?? "Alert")(\(customerWalletNumber))" case .normal: customerTitle = title ?? "" case .success: customerTitle = title ?? "" } return UIAlertController(title: customerTitle, message: message, preferredStyle: style ?? .alert) } func present(_ alert: UIAlertController, asActionsheetInSourceView sourceView: Any) { if UI_USER_INTERFACE_IDIOM() == .pad { alert.modalPresentationStyle = .popover if let presenter = alert.popoverPresentationController { if sourceView is UIBarButtonItem { presenter.barButtonItem = sourceView as? UIBarButtonItem } else if sourceView is UIView { guard let view = sourceView as? UIView else { return } presenter.sourceView = view presenter.sourceRect = view.bounds } } } self.present(alert, animated: true, completion: nil) } func gmeAlert( type: AlertType = .normal, title: String = "Alert", message: String? = nil, rightButtonTitle: String = "Ok", leftButtonTitle: String? = nil, rightButtonAction: (() -> Void)? = nil, leftButtonAction: (() -> Void)? = nil ) { let titleText: String let customerWalletNumber = GMEDB.shared.user.string(.referralCode) ?? "" switch type { case .error: if customerWalletNumber != "" { titleText = "\(title)" } else { titleText = title } case .normal: titleText = title case .success: titleText = title } let gmeAlertVC = GMEAlertViewController( type: type, title: titleText, message: message, rightButtonTitle: rightButtonTitle, leftButtonTitle: leftButtonTitle, rightButtonAction: rightButtonAction, leftButtonAction: leftButtonAction ) present(gmeAlertVC, animated: false, completion: nil) } } extension UIAlertController { func addAction(title: String?, style: UIAlertAction.Style = .default, handler: (() -> Void)? = nil) { let action = UIAlertAction(title: title, style: style, handler: {_ in handler?() }) self.addAction(action) } } struct Associate { static var hud: UInt8 = 0 static var messageCount: UInt8 = 1 static var csButton: UInt8 = 2 static var channelIO: UInt8 = 3 } // MARK: HUD extension UIViewController { private func setProgressHud() -> MBProgressHUD { let progressHud: MBProgressHUD = MBProgressHUD.showAdded(to: self.view, animated: true) progressHud.tintColor = UIColor.darkGray progressHud.removeFromSuperViewOnHide = true objc_setAssociatedObject( self, &Associate.hud, progressHud, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC ) return progressHud } var progressHud: MBProgressHUD { if let progressHud = objc_getAssociatedObject(self, &Associate.hud) as? MBProgressHUD { progressHud.isUserInteractionEnabled = true return progressHud } return setProgressHud() } var progressHudIsShowing: Bool { return self.progressHud.isHidden } func showProgressHud( backgroundColor: UIColor = .themeBackgroundGray, loadingColor: UIColor = .themeRed, textColor: UIColor = .white ) { let activityData = ActivityData( size: CGSize(width: 50, height: 50), message: "loading_text".localized(), messageFont: .sanfrancisco(.medium, size: 14), messageSpacing: 2, type: .lineScale, color: loadingColor, padding: 5, backgroundColor: backgroundColor, textColor: textColor ) NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData, nil) } func hideProgressHud() { NVActivityIndicatorPresenter.sharedInstance.stopAnimating(nil) } } // MARK: - LargeTitle extension UIViewController { func setLargeTitle() { if #available(iOS 11.0, *) { self.navigationController?.navigationBar.prefersLargeTitles = true self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.font: UIFont.sanfrancisco(.medium, size: 20)] } // UINavigationBar.appearance().titleTextAttributes = // [NSAttributedStringKey.font: font] } } // MARK: - Activity Indicator private var associatedObjectHandle: UInt8 = 0 extension UIViewController { private var progressView: UIView? { get { guard let view = objc_getAssociatedObject(self, &associatedObjectHandle) as? UIView else { return nil } return view } set { objc_setAssociatedObject( self, &associatedObjectHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC ) } } func setPercent(with percent: CGFloat) { let progressSubView: UIView if let view = progressView { progressSubView = view } else { progressSubView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 2)) progressView = progressSubView } progressSubView.removeFromSuperview() view.addSubview(progressSubView) print("progress: \(percent)") progressSubView.backgroundColor = .themeRed let width = view.frame.width * percent UIView.animate( withDuration: 0.5, animations: { progressSubView.frame = CGRect(x: 0, y: 0, width: width, height: 2) }, completion: { _ in if width == self.view.frame.width { UIView.animate( withDuration: 0.2, animations: { progressSubView.alpha = 0.0 }, completion: { _ in progressSubView.frame = CGRect(x: 0, y: 0, width: 0, height: 2) progressSubView.alpha = 1.0 }) } } ) } } extension UIViewController { func presentDatePicker(completion: ((_ from: String?, _ to: String?) -> Void)?) { guard let datePickerViewController = UIStoryboard .init(name: "TransactionHistoryDatePicker", bundle: nil) .instantiateViewController( withIdentifier: "DatePickerViewController" ) as? DatePickerViewController else { return } datePickerViewController.completion = completion present(datePickerViewController, animated: true, completion: nil) } } extension UIViewController { func openPasswordInput(success: @escaping (String) -> Void){ let passwordVC = PasswordInputViewController() passwordVC.enteredPassword = { value in success(value) } passwordVC.modalPresentationStyle = .overFullScreen passwordVC.modalTransitionStyle = .crossDissolve self.present(passwordVC, animated: false) } } // MARK: TOAST extension UIViewController { func showToast(message : String, font: UIFont) { let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-200, width: 150, height: 35)) toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6) toastLabel.textColor = UIColor.white toastLabel.font = font toastLabel.textAlignment = .center; toastLabel.text = message toastLabel.alpha = 1.0 toastLabel.layer.cornerRadius = 10; toastLabel.clipsToBounds = true self.view.addSubview(toastLabel) UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: { toastLabel.alpha = 0.0 }, completion: {(isCompleted) in toastLabel.removeFromSuperview() }) } }