OOCalendarLeftMenuController.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // OOCalendarLeftMenuController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2018/8/3.
  6. // Copyright © 2018 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class OOCalendarLeftMenuController: UITableViewController {
  11. var calendarIds:[String] = []
  12. private var myCalendarList: [OOCalendarInfo] = []
  13. private var departmentCalendarList: [OOCalendarInfo] = []
  14. private var followCalendarList: [OOCalendarInfo] = []
  15. private lazy var viewModel: OOCalendarViewModel = {
  16. return OOCalendarViewModel()
  17. }()
  18. @IBOutlet weak var addCalendarBtnView: UIView!
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. self.navigationItem.title = "日历"
  22. self.tableView.tableFooterView = UIView(frame: CGRect.zero)
  23. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "日历广场", style: .plain, target: self, action: #selector(openCalendars))
  24. addCalendarBtnView.addTapGesture { (tap) in
  25. DDLogInfo("点击了新增日历。。。。。。")
  26. self.performSegue(withIdentifier: "showCalendarSegue", sender: "add")
  27. }
  28. }
  29. override func viewWillAppear(_ animated: Bool) {
  30. viewModel.getMyCalendarList().then { (calendars) in
  31. self.myCalendarList = calendars.myCalendars ?? []
  32. self.departmentCalendarList = calendars.unitCalendars ?? []
  33. self.followCalendarList = calendars.followCalendars ?? []
  34. self.loadCalendarIds()
  35. self.tableView.reloadData()
  36. }.catch { (error) in
  37. DDLogError(error.localizedDescription)
  38. }
  39. }
  40. override func didReceiveMemoryWarning() {
  41. super.didReceiveMemoryWarning()
  42. // Dispose of any resources that can be recreated.
  43. }
  44. // MARK: - Table view data source
  45. override func numberOfSections(in tableView: UITableView) -> Int {
  46. // #warning Incomplete implementation, return the number of sections
  47. return 3
  48. }
  49. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  50. // #warning Incomplete implementation, return the number of rows
  51. switch section {
  52. case 0:
  53. return myCalendarList.count
  54. case 1:
  55. return departmentCalendarList.count
  56. case 2:
  57. return followCalendarList.count
  58. default:
  59. return 0
  60. }
  61. }
  62. override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  63. let headerBase = UIView.init()
  64. headerBase.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 21)
  65. headerBase.backgroundColor = UIColor.init(r: CGFloat(240), g: CGFloat(240), b: CGFloat(240))
  66. let topLine = UIView.init()
  67. topLine.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 1)
  68. topLine.backgroundColor = UIColor.lightGray
  69. headerBase.addSubview(topLine)
  70. let header = UILabel.init()
  71. header.frame = CGRect(x: CGFloat(10), y: 1, width: 100, height: 20)
  72. header.font = setting_item_textFont
  73. header.theme_textColor = ThemeColorPicker(keyPath: "Base.base_color")
  74. var title = ""
  75. switch section {
  76. case 0:
  77. title = "我的日历"
  78. case 1:
  79. title = "部门日历"
  80. case 2:
  81. title = "关注的日历"
  82. default:
  83. title = ""
  84. }
  85. header.text = title
  86. headerBase.addSubview(header)
  87. return headerBase
  88. }
  89. /**/
  90. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  91. let cell = tableView.dequeueReusableCell(withIdentifier: "calendarTableCell", for: indexPath) as! CalendarTableViewCell
  92. switch indexPath.section {
  93. case 0:
  94. cell.renderCalendar(info: self.myCalendarList[indexPath.row], self.calendarIds)
  95. break
  96. case 1:
  97. cell.renderCalendar(info: self.departmentCalendarList[indexPath.row], self.calendarIds)
  98. break
  99. case 2:
  100. cell.renderCalendar(info: self.followCalendarList[indexPath.row], self.calendarIds)
  101. break
  102. default:
  103. DDLogInfo("没有的事。。。。。")
  104. }
  105. cell.calendarCellDelegate = self
  106. return cell
  107. }
  108. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  109. DDLogInfo("点击了table row ,section: \(indexPath.section) ,row: \(indexPath.row)")
  110. //修改
  111. if indexPath.section == 0 {
  112. self.performSegue(withIdentifier: "showCalendarSegue", sender: "update")
  113. }
  114. if indexPath.section == 1 {
  115. self.performSegue(withIdentifier: "showCalendarSegue", sender: "updateDept")
  116. }
  117. tableView.deselectRow(at: indexPath, animated: false)
  118. }
  119. /*
  120. // MARK: - Navigation
  121. */
  122. // In a storyboard-based application, you will often want to do a little preparation before navigation
  123. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  124. if segue.identifier == "showCalendarSegue" {
  125. if let type = (sender as? String) {
  126. if type == "update" {
  127. let cc = segue.destination as! OOCalendarViewController
  128. let row = tableView.indexPathForSelectedRow!.row
  129. cc.calendarInfo = self.myCalendarList[row]
  130. }
  131. if type == "updateDept"{
  132. let cc = segue.destination as! OOCalendarViewController
  133. let row = tableView.indexPathForSelectedRow!.row
  134. cc.calendarInfo = self.departmentCalendarList[row]
  135. }
  136. }
  137. }
  138. }
  139. //前一页传过来的ids如果有值 和 这边重新从网络获取的数据进行id比较合并
  140. private func loadCalendarIds() {
  141. var newCalendarids:[String] = []
  142. self.myCalendarList.forEach { (c) in
  143. newCalendarids.append(c.id!)
  144. }
  145. self.followCalendarList.forEach { (c) in
  146. newCalendarids.append(c.id!)
  147. }
  148. self.departmentCalendarList.forEach { (c) in
  149. newCalendarids.append(c.id!)
  150. }
  151. self.calendarIds.forEach { (id) in
  152. if !newCalendarids.contains(id) {
  153. self.calendarIds.removeFirst(id)
  154. }
  155. }
  156. }
  157. //日历广场
  158. @objc private func openCalendars() {
  159. self.performSegue(withIdentifier: "showCalendarStore", sender: nil)
  160. }
  161. }
  162. // extension
  163. extension OOCalendarLeftMenuController: CalendarCellSwithOnDelegate {
  164. func click(isOn: Bool, calendar: OOCalendarInfo?) {
  165. if !self.calendarIds.isEmpty {
  166. if isOn {
  167. self.calendarIds.append(calendar!.id!)
  168. }else {
  169. self.calendarIds.removeFirst(calendar!.id!)
  170. }
  171. }else { //第一次
  172. var newCalendarids:[String] = []
  173. self.myCalendarList.forEach { (c) in
  174. if c.id == calendar?.id {
  175. if isOn {
  176. newCalendarids.append(c.id!)
  177. }
  178. }else {
  179. newCalendarids.append(c.id!)
  180. }
  181. }
  182. self.followCalendarList.forEach { (c) in
  183. if c.id == calendar?.id {
  184. if isOn {
  185. newCalendarids.append(c.id!)
  186. }
  187. }else {
  188. newCalendarids.append(c.id!)
  189. }
  190. }
  191. self.departmentCalendarList.forEach { (c) in
  192. if c.id == calendar?.id {
  193. if isOn {
  194. newCalendarids.append(c.id!)
  195. }
  196. }else {
  197. newCalendarids.append(c.id!)
  198. }
  199. }
  200. self.calendarIds = newCalendarids
  201. }
  202. NotificationCenter.default.post(name: OONotification.calendarIds.notificationName, object: self.calendarIds)
  203. }
  204. }