// // DRTimeLineViewController.m // YouHuiProject // // Created by 小花 on 2018/12/27. // Copyright © 2018年 kuxuan. All rights reserved. // #import "DRTimeLineViewController.h" #import "DRTimeLineTableCell.h" #import "UIView+SDAutoLayout.h" #import "UITableView+SDAutoTableViewCellHeight.h" #import "DRShareGoodViewController.h" #import "DRTimeLineChannelModel.h" #import "DRTimeLineTopView.h" #import "DRGoodDetailViewController.h" #import "DRShareRequestViewModel.h" #import "DRShareRequestViewModel.h" #import "DRLoginViewController.h" #import "DRTimeLineRecomCell.h" #import "DRTimeLineShareView.h" #import "DRShareManager.h" #import #import #import @interface DRTimeLineViewController () { NSInteger _page; ActivityIndicatorView *_indicatorView; } @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataArr; @property (nonatomic, assign) NSInteger page; @property (nonatomic, strong) DRTimeLineTopView *topHeader; @end @implementation DRTimeLineViewController - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [LoadingView dismiss]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; } - (void)viewDidLoad { [super viewDidLoad]; [self configTableView]; [self configNavigationBar]; [self requestData]; } - (void)configTableView { _page = 1; [self.view addSubview:self.tableView]; self.tableView.tableHeaderView = self.topHeader; if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:ChangeSex object:nil]; 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)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)}; [DRHttp post:url params:para success:^(id json) { if ([self.tableView.mj_header isRefreshing]) { [self.dataArr removeAllObjects]; } NSArray *list = [NSArray yy_modelArrayWithClass:[DRTimeLineModel 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]; [DRHttp post:url params:nil success:^(id json) { NSArray *list = [NSArray yy_modelArrayWithClass:[DRTimeLineChannelModel class] json:json]; [self.topHeader setData:list]; } failure:^(NSError *error) { }]; } #pragma ----private - (void)shareWithModel:(DRTimeLineModel *)model indexPath:(NSIndexPath *)indexPath{ if (![AccountTool isLogin]) { DRLoginViewController *login = [[DRLoginViewController alloc] init]; [self presentViewController:login animated:YES completion:nil]; return; } 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]; [LoadingView show]; [DRShareRequestViewModel requestGoodDetailParamGoods_id:requestModel success:^(DRShareInfoModel * _Nonnull infoModel, NSDictionary * _Nonnull dic) { [LoadingView dismiss]; DRShareGoodViewController *shareVc = [[DRShareGoodViewController alloc] init]; shareVc.goodModel = infoModel; shareVc.shareResult = ^{ [self shareRequest:model indexPath:indexPath]; }; [self.navigationController pushViewController:shareVc animated:YES]; } failure:^(NSError * _Nonnull error) { [LoadingView dismiss]; }]; } - (void)shareRequest:(DRTimeLineModel *)model indexPath:(NSIndexPath *)indexPath { NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/youXuanShareUser",BaseURL]; NSDictionary *para = @{@"goods_id":model.goods_id,@"type":model.type}; [DRHttp 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:(DRTimeLineModel *)model indexPath:(NSIndexPath *)indexPath{ DRTimeLineShareView *shareView = [[DRTimeLineShareView 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 DRTimeLineModel *)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:(DRTimeLineModel *)model indexPath:(NSIndexPath *)inexPath{ if (platformType == UMSocialPlatformType_WechatTimeLine) { [MobClick event:EveryDayMorningCircleShare]; }else { [MobClick event:EveryDayMorningFriendShare]; } [self shareRequest:model indexPath:inexPath]; if (model.img) { [DRShareManager shareImageWithplatformType:platformType withImg:model.img complete:^(id result, NSError *error) { }]; } } - (void)saveImage:(DRTimeLineModel *)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 { DRTimeLineModel *model = self.dataArr[indexPath.row]; switch ([model.type integerValue]) { case 1: { DRTimeLineTableCell *cell = [DRTimeLineTableCell cellWithTableView:tableView]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.shareClickBlock = ^{ [self shareWithModel:model indexPath:indexPath]; [MobClick event:OptimizingCircleShareClick]; }; cell.tapBlock = ^{ DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithLDTimeLineModel:model]; DRGoodDetailViewController *detail = [[DRGoodDetailViewController alloc] init]; detail.requestModel = requestModel; [self.navigationController pushViewController:detail animated:YES]; [MobClick event:OptimizingCircleListClick]; }; cell.model = model; return cell; } break; case 2: { DRTimeLineRecomCell *cell = [DRTimeLineRecomCell 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: { DRTimeLineTableCell *cell = [DRTimeLineTableCell cellWithTableView:tableView]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.shareClickBlock = ^{ [self shareWithModel:model indexPath:indexPath]; }; cell.model = model; return cell; } break; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { DRTimeLineModel *model = self.dataArr[indexPath.row]; switch ([model.type integerValue]) { case 1: return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[DRTimeLineTableCell class] contentViewWidth:SCREEN_WIDTH]; break; case 2: return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[DRTimeLineRecomCell class] contentViewWidth:SCREEN_WIDTH]; break; default: return 0; break; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // DRTimeLineModel *model = self.dataArr[indexPath.row]; // if (model.type.integerValue == 1) { // 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]; // DRGoodDetailViewController *detail = [[DRGoodDetailViewController alloc] init]; // detail.requestModel = requestModel; // [self.navigationController pushViewController:detail 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]; }]; _tableView.mj_footer = footer; } return _tableView; } - (NSMutableArray *)dataArr { if (!_dataArr) { _dataArr = [NSMutableArray array]; } return _dataArr; } - (DRTimeLineTopView *)topHeader { if (!_topHeader) { _topHeader = [[DRTimeLineTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 133)]; } return _topHeader; } @end