MainViewController.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // MainViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/5/19.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import UIKit
  9. import NVActivityIndicatorView
  10. import WHToast
  11. /// 主窗体
  12. class MainViewController: UITabBarController {
  13. // MARK: - 生命周期相关
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. /// 设置委托代理
  17. self.delegate = self
  18. /// 初始化界面
  19. buildView()
  20. }
  21. override func viewWillAppear(_ animated: Bool) {
  22. super.viewWillAppear(animated)
  23. /// 开始查询公告消息数据
  24. MTP2BusinessCore.shared.noticeManager?.startAutoQueryNotices()
  25. }
  26. override func viewDidAppear(_ animated: Bool) {
  27. super.viewDidAppear(animated)
  28. /// 添加广播通知接受
  29. MTP2BusinessCore.shared.broadcastManager?.addBroadcastListener(owner: self, action: .NoticeChange, block: { (obj) in
  30. DispatchQueue.main.async {
  31. if let objs = MTP2BusinessCore.shared.noticeManager?.notices.filter({$0.readed == false}),
  32. objs.count != 0 {
  33. self.viewControllers?[2].tabBarItem.badgeValue = "\(objs.count)"
  34. }
  35. }
  36. })
  37. }
  38. override func viewDidDisappear(_ animated: Bool) {
  39. super.viewDidAppear(animated)
  40. MTP2BusinessCore.shared.broadcastManager?.removeBroadcastListener(owner: self)
  41. }
  42. // MARK: - 广播通知
  43. // MARK: - 加载配置项功能相关
  44. private func buildView() {
  45. /// 设置tabbar背景色
  46. self.tabBar.barTintColor = UIColor.white
  47. /// 去掉毛玻璃效果
  48. self.tabBar.isTranslucent = false
  49. if let fundations = ConfigUtils.getValue(key: .functionModule) as? [String] {
  50. var viewcontrollers = [UIViewController]()
  51. for id in fundations {
  52. if id == "Home" { /// 首页
  53. let navigationController = UIStoryboard(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "Home") as! BaseNavigationController
  54. viewcontrollers.append(navigationController)
  55. } else if id == "Mine" { /// 我的页面
  56. let navigationController = UIStoryboard(name: "Mine", bundle: nil).instantiateViewController(withIdentifier: "Mine") as! BaseNavigationController
  57. viewcontrollers.append(navigationController)
  58. } else if id == "Quote" { /// 行情
  59. let navigationController = UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "Quote") as! BaseNavigationController
  60. viewcontrollers.append(navigationController)
  61. } else if id == "Message" { /// 消息
  62. let navigationController = UIStoryboard(name: "Message", bundle: nil).instantiateViewController(withIdentifier: "Message") as! BaseNavigationController
  63. viewcontrollers.append(navigationController)
  64. }
  65. }
  66. self.viewControllers = viewcontrollers
  67. }
  68. }
  69. }
  70. extension MainViewController: UITabBarControllerDelegate {
  71. func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
  72. /// 未登录
  73. if let sub = (viewController as? BaseNavigationController)?.viewControllers[0],
  74. sub.isKind(of: MessageViewController.self),
  75. !MTP2BusinessCore.shared.getLoginStatus() {
  76. /// 弹出登录页面
  77. tabBarController.viewControllers?[0].present(storyboardName: "Main", storyboardId: "Login")
  78. return false
  79. }
  80. return true
  81. }
  82. }