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.

423 lines
13 KiB

6 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
3 years ago
2 years ago
2 years ago
4 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. import NVActivityIndicatorView
  12. #if DEBUG
  13. import FLEX
  14. #endif
  15. // MARK: - Navigation
  16. extension UIViewController {
  17. func setupPicturedNavBar(sideMenuAction: Selector? = nil) {
  18. self.navigationController?.isNavigationBarHidden = false
  19. self.navigationController?.navigationBar.isTranslucent = false
  20. self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
  21. if let selector = sideMenuAction {
  22. let leftButton = UIBarButtonItem(
  23. image: UIImage(named: "new_menu")?.withRenderingMode(.alwaysOriginal),
  24. style: .plain,
  25. target: self,
  26. action: selector
  27. )
  28. self.navigationItem.leftBarButtonItem = leftButton
  29. } else {
  30. self.navigationItem.leftBarButtonItem = nil
  31. }
  32. self.navigationController?.navigationBar.barTintColor = .white
  33. let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 125, height: 30))
  34. imageView.contentMode = .scaleAspectFit
  35. let image = UIImage(named: "nav_logo")?.withRenderingMode(.alwaysOriginal)
  36. imageView.image = image
  37. self.navigationItem.titleView = imageView
  38. self.setupNavigationShadow(isUse: false)
  39. }
  40. func setupNormalNavigation(color: UIColor = .themeWhite) {
  41. self.navigationController?.isNavigationBarHidden = false
  42. self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "ic_back_button")?.withRenderingMode(.alwaysOriginal)
  43. self.navigationController?.navigationBar.isTranslucent = false
  44. self.navigationController?.navigationBar
  45. .backIndicatorTransitionMaskImage = UIImage(named: "ic_back_button")?.withRenderingMode(.alwaysOriginal)
  46. self.navigationController?.navigationBar.titleTextAttributes = [
  47. NSAttributedString.Key.font: UIFont.sanfrancisco(.medium, size: 17)
  48. ]
  49. self.navigationController?.navigationBar.barTintColor = color
  50. self.setupNavigationShadow(isUse: false)
  51. }
  52. func setupNavigationShadow(isUse: Bool = true) {
  53. navigationController?.navigationBar.setValue(!isUse, forKey: "hidesShadow")
  54. }
  55. }
  56. // MARK: - Alert
  57. extension UIViewController {
  58. enum AlertType {
  59. case success
  60. case error
  61. case normal
  62. }
  63. func alert(
  64. type: AlertType = .normal,
  65. message: String?,
  66. title: String? = nil,
  67. rightButtomTitle: String = "ok_text".localized(),
  68. okAction: (() -> Void)? = nil
  69. ) {
  70. let settedTitle: String
  71. switch type {
  72. case .normal:
  73. settedTitle = title == nil ? "alert_text".localized() : title!
  74. case .error:
  75. settedTitle = title == nil ? "error_text".localized() : title!
  76. case .success:
  77. settedTitle = title == nil ? "success_text".localized() : title!
  78. }
  79. gmeAlert(
  80. type: type,
  81. title: settedTitle,
  82. message: message,
  83. rightButtonTitle: rightButtomTitle,
  84. leftButtonTitle: nil,
  85. rightButtonAction: okAction,
  86. leftButtonAction: nil
  87. )
  88. }
  89. func alertWithOkCancel(
  90. type: AlertType = .normal,
  91. message: String?,
  92. title: String? = nil,
  93. okTitle: String = "ok_text".localized(),
  94. cancelTitle: String = "cancel_text".localized(),
  95. okAction: (() -> Void)? = nil,
  96. cancelAction: (() -> Void)? = nil
  97. ) {
  98. let settedTitle: String
  99. switch type {
  100. case .normal:
  101. settedTitle = title == nil ? "alert_text".localized() : title!
  102. case .error:
  103. settedTitle = title == nil ? "error_text".localized() : title!
  104. case .success:
  105. settedTitle = title == nil ? "success_text".localized() : title!
  106. }
  107. gmeAlert(
  108. type: type,
  109. title: settedTitle,
  110. message: message,
  111. rightButtonTitle: okTitle,
  112. leftButtonTitle: cancelTitle,
  113. rightButtonAction: okAction,
  114. leftButtonAction: cancelAction
  115. )
  116. }
  117. func alertWithOk(
  118. type: AlertType = .normal,
  119. message: String?,
  120. title: String? = nil,
  121. okTitle: String = "ok_text".localized(),
  122. okAction: (() -> Void)? = nil
  123. ) {
  124. let settedTitle: String
  125. switch type {
  126. case .normal:
  127. settedTitle = title == nil ? "alert_text".localized() : title!
  128. case .error:
  129. settedTitle = title == nil ? "error_text".localized() : title!
  130. case .success:
  131. settedTitle = title == nil ? "success_text".localized() : title!
  132. }
  133. gmeAlert(
  134. type: type,
  135. title: settedTitle,
  136. message: message,
  137. rightButtonTitle: okTitle,
  138. leftButtonTitle: nil,
  139. rightButtonAction: okAction,
  140. leftButtonAction: nil
  141. )
  142. }
  143. private func getAlert(
  144. type: AlertType,
  145. message: String?,
  146. title: String?,
  147. style: UIAlertController.Style? = .alert
  148. ) -> UIAlertController {
  149. let customerWalletNumber = GMEDB.shared.user.string(.referralCode) ?? ""
  150. let customerTitle: String
  151. switch type {
  152. case .error:
  153. customerTitle = "\(title ?? "Alert")(\(customerWalletNumber))"
  154. case .normal:
  155. customerTitle = title ?? ""
  156. case .success:
  157. customerTitle = title ?? ""
  158. }
  159. return UIAlertController(title: customerTitle, message: message, preferredStyle: style ?? .alert)
  160. }
  161. func present(_ alert: UIAlertController, asActionsheetInSourceView sourceView: Any) {
  162. if UI_USER_INTERFACE_IDIOM() == .pad {
  163. alert.modalPresentationStyle = .popover
  164. if let presenter = alert.popoverPresentationController {
  165. if sourceView is UIBarButtonItem {
  166. presenter.barButtonItem = sourceView as? UIBarButtonItem
  167. } else if sourceView is UIView {
  168. guard let view = sourceView as? UIView else {
  169. return
  170. }
  171. presenter.sourceView = view
  172. presenter.sourceRect = view.bounds
  173. }
  174. }
  175. }
  176. self.present(alert, animated: true, completion: nil)
  177. }
  178. func gmeAlert(
  179. type: AlertType = .normal,
  180. title: String = "Alert",
  181. message: String? = nil,
  182. rightButtonTitle: String = "Ok",
  183. leftButtonTitle: String? = nil,
  184. rightButtonAction: (() -> Void)? = nil,
  185. leftButtonAction: (() -> Void)? = nil
  186. ) {
  187. let titleText: String
  188. let customerWalletNumber = GMEDB.shared.user.string(.referralCode) ?? ""
  189. switch type {
  190. case .error:
  191. if customerWalletNumber != "" {
  192. titleText = "\(title)"
  193. } else {
  194. titleText = title
  195. }
  196. case .normal:
  197. titleText = title
  198. case .success:
  199. titleText = title
  200. }
  201. let gmeAlertVC = GMEAlertViewController(
  202. type: type,
  203. title: titleText,
  204. message: message,
  205. rightButtonTitle: rightButtonTitle,
  206. leftButtonTitle: leftButtonTitle,
  207. rightButtonAction: rightButtonAction,
  208. leftButtonAction: leftButtonAction
  209. )
  210. present(gmeAlertVC, animated: false, completion: nil)
  211. }
  212. }
  213. extension UIAlertController {
  214. func addAction(title: String?, style: UIAlertAction.Style = .default, handler: (() -> Void)? = nil) {
  215. let action = UIAlertAction(title: title, style: style, handler: {_ in
  216. handler?()
  217. })
  218. self.addAction(action)
  219. }
  220. }
  221. struct Associate {
  222. static var hud: UInt8 = 0
  223. static var messageCount: UInt8 = 1
  224. static var csButton: UInt8 = 2
  225. static var channelIO: UInt8 = 3
  226. }
  227. // MARK: HUD
  228. extension UIViewController {
  229. private func setProgressHud() -> MBProgressHUD {
  230. let progressHud: MBProgressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
  231. progressHud.tintColor = UIColor.darkGray
  232. progressHud.removeFromSuperViewOnHide = true
  233. objc_setAssociatedObject(
  234. self, &Associate.hud,
  235. progressHud,
  236. objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC
  237. )
  238. return progressHud
  239. }
  240. var progressHud: MBProgressHUD {
  241. if let progressHud = objc_getAssociatedObject(self, &Associate.hud) as? MBProgressHUD {
  242. progressHud.isUserInteractionEnabled = true
  243. return progressHud
  244. }
  245. return setProgressHud()
  246. }
  247. var progressHudIsShowing: Bool {
  248. return self.progressHud.isHidden
  249. }
  250. func showProgressHud(
  251. backgroundColor: UIColor = .themeBackgroundGray,
  252. loadingColor: UIColor = .themeRed,
  253. textColor: UIColor = .white
  254. ) {
  255. let activityData = ActivityData(
  256. size: CGSize(width: 50, height: 50),
  257. message: "loading_text".localized(),
  258. messageFont: .sanfrancisco(.medium, size: 14),
  259. messageSpacing: 2,
  260. type: .lineScale,
  261. color: loadingColor,
  262. padding: 5,
  263. backgroundColor: backgroundColor,
  264. textColor: textColor
  265. )
  266. NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData, nil)
  267. }
  268. func hideProgressHud() {
  269. NVActivityIndicatorPresenter.sharedInstance.stopAnimating(nil)
  270. }
  271. }
  272. // MARK: - LargeTitle
  273. extension UIViewController {
  274. func setLargeTitle() {
  275. if #available(iOS 11.0, *) {
  276. self.navigationController?.navigationBar.prefersLargeTitles = true
  277. self.navigationController?.navigationBar.largeTitleTextAttributes =
  278. [NSAttributedString.Key.font: UIFont.sanfrancisco(.medium, size: 20)]
  279. }
  280. // UINavigationBar.appearance().titleTextAttributes =
  281. // [NSAttributedStringKey.font: font]
  282. }
  283. }
  284. // MARK: - Activity Indicator
  285. private var associatedObjectHandle: UInt8 = 0
  286. extension UIViewController {
  287. private var progressView: UIView? {
  288. get {
  289. guard let view = objc_getAssociatedObject(self, &associatedObjectHandle) as? UIView else {
  290. return nil
  291. }
  292. return view
  293. }
  294. set {
  295. objc_setAssociatedObject(
  296. self,
  297. &associatedObjectHandle,
  298. newValue,
  299. objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC
  300. )
  301. }
  302. }
  303. func setPercent(with percent: CGFloat) {
  304. let progressSubView: UIView
  305. if let view = progressView {
  306. progressSubView = view
  307. } else {
  308. progressSubView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 2))
  309. progressView = progressSubView
  310. }
  311. progressSubView.removeFromSuperview()
  312. view.addSubview(progressSubView)
  313. print("progress: \(percent)")
  314. progressSubView.backgroundColor = .themeRed
  315. let width = view.frame.width * percent
  316. UIView.animate(
  317. withDuration: 0.5,
  318. animations: {
  319. progressSubView.frame = CGRect(x: 0, y: 0, width: width, height: 2)
  320. },
  321. completion: { _ in
  322. if width == self.view.frame.width {
  323. UIView.animate(
  324. withDuration: 0.2,
  325. animations: {
  326. progressSubView.alpha = 0.0
  327. },
  328. completion: { _ in
  329. progressSubView.frame = CGRect(x: 0, y: 0, width: 0, height: 2)
  330. progressSubView.alpha = 1.0
  331. })
  332. }
  333. }
  334. )
  335. }
  336. }
  337. extension UIViewController {
  338. func presentDatePicker(completion: ((_ from: String?, _ to: String?) -> Void)?) {
  339. guard let datePickerViewController = UIStoryboard
  340. .init(name: "TransactionHistoryDatePicker", bundle: nil)
  341. .instantiateViewController(
  342. withIdentifier: "DatePickerViewController"
  343. ) as? DatePickerViewController else { return }
  344. datePickerViewController.completion = completion
  345. present(datePickerViewController, animated: true, completion: nil)
  346. }
  347. }
  348. extension UIViewController {
  349. func openPasswordInput(success: @escaping (String) -> Void){
  350. let passwordVC = PasswordInputViewController()
  351. passwordVC.enteredPassword = { value in
  352. success(value)
  353. }
  354. passwordVC.modalPresentationStyle = .overFullScreen
  355. passwordVC.modalTransitionStyle = .crossDissolve
  356. self.present(passwordVC, animated: false)
  357. }
  358. }