You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

356 lines
9.4 KiB

//
// 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
// MARK: - Navigation
extension UIViewController {
func setupPicturedNavBar(sideMenuAction: Selector? = nil) {
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.isTranslucent = false
if let selector = sideMenuAction {
let leftButton = UIBarButtonItem(
image: UIImage(named: "ic_hamburger"),
style: .plain,
target: self,
action: selector
)
self.navigationController?.navigationBar.tintColor = AppConstants.themeRedColor
self.navigationItem.leftBarButtonItem = leftButton
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.white
}
self.navigationController?.navigationBar.tintColor = .white
self.navigationController?.navigationBar.barTintColor = AppConstants.themeRedColor
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 125, height: 30))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "ic_gme_new")
imageView.image = image
self.navigationItem.titleView = imageView
}
func setupNormalNavigation() {
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "backIconBlack")
self.navigationController?.navigationBar
.backIndicatorTransitionMaskImage = UIImage(named: "backIconBlack")
self.navigationController?.navigationBar.tintColor = .black
self.navigationController?.navigationBar.barTintColor = .white
}
}
// MARK: - Alert
extension UIViewController {
enum AlertType {
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!
}
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!
}
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!
}
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(.walletNumber) ?? ""
let customerTitle: String
switch type {
case .error:
customerTitle = "\(title ?? "Alert")(\(customerWalletNumber))"
case .normal:
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(.walletNumber) ?? ""
switch type {
case .error:
if customerWalletNumber != "" {
titleText = "\(title) (\(customerWalletNumber))"
} else {
titleText = title
}
case .normal:
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
}
// 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() {
self.progressHud.show(animated: false)
}
func hideProgressHud() {
self.progressHud.label.text = ""
self.progressHud.completionBlock = {
objc_setAssociatedObject(
self,
&Associate.hud,
nil,
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}
self.progressHud.hide(animated: false)
}
}
// MARK: - LargeTitle
extension UIViewController {
func setLargeTitle() {
let font = UIFont(name: "SanFranciscoDisplay-Medium", size: 20)!
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes =
[NSAttributedString.Key.font: font]
}
// UINavigationBar.appearance().titleTextAttributes =
// [NSAttributedStringKey.font: font]
}
}
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 = AppConstants.themeRedColor
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
})
}
}
)
}
}