// // FKRecommendViewModel.m // FirstLink // // Created by jack on 15/11/30. // Copyright © 2015年 FirstLink. All rights reserved. // #import "FKRecommendViewModel.h" #import "FKAdvertiseViewCell.h" #import "FKRecoIconListCell.h" #import "FKRecoNewsCell.h" #import "FKRecoLimitBuyCell.h" #import "FKRecoPromotionLimitCell.h" #import "FKRecoPromotionCategoryCell.h" #import "FKRecoOfficalActivityCell.h" #import "FKRecoBrandListCell.h" #import "FKExploreTableViewCell.h" #import "FKRecoUserRecoProductCell.h" #import "FKRecoScanRecoProductCell.h" #import "FKRecoHotProductCell.h" #import "FKHotProductItem.h" #import "UserDefaultManager.h" static NSString *REC_TARGET_URL_TAG_KEY = @"REC_TARGET_URL_TAG_KEY"; @interface FKRecommendViewModel () @property (nonatomic, strong) NSMutableDictionary *sectionTypeDict; @end @implementation FKRecommendViewModel #pragma mark - API - (NSUInteger)numberOfSections { [self.sectionTypeDict removeAllObjects]; // 广告,运营图,icon必定存在一个 NSUInteger count = 0; [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeAdvertise] forKey:[NSString stringWithFormat:@"%lu", count++]]; // if (self.limitBuyArea.dataArray.count > 0) { // [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeLimitBuy] forKey:[NSString stringWithFormat:@"%lu", count++]]; // } if ((self.flashSaleItem && self.haoHuoItem) || self.operActivityArray.count > 0) { [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypePromotion] forKey:[NSString stringWithFormat:@"%lu", count++]]; } if (self.officalActivityArea.dataArray.count > 0) { [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeOfficalActivity] forKey:[NSString stringWithFormat:@"%lu", count++]]; } if (self.brandRecommendArea.dataArray.count > 0) { [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeBrandReco] forKey:[NSString stringWithFormat:@"%lu", count++]]; } if (self.operationActivityArea.dataArray.count > 0) { [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeOperationActivity] forKey:[NSString stringWithFormat:@"%lu", count++]]; } if (self.userRecoArea.dataArray.count > 0) { [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeUserReco] forKey:[NSString stringWithFormat:@"%lu", count++]]; } if (self.scanRecoArea.dataArray.count > 0) { [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeScanReco] forKey:[NSString stringWithFormat:@"%lu", count++]]; } if (self.hotProductArea.dataArray.count > 0) { [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeHotProduct] forKey:[NSString stringWithFormat:@"%lu", count++]]; } return count; } - (NSUInteger)numberOfRowsInSection:(NSInteger)section { kRecoSectionType sectionType = [self sectionTypeForSection:section]; if (sectionType == kRecoSectionTypeAdvertise) { return (self.advertiseItems.count > 0) + ([FLStringHelper isValidString:self.operationItem.picUrl]) + (self.iconListArray.count > 0) + (self.recoNewsItem != nil); } else if (sectionType == kRecoSectionTypePromotion) { NSInteger count = 0; if (self.flashSaleItem && self.haoHuoItem) { count++; } if (self.operActivityArray.count > 0) { count++; } return count; } else if (sectionType == kRecoSectionTypeOfficalActivity){ return (self.officalActivityArea.dataArray.count/2 + 1); } else if (sectionType == kRecoSectionTypeBrandReco){ return (1 + 1); } else if (sectionType == kRecoSectionTypeOperationActivity) { return (self.operationActivityArea.dataArray.count + 1); } else if (sectionType == kRecoSectionTypeUserReco) { return (1 + 1); } else if (sectionType == kRecoSectionTypeScanReco) { return (self.scanRecoArea.dataArray.count + 1); } else if (sectionType == kRecoSectionTypeHotProduct) { return (self.hotProductArea.dataArray.count + 1); } return 0; } - (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath { kRecoCellType cellType = [self cellTypeForIndexPath:indexPath]; switch (cellType) { case kRecoCellTypeAdvertise: return [FKAdvertiseViewCell height]; case kRecoCellTypeOperationImage: return ceil(UISCREENWIDTH * 20.0f / 75.0f); case kRecoCellTypeIconList: return [FKRecoIconListCell heightWithIconCount:self.iconListArray.count]; case kRecoCellTypeRecoNews: return [FKRecoNewsCell height]; case kRecoCellTypeHeader: { return 35; } case kRecoCellTypeLimitBuy: return [FKRecoLimitBuyCell height:self.limitBuyArea.dataArray.count]; case kRecoCellTypPromotionLimit: { return [FKRecoPromotionLimitCell height]; } case kRecoCellTypPromotionCategory: { return [FKRecoPromotionCategoryCell height]; } case kRecoCellTypeOfficalActivity: { NSInteger lastRowIndex = ([self numberOfRowsInSection:indexPath.section] - 1); CGFloat offset = (indexPath.row == lastRowIndex ? 10 : 0); return ([FKRecoOfficalActivityCell height] + offset); } case kRecoCellTypeBrandReco: { return ([FKRecoBrandListCell height]* [FKRecoBrandListCell rowsWithBrandCount:self.brandRecommendArea.dataArray.count] + 15); break; } case kRecoCellTypeOperationActivity: { return [FKExploreTableViewCell height]; } case kRecoCellTypeScanRecoProduct: { return [FKRecoScanRecoProductCell height]; break; } case kRecoCellTypeUserRecoProduct: { return [FKRecoUserRecoProductCell height]; break; } case kRecoCellTypeHotProduct: return [FKRecoHotProductCell height]; default: break; } return 0; } // 第6, 16, 26行插入一条品牌推荐 - (NSInteger)brandCountWithRows:(NSInteger)rows { if (rows < 5) { return 0; } rows -= 5; NSInteger brandCount = 1; return (brandCount + rows/9); } #pragma mark - Method - (kRecoSectionType)sectionTypeForSection:(NSInteger)section { NSNumber *value = self.sectionTypeDict[[NSString stringWithFormat:@"%lu", section]]; if (value) { return (kRecoSectionType)(value.integerValue); } return kRecoSectionTypeNone; } - (kRecoCellType)cellTypeForIndexPath:(NSIndexPath *)indexPath{ kRecoSectionType sectionType = [self sectionTypeForSection:indexPath.section]; if (sectionType == kRecoSectionTypeAdvertise) { NSInteger startIndex = -1; if (self.advertiseItems.count > 0) startIndex++; if (indexPath.row == startIndex) return kRecoCellTypeAdvertise; if ([FLStringHelper isValidString:self.operationItem.picUrl]) startIndex++; if (indexPath.row == startIndex) return kRecoCellTypeOperationImage; if (self.iconListArray.count > 0) startIndex++; if (indexPath.row == startIndex) return kRecoCellTypeIconList; if (self.recoNewsItem != nil) startIndex++; if (indexPath.row == startIndex) return kRecoCellTypeRecoNews; } else if (sectionType == kRecoSectionTypePromotion) { if (indexPath.row == 0 && (self.flashSaleItem && self.haoHuoItem)) { return kRecoCellTypPromotionLimit; } return kRecoCellTypPromotionCategory; } else if (sectionType == kRecoSectionTypeOfficalActivity) { return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeOfficalActivity); } else if (sectionType == kRecoSectionTypeBrandReco) { return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeBrandReco); } else if (sectionType == kRecoSectionTypeOperationActivity) { return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeOperationActivity); } else if (sectionType == kRecoSectionTypeUserReco) { return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeUserRecoProduct); } else if (sectionType == kRecoSectionTypeScanReco) { return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeScanRecoProduct); } else if (sectionType == kRecoSectionTypeHotProduct) { return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeHotProduct); } return kRecoCellTypeUnknow; } - (BOOL)needShowScoreMsgWithServeTime:(NSString *)serveTime{ NSString *lastTime = [[UserDefaultManager sharedManager] getUserDefaultObject:LAST_SHOW_SCORE_MESSAGE_TIME]; if (serveTime.length == 0) return NO; if (lastTime.length == 0) return YES; NSDate *lastDate = [FLStringHelper convertToShortDateFromString:lastTime]; NSDate *nowDate = [FLStringHelper convertToShortDateFromString:serveTime]; NSTimeInterval interval = [nowDate timeIntervalSinceDate:lastDate]; if (interval > 0) { return YES; } return NO; } - (BOOL)needShowMsgWithServeTime:(NSString *)serveTime key:(NSString *)key{ NSString *lastTime = [[UserDefaultManager sharedManager] getUserDefaultObject:key]; if (serveTime.length == 0) return NO; if (lastTime.length == 0) return YES; NSDate *lastDate = [FLStringHelper convertToShortDateFromString:lastTime]; NSDate *nowDate = [FLStringHelper convertToShortDateFromString:serveTime]; NSTimeInterval interval = [nowDate timeIntervalSinceDate:lastDate]; if (interval > 0) { return YES; } return NO; } - (void)didShowMsgWithServeTime:(NSString *)serveTime key:(NSString *)key{ if (serveTime){ [[UserDefaultManager sharedManager] setUserDefaultObject:serveTime key:key]; } } - (CGFloat)headerHeightForSection:(NSInteger)section{ return CGFLOAT_MIN; } - (FKAdvertiseItem*)advertiseItemAtIndex:(NSUInteger)index{ if (index < self.advertiseItems.count) { return self.advertiseItems[index]; } return nil; } - (FKIconItem *)iconItemAtIndex:(NSUInteger)index{ if (index < self.iconListArray.count) { return self.iconListArray[index]; } return nil; } - (FKLimitBuyItem *)limitBuyItemAtIndex:(NSUInteger)index { if (index < self.limitBuyArea.dataArray.count) { return self.limitBuyArea.dataArray[index]; } return nil; } - (FKActivityItem *)officalActivityItemAtIndex:(NSUInteger)index { if (index < self.officalActivityArea.dataArray.count) { return self.officalActivityArea.dataArray[index]; } return nil; } - (FKRecoBrandItem *)recoBrandItemAtIndex:(NSUInteger)index { if (index < self.brandRecommendArea.dataArray.count) { return self.brandRecommendArea.dataArray[index]; } return nil; } - (FKActivityItem *)operationActivityItemAtIndex:(NSUInteger)index { if (index < self.operationActivityArea.dataArray.count) { return self.operationActivityArea.dataArray[index]; } return nil; } - (FKHotProductItem *)hotProductItemAtIndex:(NSUInteger)index { if (index < self.hotProductArea.dataArray.count) { return self.hotProductArea.dataArray[index]; } return nil; } - (FKHotProductItem *)recommendItemAtIndex:(NSUInteger)index{ if (index < self.hotProductArea.dataArray.count) { return self.hotProductArea.dataArray[index]; } return nil; } - (NSURL*)adUrlForIndex:(NSUInteger)index { FKAdvertiseItem *item = [self advertiseItemAtIndex:index]; if (item) { NSString *url = [NSString stringWithFormat:@"%@%@", item.photoURL, [FLStringHelper cdnParamaterString:(int)UISCREENWIDTH height:ADSCROLLVIEW_HEIGHT]]; return [NSURL URLWithString:url]; } return nil; } - (NSArray *)advertiseUrlArray{ if (_advertiseUrlArray == nil) { NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:self.advertiseItems.count]; for (FKAdvertiseItem *item in self.advertiseItems) { NSString *url = [NSString stringWithFormat:@"%@%@", item.photoURL, [FLStringHelper cdnParamaterString:(int)UISCREENWIDTH height:ADSCROLLVIEW_HEIGHT]]; [arrayM addObject:url]; } return arrayM; } return nil; } - (BOOL)isNeedReloadAdvForOldUrls:(NSArray *)oldUrls { NSArray *currendUrls = self.advertiseUrlArray; if (currendUrls.count != oldUrls.count) { return YES; } NSString *curUrl; NSString *oldUrl; for (int idx = 0; idx < MIN(currendUrls.count, oldUrls.count); idx++) { curUrl = currendUrls[idx]; oldUrl = oldUrls[idx]; if (![curUrl isEqualToString:oldUrl]) { return YES; } } return NO; } - (NSUInteger)getAllRecommendItemCount{ return self.hotProductArea.dataArray.count; } - (BOOL)allEmpty{ if (!self.advertiseItems.count && !self.limitBuyArea.dataArray.count && !self.hotProductArea.dataArray.count) { return YES; } return NO; } - (BOOL)needShowNewTagAtIndex:(NSInteger)index { FKIconItem *item = [self iconItemAtIndex:index]; if (![FLStringHelper isValidString:item.targetUrl]) return NO; NSString *lastTarget = [[NSUserDefaults standardUserDefaults] stringForKey:REC_TARGET_URL_TAG_KEY]; if (!lastTarget) return YES; if ([lastTarget isEqualToString:item.targetUrl]){ return NO; }else{ return YES; } } - (void)didClickNewTagAtIndex:(NSInteger)index { FKIconItem *item = [self iconItemAtIndex:index]; if (![FLStringHelper isValidString:item.targetUrl]) return; if ([item.mark isEqualToString:@"1"]) { [[NSUserDefaults standardUserDefaults] setObject:item.targetUrl forKey:REC_TARGET_URL_TAG_KEY]; [[NSUserDefaults standardUserDefaults] synchronize]; } } - (NSInteger)limitBuyMaxRemaindTime { NSInteger maxRemaindTime = 0; for (FKLimitBuyItem *item in self.limitBuyArea.dataArray) { maxRemaindTime = MAX(maxRemaindTime, item.remaindTime); } return maxRemaindTime; } - (NSAttributedString *)limitBuyMaxRemaindTimeAttributedString { NSString *timeString = [FLStringHelper convertTimeIntervalToHourCountStringStyleColon:([self limitBuyMaxRemaindTime] - self.elapseSecond)]; NSString *allStirng = [NSString stringWithFormat:@"距本场结束: %@", timeString]; NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithString:allStirng]; [mutableString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xff624a) range:NSMakeRange(allStirng.length - timeString.length, timeString.length)]; [mutableString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(allStirng.length - timeString.length, timeString.length)]; return mutableString; } - (void)setContentOffset:(CGFloat)offsetX forKey:(NSInteger)key { if (!_contensOffsetDict) { _contensOffsetDict = [NSMutableDictionary dictionary]; } NSNumber *number = [NSNumber numberWithFloat:offsetX]; NSString *keySring = [NSString stringWithFormat:@"%ld",(long)key]; [self.contensOffsetDict setValue:number forKey:keySring]; } - (CGFloat)contentOffsetForKey:(NSInteger)key { NSString *keySring = [NSString stringWithFormat:@"%ld",(long)key]; NSNumber *value = [self.contensOffsetDict valueForKey:keySring]; if ([value isKindOfClass:[NSNumber class]]) { return value.floatValue; } return 0; } #pragma mark - Property - (NSMutableDictionary *)sectionTypeDict { if (!_sectionTypeDict) { _sectionTypeDict = [NSMutableDictionary dictionary]; } return _sectionTypeDict; } @end