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

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