123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- //
- // FKCirclePublicViewModel.m
- // FirstLink
- //
- // Created by ascii on 16/6/8.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKCirclePublicViewModel.h"
- #import "NSArray+Extra.h"
- #import "FKCircleDetailItem.h"
- NSString *const CIRCLE_PUBLIC_DESC_PALCEHOLDER = @"介绍一下你买到的海外好物,说说你的购物心得吧~";
- NSString *const CIRCLE_PUBLIC_IMAGETEXTVIEW_PALCEHOLDER = @"这里可添加照片描述哦";
- NSInteger const FKCIRCLE_PUBLIC_IMAGE_LENGTH = 400;
- NSString *const FKCirclePublicUserCellIdentify = @"FKCirclePublicUserCellIdentify";
- NSString *const FKCirclePublicProductDescCellIdentify = @"FKCirclePublicProductDescCellIdentify";
- NSString *const FKCirclePublicImageCellIdentify = @"FKCirclePublicImageCellIdentify";
- NSString *const FKCirclePublicProductCellIdentify = @"FKCirclePublicProductCellIdentify";
- @implementation FKCirclePublicViewModel
- #pragma mark - API
- - (void)prepareInitData {
- [self.modelMutArray removeAllObjects];
-
- FKCirclePublicTextItem *item = [FKCirclePublicTextItem new];
- item.palceholder = CIRCLE_PUBLIC_DESC_PALCEHOLDER;
- [self.modelMutArray addObject:item];
- }
- - (void)loadDataFrom:(FKCircleDetailItem *)detailItem {
- [self.modelMutArray removeAllObjects];
-
- if ([detailItem isKindOfClass:[FKCircleDetailItem class]]) {
- self.itemID = detailItem.shareItem.itemId;
-
- FKCirclePublicUserItem *userItem = [FKCirclePublicUserItem new];
- userItem.userID = detailItem.userItem.userId;
- userItem.nickname = detailItem.userItem.nickName;
- userItem.headURL = detailItem.userItem.picUrl;
- [self.modelMutArray addObject:userItem];
-
- FKCirclePublicTextItem *textItem = [FKCirclePublicTextItem new];
- textItem.palceholder = CIRCLE_PUBLIC_DESC_PALCEHOLDER;
- textItem.title = detailItem.shareItem.title;
- textItem.text = detailItem.shareItem.desc;
- [self.modelMutArray addObject:textItem];
-
- for (FKCirDetailContentItem *contentItem in detailItem.contentArray) {
- if ([contentItem.contentType isEqualToString:@"1"]) {
- FKCirclePublicImageItem *imageItem = [[FKCirclePublicImageItem alloc] init];
- imageItem.photoURL = contentItem.contentData.picUrl;
- imageItem.imageDesc = contentItem.contentData.contentText;
- [self.modelMutArray addObject:imageItem];
- } else if ([contentItem.contentType isEqualToString:@"2"]) {
- FKCircleChoiceProductItem *productItem = [[FKCircleChoiceProductItem alloc] init];
- productItem.itemID = contentItem.contentData.productItem.itemId;
- productItem.title = contentItem.contentData.productItem.title;
- productItem.photoURL = contentItem.contentData.productItem.firstPicUrl;
- productItem.price = contentItem.contentData.productItem.price;
- productItem.isSelected = YES;
- [self.modelMutArray addObject:productItem];
- }
- }
-
- self.selTagIDs = detailItem.realTagIDArray;
- }
- }
- - (kCirclePublicType)cellTypeAtIndexPath:(NSIndexPath *)indexPath {
- id item = [self modelItemAtIndexPath:indexPath];
- if ([item isKindOfClass:[FKCirclePublicUserItem class]]) {
- return kCirclePublicUser;
- } else if ([item isKindOfClass:[FKCirclePublicTextItem class]]) {
- return kCirclePublicProductDesc;
- } else if ([item isKindOfClass:[FKCirclePublicImageItem class]]) {
- return kCirclePublicImage;
- } else if ([item isKindOfClass:[FKCircleChoiceProductItem class]]) {
- return kCirclePublicProduct;
- }
- return kCirclePublicNone;
- }
- - (NSString *)orderCellIdentifyWith:(kCirclePublicType)cellType {
- switch (cellType) {
- case kCirclePublicUser: {
- return FKCirclePublicUserCellIdentify;
- }
- case kCirclePublicProductDesc: {
- return FKCirclePublicProductDescCellIdentify;
- }
- case kCirclePublicImage: {
- return FKCirclePublicImageCellIdentify;
- }
- case kCirclePublicProduct: {
- return FKCirclePublicProductCellIdentify;
- }
- default:
- break;
- }
- return nil;
- }
- - (id)modelItemAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.section < self.modelMutArray.count) {
- return self.modelMutArray[indexPath.section];
- }
- return nil;
- }
- - (void)removeModelItemAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.section < self.modelMutArray.count) {
- [self.modelMutArray removeObjectAtIndex:indexPath.section];
- }
- }
- - (NSArray *)productIDs {
- NSMutableArray *array = [NSMutableArray array];
- for (FKCircleChoiceProductItem *item in self.modelMutArray) {
- if ([item isKindOfClass:[FKCircleChoiceProductItem class]]) {
- [array addObject:item.itemID];
- }
- }
- return array;
- }
- - (NSString *)validDataMessage {
- for (id item in self.modelMutArray) {
- if ([item isKindOfClass:[FKCirclePublicTextItem class]]) {
- FKCirclePublicTextItem *textItem = (FKCirclePublicTextItem *)item;
- if ([textItem isKindOfClass:[FKCirclePublicTextItem class]]) {
- if (textItem.text.length == 0) {
- return @"请输入内容描述";
- }
- }
- }
- }
- NSInteger count = 0;
- for (id item in self.modelMutArray) {
- if ([item isKindOfClass:[FKCirclePublicImageItem class]]) {
- count++;
- break;
- }
- }
- if (count == 0) {
- return @"请至少插入一张图片";
- } else if (count > 9) {
- return @"最多只能上传9张图片";
- }
-
- count = 0;
- for (id item in self.modelMutArray) {
- if (![item isKindOfClass:[FKCirclePublicUserItem class]]) {
- count++;
- }
- }
- if (count > 20) {
- return @"最多只能插入20个商品链接";
- }
-
- return nil;
- }
- - (NSDictionary *)jsonForCreateShare {
- NSMutableDictionary *mutDict = [NSMutableDictionary dictionary];
-
- NSArray *keys;
- NSArray *values;
- NSString *itemJSONString;
-
- for (id item in self.modelMutArray) {
- if ([item isKindOfClass:[FKCirclePublicTextItem class]]) {
- FKCirclePublicTextItem *textItem = (FKCirclePublicTextItem *)item;
- if (self.itemID.length > 0) {
- keys = @[@"title", @"description", @"id"];
- values = @[[FLStringHelper replaceNilWithEmpty:textItem.title]
- , [FLStringHelper replaceNilWithEmpty:textItem.text]
- , [FLStringHelper replaceNilWithEmpty:self.itemID]];
- } else {
- keys = @[@"type", @"title", @"description"];
- values = @[@"1", @"", [FLStringHelper replaceNilWithEmpty:textItem.text]];
- }
- itemJSONString = [FLRequestHelper JSONStringWithKeys:keys
- values:values];
-
- [mutDict setValue:itemJSONString forKey:@"share_json"];
- }
- }
-
- if (self.modelMutArray.count > 1) {
- NSMutableArray *array = [NSMutableArray array];
- NSMutableDictionary *dict;
- for (int idx = 1; idx < self.modelMutArray.count; idx++) {
- id item = self.modelMutArray[idx];
- dict = [NSMutableDictionary dictionary];
-
- if ([item isKindOfClass:[FKCirclePublicImageItem class]]) {
- FKCirclePublicImageItem *imageItem = (FKCirclePublicImageItem*)item;
- [dict setValue:@{@"pic_url" : [FLStringHelper replaceNilWithEmpty:imageItem.photoURL]
- , @"content" : [FLStringHelper replaceNilWithEmpty:imageItem.imageDesc]
- , @"size" : [FLStringHelper replaceNilWithEmpty:imageItem.imageSizeString]}
- forKey:@"content_data"];
- [dict setValue:@"1" forKey:@"content_type"];
-
- [array addObject:dict];
- } else if ([item isKindOfClass:[FKCircleChoiceProductItem class]]) {
- FKCircleChoiceProductItem *productItem = (FKCircleChoiceProductItem*)item;
- [dict setValue:@{@"id": [FLStringHelper replaceNilWithEmpty:productItem.itemID]}
- forKey:@"content_data"];
- [dict setValue:@"2" forKey:@"content_type"];
-
- [array addObject:dict];
- }
- }
- [mutDict setValue:[array json] forKey:@"share_contents_json"];
- }
-
- if (self.tags.count > 0) {
- NSMutableArray *array = [NSMutableArray array];
- NSMutableDictionary *dict;
- for (FKCircleTagItem *item in self.tags) {
- dict = [NSMutableDictionary dictionary];
- if ([item isKindOfClass:[FKCircleTagItem class]]) {
- [dict setValue:@"17" forKey:@"object_type"];
- [dict setValue:[FLStringHelper replaceNilWithEmpty:item.itemID] forKey:@"object_id"];
- [array addObject:dict];
- }
- }
- [mutDict setValue:[array json] forKey:@"share_x_marks_json"];
- }
- return mutDict;
- }
- #pragma mark - Property
- - (NSMutableArray *)modelMutArray {
- if (!_modelMutArray) {
- _modelMutArray = [NSMutableArray array];
- }
- return _modelMutArray;
- }
- @end
|