Açıklama Yok

FKMyBookController.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. //
  2. // FKMyBookController.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/4/28.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKMyBookController.h"
  9. #import "FKProDetailController.h"
  10. #import "FKBookManagerController.h"
  11. #import "FKBookRecommendCell.h"
  12. #import "FKBookProductCell.h"
  13. #import "FKBookTitleBtn.h"
  14. #import "FKBrandSelectView.h"
  15. #import "FKExploreListEmptyView.h"
  16. #import "FKBookRequest.h"
  17. #import "FKBookReform.h"
  18. #import "FKBookNormalViewModel.h"
  19. #import "FKBookRecViewModel.h"
  20. #import "FKBookRecItem.h"
  21. #import "FKBookProItem.h"
  22. #import "UITableViewCell+MyBook.h"
  23. #import "FKMyBookDataSource.h"
  24. #import "SchemaManager.h"
  25. #define BOOK_NORMAL_LIST_REQ 701
  26. #define BOOK_MORE_NORMAL_LIST_REQ 702
  27. #define BOOK_REC_LIST_REQ 703
  28. #define BOOK_REC_BOOK_BRAND 704
  29. #define BOOK_REC_CANCEL_BOOK 705
  30. @interface FKMyBookController () <UITableViewDataSource, UITableViewDelegate, FLNetworkDelegate>
  31. @property (nonatomic, strong) id <FKMyBookDataSource> viewModel;
  32. @property (nonatomic, strong) UIView *tipHeaderView;
  33. @property (nonatomic, strong) UITableView *tableView;
  34. @property (nonatomic, strong) FKBookTitleBtn *titleView;
  35. @property (nonatomic, strong) FKBrandSelectView *brandSelectView;
  36. @property (nonatomic, strong) FKExploreListEmptyView *emptyView;
  37. @property (nonatomic, strong) NSString *selectedBrandId;
  38. @end
  39. @implementation FKMyBookController
  40. - (void)viewDidLoad{
  41. [super viewDidLoad];
  42. [self addAllSubviews];
  43. [self configNavBarItem];
  44. [self reqBookListWithBrandId:nil];
  45. [self addRefreshControl];
  46. }
  47. - (void)viewWillAppear:(BOOL)animated{
  48. [super viewWillAppear:animated];
  49. [self.navigationController setNavigationBarHidden:NO animated:YES];
  50. [self configTitleView];
  51. }
  52. //- (void)viewDidAppear:(BOOL)animated{
  53. // [super viewDidAppear:animated];
  54. //}
  55. - (void)viewWillDisappear:(BOOL)animated{
  56. [super viewWillDisappear:animated];
  57. [self hiddenBrandSelectView];
  58. self.navigationItem.titleView = nil;
  59. }
  60. - (void)addAllSubviews{
  61. [self.view addSubview:self.emptyView];
  62. [self.view addSubview:self.tableView];
  63. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.edges.insets(UIEdgeInsetsZero);
  65. }];
  66. [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.edges.insets(UIEdgeInsetsZero);
  68. }];
  69. }
  70. - (void)addRefreshControl{
  71. [self initRefreshControlWithTableView:self.tableView];
  72. }
  73. - (void)enableBottomRefresh{
  74. [self.refreshControl setBottomEnabled:self.viewModel.enableBottomRefresh];
  75. }
  76. #pragma mark - refresh delegate
  77. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction{
  78. if (direction == RefreshDirectionTop) {
  79. [FKBookRequest reqBookListWithIdentify:BOOK_NORMAL_LIST_REQ
  80. brandID:self.selectedBrandId
  81. startRow:0
  82. serveTime:nil
  83. delegate:self];
  84. }else if (direction == RefreshDirectionBottom){
  85. [self reqMoreBookWithBrandId:self.selectedBrandId];
  86. }
  87. }
  88. #pragma mark - request
  89. - (void)reqBookListWithBrandId:(NSString *)brandID{
  90. [self.hudView show:YES];
  91. [FKBookRequest reqBookListWithIdentify:BOOK_NORMAL_LIST_REQ
  92. brandID:brandID
  93. startRow:0
  94. serveTime:nil
  95. delegate:self];
  96. }
  97. - (void)reqBookRecList{
  98. [self.hudView show:YES];
  99. [FKBookRequest reqRecBookListWithIdentify:BOOK_REC_LIST_REQ delegate:self];
  100. }
  101. - (void)reqMoreBookWithBrandId:(NSString *)brandID{
  102. if ([self.viewModel isKindOfClass:[FKBookNormalViewModel class]]){
  103. FKBookNormalViewModel *normalModel = (FKBookNormalViewModel *)self.viewModel;
  104. [FKBookRequest reqBookListWithIdentify:BOOK_MORE_NORMAL_LIST_REQ
  105. brandID:brandID
  106. startRow:self.viewModel.itemArray.count
  107. serveTime:normalModel.serveTime
  108. delegate:self];
  109. }
  110. }
  111. #pragma mark - response
  112. - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header {
  113. [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
  114. [self.refreshControl finishRefreshingDirection:RefreshDirectionBottom];
  115. if (header.code.intValue == RESPONSE_MSG_NORMAL) {
  116. if (identify == BOOK_NORMAL_LIST_REQ){
  117. FKBookNormalViewModel *viewModel = [FKBookReform parserBookNormalModelWithDict:response];
  118. if (!viewModel.brandArray.count){
  119. [self reqBookRecList];
  120. return;
  121. }
  122. self.viewModel = viewModel;
  123. [viewModel saveServeTime];
  124. self.brandSelectView.titleArray = [viewModel getBrandStrArray];
  125. [self addTapGesture];
  126. [self configTitleView];
  127. [self configEmptyView];
  128. [self.hudView hide:NO];
  129. }else if (identify == BOOK_REC_LIST_REQ){
  130. [self.hudView hide:NO];
  131. self.viewModel = [FKBookReform parserBookRecModelWithDict:response];
  132. [self configTitleView];
  133. [self configEmptyView];
  134. [self.refreshControl setTopEnabled:NO];
  135. }else if (identify == BOOK_MORE_NORMAL_LIST_REQ){
  136. FKBookNormalViewModel *viewModel = [FKBookReform parserBookNormalModelWithDict:response];
  137. [self.viewModel appendMoreItem:viewModel.itemArray];
  138. }
  139. [self.tableView reloadData];
  140. [self enableBottomRefresh];
  141. }else{
  142. [self.hudView hide:NO];
  143. [FLProgressHUDHelper showText:header.msg inView:self.view];
  144. }
  145. }
  146. - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header{
  147. [self.hudView hide:NO];
  148. [FLProgressHUDHelper showText:header.msg inView:self.view];
  149. }
  150. #pragma mark - tableView dataSource
  151. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  152. return [self.viewModel numberOfRowsInSection:section];
  153. }
  154. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  155. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self.viewModel cellIdentify]];
  156. // id proItem = [self.viewModel proModelAtIndex:indexPath.row];
  157. [cell fk_configWithViewModel:self.viewModel indexPath:indexPath];
  158. if ([cell isKindOfClass:[FKBookRecommendCell class]]){
  159. WeakSelf(weakSelf);
  160. FKBookRecommendCell *recCell = (FKBookRecommendCell *)cell;
  161. [recCell.bookButton addTarget:self action:@selector(clickRecBookBtn:) forControlEvents:UIControlEventTouchUpInside];
  162. recCell.goToDetail = ^(NSString *productId){
  163. [weakSelf pushDetailWithProductId:productId];
  164. };
  165. }
  166. return cell;
  167. }
  168. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  169. return [self.viewModel heightForRowAtIndexPath:indexPath];
  170. }
  171. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  172. if (self.viewModel){
  173. return [self.viewModel heightForHeaderInSection:section];
  174. }
  175. return CGFLOAT_MIN;
  176. }
  177. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  178. if (self.viewModel.contentType == BookContentTypeRecommend){
  179. return self.tipHeaderView;
  180. }
  181. return [UIView new];
  182. }
  183. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  184. [self didSelectIndexPath:indexPath];
  185. }
  186. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  187. if (scrollView.tracking){
  188. [self hiddenBrandSelectView];
  189. }
  190. }
  191. #pragma mark - action
  192. - (void)clickRightNavItem{
  193. WeakSelf(weakSelf);
  194. FKBookManagerController *controller = [[FKBookManagerController alloc] initWithSelectPreferDone:^{
  195. [weakSelf reqBookListWithBrandId:self.selectedBrandId];
  196. }];
  197. [self.navigationController pushViewController:controller animated:YES];
  198. }
  199. - (void)clickTitleView:(FKBookTitleBtn *)sender{
  200. if (self.brandSelectView.superview){
  201. [self hiddenBrandSelectView];
  202. return;
  203. }
  204. sender.selected = !sender.selected;
  205. [self.navigationController.view addSubview:self.brandSelectView];
  206. [self.brandSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
  207. make.top.equalTo(self.navigationController.view).offset(56);
  208. make.centerX.equalTo(self.navigationController.view);
  209. make.size.mas_equalTo(CGSizeMake(160, 220));
  210. }];
  211. }
  212. - (void)clickRecBookBtn:(UIButton *)sender{
  213. FKBookRecItem *item = [self.viewModel proModelAtIndex:sender.tag];
  214. if ([item isKindOfClass:[FKBookRecItem class]]){
  215. if (sender.selected){
  216. // 取消订阅
  217. [FKBookRequest reqBookBrand:item.brandItem.itemID
  218. isCancel:YES
  219. identify:BOOK_REC_CANCEL_BOOK
  220. delegate:self];
  221. }else{
  222. // 订阅
  223. [FKBookRequest reqBookBrand:item.brandItem.itemID
  224. isCancel:NO
  225. identify:BOOK_REC_BOOK_BRAND
  226. delegate:self];
  227. }
  228. item.brandItem.didBook = !item.brandItem.didBook;
  229. [self.tableView reloadData];
  230. }
  231. }
  232. - (void)tapGestureClick:(UIGestureRecognizer *)gesture{
  233. if (gesture.state == UIGestureRecognizerStateEnded){
  234. CGPoint point = [gesture locationInView:self.tableView];
  235. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
  236. if (indexPath){
  237. [self didSelectIndexPath:indexPath];
  238. }else{
  239. [self hiddenBrandSelectView];
  240. }
  241. }
  242. }
  243. - (void)clickEmptyView{
  244. [self hiddenBrandSelectView];
  245. }
  246. #pragma mark - method
  247. - (void)pushDetailWithProductId:(NSString *)productId{
  248. if (![FLStringHelper isValidString:productId]) return;
  249. NSString *url = [NSString stringWithFormat:@"firstlinkapp://product.fine3q.com/detail?id=%@&refer=subscribe", productId];
  250. [[SchemaManager sharedManager] parserURL:[NSURL URLWithString:url] shouldCache:NO];
  251. }
  252. - (void)hiddenBrandSelectView{
  253. if (self.brandSelectView.superview){
  254. self.titleView.selected = NO;
  255. [self.brandSelectView removeFromSuperview];
  256. }
  257. }
  258. - (void)didSelectIndexPath:(NSIndexPath *)indexPath{
  259. if (self.brandSelectView.superview){
  260. [self hiddenBrandSelectView];
  261. }else{
  262. if (self.viewModel.contentType == BookContentTypeNormal){
  263. FKBookProItem *item = [self.viewModel proModelAtIndex:indexPath.row];
  264. [self pushDetailWithProductId:item.itemID];
  265. }
  266. }
  267. }
  268. - (void)configNavBarItem{
  269. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"管理 " style:UIBarButtonItemStylePlain target:self action:@selector(clickRightNavItem)];
  270. [rightItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14],
  271. NSForegroundColorAttributeName : UIColorFromRGB(0x4a4a4a)}
  272. forState:UIControlStateNormal];
  273. self.navigationItem.rightBarButtonItem = rightItem;
  274. }
  275. - (void)configTitleView{
  276. if (!self.viewModel || self.viewModel.contentType == BookContentTypeRecommend){
  277. self.navigationItem.title = @"我的订阅";
  278. }else{
  279. if (!self.navigationItem.titleView){
  280. self.navigationItem.titleView = self.titleView;
  281. }
  282. }
  283. }
  284. - (void)configEmptyView{
  285. if (!self.viewModel.itemArray.count){
  286. self.emptyView.hidden = NO;
  287. self.emptyView.userInteractionEnabled = YES;
  288. [self.view addSubview:self.emptyView];
  289. self.emptyView.frame = self.view.bounds;
  290. }else{
  291. self.emptyView.hidden = YES;
  292. self.emptyView.userInteractionEnabled = NO;
  293. [self.emptyView removeFromSuperview];
  294. }
  295. }
  296. - (void)changeToBrandIndex:(NSInteger)index{
  297. if (index == - 1){
  298. // 全部
  299. self.selectedBrandId = nil;
  300. [self reqBookListWithBrandId:nil];
  301. self.titleView.titleLabel.text = @"我的订阅";
  302. }else if (index >= 0){
  303. self.selectedBrandId = [self brandIdAtIndex:index];
  304. [self reqBookListWithBrandId:self.selectedBrandId];
  305. NSString *selectBrandName = [self brandNameAtIndex:index];
  306. if (![FLStringHelper isValidString:selectBrandName]) selectBrandName = @"我的订阅";
  307. self.titleView.titleLabel.text = selectBrandName;
  308. }
  309. [self hiddenBrandSelectView];
  310. }
  311. - (NSString *)brandIdAtIndex:(NSUInteger)index{
  312. if ([self.viewModel isKindOfClass:[FKBookNormalViewModel class]]){
  313. FKBookNormalViewModel *viewModel = (FKBookNormalViewModel *)self.viewModel;
  314. return [viewModel brandIdAtIndex:index];
  315. }
  316. return nil;
  317. }
  318. - (NSString *)brandNameAtIndex:(NSUInteger)index{
  319. if ([self.viewModel isKindOfClass:[FKBookNormalViewModel class]]){
  320. FKBookNormalViewModel *viewModel = (FKBookNormalViewModel *)self.viewModel;
  321. return [viewModel brandNameAtIndex:index];
  322. }
  323. return nil;
  324. }
  325. - (void)addTapGesture{
  326. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick:)];
  327. [self.tableView addGestureRecognizer:tap];
  328. }
  329. #pragma mark - property
  330. - (UITableView *)tableView{
  331. if (_tableView == nil) {
  332. _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  333. _tableView.dataSource = self;
  334. _tableView.delegate = self;
  335. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  336. _tableView.backgroundColor = UIColorFromRGB(0xf8f8f8);
  337. [_tableView registerClass:[FKBookRecommendCell class] forCellReuseIdentifier:FK_BOOK_REC_CELL_IDENTIFY];
  338. [_tableView registerClass:[FKBookProductCell class] forCellReuseIdentifier:FK_BOOK_PRO_CELL_IDENTIFY];
  339. }
  340. return _tableView;
  341. }
  342. - (UIView *)tipHeaderView{
  343. if (_tipHeaderView == nil) {
  344. _tipHeaderView = [[UIView alloc]init];
  345. UILabel *titleLabel = [self createHeaderTipLabel];
  346. [_tipHeaderView addSubview:titleLabel];
  347. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  348. make.center.equalTo(_tipHeaderView);
  349. }];
  350. }
  351. return _tipHeaderView;
  352. }
  353. - (UILabel *)createHeaderTipLabel{
  354. UILabel *titleLabel = [[UILabel alloc]init];
  355. titleLabel.textColor = UIColorFromRGB(0x9b9b9b);
  356. titleLabel.font = [UIFont systemFontOfSize:14];
  357. titleLabel.numberOfLines = 0;
  358. titleLabel.text = @"您还没有订阅\n看看大家订阅了什么";
  359. titleLabel.textAlignment = NSTextAlignmentCenter;
  360. return titleLabel;
  361. }
  362. - (FKBookTitleBtn *)titleView{
  363. if (_titleView == nil) {
  364. _titleView = [[FKBookTitleBtn alloc]init];
  365. _titleView.bounds = CGRectMake(0, 0, 100, 25);
  366. _titleView.center = CGPointMake(UISCREENWIDTH / 2.0f, 22);
  367. [_titleView addTarget:self action:@selector(clickTitleView:) forControlEvents:UIControlEventTouchUpInside];
  368. }
  369. return _titleView;
  370. }
  371. - (FKBrandSelectView *)brandSelectView{
  372. if (_brandSelectView == nil){
  373. WeakSelf(weakSelf);
  374. _brandSelectView = [[FKBrandSelectView alloc]initWithAction:^(NSInteger index) {
  375. [weakSelf changeToBrandIndex:index];
  376. }];
  377. }
  378. return _brandSelectView;
  379. }
  380. - (FKExploreListEmptyView *)emptyView {
  381. if (!_emptyView) {
  382. _emptyView = [FKExploreListEmptyView new];
  383. _emptyView.backgroundColor = UIColorFromRGB(0xf4f5f7);
  384. _emptyView.hidden = YES;
  385. _emptyView.titleLabel.text = @"啊哦~你订阅的品牌暂时没有推送哦";
  386. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickEmptyView)];
  387. [_emptyView addGestureRecognizer:tap];
  388. }
  389. return _emptyView;
  390. }
  391. @end