// // FKExploreViewModel.m // FirstLink // // Created by ascii on 16/2/16. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKExploreViewModel.h" #import "FKExploreTopicTag.h" int const FKExploreRequestPageSize = 6; NSString * const FKExploreTopicTagKey = @"FKExploreTopicTagKey"; int const FKExploreRequestTopicTags = 100; int const FKExploreRequestTopicList = 200; int const FKExploreRequestTopicListNextPage = 300; @implementation FKExploreViewModel - (NSString *)topicTagIDAtIndex:(NSInteger)index { if (index < self.topicTagArray.count) { return ((FKExploreTopicTag *)self.topicTagArray[index]).tagID; } return nil; } - (NSString *)startRowWithTagID:(NSString *)tagID { if (tagID) { NSArray *topicArray = self.topicListCache[tagID]; return [NSString stringWithFormat:@"%lu", (unsigned long)topicArray.count]; } return nil; } - (NSArray *)validTopicListWithTagID:(NSString *)tagID { if (tagID) { NSArray *topicArray = self.topicListCache[tagID]; NSMutableArray *validArray = [NSMutableArray array]; [topicArray enumerateObjectsUsingBlock:^(FKExploreTopicListItem *obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.subItemArray.count > 0) { [validArray addObject:obj]; } }]; return validArray; } return nil; } - (FKExploreTopicListItem *)validTopicListItemWithTagID:(NSString *)tagID atIndex:(NSInteger)index { NSArray *array = [self validTopicListWithTagID:tagID]; if (index < array.count) { return array[index]; } return nil; } - (void)setContentOffset:(CGFloat)offsetX forKey:(NSInteger)key { if (!_contensOffsetArray) { _contensOffsetArray = [NSMutableDictionary dictionary]; } NSNumber *number = [NSNumber numberWithFloat:offsetX]; NSString *keySring = [NSString stringWithFormat:@"%ld",(long)key]; [self.contensOffsetArray setValue:number forKey:keySring]; } - (CGFloat)contentOffsetForKey:(NSInteger)key { NSString *keySring = [NSString stringWithFormat:@"%ld",(long)key]; NSNumber *value = [self.contensOffsetArray valueForKey:keySring]; if ([value isKindOfClass:[NSNumber class]]) { return value.floatValue; } return 0; } #pragma mark - Property - (NSArray *)topicTagArray { if (!_topicTagArray) { _topicTagArray = [NSArray array]; } return _topicTagArray; } - (NSArray *)sectionTitlesArray { NSMutableArray *array = [NSMutableArray array]; for (FKExploreTopicTag *tag in self.topicTagArray) { [array addObject:tag.name]; } return array; } - (NSMutableDictionary *)topicListCache { if (!_topicListCache) { _topicListCache = [NSMutableDictionary dictionary]; } return _topicListCache; } @end