Last active
October 21, 2017 04:03
-
-
Save matin0728/a5eba0d518cc4d0361cf22438387ebac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ZHHotRankViewController.h | |
// ZHModuleFeed | |
// | |
// Created by 马骁 on 28/09/2017. | |
// | |
#import <ZHMiddlewareList/ZHCKBaseCollectionViewController.h> | |
NS_ASSUME_NONNULL_BEGIN | |
/// New hot rank view controller | |
@interface ZHHotRankViewController : ZHCKBaseCollectionViewController | |
@property (nonatomic, readonly) NSString *listPath; | |
- (instancetype)initWithListPath:(NSString *)path; | |
@end | |
NS_ASSUME_NONNULL_END | |
// | |
// ZHHotRankViewController.m | |
// ZHModuleFeed | |
// | |
// Created by 马骁 on 28/09/2017. | |
// | |
#import "ZHHotRankViewController.h" | |
#import <ZHMiddlewareList/ZHFixedWidthFlowLayout.h> | |
#import <ZHMiddlewareList/ZHMiddlewareList.h> | |
#import <ComponentKit/ComponentKit.h> | |
// za | |
#import <ZHCoreViewController/UIViewController+ZATracker.h> | |
#import <ZHCoreZASDK/NSObject+ZTracker.h> | |
#import <ZHCoreZASDK/ZASDK.h> | |
#import <ZHMiddlewareZAAutomation/ZHEventEmiter.h> | |
// util | |
#import <ZHBasicCommon/ZHMacros.h> | |
#import <ZHCoreTheme/ZHCoreTheme.h> | |
#import <ZHCoreFont/UIFont+Zhihu.h> | |
#import "ZHHotRankFeed.h" | |
@interface ZHHotRankViewController (){ | |
BOOL _fetchingOnAppear; | |
} | |
@end | |
@implementation ZHHotRankViewController | |
- (nonnull instancetype)initWithListPath:(nonnull NSString *)path { | |
ZHListCursorFetcher *fetcher = [[ZHListCursorFetcher alloc] initWithPath:path modelGenerator:^id _Nullable(id _Nonnull aJSONObject) { | |
//id obj = [[ZHObjectFactory sharedFactory] objectWithProperties:aJSONObject]; | |
return nil; | |
}]; | |
ZHListViewModel *vm = [[ZHListViewModel alloc] initWithFetcher:fetcher cacheProvider:[PINCache sharedCache]]; | |
vm.useCache = NO; | |
vm.fetchLimit = 10; //优化加载速度. | |
UICollectionViewFlowLayout *flowLayout = [[ZHFixedWidthFlowLayout alloc] init]; | |
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; | |
[flowLayout setMinimumInteritemSpacing:0]; | |
[flowLayout setMinimumLineSpacing:[ZHCKBaseCollectionViewController marginTopForCardStyle]]; | |
self = [super initWithCollectionViewLayout:flowLayout viewModel:vm]; | |
if (self) { | |
_listPath = path; | |
_fetchingOnAppear= YES; | |
} | |
return self; | |
} | |
- (void)dealloc{ | |
THEME_REMOVE_SELF; | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// za | |
[self setIsZaEventRootLayer:YES]; | |
} | |
- (void)viewDidAppear:(BOOL)animated { | |
[super viewDidAppear:animated]; | |
if (self->_fetchingOnAppear) { | |
self->_fetchingOnAppear = NO; | |
[self backToTopWithRefreshing:YES]; | |
} | |
} | |
#pragma mark-- UICollectionViewDelegate | |
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { | |
if ([[self superclass] instancesRespondToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)]) { | |
[super scrollViewDidEndDragging:scrollView willDecelerate:decelerate]; | |
} | |
if (decelerate == NO) { | |
[self recordVisibleCellCardShow]; | |
} | |
} | |
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ | |
if ([[self superclass] instancesRespondToSelector:@selector(scrollViewDidEndDecelerating:)]) { | |
[super scrollViewDidEndDecelerating:scrollView]; | |
} | |
[self recordVisibleCellCardShow]; | |
} | |
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { | |
id model = [self.dataSource modelForItemAtIndexPath:indexPath]; | |
// if ([model isKindOfClass:ZHFeed.class]) { | |
// [self navigateWithFeed:(ZHFeed *)model]; | |
// } | |
} | |
- (void)collectionView:(UICollectionView *)collectionView | |
willDisplayCell:(UICollectionViewCell *)cell | |
forItemAtIndexPath:(NSIndexPath *)indexPath { | |
if ([[self superclass] instancesRespondToSelector:@selector(collectionView:willDisplayCell:forItemAtIndexPath:)]) { | |
[super collectionView:collectionView willDisplayCell:cell forItemAtIndexPath:indexPath]; | |
} | |
/* Legacy support for lastReadTouch */ | |
id rowModel = (NSObject *)[self.dataSource modelForItemAtIndexPath:indexPath]; | |
// CardShow | |
[self logCardShowWithCell:cell | |
indexPath:indexPath | |
scrollView:collectionView | |
rowModel:rowModel]; | |
} | |
- (UIEdgeInsets)defaultContentInset { | |
UIEdgeInsets insets = [super defaultContentInset]; | |
insets.top = insets.top + [self.class marginTopForCardStyle]; | |
return [self insetWithBottomBar:insets]; | |
} | |
#pragma mark - Private | |
- (void)recordVisibleCellCardShow { | |
UICollectionView *t = self.collectionView; | |
NSArray<__kindof UICollectionViewCell *> *cells = [t visibleCells]; | |
[cells enumerateObjectsUsingBlock:^(UICollectionViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { | |
NSIndexPath *indexPath = [t indexPathForCell:obj]; | |
id rowModel = [self.dataSource modelForItemAtIndexPath:indexPath]; | |
[self logCardShowWithCell:obj | |
indexPath:indexPath | |
scrollView:t | |
rowModel:rowModel]; | |
}]; | |
} | |
- (void)logCardShowWithCell:(UIView *)cell | |
indexPath:(NSIndexPath *)indexPath | |
scrollView:(UIScrollView *)scrollView | |
rowModel:(NSObject *)rowModel { | |
if ([scrollView logCardShowDiabled] || | |
rowModel.hasTracked || | |
scrollView.tracking) { | |
return; | |
} | |
[rowModel setHasTracked:YES]; | |
[cell logCardShowInPageWithName:self.zaViewName]; | |
} | |
#pragma mark - ComponentProvider | |
+ (CKComponent *)componentForModel:(id<NSObject>)model context:(id<NSObject>)context { | |
if ([model isKindOfClass:ZHFeed.class]) { | |
// return [ZHCKActivityComponent newWithFeed:(ZHFeed *)model context:(ZHCKCommonContext *)context]; | |
} | |
return [super componentForModel:model context:context]; | |
} | |
#pragma mark - ZA | |
- (ViewLocation *)zaLayerViewLocation { | |
return [ViewLocation createWithBuilder:^(ViewLocation *v) { | |
v.module = Module_Type_FeedList; | |
}]; | |
} | |
- (ModuleInfo *)zaLayerModuleInfo { | |
return [ModuleInfo createWithBuilder:^(ModuleInfo *m) { | |
m.list.listSize = (int32_t)self.viewModel.objects.count; | |
}]; | |
} | |
- (ZHZaEventEmiter *)eventEmiter { | |
return [[ZHZaEventEmiter alloc] initWithZaViewName:self.zaViewName]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment