OOCalendarStoreViewController.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // OOCalendarStoreViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/9/22.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class OOCalendarStoreViewController: UITableViewController {
  11. private var publicCalendarList: [OOCalendarInfo] = []
  12. private lazy var viewModel: OOCalendarViewModel = {
  13. return OOCalendarViewModel()
  14. }()
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. self.tableView.tableFooterView = UIView(frame: CGRect.zero)
  18. self.viewModel.getPublicCalendarList().then { (list) in
  19. self.publicCalendarList = list
  20. self.tableView.reloadData()
  21. }.catch{ err in
  22. DDLogError("请求错误,\(err.localizedDescription)")
  23. self.showError(title: "获取日历失败!")
  24. }
  25. }
  26. // MARK: - Table view data source
  27. override func numberOfSections(in tableView: UITableView) -> Int {
  28. return 1
  29. }
  30. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  31. return self.publicCalendarList.count
  32. }
  33. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  34. if let cell = tableView.dequeueReusableCell(withIdentifier: "calendarStoreTableCell", for: indexPath) as? CalendarStoreTableViewCell {
  35. cell.setOOCalendarInfo(calendar: self.publicCalendarList[indexPath.row])
  36. cell.delegate = self
  37. return cell
  38. }else {
  39. return UITableViewCell()
  40. }
  41. }
  42. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  43. return 48.0
  44. }
  45. private func setFollow(follow: Bool, id: String) {
  46. for (idx, c) in (self.publicCalendarList.enumerated()) {
  47. if c.id == id {
  48. c.followed = follow
  49. self.publicCalendarList[idx] = c
  50. DDLogDebug("设置了。。。\(follow)")
  51. }
  52. }
  53. self.tableView.reloadData()
  54. }
  55. }
  56. extension OOCalendarStoreViewController: CalendarStoreCellFollowDelegate {
  57. func follow(calendar: OOCalendarInfo?) {
  58. if let c = calendar {
  59. if c.followed == true {
  60. self.viewModel.followCalendarCancel(id: c.id!).then { (v) in
  61. self.setFollow(follow: false, id: c.id!)
  62. }.catch { (err) in
  63. DDLogError("请求错误,\(err.localizedDescription)")
  64. self.showError(title: "取消失败!")
  65. }
  66. }else {
  67. self.viewModel.followCalendar(id: c.id!).then{ v in
  68. self.setFollow(follow: true, id: c.id!)
  69. }.catch { (err) in
  70. DDLogError("请求错误,\(err.localizedDescription)")
  71. self.showError(title: "关注失败!")
  72. }
  73. }
  74. }
  75. }
  76. }