Bez popisu

FKRecommendViewModel.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. //
  2. // FKRecommendViewModel.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/11/30.
  6. // Copyright © 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKRecommendViewModel.h"
  9. #import "FKAdvertiseViewCell.h"
  10. #import "FKRecoIconListCell.h"
  11. #import "FKRecoNewsCell.h"
  12. #import "FKRecoLimitBuyCell.h"
  13. #import "FKRecoPromotionLimitCell.h"
  14. #import "FKRecoPromotionCategoryCell.h"
  15. #import "FKRecoOfficalActivityCell.h"
  16. #import "FKRecoBrandListCell.h"
  17. #import "FKExploreTableViewCell.h"
  18. #import "FKRecoUserRecoProductCell.h"
  19. #import "FKRecoScanRecoProductCell.h"
  20. #import "FKRecoHotProductCell.h"
  21. #import "FKHotProductItem.h"
  22. #import "UserDefaultManager.h"
  23. static NSString *REC_TARGET_URL_TAG_KEY = @"REC_TARGET_URL_TAG_KEY";
  24. @interface FKRecommendViewModel ()
  25. @property (nonatomic, strong) NSMutableDictionary *sectionTypeDict;
  26. @end
  27. @implementation FKRecommendViewModel
  28. #pragma mark - API
  29. - (NSUInteger)numberOfSections {
  30. [self.sectionTypeDict removeAllObjects];
  31. // 广告,运营图,icon必定存在一个
  32. NSUInteger count = 0;
  33. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeAdvertise] forKey:[NSString stringWithFormat:@"%lu", count++]];
  34. // if (self.limitBuyArea.dataArray.count > 0) {
  35. // [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeLimitBuy] forKey:[NSString stringWithFormat:@"%lu", count++]];
  36. // }
  37. if ((self.flashSaleItem && self.haoHuoItem) || self.operActivityArray.count > 0) {
  38. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypePromotion] forKey:[NSString stringWithFormat:@"%lu", count++]];
  39. }
  40. if (self.officalActivityArea.dataArray.count > 0) {
  41. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeOfficalActivity] forKey:[NSString stringWithFormat:@"%lu", count++]];
  42. }
  43. if (self.brandRecommendArea.dataArray.count > 0) {
  44. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeBrandReco] forKey:[NSString stringWithFormat:@"%lu", count++]];
  45. }
  46. if (self.operationActivityArea.dataArray.count > 0) {
  47. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeOperationActivity] forKey:[NSString stringWithFormat:@"%lu", count++]];
  48. }
  49. if (self.userRecoArea.dataArray.count > 0) {
  50. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeUserReco] forKey:[NSString stringWithFormat:@"%lu", count++]];
  51. }
  52. if (self.scanRecoArea.dataArray.count > 0) {
  53. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeScanReco] forKey:[NSString stringWithFormat:@"%lu", count++]];
  54. }
  55. if (self.hotProductArea.dataArray.count > 0) {
  56. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kRecoSectionTypeHotProduct] forKey:[NSString stringWithFormat:@"%lu", count++]];
  57. }
  58. return count;
  59. }
  60. - (NSUInteger)numberOfRowsInSection:(NSInteger)section {
  61. kRecoSectionType sectionType = [self sectionTypeForSection:section];
  62. if (sectionType == kRecoSectionTypeAdvertise) {
  63. return (self.advertiseItems.count > 0) + ([FLStringHelper isValidString:self.operationItem.picUrl]) + (self.iconListArray.count > 0) + (self.recoNewsItem != nil);
  64. } else if (sectionType == kRecoSectionTypePromotion) {
  65. NSInteger count = 0;
  66. if (self.flashSaleItem && self.haoHuoItem) {
  67. count++;
  68. }
  69. if (self.operActivityArray.count > 0) {
  70. count++;
  71. }
  72. return count;
  73. } else if (sectionType == kRecoSectionTypeOfficalActivity){
  74. return (self.officalActivityArea.dataArray.count/2 + 1);
  75. } else if (sectionType == kRecoSectionTypeBrandReco){
  76. return (1 + 1);
  77. } else if (sectionType == kRecoSectionTypeOperationActivity) {
  78. return (self.operationActivityArea.dataArray.count + 1);
  79. } else if (sectionType == kRecoSectionTypeUserReco) {
  80. return (1 + 1);
  81. } else if (sectionType == kRecoSectionTypeScanReco) {
  82. return (self.scanRecoArea.dataArray.count + 1);
  83. } else if (sectionType == kRecoSectionTypeHotProduct) {
  84. return (self.hotProductArea.dataArray.count + 1);
  85. }
  86. return 0;
  87. }
  88. - (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  89. kRecoCellType cellType = [self cellTypeForIndexPath:indexPath];
  90. switch (cellType) {
  91. case kRecoCellTypeAdvertise:
  92. return [FKAdvertiseViewCell height];
  93. case kRecoCellTypeOperationImage:
  94. return ceil(UISCREENWIDTH * 20.0f / 75.0f);
  95. case kRecoCellTypeIconList:
  96. return [FKRecoIconListCell heightWithIconCount:self.iconListArray.count];
  97. case kRecoCellTypeRecoNews:
  98. return [FKRecoNewsCell height];
  99. case kRecoCellTypeHeader: {
  100. return 35;
  101. }
  102. case kRecoCellTypeLimitBuy:
  103. return [FKRecoLimitBuyCell height:self.limitBuyArea.dataArray.count];
  104. case kRecoCellTypPromotionLimit: {
  105. return [FKRecoPromotionLimitCell height];
  106. }
  107. case kRecoCellTypPromotionCategory: {
  108. return [FKRecoPromotionCategoryCell height];
  109. }
  110. case kRecoCellTypeOfficalActivity: {
  111. NSInteger lastRowIndex = ([self numberOfRowsInSection:indexPath.section] - 1);
  112. CGFloat offset = (indexPath.row == lastRowIndex ? 10 : 0);
  113. return ([FKRecoOfficalActivityCell height] + offset);
  114. }
  115. case kRecoCellTypeBrandReco: {
  116. return ([FKRecoBrandListCell height]* [FKRecoBrandListCell rowsWithBrandCount:self.brandRecommendArea.dataArray.count] + 15);
  117. break;
  118. }
  119. case kRecoCellTypeOperationActivity: {
  120. return [FKExploreTableViewCell height];
  121. }
  122. case kRecoCellTypeScanRecoProduct: {
  123. return [FKRecoScanRecoProductCell height];
  124. break;
  125. }
  126. case kRecoCellTypeUserRecoProduct: {
  127. return [FKRecoUserRecoProductCell height];
  128. break;
  129. }
  130. case kRecoCellTypeHotProduct:
  131. return [FKRecoHotProductCell height];
  132. default:
  133. break;
  134. }
  135. return 0;
  136. }
  137. // 第6, 16, 26行插入一条品牌推荐
  138. - (NSInteger)brandCountWithRows:(NSInteger)rows {
  139. if (rows < 5) {
  140. return 0;
  141. }
  142. rows -= 5;
  143. NSInteger brandCount = 1;
  144. return (brandCount + rows/9);
  145. }
  146. #pragma mark - Method
  147. - (kRecoSectionType)sectionTypeForSection:(NSInteger)section {
  148. NSNumber *value = self.sectionTypeDict[[NSString stringWithFormat:@"%lu", section]];
  149. if (value) {
  150. return (kRecoSectionType)(value.integerValue);
  151. }
  152. return kRecoSectionTypeNone;
  153. }
  154. - (kRecoCellType)cellTypeForIndexPath:(NSIndexPath *)indexPath{
  155. kRecoSectionType sectionType = [self sectionTypeForSection:indexPath.section];
  156. if (sectionType == kRecoSectionTypeAdvertise) {
  157. NSInteger startIndex = -1;
  158. if (self.advertiseItems.count > 0) startIndex++;
  159. if (indexPath.row == startIndex) return kRecoCellTypeAdvertise;
  160. if ([FLStringHelper isValidString:self.operationItem.picUrl]) startIndex++;
  161. if (indexPath.row == startIndex) return kRecoCellTypeOperationImage;
  162. if (self.iconListArray.count > 0) startIndex++;
  163. if (indexPath.row == startIndex) return kRecoCellTypeIconList;
  164. if (self.recoNewsItem != nil) startIndex++;
  165. if (indexPath.row == startIndex) return kRecoCellTypeRecoNews;
  166. } else if (sectionType == kRecoSectionTypePromotion) {
  167. if (indexPath.row == 0 && (self.flashSaleItem && self.haoHuoItem)) {
  168. return kRecoCellTypPromotionLimit;
  169. }
  170. return kRecoCellTypPromotionCategory;
  171. } else if (sectionType == kRecoSectionTypeOfficalActivity) {
  172. return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeOfficalActivity);
  173. } else if (sectionType == kRecoSectionTypeBrandReco) {
  174. return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeBrandReco);
  175. } else if (sectionType == kRecoSectionTypeOperationActivity) {
  176. return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeOperationActivity);
  177. } else if (sectionType == kRecoSectionTypeUserReco) {
  178. return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeUserRecoProduct);
  179. } else if (sectionType == kRecoSectionTypeScanReco) {
  180. return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeScanRecoProduct);
  181. } else if (sectionType == kRecoSectionTypeHotProduct) {
  182. return (indexPath.row == 0 ? kRecoCellTypeHeader : kRecoCellTypeHotProduct);
  183. }
  184. return kRecoCellTypeUnknow;
  185. }
  186. - (BOOL)needShowScoreMsgWithServeTime:(NSString *)serveTime{
  187. NSString *lastTime = [[UserDefaultManager sharedManager] getUserDefaultObject:LAST_SHOW_SCORE_MESSAGE_TIME];
  188. if (serveTime.length == 0) return NO;
  189. if (lastTime.length == 0) return YES;
  190. NSDate *lastDate = [FLStringHelper convertToShortDateFromString:lastTime];
  191. NSDate *nowDate = [FLStringHelper convertToShortDateFromString:serveTime];
  192. NSTimeInterval interval = [nowDate timeIntervalSinceDate:lastDate];
  193. if (interval > 0) {
  194. return YES;
  195. }
  196. return NO;
  197. }
  198. - (BOOL)needShowMsgWithServeTime:(NSString *)serveTime key:(NSString *)key{
  199. NSString *lastTime = [[UserDefaultManager sharedManager] getUserDefaultObject:key];
  200. if (serveTime.length == 0) return NO;
  201. if (lastTime.length == 0) return YES;
  202. NSDate *lastDate = [FLStringHelper convertToShortDateFromString:lastTime];
  203. NSDate *nowDate = [FLStringHelper convertToShortDateFromString:serveTime];
  204. NSTimeInterval interval = [nowDate timeIntervalSinceDate:lastDate];
  205. if (interval > 0) {
  206. return YES;
  207. }
  208. return NO;
  209. }
  210. - (void)didShowMsgWithServeTime:(NSString *)serveTime key:(NSString *)key{
  211. if (serveTime){
  212. [[UserDefaultManager sharedManager] setUserDefaultObject:serveTime
  213. key:key];
  214. }
  215. }
  216. - (CGFloat)headerHeightForSection:(NSInteger)section{
  217. return CGFLOAT_MIN;
  218. }
  219. - (FKAdvertiseItem*)advertiseItemAtIndex:(NSUInteger)index{
  220. if (index < self.advertiseItems.count) {
  221. return self.advertiseItems[index];
  222. }
  223. return nil;
  224. }
  225. - (FKIconItem *)iconItemAtIndex:(NSUInteger)index{
  226. if (index < self.iconListArray.count) {
  227. return self.iconListArray[index];
  228. }
  229. return nil;
  230. }
  231. - (FKLimitBuyItem *)limitBuyItemAtIndex:(NSUInteger)index {
  232. if (index < self.limitBuyArea.dataArray.count) {
  233. return self.limitBuyArea.dataArray[index];
  234. }
  235. return nil;
  236. }
  237. - (FKActivityItem *)officalActivityItemAtIndex:(NSUInteger)index {
  238. if (index < self.officalActivityArea.dataArray.count) {
  239. return self.officalActivityArea.dataArray[index];
  240. }
  241. return nil;
  242. }
  243. - (FKRecoBrandItem *)recoBrandItemAtIndex:(NSUInteger)index {
  244. if (index < self.brandRecommendArea.dataArray.count) {
  245. return self.brandRecommendArea.dataArray[index];
  246. }
  247. return nil;
  248. }
  249. - (FKActivityItem *)operationActivityItemAtIndex:(NSUInteger)index {
  250. if (index < self.operationActivityArea.dataArray.count) {
  251. return self.operationActivityArea.dataArray[index];
  252. }
  253. return nil;
  254. }
  255. - (FKHotProductItem *)hotProductItemAtIndex:(NSUInteger)index {
  256. if (index < self.hotProductArea.dataArray.count) {
  257. return self.hotProductArea.dataArray[index];
  258. }
  259. return nil;
  260. }
  261. - (FKHotProductItem *)recommendItemAtIndex:(NSUInteger)index{
  262. if (index < self.hotProductArea.dataArray.count) {
  263. return self.hotProductArea.dataArray[index];
  264. }
  265. return nil;
  266. }
  267. - (NSURL*)adUrlForIndex:(NSUInteger)index {
  268. FKAdvertiseItem *item = [self advertiseItemAtIndex:index];
  269. if (item) {
  270. NSString *url = [NSString stringWithFormat:@"%@%@", item.photoURL,
  271. [FLStringHelper cdnParamaterString:(int)UISCREENWIDTH
  272. height:ADSCROLLVIEW_HEIGHT]];
  273. return [NSURL URLWithString:url];
  274. }
  275. return nil;
  276. }
  277. - (NSArray *)advertiseUrlArray{
  278. if (_advertiseUrlArray == nil) {
  279. NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:self.advertiseItems.count];
  280. for (FKAdvertiseItem *item in self.advertiseItems) {
  281. NSString *url = [NSString stringWithFormat:@"%@%@", item.photoURL,
  282. [FLStringHelper cdnParamaterString:(int)UISCREENWIDTH
  283. height:ADSCROLLVIEW_HEIGHT]];
  284. [arrayM addObject:url];
  285. }
  286. return arrayM;
  287. }
  288. return nil;
  289. }
  290. - (BOOL)isNeedReloadAdvForOldUrls:(NSArray *)oldUrls {
  291. NSArray *currendUrls = self.advertiseUrlArray;
  292. if (currendUrls.count != oldUrls.count) {
  293. return YES;
  294. }
  295. NSString *curUrl;
  296. NSString *oldUrl;
  297. for (int idx = 0; idx < MIN(currendUrls.count, oldUrls.count); idx++) {
  298. curUrl = currendUrls[idx];
  299. oldUrl = oldUrls[idx];
  300. if (![curUrl isEqualToString:oldUrl]) {
  301. return YES;
  302. }
  303. }
  304. return NO;
  305. }
  306. - (NSUInteger)getAllRecommendItemCount{
  307. return self.hotProductArea.dataArray.count;
  308. }
  309. - (BOOL)allEmpty{
  310. if (!self.advertiseItems.count &&
  311. !self.limitBuyArea.dataArray.count &&
  312. !self.hotProductArea.dataArray.count) {
  313. return YES;
  314. }
  315. return NO;
  316. }
  317. - (BOOL)needShowNewTagAtIndex:(NSInteger)index {
  318. FKIconItem *item = [self iconItemAtIndex:index];
  319. if (![FLStringHelper isValidString:item.targetUrl]) return NO;
  320. NSString *lastTarget = [[NSUserDefaults standardUserDefaults] stringForKey:REC_TARGET_URL_TAG_KEY];
  321. if (!lastTarget) return YES;
  322. if ([lastTarget isEqualToString:item.targetUrl]){
  323. return NO;
  324. }else{
  325. return YES;
  326. }
  327. }
  328. - (void)didClickNewTagAtIndex:(NSInteger)index {
  329. FKIconItem *item = [self iconItemAtIndex:index];
  330. if (![FLStringHelper isValidString:item.targetUrl]) return;
  331. if ([item.mark isEqualToString:@"1"]) {
  332. [[NSUserDefaults standardUserDefaults] setObject:item.targetUrl forKey:REC_TARGET_URL_TAG_KEY];
  333. [[NSUserDefaults standardUserDefaults] synchronize];
  334. }
  335. }
  336. - (NSInteger)limitBuyMaxRemaindTime {
  337. NSInteger maxRemaindTime = 0;
  338. for (FKLimitBuyItem *item in self.limitBuyArea.dataArray) {
  339. maxRemaindTime = MAX(maxRemaindTime, item.remaindTime);
  340. }
  341. return maxRemaindTime;
  342. }
  343. - (NSAttributedString *)limitBuyMaxRemaindTimeAttributedString {
  344. NSString *timeString = [FLStringHelper convertTimeIntervalToHourCountStringStyleColon:([self limitBuyMaxRemaindTime] - self.elapseSecond)];
  345. NSString *allStirng = [NSString stringWithFormat:@"距本场结束: %@", timeString];
  346. NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithString:allStirng];
  347. [mutableString addAttribute:NSForegroundColorAttributeName
  348. value:UIColorFromRGB(0xff624a)
  349. range:NSMakeRange(allStirng.length - timeString.length, timeString.length)];
  350. [mutableString addAttribute:NSFontAttributeName
  351. value:[UIFont boldSystemFontOfSize:13]
  352. range:NSMakeRange(allStirng.length - timeString.length, timeString.length)];
  353. return mutableString;
  354. }
  355. - (void)setContentOffset:(CGFloat)offsetX forKey:(NSInteger)key {
  356. if (!_contensOffsetDict) {
  357. _contensOffsetDict = [NSMutableDictionary dictionary];
  358. }
  359. NSNumber *number = [NSNumber numberWithFloat:offsetX];
  360. NSString *keySring = [NSString stringWithFormat:@"%ld",(long)key];
  361. [self.contensOffsetDict setValue:number forKey:keySring];
  362. }
  363. - (CGFloat)contentOffsetForKey:(NSInteger)key {
  364. NSString *keySring = [NSString stringWithFormat:@"%ld",(long)key];
  365. NSNumber *value = [self.contensOffsetDict valueForKey:keySring];
  366. if ([value isKindOfClass:[NSNumber class]]) {
  367. return value.floatValue;
  368. }
  369. return 0;
  370. }
  371. #pragma mark - Property
  372. - (NSMutableDictionary *)sectionTypeDict {
  373. if (!_sectionTypeDict) {
  374. _sectionTypeDict = [NSMutableDictionary dictionary];
  375. }
  376. return _sectionTypeDict;
  377. }
  378. @end