ContactGroupPickerViewController.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // ContactGroupPickerViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/8/12.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class ContactGroupPickerViewController: UITableViewController {
  11. private var groupDataList:[OOGroupModel] = []
  12. private let viewModel: ContactPickerViewModel = {
  13. return ContactPickerViewModel()
  14. }()
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. DDLogDebug("viewdidload ............group")
  18. //分页刷新功能
  19. self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
  20. self.loadFirstPageData()
  21. })
  22. self.tableView.mj_footer = MJRefreshAutoFooter(refreshingBlock: {
  23. self.loadNextPageData()
  24. })
  25. self.loadFirstPageData()
  26. }
  27. // MARK: - Table view data source
  28. override func numberOfSections(in tableView: UITableView) -> Int {
  29. return 1
  30. }
  31. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  32. return self.groupDataList.count
  33. }
  34. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  35. return 10
  36. }
  37. override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  38. return 3
  39. }
  40. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  41. let cell = tableView.dequeueReusableCell(withIdentifier: "groupPickerViewCell", for: indexPath) as! GroupPickerTableViewCell
  42. let group = self.groupDataList[indexPath.row]
  43. cell.loadGroupInfo(info: group, checked: self.isSelected(value: group.distinguishedName!))
  44. return cell
  45. }
  46. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  47. let value = self.groupDataList[indexPath.row].distinguishedName!
  48. if self.isSelected(value: value) {
  49. self.removeSelected(value: value)
  50. }else {
  51. self.addSelected(group: self.groupDataList[indexPath.row])
  52. }
  53. self.tableView.reloadRows(at: [indexPath], with: .automatic)
  54. self.tableView.deselectRow(at: indexPath, animated: false)
  55. }
  56. /*
  57. // MARK: - Navigation
  58. // In a storyboard-based application, you will often want to do a little preparation before navigation
  59. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  60. // Get the new view controller using segue.destination.
  61. // Pass the selected object to the new view controller.
  62. }
  63. */
  64. //MARK: - private method
  65. private func loadFirstPageData() {
  66. DDLogDebug("loadFirstPageData ............group")
  67. self.showLoading()
  68. viewModel.loadGroupList(lastId: "(0)").then { (list) in
  69. self.groupDataList.removeAll()
  70. self.groupDataList = list
  71. self.tableView.reloadData()
  72. self.hideLoading()
  73. if self.tableView.mj_header.isRefreshing(){
  74. self.tableView.mj_header.endRefreshing()
  75. }
  76. DDLogDebug("loadFirstPageData ............finish")
  77. }.catch { (error) in
  78. DDLogError(error.localizedDescription)
  79. self.hideLoading()
  80. if self.tableView.mj_header.isRefreshing(){
  81. self.tableView.mj_header.endRefreshing()
  82. }
  83. }
  84. }
  85. private func loadNextPageData() {
  86. DDLogDebug("loadNextPageData ............group")
  87. if let last = self.groupDataList.last?.id {
  88. viewModel.loadGroupList(lastId: last).then { (list) in
  89. list.forEach({ (model) in
  90. self.groupDataList.append(model)
  91. })
  92. self.tableView.reloadData()
  93. self.hideLoading()
  94. if self.tableView.mj_footer.isRefreshing(){
  95. self.tableView.mj_footer.endRefreshing()
  96. }
  97. }.catch { (error) in
  98. DDLogError(error.localizedDescription)
  99. self.hideLoading()
  100. if self.tableView.mj_footer.isRefreshing(){
  101. self.tableView.mj_footer.endRefreshing()
  102. }
  103. }
  104. }else {
  105. self.loadFirstPageData()
  106. }
  107. }
  108. private func isSelected(value: String) -> Bool {
  109. if let vc = self.parent as? ContactPickerViewController {
  110. return vc.isSelectedValue(type: .group, value: value)
  111. }
  112. return false
  113. }
  114. private func removeSelected(value: String) {
  115. if let vc = self.parent as? ContactPickerViewController {
  116. vc.removeSelectedValue(type: .group, value: value)
  117. }
  118. }
  119. private func addSelected(group: OOGroupModel) {
  120. if let vc = self.parent as? ContactPickerViewController {
  121. vc.addSelectedGroup(group: group)
  122. }
  123. }
  124. }