暂无描述

FKBrandSubscribeController.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // FKCategoryController.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/2/19.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKBrandSubscribeController.h"
  9. #import "FLControllerHelper.h"
  10. #import "FLProgressHUDHelper.h"
  11. #import "FKExploreKeywordController.h"
  12. #import "FKExploreListController.h"
  13. #import "FKEntireCategoryController.h"
  14. #import "FKExploreListController.h"
  15. #import "FKBrandSubscribeRequest.h"
  16. #import "FKBrandSubscribeReform.h"
  17. #import "FKFilterChoiceTableCell.h"
  18. #import "FKBrandSubscribeItem.h"
  19. #import "FKBrandSubscribeViewModel.h"
  20. @interface FKBrandSubscribeController ()
  21. <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, FLNetworkDelegate>
  22. @property (nonatomic, strong) UIView *searchBarWhiteBgView;
  23. @property (nonatomic, strong) UIView *searchBarGrayBgView;
  24. @property (nonatomic, strong) UISearchBar *searchBar;
  25. @property (nonatomic, strong) UITableView *tableView;
  26. @property (nonatomic, strong) FKBrandSubscribeViewModel *viewModel;
  27. @property (nonatomic, strong) NSString *naviTitle;
  28. @end
  29. @implementation FKBrandSubscribeController
  30. - (instancetype)initWithCategoryID:(NSString *)categoryID title:(NSString *)title subscribe:(NSString *)subscribe {
  31. self = [super init];
  32. if (self) {
  33. self.naviTitle = title;
  34. self.viewModel.subscribe = subscribe;
  35. self.viewModel.categoryID = categoryID;
  36. self.view.backgroundColor = [UIColor whiteColor];
  37. }
  38. return self;
  39. }
  40. - (void)viewDidLoad{
  41. [super viewDidLoad];
  42. [self addAllSubviews];
  43. [self requestBrandSubscribeList];
  44. }
  45. - (void)viewWillAppear:(BOOL)animated {
  46. [super viewWillAppear:animated];
  47. self.navigationItem.title = (self.naviTitle ? self.naviTitle : @"全部品牌");
  48. [self.navigationController setNavigationBarHidden:NO animated:YES];
  49. }
  50. - (void)viewDidAppear:(BOOL)animated {
  51. [super viewDidAppear:animated];
  52. }
  53. - (void)viewDidDisappear:(BOOL)animated {
  54. [super viewDidDisappear:animated];
  55. }
  56. #pragma mark - Request
  57. - (void)requestBrandSubscribeList {
  58. [self.hudView show:YES];
  59. [FKBrandSubscribeRequest requestBrandWithCategoryID:self.viewModel.categoryID
  60. subscribe:self.viewModel.subscribe
  61. identify:FKBrandSubscribeRequestList
  62. delegate:self];
  63. }
  64. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  65. if (direction == RefreshDirectionTop) {
  66. [self requestBrandSubscribeList];
  67. }
  68. }
  69. - (void)refreshSubscribeAtIndexPath:(NSIndexPath *)indexPath {
  70. if (![FKUserManager isUserLogin]) {
  71. [self showLoginActionMenu];
  72. return;
  73. }
  74. FKBrandSubscribeItem *item = [self.viewModel itemAtIndex:indexPath];
  75. if ([item isSubscribed]) {
  76. [FKBrandSubscribeRequest requestCancelSubscribe:@[item.itemID] identify:FKBrandSubscribeRequestChangeStatus delegate:self];
  77. } else {
  78. [FKBrandSubscribeRequest requestCreateSubscribe:@[item.itemID] identify:FKBrandSubscribeRequestChangeStatus delegate:self];
  79. }
  80. }
  81. #pragma mark - Response
  82. - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header {
  83. [self.hudView hide:NO];
  84. [self finishLoadingData];
  85. [FLProgressHUDHelper showText:header.msg inView:self.view];
  86. }
  87. - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header userInfo:(NSDictionary *)userInfo {
  88. [self.hudView hide:NO];
  89. [self finishLoadingData];
  90. if (header.code.intValue == RESPONSE_MSG_NORMAL) {
  91. if (identify == FKBrandSubscribeRequestList) {
  92. self.viewModel.brandIndexArray = [FKBrandSubscribeReform parserIndexList:response];
  93. self.viewModel.brandSubscribeArray = [FKBrandSubscribeReform parserBrandList:response];
  94. [self.tableView reloadData];
  95. } else if (identify == FKBrandSubscribeRequestChangeStatus) {
  96. [self requestBrandSubscribeList];
  97. }
  98. } else {
  99. [FLProgressHUDHelper showText:header.msg inView:self.view];
  100. }
  101. }
  102. #pragma mark - UITableViewDataSource
  103. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  104. return MIN(self.viewModel.brandIndexArray.count, self.viewModel.brandSubscribeArray.count);
  105. }
  106. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  107. NSArray *object = self.viewModel.brandSubscribeArray[section];
  108. if ([object isKindOfClass:[NSArray class]]) {
  109. return object.count;
  110. }
  111. return 0;
  112. }
  113. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  114. return 55;
  115. }
  116. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  117. return 30;
  118. }
  119. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  120. UILabel *indexLabel = [[UILabel alloc] init];
  121. indexLabel.frame = CGRectMake(15, 0, UISCREENWIDTH, 30);
  122. indexLabel.font = [UIFont systemFontOfSize:13];
  123. indexLabel.text = self.viewModel.brandIndexArray[section];
  124. UIView *headerView = [[UIView alloc] init];
  125. [headerView addSubview:indexLabel];
  126. headerView.backgroundColor = UIColorFromRGB(0xf5f5f5);
  127. return headerView;
  128. }
  129. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  130. return [self.viewModel getConvertedBrandIndexArray];
  131. }
  132. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  133. FKFilterChoiceTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKFilterChoiceTableCell class])];
  134. FKBrandSubscribeItem *item = [self.viewModel itemAtIndex:indexPath];
  135. cell.titleLabel.text = item.name;
  136. cell.cellType = FKFilterChoiceTableCellTypeImage;
  137. [cell.brandImageView sd_setImageWithURL:[NSURL URLWithString:item.photoURL]];
  138. if ([item isSubscribed]) {
  139. cell.confirmSubscribeBtn.hidden = YES;
  140. cell.cancelSubscribeBtn.hidden = NO;
  141. } else {
  142. cell.confirmSubscribeBtn.hidden = NO;
  143. cell.cancelSubscribeBtn.hidden = YES;
  144. }
  145. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  146. WeakSelf(weakSelf);
  147. cell.clickCallback = ^() {
  148. [weakSelf refreshSubscribeAtIndexPath:indexPath];
  149. };
  150. return cell;
  151. }
  152. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  153. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  154. FKBrandSubscribeItem *item = [self.viewModel itemAtIndex:indexPath];
  155. FKExploreListController *controller = [[FKExploreListController alloc] initWithSource:FKExploreListSourceBrand];
  156. [controller.conditionManager addConditionWithType:FKExploreConditionTypeBrand
  157. itemID:item.itemID
  158. value:item.name];
  159. controller.hidesBottomBarWhenPushed = YES;
  160. [self.navigationController pushViewController:controller animated:YES];
  161. [[FKPageRouterUtil sharedInstance] pushPageRefer:PRPageSearchBrand
  162. itemID:item.itemID
  163. value:item.name];
  164. }
  165. #pragma mark - UISearchBarDelegate
  166. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
  167. WeakSelf(weakSelf);
  168. FKExploreKeywordController *controller = [[FKExploreKeywordController alloc] initWithKeyword:nil type:FKExploreControllerTypeLocalBrand];
  169. [controller updateLocalBrands:[self.viewModel getBrandNameArray]];
  170. controller.executeSearchHandler = ^(FKExploreConditionType type, NSString *itemID, NSString *value) {
  171. if (value.length > 0) {
  172. FKExploreListController *listController = [[FKExploreListController alloc] initWithSource:FKExploreListSourceKeyword];
  173. if (type == FKExploreConditionTypeKeyword) {
  174. NSArray *keywords = [value componentsSeparatedByString:@" "];
  175. for (NSString *value in keywords) {
  176. [listController.conditionManager addConditionWithType:FKExploreConditionTypeKeyword
  177. itemID:nil
  178. value:value];
  179. }
  180. [[FKPageRouterUtil sharedInstance] pushPageRefer:PRPageSearchKeyword values:@[value]];
  181. } else if (type == FKExploreConditionTypeBrand) {
  182. [listController.conditionManager addConditionWithType:FKExploreConditionTypeBrand
  183. itemID:nil
  184. value:value];
  185. [[FKPageRouterUtil sharedInstance] pushPageRefer:PRPageSearchKeyword values:@[value]];
  186. }
  187. listController.hidesBottomBarWhenPushed = YES;
  188. [weakSelf.navigationController pushViewController:listController animated:YES];
  189. }
  190. };
  191. [self.navigationController presentViewController:controller
  192. animated:NO
  193. completion:^{}];
  194. return NO;
  195. }
  196. #pragma mark - Action
  197. - (void)pushEntireCategoryController {
  198. FKEntireCategoryController *controller = [[FKEntireCategoryController alloc] init];
  199. controller.hidesBottomBarWhenPushed = YES;
  200. [self.navigationController pushViewController:controller animated:YES];
  201. }
  202. #pragma mark - Property
  203. - (UIView *)searchBarWhiteBgView {
  204. if (!_searchBarWhiteBgView) {
  205. _searchBarWhiteBgView = [UIView new];
  206. _searchBarWhiteBgView.backgroundColor = UIColorFromRGB(0xffffff);
  207. }
  208. return _searchBarWhiteBgView;
  209. }
  210. - (UIView *)searchBarGrayBgView {
  211. if (!_searchBarGrayBgView) {
  212. _searchBarGrayBgView = [UIView new];
  213. _searchBarGrayBgView.layer.cornerRadius = 6;
  214. _searchBarGrayBgView.clipsToBounds = YES;
  215. _searchBarGrayBgView.backgroundColor = UIColorFromRGB(0xf1f1f1);
  216. }
  217. return _searchBarGrayBgView;
  218. }
  219. - (UISearchBar *)searchBar {
  220. if (!_searchBar) {
  221. _searchBar = [UISearchBar new];
  222. _searchBar.placeholder = @"品牌搜索";
  223. _searchBar.delegate = self;
  224. _searchBar.returnKeyType = UIReturnKeyDone;
  225. _searchBar.searchBarStyle = UISearchBarStyleMinimal;
  226. _searchBar.backgroundColor = UIColorFromRGB(0xf1f1f1);
  227. [_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"SearchBarBackground"] forState:UIControlStateNormal];
  228. }
  229. return _searchBar;
  230. }
  231. - (UITableView *)tableView {
  232. if (!_tableView) {
  233. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  234. _tableView.dataSource = self;
  235. _tableView.delegate = self;
  236. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  237. _tableView.backgroundColor = [UIColor whiteColor];
  238. _tableView.sectionIndexColor = UIColorFromRGB(0x666666);
  239. _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
  240. _tableView.showsVerticalScrollIndicator = NO;
  241. _tableView.sectionIndexColor = UIColorFromRGB(0x666666);
  242. _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
  243. [_tableView registerClass:[FKFilterChoiceTableCell class] forCellReuseIdentifier:NSStringFromClass([FKFilterChoiceTableCell class])];
  244. // [self initRefreshControlWithTableView:_tableView];
  245. // self.refreshControl.bottomEnabled = NO;
  246. }
  247. return _tableView;
  248. }
  249. - (FKBrandSubscribeViewModel *)viewModel {
  250. if (!_viewModel) {
  251. _viewModel = [FKBrandSubscribeViewModel new];
  252. }
  253. return _viewModel;
  254. }
  255. #pragma mark - Layout
  256. - (void)addAllSubviews {
  257. CGFloat height = ([self.viewModel.subscribe isEqualToString:@"1"] ? 0 : 44);
  258. if (height > 0) {
  259. self.searchBarWhiteBgView.frame = CGRectMake(0, 0, (UISCREENWIDTH), height);
  260. self.searchBarGrayBgView.frame = CGRectMake(8, 8, (UISCREENWIDTH - 16), 28);
  261. self.searchBar.frame = CGRectMake(0, 1, CGRectGetWidth(self.searchBarGrayBgView.frame), 28);
  262. [self.searchBarGrayBgView addSubview:self.searchBar];
  263. [self.searchBarWhiteBgView addSubview:self.searchBarGrayBgView];
  264. [self.view addSubview:self.searchBarWhiteBgView];
  265. UIView *line = [UIView new];
  266. line.backgroundColor = UIColorFromRGB(0xcccccc);
  267. [self.searchBarWhiteBgView addSubview:line];
  268. [line mas_makeConstraints:^(MASConstraintMaker *make) {
  269. make.left.right.bottom.equalTo(self.searchBarWhiteBgView);
  270. make.height.mas_equalTo(1.0/[UIScreen mainScreen].scale);
  271. }];
  272. }
  273. [self.view addSubview:self.tableView];
  274. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  275. make.left.right.bottom.equalTo(self.view);
  276. make.top.equalTo(self.view).offset(height);
  277. }];
  278. }
  279. @end