// // LDTimeLineViewController.m // YouHuiProject // // Created by 小花 on 2018/12/27. // Copyright © 2018年 kuxuan. All rights reserved. // #import "LDTimeLineViewController.h" #import "LDTimeLineTableCell.h" #import "UIView+SDAutoLayout.h" #import "UITableView+SDAutoTableViewCellHeight.h" #import "LDShareGoodViewController.h" #import "LDTimeLineChannelModel.h" #import "LDTimeLineTopView.h" #import "LDShareRequestViewModel.h" #import "LDShareRequestViewModel.h" #import "LDLoginViewController.h" #import "LDTimeLineRecomCell.h" #import "LDTimeLineShareView.h" #import "LDShareManager.h" #import #import #import #import "LDTimeLineSeconeListVc.h" #import #import #import "LDTaobaoWebAuthorController.h" @interface LDTimeLineViewController () { NSInteger _page; ActivityIndicatorView *_indicatorView; } @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataArr; @property (nonatomic, assign) NSInteger page; @property (nonatomic, strong) LDTimeLineTopView *topHeader; @end @implementation LDTimeLineViewController - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [LoadingView dismiss]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidLoad { [super viewDidLoad]; [self configTableView]; [self configNavigationBar]; [self requestData]; [self addRefreshNotifacation]; } - (void)configTableView { _page = 1; [self.view addSubview:self.tableView]; if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)]; _indicatorView = indicatorView; } - (void)configNavigationBar{ [self.navigationBar setNavTitle:@"优选圈"]; self.view.backgroundColor = [UIColor whiteColor]; self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor]; self.navigationBar.backgroundColor = [UIColor changeColor]; } - (void)addRefreshNotifacation { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:ChangeSex object:nil]; } - (void)refreshData { [self.tableView.mj_header beginRefreshing]; } - (void)requestData { [self loadTimelineData]; [self loadChannelHeaderData]; } - (void)loadTimelineData { NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/youXuanGoodsList",BaseURL]; NSDictionary *para = @{@"page":@(_page)}; [LDHttp post:url params:para success:^(id json) { if ([self.tableView.mj_header isRefreshing]) { [self.dataArr removeAllObjects]; } NSArray *list = [NSArray yy_modelArrayWithClass:[LDTimeLineModel class] json:json]; [self.dataArr addObjectsFromArray:list]; if (list.count == 0) { [self.tableView.mj_footer endRefreshingWithNoMoreData]; }else { [self.tableView.mj_footer endRefreshing]; } [self.tableView.mj_header endRefreshing]; [self.tableView reloadData]; [_indicatorView stopAnimating]; } failure:^(NSError *error) { [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshing]; [_indicatorView stopAnimating]; }]; } - (void)loadChannelHeaderData { NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/labelList",BaseURL]; [LDHttp post:url params:nil success:^(id json) { NSArray *list = [NSArray yy_modelArrayWithClass:[LDTimeLineChannelModel class] json:json]; if (list.count ==0) { self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)]; }else{ self.tableView.tableHeaderView = self.topHeader; [self.topHeader setData:list]; } } failure:^(NSError *error) { }]; } #pragma ----private - (void)shareWithModel:(LDTimeLineModel *)model indexPath:(NSIndexPath *)indexPath{ if (![AccountTool isLogin]) { LDNewLoginVC *login = [[LDNewLoginVC alloc] init]; login.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:login animated:YES completion:nil]; return; } NSString *url = [NSString stringWithFormat:@"%@/api/v2/relationAuth/isAuthOrNo",BaseURL]; [LDHttp post:url params:nil success:^(id json) { NSNumber *flag = json[@"flag"]; if (flag.boolValue) { //已授权 直接分享 DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id is_coupon:model.is_coupon coupon_price:model.coupon_price price:model.price discount_price:model.discount_price commission_rate:model.commission_rate coupon_start_time:model.coupon_start_time coupon_end_time:model.coupon_end_time coupon_id:model.coupon_id]; [LoadingView show]; [LDShareRequestViewModel requestGoodDetailParamGoods_id:requestModel success:^(LDShareInfoModel * _Nonnull infoModel, NSDictionary * _Nonnull dic) { [LoadingView dismiss]; LDShareGoodViewController *shareVc = [[LDShareGoodViewController alloc] init]; shareVc.goodModel = infoModel; shareVc.shareResult = ^{ [self shareRequest:model indexPath:indexPath]; }; [self.navigationController pushViewController:shareVc animated:YES]; } failure:^(NSError * _Nonnull error) { [LoadingView dismiss]; }]; }else { //未授权 去授权 [LDTaobaoAuthorTool taobaoAuthorManager:json[@"auth_url"] type:@"1" currentVc:self]; } } failure:^(NSError *error) { }]; } - (void)shareRequest:(LDTimeLineModel *)model indexPath:(NSIndexPath *)indexPath { NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/youXuanShareUser",BaseURL]; NSDictionary *para = @{@"goods_id":model.goods_id,@"type":model.type}; [LDHttp post:url params:para success:^(id json) { NSNumber *flag = json[@"flag"]; if (flag.boolValue) { NSMutableArray *arr = [NSMutableArray array]; [arr addObjectsFromArray:model.shareUser]; NSString *nick = [AccountTool account].nickname; if (nick.length == 0) { nick = @"我"; } if ([arr containsObject:nick]) { [arr removeObject:nick]; } [arr addObject:nick]; model.shareUser = arr; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; } } failure:^(NSError *error) { }]; } - (void)showShareView:(LDTimeLineModel *)model indexPath:(NSIndexPath *)indexPath{ LDTimeLineShareView *shareView = [[LDTimeLineShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; shareView.delegate = self; shareView.model = model; shareView.indexPath = indexPath; UIWindow *window = [UIApplication sharedApplication].keyWindow; [shareView showInView:window]; } #pragma mark --- timelineDelegate --- - (void)timelineShareViewDidSeledtedIndex:(NSInteger)index model:(nonnull LDTimeLineModel *)model indexPath:(nonnull NSIndexPath *)indexPath{ switch (index) { case 0: [self shareImgToWeChat:UMSocialPlatformType_WechatTimeLine model:model indexPath:indexPath]; break; case 1: [self shareImgToWeChat:UMSocialPlatformType_WechatSession model:model indexPath:indexPath]; break; case 2: [self saveImage:model]; break; default: break; } } /** 分享 */ - (void)shareImgToWeChat:(UMSocialPlatformType)platformType model:(LDTimeLineModel *)model indexPath:(NSIndexPath *)inexPath{ if (platformType == UMSocialPlatformType_WechatTimeLine) { [MobClick event:EveryDayMorningCircleShare]; }else { [MobClick event:EveryDayMorningFriendShare]; } [self shareRequest:model indexPath:inexPath]; if (model.img) { [LDShareManager shareImageWithplatformType:platformType withImg:model.img complete:^(id result, NSError *error) { }]; } } - (void)saveImage:(LDTimeLineModel *)model { [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.img] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init]; [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { if (error) { UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil]; [alert show]; }else { [XHToast showCenterWithText:@"保存成功"]; } lib = nil; }]; }]; } #pragma mark ============ UITableView Delegate && DataSource ========== - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { LDTimeLineModel *model = self.dataArr[indexPath.row]; switch ([model.type integerValue]) {//列表 case 1: { LDTimeLineTableCell *cell = [LDTimeLineTableCell cellWithTableView:tableView]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.shareClickBlock = ^{ [self shareWithModel:model indexPath:indexPath]; [MobClick event:OptimizingCircleShareClick]; }; cell.tapBlock = ^{ DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithLDTimeLineModel:model]; LDGoodDetailViewController *detail = [[LDGoodDetailViewController alloc] init]; detail.requestModel = requestModel; [self.navigationController pushViewController:detail animated:YES]; [MobClick event:OptimizingCircleListClick]; }; cell.clickBtn = ^(LDTimeLineModel * _Nonnull models) { LDTimeLineChannelModel*listModel =[[LDTimeLineChannelModel alloc]init]; listModel.Id=models.label_id; listModel.title=models.subscribe_title; [self clickModel:listModel]; }; cell.model = model; return cell; } break; case 2: { LDTimeLineRecomCell *cell = [LDTimeLineRecomCell cellWithTableView:tableView]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.model = model; cell.shareClickBlock = ^{ [self showShareView:model indexPath:indexPath]; [MobClick event:OptimizingCircleShareClick label:model.img]; }; return cell; } break; default: { LDTimeLineTableCell *cell = [LDTimeLineTableCell cellWithTableView:tableView]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.shareClickBlock = ^{ [self shareWithModel:model indexPath:indexPath]; }; cell.clickBtn = ^(LDTimeLineModel * _Nonnull models) { LDTimeLineChannelModel*listModel =[[LDTimeLineChannelModel alloc]init]; listModel.Id=models.label_id; listModel.title=models.subscribe_title; [self clickModel:listModel]; }; cell.model = model; return cell; } break; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { LDTimeLineModel *model = self.dataArr[indexPath.row]; switch ([model.type integerValue]) { case 1: NSLog(@"%.2f",[self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[LDTimeLineTableCell class] contentViewWidth:SCREEN_WIDTH]); return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[LDTimeLineTableCell class] contentViewWidth:SCREEN_WIDTH]; break; case 2: return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[LDTimeLineRecomCell class] contentViewWidth:SCREEN_WIDTH]; break; default: return 0; break; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // LDTimeLineModel *models =self.dataArr[indexPath.row]; // LDTimeLineChannelModel*listModel =[[LDTimeLineChannelModel alloc]init]; // listModel.Id=models.label_id; // listModel.title=models.subscribe_title; // [self clickModel:listModel]; } #pragma mark------LDTimeLineTopViewDelegate -(void)clickModel:(LDTimeLineChannelModel *)model { LDTimeLineSeconeListVc *listV =[[LDTimeLineSeconeListVc alloc]init]; listV.model=model; [self.navigationController pushViewController:listV animated:YES]; } #pragma mark ------- layzer --------- - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight) style:UITableViewStyleGrouped]; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.sectionFooterHeight = 0; _tableView.sectionHeaderHeight = 0; _tableView.estimatedRowHeight = 0; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; _tableView.backgroundColor = [UIColor whiteColor]; _tableView.bounces = YES; _tableView.showsVerticalScrollIndicator = NO; _tableView.separatorColor = [UIColor lineColor]; _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ self.page = 1; [_tableView.mj_footer resetNoMoreData]; [self requestData]; }]; MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ _page++; [self loadTimelineData]; }]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; }else { self.automaticallyAdjustsScrollViewInsets = NO; } _tableView.mj_footer = footer; } return _tableView; } - (NSMutableArray *)dataArr { if (!_dataArr) { _dataArr = [NSMutableArray array]; } return _dataArr; } - (LDTimeLineTopView *)topHeader { if (!_topHeader) { _topHeader = [[LDTimeLineTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 133)]; _topHeader.delegate=self; } return _topHeader; } @end