// // 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