CommunityFollowUserViewModel.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // CommunityFollowUserViewModel.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/7/2.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. class CommunityFollowUserViewModel: NSObject {
  10. static let shared : CommunityFollowUserViewModel = CommunityFollowUserViewModel()
  11. /// 关注
  12. func userFollow(followUid:Int,completion: @escaping (Int) -> Void) {
  13. SwiftMoyaNetWorkServiceUser.shared().userFollowApi(followUid: followUid) { (followStatusModel) -> (Void) in
  14. let followStatusModel = followStatusModel as? FollowStatusModel
  15. completion(followStatusModel?.isFollowStatus ?? 0)
  16. }
  17. }
  18. /// 取消关注
  19. func userCancelFollow(followUid:Int,completion: @escaping (Int) -> Void) {
  20. SwiftMoyaNetWorkServiceUser.shared().userCancelFollowApi(followUid: followUid) {
  21. (followStatusModel) -> (Void) in
  22. let followStatusModel = followStatusModel as? FollowStatusModel
  23. completion(followStatusModel?.isFollowStatus ?? 0)
  24. }
  25. }
  26. /// 设置关注
  27. ///
  28. /// - Parameters:
  29. /// - followButton: 关注button
  30. /// - followType: 关注类型
  31. func setFollowType(followButton:UIButton,followType: FollowType) {
  32. switch followType {
  33. case .futureFollow:
  34. followButton.setTitle("关注", for: UIControl.State.normal)
  35. followButton.setTitleColor(kThemeColor, for: UIControl.State.normal)
  36. followButton.setImage(kImage(name: "star_attention"), for: UIControl.State.normal)
  37. followButton.layer.borderColor = kThemeColor.cgColor
  38. followButton.setBackgroundImage(UIImage.imageWithColor(color: kffffffColor), for: UIControl.State.normal)
  39. followButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 2)
  40. break
  41. case .alreadyFollow:
  42. followButton.setTitle("关注", for: UIControl.State.normal)
  43. followButton.setTitleColor(k333333Color, for: UIControl.State.normal)
  44. followButton.setImage(kImage(name: "star_followed"), for: UIControl.State.normal)
  45. followButton.layer.borderColor = kf7f8faColor.cgColor
  46. followButton.setBackgroundImage(UIImage.imageWithColor(color: kf7f8faColor), for: UIControl.State.normal)
  47. followButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 2)
  48. break
  49. case .mutualFollow:
  50. followButton.setTitle("互相关注", for: UIControl.State.normal)
  51. followButton.setTitleColor(k333333Color, for: UIControl.State.normal)
  52. followButton.setImage(nil, for: UIControl.State.normal)
  53. followButton.layer.borderColor = kCCCCCCColor.cgColor
  54. followButton.setBackgroundImage(UIImage.imageWithColor(color: kffffffColor), for: UIControl.State.normal)
  55. followButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 0)
  56. break
  57. }
  58. }
  59. /// 设置关注
  60. ///
  61. /// - Parameters:
  62. /// - followButton: 关注button
  63. /// - followType: 关注类型
  64. func setVideoFollowType(followButton:UIButton,followType: FollowType) {
  65. switch followType {
  66. case .futureFollow:
  67. followButton.setTitle("关注", for: UIControl.State.normal)
  68. followButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
  69. followButton.setImage(kImage(name: "star_attention"), for: UIControl.State.normal)
  70. followButton.layer.borderColor = kThemeColor.cgColor
  71. followButton.setBackgroundImage(UIImage.imageWithColor(color: kThemeColor), for: UIControl.State.normal)
  72. followButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 2)
  73. break
  74. case .alreadyFollow:
  75. followButton.setTitle("关注", for: UIControl.State.normal)
  76. followButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
  77. followButton.setImage(kImage(name: "star_followed"), for: UIControl.State.normal)
  78. followButton.layer.borderColor = kffffffColor.cgColor
  79. followButton.setBackgroundImage(UIImage.imageWithColor(color: UIColor.clear), for: UIControl.State.normal)
  80. followButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 2)
  81. break
  82. case .mutualFollow:
  83. followButton.setTitle("互相关注", for: UIControl.State.normal)
  84. followButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
  85. followButton.setImage(nil, for: UIControl.State.normal)
  86. followButton.layer.borderColor = kffffffColor.cgColor
  87. followButton.setBackgroundImage(UIImage.imageWithColor(color: UIColor.clear), for: UIControl.State.normal)
  88. followButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 0)
  89. break
  90. }
  91. }
  92. }
  93. // MARK: - communityPostDetailModel
  94. extension CommunityFollowUserViewModel {
  95. /// 关注/取消关注
  96. ///
  97. /// - Parameter communityPostDetailModel: 帖子模型
  98. func follow(communityPostDetailModel: CommunityPostDetailModel,button:UIButton) {
  99. if communityPostDetailModel.isFollow == 0 {//加关注
  100. self.userFollow(followUid: communityPostDetailModel.uid ?? 0, completion: { (isFollow) -> Void in
  101. communityPostDetailModel.isFollow = isFollow
  102. CommunityFollowUserViewModel.shared.setFollowType(followButton: button, followType: FollowType(rawValue: communityPostDetailModel.isFollow ?? 0) ?? .futureFollow)
  103. })
  104. }else {//取消关注
  105. self.userCancelFollow(followUid: communityPostDetailModel.uid ?? 0, completion: { (isFollow) -> Void in
  106. communityPostDetailModel.isFollow = isFollow
  107. CommunityFollowUserViewModel.shared.setFollowType(followButton: button, followType: FollowType(rawValue: communityPostDetailModel.isFollow ?? 0) ?? .futureFollow)
  108. })
  109. }
  110. }
  111. }
  112. // MARK: - CommunityRecommendDataModel
  113. extension CommunityFollowUserViewModel {
  114. /// 关注/取消关注
  115. ///
  116. /// - Parameter communityRecommendDataModel: 帖子模型
  117. func follow(communityRecommendDataModel: CommunityRecommendDataModel,communityRecommendDataModels: Array<CommunityRecommendDataModel>,tableView:UITableView) {
  118. if communityRecommendDataModel.isFollow == 0 {//加关注
  119. self.userFollow(followUid: communityRecommendDataModel.uid ?? 0, completion: { (isFollow) -> Void in
  120. for (_,model) in communityRecommendDataModels.enumerated() {
  121. if model.uid == communityRecommendDataModel.uid {
  122. model.isFollow = isFollow
  123. tableView.reloadData()
  124. }
  125. }
  126. })
  127. }else {//取消关注
  128. self.userCancelFollow(followUid: communityRecommendDataModel.uid ?? 0, completion: { (isFollow) -> Void in
  129. for (_,model) in communityRecommendDataModels.enumerated() {
  130. if model.uid == communityRecommendDataModel.uid {
  131. model.isFollow = isFollow
  132. tableView.reloadData()
  133. }
  134. }
  135. })
  136. }
  137. }
  138. }
  139. // MARK: - communityRecommendTypeDataModel
  140. extension CommunityFollowUserViewModel {
  141. /// 关注/取消关注
  142. ///
  143. /// - Parameter communityRecommendTypeDataModel: 帖子模型
  144. func follow(communityRecommendTypeDataModel: CommunityRecommendTypeDataModel,communityRecommendTypeDataModels: Array<CommunityRecommendTypeDataModel>,collectionView:UICollectionView,completion: @escaping (Int) -> Void) {
  145. if communityRecommendTypeDataModel.followStatus == 0 {//加关注
  146. self.userFollow(followUid: communityRecommendTypeDataModel.uid ?? 0, completion: { (isFollow) -> Void in
  147. for (_,model) in communityRecommendTypeDataModels.enumerated() {
  148. if model.uid == communityRecommendTypeDataModel.uid {
  149. model.followStatus = isFollow
  150. collectionView.reloadData()
  151. }
  152. }
  153. })
  154. }else {//取消关注
  155. self.userCancelFollow(followUid: communityRecommendTypeDataModel.uid ?? 0, completion: { (isFollow) -> Void in
  156. for (_,model) in communityRecommendTypeDataModels.enumerated() {
  157. if model.uid == communityRecommendTypeDataModel.uid {
  158. model.followStatus = isFollow
  159. collectionView.reloadData()
  160. }
  161. }
  162. })
  163. }
  164. }
  165. }
  166. // MARK: - cmsMemberModel
  167. extension CommunityFollowUserViewModel {
  168. /// 关注/取消关注
  169. ///
  170. /// - Parameter cmsMemberModel: 帖子模型
  171. func follow(cmsMemberModel: CMSMemberModel,cmsMemberModels: Array<CMSMemberModel>,collectionView:UICollectionView,completion: @escaping (Int) -> Void) {
  172. if cmsMemberModel.followStatus == 0 {//加关注
  173. self.userFollow(followUid: cmsMemberModel.uid ?? 0, completion: { (isFollow) -> Void in
  174. for (_,model) in cmsMemberModels.enumerated() {
  175. if model.uid == cmsMemberModel.uid {
  176. model.followStatus = isFollow
  177. collectionView.reloadData()
  178. }
  179. }
  180. })
  181. }else {//取消关注
  182. self.userCancelFollow(followUid: cmsMemberModel.uid ?? 0, completion: { (isFollow) -> Void in
  183. for (_,model) in cmsMemberModels.enumerated() {
  184. if model.uid == cmsMemberModel.uid {
  185. model.followStatus = isFollow
  186. collectionView.reloadData()
  187. }
  188. }
  189. })
  190. }
  191. }
  192. }
  193. // MARK: - communityFollowDataModel
  194. extension CommunityFollowUserViewModel {
  195. /// 关注/取消关注
  196. ///
  197. /// - Parameter communityRecommendDataModel: 帖子模型
  198. func follow(communityFollowDataModel: CommunityFollowDataModel,communityFollowDataModels: Array<CommunityFollowDataModel>,tableView:UITableView) {
  199. if communityFollowDataModel.relateData?.isFollow == 0 {//加关注
  200. self.userFollow(followUid: communityFollowDataModel.relateData?.uid ?? 0, completion: { (isFollow) -> Void in
  201. for (_,model) in communityFollowDataModels.enumerated() {
  202. if model.relateData?.uid == communityFollowDataModel.relateData?.uid {
  203. model.relateData?.isFollow = isFollow
  204. tableView.reloadData()
  205. }
  206. }
  207. })
  208. }else {//取消关注
  209. self.userCancelFollow(followUid: communityFollowDataModel.relateData?.uid ?? 0, completion: { (isFollow) -> Void in
  210. for (_,model) in communityFollowDataModels.enumerated() {
  211. if model.relateData?.uid == communityFollowDataModel.relateData?.uid {
  212. model.relateData?.isFollow = isFollow
  213. tableView.reloadData()
  214. }
  215. }
  216. })
  217. }
  218. }
  219. }
  220. // MARK: - CommunityVideoItemModel
  221. extension CommunityFollowUserViewModel {
  222. /// 关注/取消关注
  223. ///
  224. /// - Parameter communityVideoItemModel: 帖子模型
  225. func follow(communityVideoItemModel: CommunityVideoItemModel,videoItemList:Array<CommunityVideoItemModel>,button:UIButton) {
  226. if communityVideoItemModel.isFollow == 0 {//加关注
  227. self.userFollow(followUid: communityVideoItemModel.uid ?? 0, completion: { (isFollow) -> Void in
  228. communityVideoItemModel.isFollow = isFollow
  229. CommunityFollowUserViewModel.shared.setFollowType(followButton: button, followType: FollowType(rawValue: communityVideoItemModel.isFollow ?? 0) ?? .futureFollow)
  230. for (_,model) in videoItemList.enumerated() {
  231. if model.uid == communityVideoItemModel.uid {
  232. model.isFollow = isFollow
  233. }
  234. }
  235. })
  236. }else {//取消关注
  237. self.userCancelFollow(followUid: communityVideoItemModel.uid ?? 0, completion: { (isFollow) -> Void in
  238. communityVideoItemModel.isFollow = isFollow
  239. CommunityFollowUserViewModel.shared.setFollowType(followButton: button, followType: FollowType(rawValue: communityVideoItemModel.isFollow ?? 0) ?? .futureFollow)
  240. for (_,model) in videoItemList.enumerated() {
  241. if model.uid == communityVideoItemModel.uid {
  242. model.isFollow = isFollow
  243. }
  244. }
  245. })
  246. }
  247. }
  248. }