OOMeetingMainViewModel.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // OOMeetingMainViewModel.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2018/1/17.
  6. // Copyright © 2018年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import Promises
  10. class OOMeetingMainViewModel: NSObject {
  11. //HTTP API
  12. private let o2MeetingAPI = OOMoyaProvider<O2MeetingAPI>()
  13. //所有本月所有会议
  14. private var meetingsByMonth:[OOMeetingInfo] = [] {
  15. didSet {
  16. meetingsByMonth.forEach { (item) in
  17. let startDate = String((item.startTime?.split(separator: " ").first)!)
  18. if var meetings = meetingsByMonthForDict[startDate] {
  19. meetings?.append(item)
  20. }else{
  21. let meetings:[OOMeetingInfo] = [item]
  22. meetingsByMonthForDict[startDate] = meetings
  23. }
  24. }
  25. }
  26. }
  27. //本月所有会议按日期生成key,value
  28. private var meetingsByMonthForDict:[String:[OOMeetingInfo]?] = [:]
  29. //指定日期的所有会议
  30. var theMeetingsByDay:[OOMeetingInfo] = []
  31. //回调块类型定义
  32. typealias CallbackBlockDefine = (_ msg:String?) -> Void
  33. //回调块定义
  34. var callbackExecutor:CallbackBlockDefine?
  35. override init() {
  36. super.init()
  37. }
  38. }
  39. extension OOMeetingMainViewModel {
  40. // MARK:- 读取会议室信息
  41. func loadMeetingRoomById(_ roomId:String) -> Promise<OOMeetingRoomInfo> {
  42. return Promise { fulfill,reject in
  43. self.o2MeetingAPI.request(.roomItemById(roomId)) { (result) in
  44. let myResult = OOResult<BaseModelClass<OOMeetingRoomInfo>>(result)
  45. if myResult.isResultSuccess() {
  46. if let item = myResult.model?.data {
  47. fulfill(item)
  48. }else{
  49. let customError = OOAppError.common(type: "MeetingRoom load Error", message: "会议室信息读取错误", statusCode: 7001)
  50. reject(customError)
  51. }
  52. }else{
  53. reject(myResult.error!)
  54. }
  55. }
  56. }
  57. }
  58. func loadMeetingRoomById(_ roomId:String,completed:@escaping (_ room:OOMeetingRoomInfo?) -> Void){
  59. o2MeetingAPI.request(.roomItemById(roomId)) { (result) in
  60. let myResult = OOResult<BaseModelClass<OOMeetingRoomInfo>>(result)
  61. if myResult.isResultSuccess() {
  62. let item = myResult.model?.data
  63. completed(item)
  64. }else{
  65. completed(nil)
  66. }
  67. }
  68. }
  69. // MARK:- 按月读取会议信息
  70. func getMeetingsByYearAndMonth(_ theDate:Date) -> Promise<[String:[OOMeetingInfo]?]?> {
  71. let strYear = String(theDate.year)
  72. let strMonth = String(theDate.month)
  73. return Promise { fulfill,reject in
  74. self.o2MeetingAPI.request(.meetingListByYearMonth(strYear, strMonth)) { (result) in
  75. let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  76. if myResult.isResultSuccess() {
  77. if let model = myResult.model?.data {
  78. model.forEach({ (item) in
  79. self.meetingsByMonth.append(item)
  80. })
  81. }
  82. fulfill(self.meetingsByMonthForDict)
  83. }else{
  84. reject(myResult.error!)
  85. }
  86. }
  87. }
  88. }
  89. //按月读取会议信息
  90. func getMeetingsByYearAndMonth(_ theDate:Date,completedCallback:@escaping (_ meetingsDict:[String:[OOMeetingInfo]?]?) -> Void){
  91. let strYear = String(theDate.year)
  92. let strMonth = String(theDate.month)
  93. o2MeetingAPI.request(.meetingListByYearMonth(strYear, strMonth)) { (result) in
  94. let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  95. if myResult.isResultSuccess() {
  96. if let model = myResult.model?.data {
  97. model.forEach({ (item) in
  98. self.meetingsByMonth.append(item)
  99. })
  100. }
  101. completedCallback(self.meetingsByMonthForDict)
  102. }else{
  103. completedCallback(nil)
  104. }
  105. }
  106. }
  107. // MARK:- 读取指定日期的会议列表
  108. func getMeetingByTheDay(_ theDate:Date) -> Promise<[OOMeetingInfo]> {
  109. let strYear = String(theDate.year)
  110. let strMonth = String(theDate.month)
  111. let strDay = String(theDate.day)
  112. return Promise { fulfill,reject in
  113. self.o2MeetingAPI.request(.meetingListByYearMonthDay(strYear, strMonth, strDay)) { (result) in
  114. let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  115. if myResult.isResultSuccess() {
  116. if let models = myResult.model?.data {
  117. fulfill(models)
  118. }else{
  119. fulfill([])
  120. }
  121. }else{
  122. reject(myResult.error!)
  123. }
  124. }
  125. }
  126. }
  127. // //读取指定日期的会议列表
  128. // func getMeetingByTheDay(_ theDate:Date){
  129. // let strYear = String(theDate.year)
  130. // let strMonth = String(theDate.month)
  131. // let strDay = String(theDate.day)
  132. // self.theMeetingsByDay.removeAll()
  133. // o2MeetingAPI.request(.meetingListByYearMonthDay(strYear, strMonth, strDay)) { (result) in
  134. // let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  135. // if myResult.isResultSuccess() {
  136. // if let model = myResult.model?.data {
  137. // model.forEach({ (item) in
  138. // self.theMeetingsByDay.append(item)
  139. // })
  140. // }
  141. // }
  142. // guard let block = self.callbackExecutor else {
  143. // return
  144. // }
  145. // if myResult.isResultSuccess() {
  146. // block(nil)
  147. // }else{
  148. // block(myResult.error?.errorDescription)
  149. // }
  150. // }
  151. //
  152. // }
  153. }
  154. // MARK:- UITableView DataSource
  155. extension OOMeetingMainViewModel {
  156. func numberOfSections() -> Int {
  157. return 1
  158. }
  159. func numberOfRowsInSection(_ section: Int) -> Int {
  160. return theMeetingsByDay.count
  161. }
  162. func nodeForIndexPath(_ indexPath:IndexPath) -> OOMeetingInfo {
  163. return theMeetingsByDay[indexPath.row]
  164. }
  165. }