OOCalendarViewModel.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // OOCalendarViewModel.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2018/7/24.
  6. // Copyright © 2018 zoneland. All rights reserved.
  7. //
  8. import Promises
  9. class OOCalendarViewModel: NSObject {
  10. override init() {
  11. super.init()
  12. }
  13. private let calendarAPI = OOMoyaProvider<OOCalendarAPI>()
  14. }
  15. //MARK: - 服务
  16. extension OOCalendarViewModel {
  17. // 查询我的日历
  18. func getMyCalendarList() -> Promise<OOMyCalendarList> {
  19. return Promise { fulfill, reject in
  20. self.calendarAPI.request(.myCalendarList, completion: { result in
  21. let response = OOResult<BaseModelClass<OOMyCalendarList>>(result)
  22. if response.isResultSuccess() {
  23. if let data = response.model?.data {
  24. fulfill(data)
  25. }else {
  26. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  27. }
  28. }else {
  29. reject(response.error!)
  30. }
  31. })
  32. }
  33. }
  34. // 查询日程事件
  35. func filterCalendarEventList(filter: OOCalendarEventFilter) -> Promise<OOCalendarEventResponse> {
  36. return Promise { fulfill, reject in
  37. self.calendarAPI.request(.filterCalendarEventList(filter), completion: { result in
  38. let response = OOResult<BaseModelClass<OOCalendarEventResponse>>(result)
  39. if response.isResultSuccess() {
  40. if let data = response.model?.data {
  41. fulfill(data)
  42. }else {
  43. reject(OOAppError.common(type: "calendarError", message: "查询日程为空", statusCode: 50001))
  44. }
  45. }else {
  46. reject(response.error!)
  47. }
  48. })
  49. }
  50. }
  51. // 保存日历
  52. func saveCalendar(calendar: OOCalendarInfo) -> Promise<Bool> {
  53. return Promise { fulfill, reject in
  54. self.calendarAPI.request(.saveCalendar(calendar), completion: { result in
  55. let response = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  56. if response.isResultSuccess() {
  57. if (response.model?.data) != nil {
  58. fulfill(true)
  59. }else {
  60. reject(OOAppError.common(type: "calendarError", message: "保存日历返回为空", statusCode: 50001))
  61. }
  62. }else {
  63. reject(response.error!)
  64. }
  65. })
  66. }
  67. }
  68. // 日历广场
  69. func getPublicCalendarList() -> Promise<[OOCalendarInfo]> {
  70. return Promise { fulfill, reject in
  71. self.calendarAPI.request(.getPublicCalendarList, completion: { result in
  72. let response = OOResult<BaseModelClass<[OOCalendarInfo]>>(result)
  73. if response.isResultSuccess() {
  74. if let data = response.model?.data {
  75. fulfill(data)
  76. } else {
  77. fulfill([])
  78. }
  79. } else {
  80. reject(response.error!)
  81. }
  82. })
  83. }
  84. }
  85. // 关注 日历
  86. func followCalendar(id: String) -> Promise<Bool> {
  87. return Promise { fulfill, reject in
  88. self.calendarAPI.request(.followCalendar(id), completion: { result in
  89. let response = OOResult<BaseModelClass<OOCommonValueBoolModel>>(result)
  90. if response.isResultSuccess() {
  91. if let data = response.model?.data {
  92. fulfill(data.value ?? false)
  93. } else {
  94. fulfill(false)
  95. }
  96. } else {
  97. reject(response.error!)
  98. }
  99. })
  100. }
  101. }
  102. // 取消关注 日历
  103. func followCalendarCancel(id:String) -> Promise<Bool> {
  104. return Promise { fulfill, reject in
  105. self.calendarAPI.request(.followCalendarCancel(id), completion: { result in
  106. let response = OOResult<BaseModelClass<OOCommonValueBoolModel>>(result)
  107. if response.isResultSuccess() {
  108. if let data = response.model?.data {
  109. fulfill(data.value ?? false)
  110. } else {
  111. fulfill(false)
  112. }
  113. } else {
  114. reject(response.error!)
  115. }
  116. })
  117. }
  118. }
  119. // 获取日历对象
  120. func getCalendar(id:String) -> Promise<OOCalendarInfo> {
  121. return Promise { fulfill, reject in
  122. self.calendarAPI.request(.getCalendar(id), completion: {result in
  123. let response = OOResult<BaseModelClass<OOCalendarInfo>>(result)
  124. if response.isResultSuccess() {
  125. if let data = response.model?.data {
  126. fulfill(data)
  127. } else {
  128. reject(OOAppError.common(type: "calendarError", message: "日历返回为空", statusCode: 50001))
  129. }
  130. } else {
  131. reject(response.error!)
  132. }
  133. })
  134. }
  135. }
  136. // 删除日历
  137. func deleteCalendar(id:String) -> Promise<Bool> {
  138. return Promise { fulfill, reject in
  139. self.calendarAPI.request(.deleteCalendar(id), completion: {result in
  140. let response = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  141. if response.isResultSuccess() {
  142. if (response.model?.data) != nil {
  143. fulfill(true)
  144. }else {
  145. reject(OOAppError.common(type: "calendarError", message: "删除日历返回空", statusCode: 50001))
  146. }
  147. }else {
  148. reject(response.error!)
  149. }
  150. })
  151. }
  152. }
  153. // 保存日程事件
  154. func saveCalendarEvent(event: OOCalendarEventInfo) -> Promise<Bool> {
  155. return Promise { fulfill, reject in
  156. self.calendarAPI.request(.saveCalendarEvent(event), completion: { result in
  157. let response = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  158. if response.isResultSuccess() {
  159. if (response.model?.data) != nil {
  160. fulfill(true)
  161. }else {
  162. reject(OOAppError.common(type: "calendarEventError", message: "保存日程事件返回空", statusCode: 50001))
  163. }
  164. }else {
  165. reject(response.error!)
  166. }
  167. })
  168. }
  169. }
  170. // 更新日程事件 单个
  171. func updateCalendarEventSingle(id:String, event: OOCalendarEventInfo) -> Promise<Bool> {
  172. return Promise { fulfill, reject in
  173. self.calendarAPI.request(.updateCalendarEventSingle(id, event), completion: {result in
  174. let response = OOResult<BaseModelClass<OOCommonValueIntModel>>(result)
  175. if response.isResultSuccess() {
  176. if (response.model?.data) != nil {
  177. fulfill(true)
  178. }else {
  179. reject(OOAppError.common(type: "calendarEventError", message: "更新日程事件单个返回空", statusCode: 50001))
  180. }
  181. }else {
  182. reject(response.error!)
  183. }
  184. })
  185. }
  186. }
  187. // 更新日程事件 之后
  188. func updateCalendarEventAfter(id: String, event: OOCalendarEventInfo) -> Promise<Bool> {
  189. return Promise { fulfill, reject in
  190. self.calendarAPI.request(.updateCalendarEventAfter(id, event), completion: {result in
  191. let response = OOResult<BaseModelClass<OOCommonValueIntModel>>(result)
  192. if response.isResultSuccess() {
  193. if (response.model?.data) != nil {
  194. fulfill(true)
  195. }else {
  196. reject(OOAppError.common(type: "calendarEventError", message: "更新日程事件之后返回空", statusCode: 50001))
  197. }
  198. }else {
  199. reject(response.error!)
  200. }
  201. })
  202. }
  203. }
  204. // 更新日程事件 全部
  205. func updateCalendarEventAll(id: String, event: OOCalendarEventInfo) -> Promise<Bool> {
  206. return Promise { fulfill, reject in
  207. self.calendarAPI.request(.updateCalendarEventAll(id, event), completion: {result in
  208. let response = OOResult<BaseModelClass<OOCommonValueIntModel>>(result)
  209. if response.isResultSuccess() {
  210. if (response.model?.data) != nil {
  211. fulfill(true)
  212. }else {
  213. reject(OOAppError.common(type: "calendarEventError", message: "更新日程事件全部返回空", statusCode: 50001))
  214. }
  215. }else {
  216. reject(response.error!)
  217. }
  218. })
  219. }
  220. }
  221. // 删除日程事件 单个
  222. func deleteCalendarEventSingle(id: String) -> Promise<Bool> {
  223. return Promise { fulfill, reject in
  224. self.calendarAPI.request(.deleteCalendarEventSingle(id), completion: {result in
  225. let response = OOResult<BaseModelClass<OOCommonValueIntModel>>(result)
  226. if response.isResultSuccess() {
  227. if (response.model?.data) != nil {
  228. fulfill(true)
  229. }else {
  230. reject(OOAppError.common(type: "calendarEventError", message: "删除日程事件单个返回空", statusCode: 50001))
  231. }
  232. }else {
  233. reject(response.error!)
  234. }
  235. })
  236. }
  237. }
  238. // 删除日程事件 之后
  239. func deleteCalendarEventAfter(id: String) -> Promise<Bool> {
  240. return Promise { fulfill, reject in
  241. self.calendarAPI.request(.deleteCalendarEventAfter(id), completion: {result in
  242. let response = OOResult<BaseModelClass<OOCommonValueIntModel>>(result)
  243. if response.isResultSuccess() {
  244. if (response.model?.data) != nil {
  245. fulfill(true)
  246. }else {
  247. reject(OOAppError.common(type: "calendarEventError", message: "删除日程事件之后返回空", statusCode: 50001))
  248. }
  249. }else {
  250. reject(response.error!)
  251. }
  252. })
  253. }
  254. }
  255. // 删除日程事件 全部
  256. func deleteCalendarEventAll(id: String) -> Promise<Bool> {
  257. return Promise { fulfill, reject in
  258. self.calendarAPI.request(.deleteCalendarEventAll(id), completion: {result in
  259. let response = OOResult<BaseModelClass<OOCommonValueIntModel>>(result)
  260. if response.isResultSuccess() {
  261. if (response.model?.data) != nil {
  262. fulfill(true)
  263. }else {
  264. reject(OOAppError.common(type: "calendarEventError", message: "删除日程事件全部返回空", statusCode: 50001))
  265. }
  266. }else {
  267. reject(response.error!)
  268. }
  269. })
  270. }
  271. }
  272. }