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