123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- //
- // FKMyBookController.m
- // FirstLink
- //
- // Created by jack on 16/4/28.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKMyBookController.h"
- #import "FKProDetailController.h"
- #import "FKBookManagerController.h"
- #import "FKBookRecommendCell.h"
- #import "FKBookProductCell.h"
- #import "FKBookTitleBtn.h"
- #import "FKBrandSelectView.h"
- #import "FKExploreListEmptyView.h"
- #import "FKBookRequest.h"
- #import "FKBookReform.h"
- #import "FKBookNormalViewModel.h"
- #import "FKBookRecViewModel.h"
- #import "FKBookRecItem.h"
- #import "FKBookProItem.h"
- #import "UITableViewCell+MyBook.h"
- #import "FKMyBookDataSource.h"
- #import "SchemaManager.h"
- #define BOOK_NORMAL_LIST_REQ 701
- #define BOOK_MORE_NORMAL_LIST_REQ 702
- #define BOOK_REC_LIST_REQ 703
- #define BOOK_REC_BOOK_BRAND 704
- #define BOOK_REC_CANCEL_BOOK 705
- @interface FKMyBookController () <UITableViewDataSource, UITableViewDelegate, FLNetworkDelegate>
- @property (nonatomic, strong) id <FKMyBookDataSource> viewModel;
- @property (nonatomic, strong) UIView *tipHeaderView;
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) FKBookTitleBtn *titleView;
- @property (nonatomic, strong) FKBrandSelectView *brandSelectView;
- @property (nonatomic, strong) FKExploreListEmptyView *emptyView;
- @property (nonatomic, strong) NSString *selectedBrandId;
- @end
- @implementation FKMyBookController
- - (void)viewDidLoad{
- [super viewDidLoad];
-
- [self addAllSubviews];
- [self configNavBarItem];
- [self reqBookListWithBrandId:nil];
- [self addRefreshControl];
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- [self configTitleView];
- }
- //- (void)viewDidAppear:(BOOL)animated{
- // [super viewDidAppear:animated];
- //}
- - (void)viewWillDisappear:(BOOL)animated{
- [super viewWillDisappear:animated];
- [self hiddenBrandSelectView];
-
- self.navigationItem.titleView = nil;
- }
- - (void)addAllSubviews{
-
- [self.view addSubview:self.emptyView];
- [self.view addSubview:self.tableView];
-
- [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.insets(UIEdgeInsetsZero);
- }];
-
- [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.insets(UIEdgeInsetsZero);
- }];
- }
- - (void)addRefreshControl{
- [self initRefreshControlWithTableView:self.tableView];
- }
- - (void)enableBottomRefresh{
- [self.refreshControl setBottomEnabled:self.viewModel.enableBottomRefresh];
- }
- #pragma mark - refresh delegate
- - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction{
- if (direction == RefreshDirectionTop) {
-
- [FKBookRequest reqBookListWithIdentify:BOOK_NORMAL_LIST_REQ
- brandID:self.selectedBrandId
- startRow:0
- serveTime:nil
- delegate:self];
-
- }else if (direction == RefreshDirectionBottom){
- [self reqMoreBookWithBrandId:self.selectedBrandId];
- }
- }
- #pragma mark - request
- - (void)reqBookListWithBrandId:(NSString *)brandID{
-
- [self.hudView show:YES];
- [FKBookRequest reqBookListWithIdentify:BOOK_NORMAL_LIST_REQ
- brandID:brandID
- startRow:0
- serveTime:nil
- delegate:self];
- }
- - (void)reqBookRecList{
-
- [self.hudView show:YES];
- [FKBookRequest reqRecBookListWithIdentify:BOOK_REC_LIST_REQ delegate:self];
- }
- - (void)reqMoreBookWithBrandId:(NSString *)brandID{
-
- if ([self.viewModel isKindOfClass:[FKBookNormalViewModel class]]){
- FKBookNormalViewModel *normalModel = (FKBookNormalViewModel *)self.viewModel;
-
- [FKBookRequest reqBookListWithIdentify:BOOK_MORE_NORMAL_LIST_REQ
- brandID:brandID
- startRow:self.viewModel.itemArray.count
- serveTime:normalModel.serveTime
- delegate:self];
- }
- }
- #pragma mark - response
- - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header {
-
- [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
- [self.refreshControl finishRefreshingDirection:RefreshDirectionBottom];
-
- if (header.code.intValue == RESPONSE_MSG_NORMAL) {
- if (identify == BOOK_NORMAL_LIST_REQ){
- FKBookNormalViewModel *viewModel = [FKBookReform parserBookNormalModelWithDict:response];
-
- if (!viewModel.brandArray.count){
- [self reqBookRecList];
- return;
- }
-
- self.viewModel = viewModel;
- [viewModel saveServeTime];
- self.brandSelectView.titleArray = [viewModel getBrandStrArray];
-
- [self addTapGesture];
- [self configTitleView];
- [self configEmptyView];
- [self.hudView hide:NO];
-
- }else if (identify == BOOK_REC_LIST_REQ){
-
- [self.hudView hide:NO];
- self.viewModel = [FKBookReform parserBookRecModelWithDict:response];
- [self configTitleView];
- [self configEmptyView];
- [self.refreshControl setTopEnabled:NO];
-
- }else if (identify == BOOK_MORE_NORMAL_LIST_REQ){
- FKBookNormalViewModel *viewModel = [FKBookReform parserBookNormalModelWithDict:response];
- [self.viewModel appendMoreItem:viewModel.itemArray];
- }
-
- [self.tableView reloadData];
- [self enableBottomRefresh];
-
- }else{
- [self.hudView hide:NO];
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- }
- }
- - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header{
- [self.hudView hide:NO];
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- }
- #pragma mark - tableView dataSource
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return [self.viewModel numberOfRowsInSection:section];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self.viewModel cellIdentify]];
- // id proItem = [self.viewModel proModelAtIndex:indexPath.row];
- [cell fk_configWithViewModel:self.viewModel indexPath:indexPath];
-
- if ([cell isKindOfClass:[FKBookRecommendCell class]]){
- WeakSelf(weakSelf);
-
- FKBookRecommendCell *recCell = (FKBookRecommendCell *)cell;
- [recCell.bookButton addTarget:self action:@selector(clickRecBookBtn:) forControlEvents:UIControlEventTouchUpInside];
- recCell.goToDetail = ^(NSString *productId){
- [weakSelf pushDetailWithProductId:productId];
- };
- }
-
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- return [self.viewModel heightForRowAtIndexPath:indexPath];
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
- if (self.viewModel){
- return [self.viewModel heightForHeaderInSection:section];
- }
- return CGFLOAT_MIN;
-
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
- if (self.viewModel.contentType == BookContentTypeRecommend){
- return self.tipHeaderView;
- }
- return [UIView new];
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- [self didSelectIndexPath:indexPath];
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
- if (scrollView.tracking){
- [self hiddenBrandSelectView];
- }
- }
- #pragma mark - action
- - (void)clickRightNavItem{
- WeakSelf(weakSelf);
- FKBookManagerController *controller = [[FKBookManagerController alloc] initWithSelectPreferDone:^{
- [weakSelf reqBookListWithBrandId:self.selectedBrandId];
- }];
-
- [self.navigationController pushViewController:controller animated:YES];
- }
- - (void)clickTitleView:(FKBookTitleBtn *)sender{
-
-
- if (self.brandSelectView.superview){
- [self hiddenBrandSelectView];
- return;
- }
-
- sender.selected = !sender.selected;
-
- [self.navigationController.view addSubview:self.brandSelectView];
- [self.brandSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.navigationController.view).offset(56);
- make.centerX.equalTo(self.navigationController.view);
- make.size.mas_equalTo(CGSizeMake(160, 220));
- }];
- }
- - (void)clickRecBookBtn:(UIButton *)sender{
- FKBookRecItem *item = [self.viewModel proModelAtIndex:sender.tag];
- if ([item isKindOfClass:[FKBookRecItem class]]){
- if (sender.selected){
- // 取消订阅
- [FKBookRequest reqBookBrand:item.brandItem.itemID
- isCancel:YES
- identify:BOOK_REC_CANCEL_BOOK
- delegate:self];
- }else{
- // 订阅
- [FKBookRequest reqBookBrand:item.brandItem.itemID
- isCancel:NO
- identify:BOOK_REC_BOOK_BRAND
- delegate:self];
- }
- item.brandItem.didBook = !item.brandItem.didBook;
- [self.tableView reloadData];
- }
- }
- - (void)tapGestureClick:(UIGestureRecognizer *)gesture{
- if (gesture.state == UIGestureRecognizerStateEnded){
- CGPoint point = [gesture locationInView:self.tableView];
- NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
- if (indexPath){
- [self didSelectIndexPath:indexPath];
- }else{
- [self hiddenBrandSelectView];
- }
- }
- }
- - (void)clickEmptyView{
- [self hiddenBrandSelectView];
- }
- #pragma mark - method
- - (void)pushDetailWithProductId:(NSString *)productId{
- if (![FLStringHelper isValidString:productId]) return;
-
- NSString *url = [NSString stringWithFormat:@"firstlinkapp://product.fine3q.com/detail?id=%@&refer=subscribe", productId];
-
- [[SchemaManager sharedManager] parserURL:[NSURL URLWithString:url] shouldCache:NO];
- }
- - (void)hiddenBrandSelectView{
- if (self.brandSelectView.superview){
- self.titleView.selected = NO;
- [self.brandSelectView removeFromSuperview];
- }
- }
- - (void)didSelectIndexPath:(NSIndexPath *)indexPath{
- if (self.brandSelectView.superview){
- [self hiddenBrandSelectView];
- }else{
- if (self.viewModel.contentType == BookContentTypeNormal){
- FKBookProItem *item = [self.viewModel proModelAtIndex:indexPath.row];
- [self pushDetailWithProductId:item.itemID];
- }
- }
-
-
- }
- - (void)configNavBarItem{
-
- UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"管理 " style:UIBarButtonItemStylePlain target:self action:@selector(clickRightNavItem)];
- [rightItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14],
- NSForegroundColorAttributeName : UIColorFromRGB(0x4a4a4a)}
- forState:UIControlStateNormal];
- self.navigationItem.rightBarButtonItem = rightItem;
-
- }
- - (void)configTitleView{
- if (!self.viewModel || self.viewModel.contentType == BookContentTypeRecommend){
- self.navigationItem.title = @"我的订阅";
-
- }else{
- if (!self.navigationItem.titleView){
- self.navigationItem.titleView = self.titleView;
- }
- }
- }
- - (void)configEmptyView{
-
- if (!self.viewModel.itemArray.count){
-
- self.emptyView.hidden = NO;
- self.emptyView.userInteractionEnabled = YES;
-
- [self.view addSubview:self.emptyView];
- self.emptyView.frame = self.view.bounds;
-
- }else{
- self.emptyView.hidden = YES;
- self.emptyView.userInteractionEnabled = NO;
-
- [self.emptyView removeFromSuperview];
- }
- }
- - (void)changeToBrandIndex:(NSInteger)index{
- if (index == - 1){
- // 全部
- self.selectedBrandId = nil;
- [self reqBookListWithBrandId:nil];
- self.titleView.titleLabel.text = @"我的订阅";
- }else if (index >= 0){
- self.selectedBrandId = [self brandIdAtIndex:index];
- [self reqBookListWithBrandId:self.selectedBrandId];
-
- NSString *selectBrandName = [self brandNameAtIndex:index];
- if (![FLStringHelper isValidString:selectBrandName]) selectBrandName = @"我的订阅";
- self.titleView.titleLabel.text = selectBrandName;
- }
-
- [self hiddenBrandSelectView];
- }
- - (NSString *)brandIdAtIndex:(NSUInteger)index{
-
- if ([self.viewModel isKindOfClass:[FKBookNormalViewModel class]]){
- FKBookNormalViewModel *viewModel = (FKBookNormalViewModel *)self.viewModel;
- return [viewModel brandIdAtIndex:index];
- }
-
- return nil;
- }
- - (NSString *)brandNameAtIndex:(NSUInteger)index{
-
- if ([self.viewModel isKindOfClass:[FKBookNormalViewModel class]]){
- FKBookNormalViewModel *viewModel = (FKBookNormalViewModel *)self.viewModel;
- return [viewModel brandNameAtIndex:index];
- }
-
- return nil;
- }
- - (void)addTapGesture{
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick:)];
- [self.tableView addGestureRecognizer:tap];
- }
- #pragma mark - property
- - (UITableView *)tableView{
-
- if (_tableView == nil) {
- _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
- _tableView.dataSource = self;
- _tableView.delegate = self;
-
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- _tableView.backgroundColor = UIColorFromRGB(0xf8f8f8);
-
- [_tableView registerClass:[FKBookRecommendCell class] forCellReuseIdentifier:FK_BOOK_REC_CELL_IDENTIFY];
- [_tableView registerClass:[FKBookProductCell class] forCellReuseIdentifier:FK_BOOK_PRO_CELL_IDENTIFY];
- }
- return _tableView;
- }
- - (UIView *)tipHeaderView{
- if (_tipHeaderView == nil) {
- _tipHeaderView = [[UIView alloc]init];
-
- UILabel *titleLabel = [self createHeaderTipLabel];
-
- [_tipHeaderView addSubview:titleLabel];
- [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(_tipHeaderView);
- }];
- }
- return _tipHeaderView;
- }
- - (UILabel *)createHeaderTipLabel{
-
- UILabel *titleLabel = [[UILabel alloc]init];
- titleLabel.textColor = UIColorFromRGB(0x9b9b9b);
- titleLabel.font = [UIFont systemFontOfSize:14];
- titleLabel.numberOfLines = 0;
- titleLabel.text = @"您还没有订阅\n看看大家订阅了什么";
- titleLabel.textAlignment = NSTextAlignmentCenter;
-
- return titleLabel;
- }
- - (FKBookTitleBtn *)titleView{
- if (_titleView == nil) {
- _titleView = [[FKBookTitleBtn alloc]init];
- _titleView.bounds = CGRectMake(0, 0, 100, 25);
- _titleView.center = CGPointMake(UISCREENWIDTH / 2.0f, 22);
-
- [_titleView addTarget:self action:@selector(clickTitleView:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _titleView;
- }
- - (FKBrandSelectView *)brandSelectView{
- if (_brandSelectView == nil){
-
- WeakSelf(weakSelf);
- _brandSelectView = [[FKBrandSelectView alloc]initWithAction:^(NSInteger index) {
- [weakSelf changeToBrandIndex:index];
- }];
- }
- return _brandSelectView;
- }
- - (FKExploreListEmptyView *)emptyView {
- if (!_emptyView) {
- _emptyView = [FKExploreListEmptyView new];
- _emptyView.backgroundColor = UIColorFromRGB(0xf4f5f7);
- _emptyView.hidden = YES;
- _emptyView.titleLabel.text = @"啊哦~你订阅的品牌暂时没有推送哦";
-
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickEmptyView)];
- [_emptyView addGestureRecognizer:tap];
- }
- return _emptyView;
- }
- @end
|