OOListUnitViewController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // OOListUnitViewController.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/11/21.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import EmptyDataSet_Swift
  10. class OOListUnitViewController: UITableViewController {
  11. open var isShowSearchControl = true
  12. open var unit:OOUnitModel?{
  13. didSet {
  14. self.currentUnit = unit
  15. stackOfUnit.push(unit!)
  16. }
  17. }
  18. private var currentUnit:OOUnitModel?
  19. private var stackOfUnit = Stack<OOUnitModel>()
  20. private lazy var viewModel = {
  21. return OOListUnitViewModel()
  22. }()
  23. var searchController:OOUISearchController!
  24. private lazy var searchResultController = { () -> UIViewController? in
  25. let searchVC = self.storyboard?.instantiateViewController(withIdentifier: "OOContactSearchController")
  26. return searchVC
  27. }()
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. if isShowSearchControl {
  31. searchController = OOUISearchController(searchResultsController: searchResultController)
  32. //searchController.dimsBackgroundDuringPresentation = false
  33. searchController.searchResultsUpdater = searchResultController as! UISearchResultsUpdating
  34. self.tableView.tableHeaderView = searchController.searchBar
  35. }
  36. self.navigationItem.hidesBackButton = false
  37. self.tableView.emptyDataSetSource = self
  38. self.tableView.emptyDataSetDelegate = self
  39. //加入手势操作
  40. let rightSwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(toggleRightAction))
  41. rightSwipeGestureRecognizer.direction = .right
  42. tableView.addGestureRecognizer(rightSwipeGestureRecognizer)
  43. let leftSwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(toggleLeftAction))
  44. leftSwipeGestureRecognizer.direction = .left
  45. tableView.addGestureRecognizer(leftSwipeGestureRecognizer)
  46. viewModel.updateBlock = { msg in
  47. self.tableView.reloadData()
  48. }
  49. title = unit?.name
  50. refreshData()
  51. }
  52. private func refreshData(){
  53. title = self.currentUnit?.shortName
  54. viewModel.refreshData(self.currentUnit?.id ?? "")
  55. }
  56. @objc func toggleRightAction(){
  57. print("rightSwipe")
  58. let _ = stackOfUnit.pop()
  59. if stackOfUnit.isEmpty {
  60. guard let returnVC = self.navigationController?.popViewController(animated: true) else {
  61. self.dismiss(animated: true, completion: nil)
  62. return
  63. }
  64. }else{
  65. let preUnit = stackOfUnit.pop()
  66. self.unit = preUnit
  67. refreshData()
  68. }
  69. // if let preUnit = stackOfUnit.pop() {
  70. // if stackOfUnit.isEmpty {
  71. //
  72. // }else{
  73. // self.currentUnit = preUnit
  74. // refreshData()
  75. // }
  76. // }
  77. }
  78. @objc func toggleLeftAction(){
  79. print("leftSwipe")
  80. }
  81. @IBAction func backPreVC(_ sender: UIBarButtonItem) {
  82. toggleRightAction()
  83. }
  84. override func didReceiveMemoryWarning() {
  85. super.didReceiveMemoryWarning()
  86. // Dispose of any resources that can be recreated.
  87. }
  88. // MARK: - Table view data source
  89. override func numberOfSections(in tableView: UITableView) -> Int {
  90. return viewModel.numberOfSections()
  91. }
  92. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  93. return viewModel.numberOfRowsInSection(section)
  94. }
  95. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  96. let cell = tableView.dequeueReusableCell(withIdentifier: "CDLCell", for: indexPath)
  97. let item = viewModel.nodeForIndexPath(indexPath)
  98. let uCell = cell as! (Configurable & OOCDLCell)
  99. uCell.viewModel = viewModel
  100. uCell.config(withItem: item)
  101. return cell
  102. }
  103. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  104. let item = viewModel.nodeForIndexPath(indexPath)
  105. let m = item as! NSObject
  106. if m.isKind(of: OOUnitModel.self) {
  107. self.unit = m as? OOUnitModel
  108. self.refreshData()
  109. }else if m.isKind(of: OOPersonModel.self) {
  110. let p = m as! OOPersonModel
  111. self.performSegue(withIdentifier: "showPersonSegue", sender: p)
  112. }
  113. }
  114. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  115. return viewModel.headerHeightOfSection(section)
  116. }
  117. override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  118. let headerView = Bundle.main.loadNibNamed("OOContactUnitHeader", owner: self, options: nil)![0] as! OOContactUnitHeader
  119. headerView.setNavBar((currentUnit?.level)!,currentUnit?.levelName)
  120. return headerView
  121. }
  122. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  123. return false
  124. }
  125. override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
  126. return .none
  127. }
  128. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  129. if segue.identifier == "showPersonSegue" {
  130. let destVC = segue.destination as! OOLinkeManViewController
  131. destVC.currentPerson = sender as? OOPersonModel
  132. }
  133. }
  134. }
  135. // MARK: - DZN DataSource
  136. extension OOListUnitViewController:EmptyDataSetSource,EmptyDataSetDelegate {
  137. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  138. return #imageLiteral(resourceName: "icon_moren_2")
  139. }
  140. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  141. let text = "没有组织和人员"
  142. let titleAttributes = [NSAttributedString.Key.foregroundColor:UIColor.lightText,NSAttributedString.Key.font:UIFont.init(name: "PingFangSC-Regular", size: 18)!]
  143. return NSMutableAttributedString(string: text, attributes: titleAttributes)
  144. }
  145. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  146. return UIColor(hex:"#999999")
  147. }
  148. }