Brak opisu

FKCirclePublicViewModel.m 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // FKCirclePublicViewModel.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/6/8.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKCirclePublicViewModel.h"
  9. #import "NSArray+Extra.h"
  10. #import "FKCircleDetailItem.h"
  11. NSString *const CIRCLE_PUBLIC_DESC_PALCEHOLDER = @"介绍一下你买到的海外好物,说说你的购物心得吧~";
  12. NSString *const CIRCLE_PUBLIC_IMAGETEXTVIEW_PALCEHOLDER = @"这里可添加照片描述哦";
  13. NSInteger const FKCIRCLE_PUBLIC_IMAGE_LENGTH = 400;
  14. NSString *const FKCirclePublicUserCellIdentify = @"FKCirclePublicUserCellIdentify";
  15. NSString *const FKCirclePublicProductDescCellIdentify = @"FKCirclePublicProductDescCellIdentify";
  16. NSString *const FKCirclePublicImageCellIdentify = @"FKCirclePublicImageCellIdentify";
  17. NSString *const FKCirclePublicProductCellIdentify = @"FKCirclePublicProductCellIdentify";
  18. @implementation FKCirclePublicViewModel
  19. #pragma mark - API
  20. - (void)prepareInitData {
  21. [self.modelMutArray removeAllObjects];
  22. FKCirclePublicTextItem *item = [FKCirclePublicTextItem new];
  23. item.palceholder = CIRCLE_PUBLIC_DESC_PALCEHOLDER;
  24. [self.modelMutArray addObject:item];
  25. }
  26. - (void)loadDataFrom:(FKCircleDetailItem *)detailItem {
  27. [self.modelMutArray removeAllObjects];
  28. if ([detailItem isKindOfClass:[FKCircleDetailItem class]]) {
  29. self.itemID = detailItem.shareItem.itemId;
  30. FKCirclePublicUserItem *userItem = [FKCirclePublicUserItem new];
  31. userItem.userID = detailItem.userItem.userId;
  32. userItem.nickname = detailItem.userItem.nickName;
  33. userItem.headURL = detailItem.userItem.picUrl;
  34. [self.modelMutArray addObject:userItem];
  35. FKCirclePublicTextItem *textItem = [FKCirclePublicTextItem new];
  36. textItem.palceholder = CIRCLE_PUBLIC_DESC_PALCEHOLDER;
  37. textItem.title = detailItem.shareItem.title;
  38. textItem.text = detailItem.shareItem.desc;
  39. [self.modelMutArray addObject:textItem];
  40. for (FKCirDetailContentItem *contentItem in detailItem.contentArray) {
  41. if ([contentItem.contentType isEqualToString:@"1"]) {
  42. FKCirclePublicImageItem *imageItem = [[FKCirclePublicImageItem alloc] init];
  43. imageItem.photoURL = contentItem.contentData.picUrl;
  44. imageItem.imageDesc = contentItem.contentData.contentText;
  45. [self.modelMutArray addObject:imageItem];
  46. } else if ([contentItem.contentType isEqualToString:@"2"]) {
  47. FKCircleChoiceProductItem *productItem = [[FKCircleChoiceProductItem alloc] init];
  48. productItem.itemID = contentItem.contentData.productItem.itemId;
  49. productItem.title = contentItem.contentData.productItem.title;
  50. productItem.photoURL = contentItem.contentData.productItem.firstPicUrl;
  51. productItem.price = contentItem.contentData.productItem.price;
  52. productItem.isSelected = YES;
  53. [self.modelMutArray addObject:productItem];
  54. }
  55. }
  56. self.selTagIDs = detailItem.realTagIDArray;
  57. }
  58. }
  59. - (kCirclePublicType)cellTypeAtIndexPath:(NSIndexPath *)indexPath {
  60. id item = [self modelItemAtIndexPath:indexPath];
  61. if ([item isKindOfClass:[FKCirclePublicUserItem class]]) {
  62. return kCirclePublicUser;
  63. } else if ([item isKindOfClass:[FKCirclePublicTextItem class]]) {
  64. return kCirclePublicProductDesc;
  65. } else if ([item isKindOfClass:[FKCirclePublicImageItem class]]) {
  66. return kCirclePublicImage;
  67. } else if ([item isKindOfClass:[FKCircleChoiceProductItem class]]) {
  68. return kCirclePublicProduct;
  69. }
  70. return kCirclePublicNone;
  71. }
  72. - (NSString *)orderCellIdentifyWith:(kCirclePublicType)cellType {
  73. switch (cellType) {
  74. case kCirclePublicUser: {
  75. return FKCirclePublicUserCellIdentify;
  76. }
  77. case kCirclePublicProductDesc: {
  78. return FKCirclePublicProductDescCellIdentify;
  79. }
  80. case kCirclePublicImage: {
  81. return FKCirclePublicImageCellIdentify;
  82. }
  83. case kCirclePublicProduct: {
  84. return FKCirclePublicProductCellIdentify;
  85. }
  86. default:
  87. break;
  88. }
  89. return nil;
  90. }
  91. - (id)modelItemAtIndexPath:(NSIndexPath *)indexPath {
  92. if (indexPath.section < self.modelMutArray.count) {
  93. return self.modelMutArray[indexPath.section];
  94. }
  95. return nil;
  96. }
  97. - (void)removeModelItemAtIndexPath:(NSIndexPath *)indexPath {
  98. if (indexPath.section < self.modelMutArray.count) {
  99. [self.modelMutArray removeObjectAtIndex:indexPath.section];
  100. }
  101. }
  102. - (NSArray *)productIDs {
  103. NSMutableArray *array = [NSMutableArray array];
  104. for (FKCircleChoiceProductItem *item in self.modelMutArray) {
  105. if ([item isKindOfClass:[FKCircleChoiceProductItem class]]) {
  106. [array addObject:item.itemID];
  107. }
  108. }
  109. return array;
  110. }
  111. - (NSString *)validDataMessage {
  112. for (id item in self.modelMutArray) {
  113. if ([item isKindOfClass:[FKCirclePublicTextItem class]]) {
  114. FKCirclePublicTextItem *textItem = (FKCirclePublicTextItem *)item;
  115. if ([textItem isKindOfClass:[FKCirclePublicTextItem class]]) {
  116. if (textItem.text.length == 0) {
  117. return @"请输入内容描述";
  118. }
  119. }
  120. }
  121. }
  122. NSInteger count = 0;
  123. for (id item in self.modelMutArray) {
  124. if ([item isKindOfClass:[FKCirclePublicImageItem class]]) {
  125. count++;
  126. break;
  127. }
  128. }
  129. if (count == 0) {
  130. return @"请至少插入一张图片";
  131. } else if (count > 9) {
  132. return @"最多只能上传9张图片";
  133. }
  134. count = 0;
  135. for (id item in self.modelMutArray) {
  136. if (![item isKindOfClass:[FKCirclePublicUserItem class]]) {
  137. count++;
  138. }
  139. }
  140. if (count > 20) {
  141. return @"最多只能插入20个商品链接";
  142. }
  143. return nil;
  144. }
  145. - (NSDictionary *)jsonForCreateShare {
  146. NSMutableDictionary *mutDict = [NSMutableDictionary dictionary];
  147. NSArray *keys;
  148. NSArray *values;
  149. NSString *itemJSONString;
  150. for (id item in self.modelMutArray) {
  151. if ([item isKindOfClass:[FKCirclePublicTextItem class]]) {
  152. FKCirclePublicTextItem *textItem = (FKCirclePublicTextItem *)item;
  153. if (self.itemID.length > 0) {
  154. keys = @[@"title", @"description", @"id"];
  155. values = @[[FLStringHelper replaceNilWithEmpty:textItem.title]
  156. , [FLStringHelper replaceNilWithEmpty:textItem.text]
  157. , [FLStringHelper replaceNilWithEmpty:self.itemID]];
  158. } else {
  159. keys = @[@"type", @"title", @"description"];
  160. values = @[@"1", @"", [FLStringHelper replaceNilWithEmpty:textItem.text]];
  161. }
  162. itemJSONString = [FLRequestHelper JSONStringWithKeys:keys
  163. values:values];
  164. [mutDict setValue:itemJSONString forKey:@"share_json"];
  165. }
  166. }
  167. if (self.modelMutArray.count > 1) {
  168. NSMutableArray *array = [NSMutableArray array];
  169. NSMutableDictionary *dict;
  170. for (int idx = 1; idx < self.modelMutArray.count; idx++) {
  171. id item = self.modelMutArray[idx];
  172. dict = [NSMutableDictionary dictionary];
  173. if ([item isKindOfClass:[FKCirclePublicImageItem class]]) {
  174. FKCirclePublicImageItem *imageItem = (FKCirclePublicImageItem*)item;
  175. [dict setValue:@{@"pic_url" : [FLStringHelper replaceNilWithEmpty:imageItem.photoURL]
  176. , @"content" : [FLStringHelper replaceNilWithEmpty:imageItem.imageDesc]
  177. , @"size" : [FLStringHelper replaceNilWithEmpty:imageItem.imageSizeString]}
  178. forKey:@"content_data"];
  179. [dict setValue:@"1" forKey:@"content_type"];
  180. [array addObject:dict];
  181. } else if ([item isKindOfClass:[FKCircleChoiceProductItem class]]) {
  182. FKCircleChoiceProductItem *productItem = (FKCircleChoiceProductItem*)item;
  183. [dict setValue:@{@"id": [FLStringHelper replaceNilWithEmpty:productItem.itemID]}
  184. forKey:@"content_data"];
  185. [dict setValue:@"2" forKey:@"content_type"];
  186. [array addObject:dict];
  187. }
  188. }
  189. [mutDict setValue:[array json] forKey:@"share_contents_json"];
  190. }
  191. if (self.tags.count > 0) {
  192. NSMutableArray *array = [NSMutableArray array];
  193. NSMutableDictionary *dict;
  194. for (FKCircleTagItem *item in self.tags) {
  195. dict = [NSMutableDictionary dictionary];
  196. if ([item isKindOfClass:[FKCircleTagItem class]]) {
  197. [dict setValue:@"17" forKey:@"object_type"];
  198. [dict setValue:[FLStringHelper replaceNilWithEmpty:item.itemID] forKey:@"object_id"];
  199. [array addObject:dict];
  200. }
  201. }
  202. [mutDict setValue:[array json] forKey:@"share_x_marks_json"];
  203. }
  204. return mutDict;
  205. }
  206. #pragma mark - Property
  207. - (NSMutableArray *)modelMutArray {
  208. if (!_modelMutArray) {
  209. _modelMutArray = [NSMutableArray array];
  210. }
  211. return _modelMutArray;
  212. }
  213. @end