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.

331 lines
9.3 KiB

6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. //
  2. // UIExtension.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 12/11/17.
  6. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. import MBProgressHUD
  11. // MARK: - Navigation
  12. extension UIViewController {
  13. func setupPicturedNavBar(sideMenuAction: Selector? = nil) {
  14. self.navigationController?.isNavigationBarHidden = false
  15. self.navigationController?.navigationBar.isTranslucent = false
  16. if let selector = sideMenuAction {
  17. let leftButton = UIBarButtonItem(image: UIImage(named: "ic_hamburger"), style: .plain, target: self, action: selector)
  18. self.navigationController?.navigationBar.tintColor = AppConstants.themeRedColor
  19. self.navigationItem.leftBarButtonItem = leftButton
  20. self.navigationItem.leftBarButtonItem?.tintColor = UIColor.white
  21. }
  22. self.navigationController?.navigationBar.tintColor = .white
  23. self.navigationController?.navigationBar.barTintColor = AppConstants.themeRedColor
  24. let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 125, height: 30))
  25. imageView.contentMode = .scaleAspectFit
  26. let image = UIImage(named: "ic_gme_new")
  27. imageView.image = image
  28. self.navigationItem.titleView = imageView
  29. }
  30. func setupNormalNavigation() {
  31. self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "backIconBlack")
  32. self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "backIconBlack")
  33. self.navigationController?.navigationBar.tintColor = .black
  34. self.navigationController?.navigationBar.barTintColor = .white
  35. }
  36. }
  37. // MARK: - Alert
  38. extension UIViewController {
  39. enum AlertType {
  40. case error
  41. case normal
  42. }
  43. func alert(
  44. type: AlertType = .normal,
  45. message: String?,
  46. title: String? = nil,
  47. rightButtomTitle: String = "ok_text".localized(),
  48. okAction: (() -> Void)? = nil
  49. ) {
  50. let settedTitle: String
  51. switch type {
  52. case .normal:
  53. settedTitle = title == nil ? "alert_text".localized() : title!
  54. case .error:
  55. settedTitle = title == nil ? "error_text".localized() : title!
  56. }
  57. gmeAlert(
  58. type: type,
  59. title: settedTitle,
  60. message: message,
  61. rightButtonTitle: rightButtomTitle,
  62. leftButtonTitle: nil,
  63. rightButtonAction: okAction,
  64. leftButtonAction: nil
  65. )
  66. }
  67. func alertWithOkCancel(
  68. type: AlertType = .normal,
  69. message: String?,
  70. title: String? = nil,
  71. okTitle: String = "ok_text".localized(),
  72. style: UIAlertController.Style? = .alert,
  73. cancelTitle: String = "cancel_text".localized(),
  74. OkStyle: UIAlertAction.Style = .default,
  75. cancelStyle: UIAlertAction.Style = .default,
  76. okAction: (()->())? = nil,
  77. cancelAction: (()->())? = nil
  78. ) {
  79. let settedTitle: String
  80. switch type {
  81. case .normal:
  82. settedTitle = title == nil ? "alert_text".localized() : title!
  83. case .error:
  84. settedTitle = title == nil ? "error_text".localized() : title!
  85. }
  86. gmeAlert(
  87. type: type,
  88. title: settedTitle,
  89. message: message,
  90. rightButtonTitle: okTitle,
  91. leftButtonTitle: cancelTitle,
  92. rightButtonAction: okAction,
  93. leftButtonAction: cancelAction
  94. )
  95. }
  96. func alertWithOk(
  97. type: AlertType = .normal,
  98. message: String?,
  99. title: String? = nil,
  100. okTitle: String = "ok_text".localized(),
  101. style: UIAlertController.Style? = .alert,
  102. OkStyle: UIAlertAction.Style = .default,
  103. okAction: (()->())? = nil
  104. ) {
  105. let settedTitle: String
  106. switch type {
  107. case .normal:
  108. settedTitle = title == nil ? "alert_text".localized() : title!
  109. case .error:
  110. settedTitle = title == nil ? "error_text".localized() : title!
  111. }
  112. gmeAlert(
  113. type: type,
  114. title: settedTitle,
  115. message: message,
  116. rightButtonTitle: okTitle,
  117. leftButtonTitle: nil,
  118. rightButtonAction: okAction,
  119. leftButtonAction: nil
  120. )
  121. }
  122. private func getAlert(
  123. type: AlertType,
  124. message: String?,
  125. title: String?,
  126. style: UIAlertController.Style? = .alert
  127. ) -> UIAlertController {
  128. let customerWalletNumber = GMEDB.shared.user.string(.walletNumber) ?? ""
  129. let customerTitle: String
  130. switch type {
  131. case .error:
  132. customerTitle = "\(title ?? "Alert")(\(customerWalletNumber))"
  133. case .normal:
  134. customerTitle = title ?? ""
  135. }
  136. return UIAlertController(title: customerTitle, message: message, preferredStyle: style ?? .alert)
  137. }
  138. func present(_ alert: UIAlertController, asActionsheetInSourceView sourceView: Any) {
  139. if UI_USER_INTERFACE_IDIOM() == .pad {
  140. alert.modalPresentationStyle = .popover
  141. if let presenter = alert.popoverPresentationController {
  142. if sourceView is UIBarButtonItem {
  143. presenter.barButtonItem = sourceView as? UIBarButtonItem
  144. }else if sourceView is UIView {
  145. let view = sourceView as! UIView
  146. presenter.sourceView = view
  147. presenter.sourceRect = view.bounds
  148. }
  149. }
  150. }
  151. self.present(alert, animated: true, completion: nil)
  152. }
  153. func gmeAlert(
  154. type: AlertType = .normal,
  155. title: String = "Alert",
  156. message: String? = nil,
  157. rightButtonTitle: String = "Ok",
  158. leftButtonTitle: String? = nil,
  159. rightButtonAction: (() -> Void)? = nil,
  160. leftButtonAction: (() -> Void)? = nil
  161. ) {
  162. let titleText: String
  163. let customerWalletNumber = GMEDB.shared.user.string(.walletNumber) ?? ""
  164. switch type {
  165. case .error:
  166. if customerWalletNumber != "" {
  167. titleText = "\(title) (\(customerWalletNumber))"
  168. } else {
  169. titleText = title
  170. }
  171. case .normal:
  172. titleText = title
  173. }
  174. let gmeAlertVC = GMEAlertViewController(
  175. type: type,
  176. title: titleText,
  177. message: message,
  178. rightButtonTitle: rightButtonTitle,
  179. leftButtonTitle: leftButtonTitle,
  180. rightButtonAction: rightButtonAction,
  181. leftButtonAction: leftButtonAction
  182. )
  183. present(gmeAlertVC, animated: false, completion: nil)
  184. }
  185. }
  186. extension UIAlertController {
  187. func addAction(title: String?, style: UIAlertAction.Style = .default, handler: (()->())? = nil) {
  188. let action = UIAlertAction(title: title, style: style, handler: {_ in
  189. handler?()
  190. })
  191. self.addAction(action)
  192. }
  193. }
  194. struct Associate {
  195. static var hud: UInt8 = 0
  196. }
  197. // MARK: HUD
  198. extension UIViewController {
  199. private func setProgressHud() -> MBProgressHUD {
  200. let progressHud: MBProgressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
  201. progressHud.tintColor = UIColor.darkGray
  202. progressHud.removeFromSuperViewOnHide = true
  203. objc_setAssociatedObject(self, &Associate.hud, progressHud, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  204. return progressHud
  205. }
  206. var progressHud: MBProgressHUD {
  207. if let progressHud = objc_getAssociatedObject(self, &Associate.hud) as? MBProgressHUD {
  208. progressHud.isUserInteractionEnabled = true
  209. return progressHud
  210. }
  211. return setProgressHud()
  212. }
  213. var progressHudIsShowing: Bool {
  214. return self.progressHud.isHidden
  215. }
  216. func showProgressHud() {
  217. self.progressHud.show(animated: false)
  218. }
  219. func hideProgressHud() {
  220. self.progressHud.label.text = ""
  221. self.progressHud.completionBlock = {
  222. objc_setAssociatedObject(self, &Associate.hud, nil, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  223. }
  224. self.progressHud.hide(animated: false)
  225. }
  226. }
  227. // MARK: - LargeTitle
  228. extension UIViewController {
  229. func setLargeTitle(){
  230. let font = UIFont(name: "SanFranciscoDisplay-Medium", size: 20)!
  231. if #available(iOS 11.0, *) {
  232. self.navigationController?.navigationBar.prefersLargeTitles = true
  233. self.navigationController?.navigationBar.largeTitleTextAttributes =
  234. [NSAttributedString.Key.font: font]
  235. }
  236. // UINavigationBar.appearance().titleTextAttributes =
  237. // [NSAttributedStringKey.font: font]
  238. }
  239. }
  240. private var associatedObjectHandle: UInt8 = 0
  241. extension UIViewController {
  242. private var progressView: UIView? {
  243. get {
  244. guard let view = objc_getAssociatedObject(self, &associatedObjectHandle) as? UIView else {
  245. return nil
  246. }
  247. return view
  248. }
  249. set {
  250. objc_setAssociatedObject(self, &associatedObjectHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  251. }
  252. }
  253. func setPercent(with percent: CGFloat) {
  254. let progressSubView: UIView
  255. if let view = progressView {
  256. progressSubView = view
  257. } else {
  258. progressSubView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 2))
  259. progressView = progressSubView
  260. }
  261. progressSubView.removeFromSuperview()
  262. view.addSubview(progressSubView)
  263. print("progress: \(percent)")
  264. progressSubView.backgroundColor = AppConstants.themeRedColor
  265. let width = view.frame.width * percent
  266. UIView.animate(
  267. withDuration: 0.5,
  268. animations: {
  269. progressSubView.frame = CGRect(x: 0, y: 0, width: width, height: 2)
  270. },
  271. completion: { _ in
  272. if width == self.view.frame.width {
  273. UIView.animate(
  274. withDuration: 0.2,
  275. animations: {
  276. progressSubView.alpha = 0.0
  277. },
  278. completion: { _ in
  279. progressSubView.frame = CGRect(x: 0, y: 0, width: 0, height: 2)
  280. progressSubView.alpha = 1.0
  281. })
  282. }
  283. }
  284. )
  285. }
  286. }