《省钱达人》与《猎豆优选》UI相同版。域名tbk

DRTimeLineViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // DRTimeLineViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/12/27.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRTimeLineViewController.h"
  9. #import "DRTimeLineTableCell.h"
  10. #import "UIView+SDAutoLayout.h"
  11. #import "UITableView+SDAutoTableViewCellHeight.h"
  12. #import "DRShareGoodViewController.h"
  13. #import "DRTimeLineChannelModel.h"
  14. #import "DRTimeLineTopView.h"
  15. #import "DRGoodDetailViewController.h"
  16. #import "DRShareRequestViewModel.h"
  17. #import "DRShareRequestViewModel.h"
  18. #import "DRLoginViewController.h"
  19. #import "DRTimeLineRecomCell.h"
  20. #import "DRTimeLineShareView.h"
  21. #import "DRShareManager.h"
  22. #import <AssetsLibrary/AssetsLibrary.h>
  23. #import <AssetsLibrary/AssetsLibrary.h>
  24. #import <Photos/Photos.h>
  25. @interface DRTimeLineViewController ()<UITableViewDelegate,UITableViewDataSource,TimeLineShareViewDelegate>
  26. {
  27. NSInteger _page;
  28. ActivityIndicatorView *_indicatorView;
  29. }
  30. @property (nonatomic, strong) UITableView *tableView;
  31. @property (nonatomic, strong) NSMutableArray *dataArr;
  32. @property (nonatomic, assign) NSInteger page;
  33. @property (nonatomic, strong) DRTimeLineTopView *topHeader;
  34. @end
  35. @implementation DRTimeLineViewController
  36. - (void)viewWillDisappear:(BOOL)animated {
  37. [super viewWillDisappear:animated];
  38. [LoadingView dismiss];
  39. }
  40. - (void)viewDidAppear:(BOOL)animated {
  41. [super viewDidAppear:animated];
  42. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  43. }
  44. - (void)viewDidLoad {
  45. [super viewDidLoad];
  46. [self configTableView];
  47. [self configNavigationBar];
  48. [self requestData];
  49. }
  50. - (void)configTableView {
  51. _page = 1;
  52. [self.view addSubview:self.tableView];
  53. self.tableView.tableHeaderView = self.topHeader;
  54. if (@available(iOS 11.0, *)) {
  55. self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  56. } else {
  57. self.automaticallyAdjustsScrollViewInsets = NO;
  58. }
  59. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:ChangeSex object:nil];
  60. ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)];
  61. _indicatorView = indicatorView;
  62. }
  63. - (void)configNavigationBar{
  64. [self.navigationBar setNavTitle:@"优选圈"];
  65. self.view.backgroundColor = [UIColor whiteColor];
  66. self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
  67. self.navigationBar.backgroundColor = [UIColor changeColor];
  68. }
  69. - (void)refreshData {
  70. [self.tableView.mj_header beginRefreshing];
  71. }
  72. - (void)requestData {
  73. [self loadTimelineData];
  74. [self loadChannelHeaderData];
  75. }
  76. - (void)loadTimelineData {
  77. NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/youXuanGoodsList",BaseURL];
  78. NSDictionary *para = @{@"page":@(_page)};
  79. [DRHttp post:url params:para success:^(id json) {
  80. if ([self.tableView.mj_header isRefreshing]) {
  81. [self.dataArr removeAllObjects];
  82. }
  83. NSArray *list = [NSArray yy_modelArrayWithClass:[DRTimeLineModel class] json:json];
  84. [self.dataArr addObjectsFromArray:list];
  85. if (list.count == 0) {
  86. [self.tableView.mj_footer endRefreshingWithNoMoreData];
  87. }else {
  88. [self.tableView.mj_footer endRefreshing];
  89. }
  90. [self.tableView.mj_header endRefreshing];
  91. [self.tableView reloadData];
  92. [_indicatorView stopAnimating];
  93. } failure:^(NSError *error) {
  94. [self.tableView.mj_header endRefreshing];
  95. [self.tableView.mj_footer endRefreshing];
  96. [_indicatorView stopAnimating];
  97. }];
  98. }
  99. - (void)loadChannelHeaderData {
  100. NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/labelList",BaseURL];
  101. [DRHttp post:url params:nil success:^(id json) {
  102. NSArray *list = [NSArray yy_modelArrayWithClass:[DRTimeLineChannelModel class] json:json];
  103. [self.topHeader setData:list];
  104. } failure:^(NSError *error) {
  105. }];
  106. }
  107. #pragma ----private
  108. - (void)shareWithModel:(DRTimeLineModel *)model indexPath:(NSIndexPath *)indexPath{
  109. if (![AccountTool isLogin]) {
  110. DRLoginViewController *login = [[DRLoginViewController alloc] init];
  111. [self presentViewController:login animated:YES completion:nil];
  112. return;
  113. }
  114. 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];
  115. [LoadingView show];
  116. [DRShareRequestViewModel requestGoodDetailParamGoods_id:requestModel success:^(DRShareInfoModel * _Nonnull infoModel, NSDictionary * _Nonnull dic) {
  117. [LoadingView dismiss];
  118. DRShareGoodViewController *shareVc = [[DRShareGoodViewController alloc] init];
  119. shareVc.goodModel = infoModel;
  120. shareVc.shareResult = ^{
  121. [self shareRequest:model indexPath:indexPath];
  122. };
  123. [self.navigationController pushViewController:shareVc animated:YES];
  124. } failure:^(NSError * _Nonnull error) {
  125. [LoadingView dismiss];
  126. }];
  127. }
  128. - (void)shareRequest:(DRTimeLineModel *)model indexPath:(NSIndexPath *)indexPath {
  129. NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/youXuanShareUser",BaseURL];
  130. NSDictionary *para = @{@"goods_id":model.goods_id,@"type":model.type};
  131. [DRHttp post:url params:para success:^(id json) {
  132. NSNumber *flag = json[@"flag"];
  133. if (flag.boolValue) {
  134. NSMutableArray *arr = [NSMutableArray array];
  135. [arr addObjectsFromArray:model.shareUser];
  136. NSString *nick = [AccountTool account].nickname;
  137. if (nick.length == 0) {
  138. nick = @"我";
  139. }
  140. if ([arr containsObject:nick]) {
  141. [arr removeObject:nick];
  142. }
  143. [arr addObject:nick];
  144. model.shareUser = arr;
  145. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  146. }
  147. } failure:^(NSError *error) {
  148. }];
  149. }
  150. - (void)showShareView:(DRTimeLineModel *)model indexPath:(NSIndexPath *)indexPath{
  151. DRTimeLineShareView *shareView = [[DRTimeLineShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  152. shareView.delegate = self;
  153. shareView.model = model;
  154. shareView.indexPath = indexPath;
  155. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  156. [shareView showInView:window];
  157. }
  158. #pragma mark --- timelineDelegate ---
  159. - (void)timelineShareViewDidSeledtedIndex:(NSInteger)index model:(nonnull DRTimeLineModel *)model indexPath:(nonnull NSIndexPath *)indexPath{
  160. switch (index) {
  161. case 0:
  162. [self shareImgToWeChat:UMSocialPlatformType_WechatTimeLine model:model indexPath:indexPath];
  163. break;
  164. case 1:
  165. [self shareImgToWeChat:UMSocialPlatformType_WechatSession model:model indexPath:indexPath];
  166. break;
  167. case 2:
  168. [self saveImage:model];
  169. break;
  170. default:
  171. break;
  172. }
  173. }
  174. /**
  175. 分享
  176. */
  177. - (void)shareImgToWeChat:(UMSocialPlatformType)platformType model:(DRTimeLineModel *)model indexPath:(NSIndexPath *)inexPath{
  178. if (platformType == UMSocialPlatformType_WechatTimeLine) {
  179. [MobClick event:EveryDayMorningCircleShare];
  180. }else {
  181. [MobClick event:EveryDayMorningFriendShare];
  182. }
  183. [self shareRequest:model indexPath:inexPath];
  184. if (model.img) {
  185. [DRShareManager shareImageWithplatformType:platformType withImg:model.img complete:^(id result, NSError *error) {
  186. }];
  187. }
  188. }
  189. - (void)saveImage:(DRTimeLineModel *)model {
  190. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.img] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
  191. __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
  192. [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
  193. if (error) {
  194. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
  195. [alert show];
  196. }else {
  197. [XHToast showCenterWithText:@"保存成功"];
  198. }
  199. lib = nil;
  200. }];
  201. }];
  202. }
  203. #pragma mark ============ UITableView Delegate && DataSource ==========
  204. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  205. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  206. cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  207. }
  208. }
  209. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  210. return 0.1;
  211. }
  212. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  213. return self.dataArr.count;
  214. }
  215. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  216. DRTimeLineModel *model = self.dataArr[indexPath.row];
  217. switch ([model.type integerValue]) {
  218. case 1:
  219. {
  220. DRTimeLineTableCell *cell = [DRTimeLineTableCell cellWithTableView:tableView];
  221. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  222. cell.shareClickBlock = ^{
  223. [self shareWithModel:model indexPath:indexPath];
  224. [MobClick event:OptimizingCircleShareClick];
  225. };
  226. cell.tapBlock = ^{
  227. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithLDTimeLineModel:model];
  228. DRGoodDetailViewController *detail = [[DRGoodDetailViewController alloc] init];
  229. detail.requestModel = requestModel;
  230. [self.navigationController pushViewController:detail animated:YES];
  231. [MobClick event:OptimizingCircleListClick];
  232. };
  233. cell.model = model;
  234. return cell;
  235. }
  236. break;
  237. case 2:
  238. {
  239. DRTimeLineRecomCell *cell = [DRTimeLineRecomCell cellWithTableView:tableView];
  240. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  241. cell.model = model;
  242. cell.shareClickBlock = ^{
  243. [self showShareView:model indexPath:indexPath];
  244. [MobClick event:OptimizingCircleShareClick label:model.img];
  245. };
  246. return cell;
  247. }
  248. break;
  249. default:
  250. {
  251. DRTimeLineTableCell *cell = [DRTimeLineTableCell cellWithTableView:tableView];
  252. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  253. cell.shareClickBlock = ^{
  254. [self shareWithModel:model indexPath:indexPath];
  255. };
  256. cell.model = model;
  257. return cell;
  258. }
  259. break;
  260. }
  261. }
  262. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  263. DRTimeLineModel *model = self.dataArr[indexPath.row];
  264. switch ([model.type integerValue]) {
  265. case 1:
  266. return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[DRTimeLineTableCell class] contentViewWidth:SCREEN_WIDTH];
  267. break;
  268. case 2:
  269. return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[DRTimeLineRecomCell class] contentViewWidth:SCREEN_WIDTH];
  270. break;
  271. default:
  272. return 0;
  273. break;
  274. }
  275. }
  276. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  277. // DRTimeLineModel *model = self.dataArr[indexPath.row];
  278. // if (model.type.integerValue == 1) {
  279. // 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];
  280. // DRGoodDetailViewController *detail = [[DRGoodDetailViewController alloc] init];
  281. // detail.requestModel = requestModel;
  282. // [self.navigationController pushViewController:detail animated:YES];
  283. // }
  284. }
  285. #pragma mark ------- layzer ---------
  286. - (UITableView *)tableView {
  287. if (!_tableView) {
  288. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight) style:UITableViewStyleGrouped];
  289. _tableView.estimatedSectionHeaderHeight = 0;
  290. _tableView.estimatedSectionFooterHeight = 0;
  291. _tableView.sectionFooterHeight = 0;
  292. _tableView.sectionHeaderHeight = 0;
  293. _tableView.estimatedRowHeight = 0;
  294. _tableView.delegate = self;
  295. _tableView.dataSource = self;
  296. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  297. _tableView.backgroundColor = [UIColor whiteColor];
  298. _tableView.bounces = YES;
  299. _tableView.showsVerticalScrollIndicator = NO;
  300. _tableView.separatorColor = [UIColor lineColor];
  301. _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  302. _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  303. self.page = 1;
  304. [_tableView.mj_footer resetNoMoreData];
  305. [self requestData];
  306. }];
  307. MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  308. _page++;
  309. [self loadTimelineData];
  310. }];
  311. _tableView.mj_footer = footer;
  312. }
  313. return _tableView;
  314. }
  315. - (NSMutableArray *)dataArr {
  316. if (!_dataArr) {
  317. _dataArr = [NSMutableArray array];
  318. }
  319. return _dataArr;
  320. }
  321. - (DRTimeLineTopView *)topHeader {
  322. if (!_topHeader) {
  323. _topHeader = [[DRTimeLineTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 133)];
  324. }
  325. return _topHeader;
  326. }
  327. @end