SpotsPageViewController.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // SpotsPageViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2021/9/16.
  6. // Copyright © 2021 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. /// 现货主视图容器控制类
  11. class SpotsPageViewController: BaseViewController {
  12. // MARK: - 属性列表
  13. /// title
  14. @IBOutlet weak var segmentedView: JXSegmentedView! {
  15. didSet {
  16. segmentedView.dataSource = dataSource
  17. segmentedView.indicators = [indicator]
  18. segmentedView.delegate = self
  19. }
  20. }
  21. /// 内容承载视图
  22. @IBOutlet weak var contentView: UIView!
  23. /// 新增现货合同/购销计划
  24. @IBOutlet weak var new: UIBarButtonItem!
  25. /// 权限功能
  26. var roleTitles: [String] {
  27. get {
  28. var titles: [String] = []
  29. /// 权限控制
  30. guard let commonManager = MTP2BusinessCore.shared.commonManager else {
  31. return titles
  32. }
  33. if commonManager.containMenu(key: "client_spots_purchase") {
  34. titles.append("采购合同")
  35. }
  36. if commonManager.containMenu(key: "client_spots_sales") {
  37. titles.append("销售合同")
  38. }
  39. if commonManager.containMenu(key: "client_spots_hedgeplan") {
  40. titles.append("购销计划")
  41. }
  42. if commonManager.containMenu(key: "client_spots_link") {
  43. titles.append("期现关联")
  44. }
  45. return titles
  46. }
  47. }
  48. /// 采购视图容器控制类
  49. lazy var purchase: PurchaseViewController = {
  50. $0.econtractType = .Buy
  51. return $0
  52. }(viewController("Spots", "Purchase") as! PurchaseViewController)
  53. /// 销售视图容器控制类
  54. lazy var sales: PurchaseViewController = {
  55. $0.econtractType = .Sale
  56. return $0
  57. }(viewController("Spots", "Purchase") as! PurchaseViewController)
  58. /// 购销计划视图容器控制类
  59. lazy var hedgePlan: HedgePlanViewController = {
  60. /// 不是审核模式
  61. $0.isAduit = false
  62. return $0
  63. }(viewController("Spots", "HedgePlan") as! HedgePlanViewController)
  64. /// 期现关联视图容器控制类
  65. lazy var link: SpotsLinkViewController = {
  66. return $0
  67. }(viewController("Spots", "SpotsLink") as! SpotsLinkViewController)
  68. // MARK: - 生命周期
  69. override func viewWillAppear(_ animated: Bool) {
  70. super.viewWillAppear(animated)
  71. /// 允许横屏
  72. let app = UIApplication.shared.delegate as? AppDelegate
  73. app?.isRotation = true
  74. /// 侦听横竖屏切换
  75. NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChange(interfaceOrientation:)), name: UIDevice.orientationDidChangeNotification, object: nil)
  76. }
  77. override func viewDidDisappear(_ animated: Bool) {
  78. super.viewDidDisappear(animated)
  79. /// 允许横屏
  80. let app = UIApplication.shared.delegate as? AppDelegate
  81. app?.isRotation = true
  82. /// 移出侦听
  83. NotificationCenter.default.removeObserver(self)
  84. }
  85. @objc func orientationDidChange(interfaceOrientation: UIInterfaceOrientation) {
  86. /// 横竖屏切换时 重新计算
  87. segmentedView.width = self.view.width
  88. segmentedView.reloadData()
  89. }
  90. override func viewDidLoad() {
  91. super.viewDidLoad()
  92. // Do any additional setup after loading the view.
  93. /// 隐藏返回按钮
  94. addBackBarButtonItem(true)
  95. /// titles
  96. dataSource.titles = roleTitles
  97. segmentedView.reloadData()
  98. /// 默认显示采购视图
  99. showChildController(purchase)
  100. }
  101. // MARK: - 视图切换
  102. /// 添加子视图
  103. /// - Parameter controller: controller
  104. fileprivate func showChildController(_ controller: BaseViewController) {
  105. /// 先移出所有的子视图
  106. let childs: [UIViewController] = self.children
  107. childs.forEach { child in
  108. child.removeFromParent()
  109. child.view.removeFromSuperview()
  110. }
  111. /// 添加视图
  112. if !contentView.subviews.contains(controller.view) {
  113. controller.view.frame = contentView.bounds
  114. contentView.addSubview(controller.view)
  115. self.addChild(controller)
  116. controller.didMove(toParent: self)
  117. }
  118. }
  119. // MARK: - 交互操作
  120. /// onBarButtonItemPressed
  121. /// - Parameter sender: sender
  122. @IBAction fileprivate func onBarButtonItemPressed(_ sender: UIBarButtonItem) {
  123. switch sender {
  124. case new: /// 新增
  125. /// 新增现货合同/购销计划
  126. performSegue(withIdentifier: roleTitles[segmentedView.selectedIndex] == "购销计划" ? "ShowNewHedgePlan" : "ShowNewSpotContract", sender: nil)
  127. default:
  128. break
  129. }
  130. }
  131. // MARK: - Navigation
  132. // In a storyboard-based application, you will often want to do a little preparation before navigation
  133. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  134. // Get the new view controller using segue.destination.
  135. // Pass the selected object to the new view controller.
  136. ///
  137. if segue.identifier == "ShowNewHedgePlan", let n = segue.destination as? NewHedgePlanViewController {
  138. /// 新增购销计划
  139. n.popBlock = { (_ takeInfo: Any?) in
  140. /// 刷新列表数据
  141. self.hedgePlan.mainScrollView.triggerRefreshing()
  142. }
  143. } else if segue.identifier == "ShowNewSpotContract", let n = segue.destination as? NewSpotContractViewController {
  144. /// 新增现货合同
  145. n.popBlock = { (_ takeInfo: Any?) in
  146. if self.roleTitles[self.segmentedView.selectedIndex] == "采购合同" {
  147. self.purchase.mainScrollView.triggerRefreshing()
  148. } else {
  149. self.sales.mainScrollView.triggerRefreshing()
  150. }
  151. }
  152. DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
  153. /// 合同类型
  154. n.econtractType = self.segmentedView.selectedIndex == 0 ? .Buy : .Sale
  155. }
  156. }
  157. }
  158. }
  159. // MARK: - JXSegmentedViewDelegate
  160. extension SpotsPageViewController: JXSegmentedViewDelegate {
  161. /// didSelectedItemAt
  162. /// - Parameters:
  163. /// - segmentedView: segmentedView
  164. /// - index: index
  165. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  166. switch roleTitles[index] {
  167. case "采购合同": /// 采购合同
  168. showChildController(purchase)
  169. case "销售合同": /// 销售合同
  170. showChildController(sales)
  171. case "期现关联": /// 期现关联
  172. showChildController(link)
  173. default: /// 购销计划
  174. showChildController(hedgePlan)
  175. }
  176. }
  177. }