123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- //
- // FKCategoryController.m
- // FirstLink
- //
- // Created by ascii on 16/2/19.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKBrandSubscribeController.h"
- #import "FLControllerHelper.h"
- #import "FLProgressHUDHelper.h"
- #import "FKExploreKeywordController.h"
- #import "FKExploreListController.h"
- #import "FKEntireCategoryController.h"
- #import "FKExploreListController.h"
- #import "FKBrandSubscribeRequest.h"
- #import "FKBrandSubscribeReform.h"
- #import "FKFilterChoiceTableCell.h"
- #import "FKBrandSubscribeItem.h"
- #import "FKBrandSubscribeViewModel.h"
- @interface FKBrandSubscribeController ()
- <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, FLNetworkDelegate>
- @property (nonatomic, strong) UIView *searchBarWhiteBgView;
- @property (nonatomic, strong) UIView *searchBarGrayBgView;
- @property (nonatomic, strong) UISearchBar *searchBar;
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) FKBrandSubscribeViewModel *viewModel;
- @property (nonatomic, strong) NSString *naviTitle;
- @end
- @implementation FKBrandSubscribeController
- - (instancetype)initWithCategoryID:(NSString *)categoryID title:(NSString *)title subscribe:(NSString *)subscribe {
- self = [super init];
- if (self) {
- self.naviTitle = title;
- self.viewModel.subscribe = subscribe;
- self.viewModel.categoryID = categoryID;
- self.view.backgroundColor = [UIColor whiteColor];
- }
- return self;
- }
- - (void)viewDidLoad{
- [super viewDidLoad];
- [self addAllSubviews];
-
- [self requestBrandSubscribeList];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- self.navigationItem.title = (self.naviTitle ? self.naviTitle : @"全部品牌");
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- }
- - (void)viewDidDisappear:(BOOL)animated {
- [super viewDidDisappear:animated];
- }
- #pragma mark - Request
- - (void)requestBrandSubscribeList {
- [self.hudView show:YES];
- [FKBrandSubscribeRequest requestBrandWithCategoryID:self.viewModel.categoryID
- subscribe:self.viewModel.subscribe
- identify:FKBrandSubscribeRequestList
- delegate:self];
- }
- - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
- if (direction == RefreshDirectionTop) {
- [self requestBrandSubscribeList];
- }
- }
- - (void)refreshSubscribeAtIndexPath:(NSIndexPath *)indexPath {
- if (![FKUserManager isUserLogin]) {
- [self showLoginActionMenu];
- return;
- }
-
- FKBrandSubscribeItem *item = [self.viewModel itemAtIndex:indexPath];
- if ([item isSubscribed]) {
- [FKBrandSubscribeRequest requestCancelSubscribe:@[item.itemID] identify:FKBrandSubscribeRequestChangeStatus delegate:self];
- } else {
- [FKBrandSubscribeRequest requestCreateSubscribe:@[item.itemID] identify:FKBrandSubscribeRequestChangeStatus delegate:self];
- }
- }
- #pragma mark - Response
- - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header {
- [self.hudView hide:NO];
- [self finishLoadingData];
-
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- }
- - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header userInfo:(NSDictionary *)userInfo {
- [self.hudView hide:NO];
- [self finishLoadingData];
-
- if (header.code.intValue == RESPONSE_MSG_NORMAL) {
- if (identify == FKBrandSubscribeRequestList) {
- self.viewModel.brandIndexArray = [FKBrandSubscribeReform parserIndexList:response];
- self.viewModel.brandSubscribeArray = [FKBrandSubscribeReform parserBrandList:response];
- [self.tableView reloadData];
- } else if (identify == FKBrandSubscribeRequestChangeStatus) {
- [self requestBrandSubscribeList];
- }
- } else {
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- }
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return MIN(self.viewModel.brandIndexArray.count, self.viewModel.brandSubscribeArray.count);
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- NSArray *object = self.viewModel.brandSubscribeArray[section];
- if ([object isKindOfClass:[NSArray class]]) {
- return object.count;
- }
- return 0;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return 55;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- return 30;
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- UILabel *indexLabel = [[UILabel alloc] init];
- indexLabel.frame = CGRectMake(15, 0, UISCREENWIDTH, 30);
- indexLabel.font = [UIFont systemFontOfSize:13];
- indexLabel.text = self.viewModel.brandIndexArray[section];
-
- UIView *headerView = [[UIView alloc] init];
- [headerView addSubview:indexLabel];
- headerView.backgroundColor = UIColorFromRGB(0xf5f5f5);
-
- return headerView;
- }
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
- return [self.viewModel getConvertedBrandIndexArray];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- FKFilterChoiceTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKFilterChoiceTableCell class])];
-
- FKBrandSubscribeItem *item = [self.viewModel itemAtIndex:indexPath];
- cell.titleLabel.text = item.name;
- cell.cellType = FKFilterChoiceTableCellTypeImage;
- [cell.brandImageView sd_setImageWithURL:[NSURL URLWithString:item.photoURL]];
-
- if ([item isSubscribed]) {
- cell.confirmSubscribeBtn.hidden = YES;
- cell.cancelSubscribeBtn.hidden = NO;
- } else {
- cell.confirmSubscribeBtn.hidden = NO;
- cell.cancelSubscribeBtn.hidden = YES;
- }
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
-
- WeakSelf(weakSelf);
- cell.clickCallback = ^() {
- [weakSelf refreshSubscribeAtIndexPath:indexPath];
- };
-
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:NO];
-
- FKBrandSubscribeItem *item = [self.viewModel itemAtIndex:indexPath];
- FKExploreListController *controller = [[FKExploreListController alloc] initWithSource:FKExploreListSourceBrand];
- [controller.conditionManager addConditionWithType:FKExploreConditionTypeBrand
- itemID:item.itemID
- value:item.name];
- controller.hidesBottomBarWhenPushed = YES;
- [self.navigationController pushViewController:controller animated:YES];
-
- [[FKPageRouterUtil sharedInstance] pushPageRefer:PRPageSearchBrand
- itemID:item.itemID
- value:item.name];
- }
- #pragma mark - UISearchBarDelegate
- - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
- WeakSelf(weakSelf);
- FKExploreKeywordController *controller = [[FKExploreKeywordController alloc] initWithKeyword:nil type:FKExploreControllerTypeLocalBrand];
- [controller updateLocalBrands:[self.viewModel getBrandNameArray]];
- controller.executeSearchHandler = ^(FKExploreConditionType type, NSString *itemID, NSString *value) {
- if (value.length > 0) {
- FKExploreListController *listController = [[FKExploreListController alloc] initWithSource:FKExploreListSourceKeyword];
- if (type == FKExploreConditionTypeKeyword) {
- NSArray *keywords = [value componentsSeparatedByString:@" "];
- for (NSString *value in keywords) {
- [listController.conditionManager addConditionWithType:FKExploreConditionTypeKeyword
- itemID:nil
- value:value];
- }
- [[FKPageRouterUtil sharedInstance] pushPageRefer:PRPageSearchKeyword values:@[value]];
- } else if (type == FKExploreConditionTypeBrand) {
- [listController.conditionManager addConditionWithType:FKExploreConditionTypeBrand
- itemID:nil
- value:value];
- [[FKPageRouterUtil sharedInstance] pushPageRefer:PRPageSearchKeyword values:@[value]];
- }
-
- listController.hidesBottomBarWhenPushed = YES;
- [weakSelf.navigationController pushViewController:listController animated:YES];
- }
- };
- [self.navigationController presentViewController:controller
- animated:NO
- completion:^{}];
-
- return NO;
- }
- #pragma mark - Action
- - (void)pushEntireCategoryController {
- FKEntireCategoryController *controller = [[FKEntireCategoryController alloc] init];
- controller.hidesBottomBarWhenPushed = YES;
- [self.navigationController pushViewController:controller animated:YES];
- }
- #pragma mark - Property
- - (UIView *)searchBarWhiteBgView {
- if (!_searchBarWhiteBgView) {
- _searchBarWhiteBgView = [UIView new];
- _searchBarWhiteBgView.backgroundColor = UIColorFromRGB(0xffffff);
- }
- return _searchBarWhiteBgView;
- }
- - (UIView *)searchBarGrayBgView {
- if (!_searchBarGrayBgView) {
- _searchBarGrayBgView = [UIView new];
- _searchBarGrayBgView.layer.cornerRadius = 6;
- _searchBarGrayBgView.clipsToBounds = YES;
- _searchBarGrayBgView.backgroundColor = UIColorFromRGB(0xf1f1f1);
- }
- return _searchBarGrayBgView;
- }
- - (UISearchBar *)searchBar {
- if (!_searchBar) {
- _searchBar = [UISearchBar new];
- _searchBar.placeholder = @"品牌搜索";
- _searchBar.delegate = self;
- _searchBar.returnKeyType = UIReturnKeyDone;
- _searchBar.searchBarStyle = UISearchBarStyleMinimal;
- _searchBar.backgroundColor = UIColorFromRGB(0xf1f1f1);
-
- [_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"SearchBarBackground"] forState:UIControlStateNormal];
- }
- return _searchBar;
- }
- - (UITableView *)tableView {
- if (!_tableView) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
- _tableView.dataSource = self;
- _tableView.delegate = self;
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- _tableView.backgroundColor = [UIColor whiteColor];
-
- _tableView.sectionIndexColor = UIColorFromRGB(0x666666);
- _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
- _tableView.showsVerticalScrollIndicator = NO;
-
- _tableView.sectionIndexColor = UIColorFromRGB(0x666666);
- _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
-
- [_tableView registerClass:[FKFilterChoiceTableCell class] forCellReuseIdentifier:NSStringFromClass([FKFilterChoiceTableCell class])];
-
- // [self initRefreshControlWithTableView:_tableView];
- // self.refreshControl.bottomEnabled = NO;
- }
- return _tableView;
- }
- - (FKBrandSubscribeViewModel *)viewModel {
- if (!_viewModel) {
- _viewModel = [FKBrandSubscribeViewModel new];
- }
- return _viewModel;
- }
- #pragma mark - Layout
- - (void)addAllSubviews {
- CGFloat height = ([self.viewModel.subscribe isEqualToString:@"1"] ? 0 : 44);
- if (height > 0) {
- self.searchBarWhiteBgView.frame = CGRectMake(0, 0, (UISCREENWIDTH), height);
- self.searchBarGrayBgView.frame = CGRectMake(8, 8, (UISCREENWIDTH - 16), 28);
- self.searchBar.frame = CGRectMake(0, 1, CGRectGetWidth(self.searchBarGrayBgView.frame), 28);
-
- [self.searchBarGrayBgView addSubview:self.searchBar];
- [self.searchBarWhiteBgView addSubview:self.searchBarGrayBgView];
- [self.view addSubview:self.searchBarWhiteBgView];
-
- UIView *line = [UIView new];
- line.backgroundColor = UIColorFromRGB(0xcccccc);
- [self.searchBarWhiteBgView addSubview:line];
- [line mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.bottom.equalTo(self.searchBarWhiteBgView);
- make.height.mas_equalTo(1.0/[UIScreen mainScreen].scale);
- }];
- }
-
- [self.view addSubview:self.tableView];
- [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.bottom.equalTo(self.view);
- make.top.equalTo(self.view).offset(height);
- }];
- }
- @end
|