省钱达人

DRClassifyViewController.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // DRClassifyViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/4/28.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRClassifyViewController.h"
  9. #import "DRClassifyLeftView.h"
  10. #import "DRClassifyRightView.h"
  11. #import "DRCategoryModel.h"
  12. #import "DRSecondCategoryModel.h"
  13. #import "DRGoodListViewController.h"
  14. #import "DRClassifyListController.h"
  15. #import "DRFindNavBarStaticView.h"
  16. #import "PYSearchViewController.h"
  17. #import "DRFindSearchResultViewController.h"
  18. #import "DRFindRequestViewModel.h"
  19. @interface DRClassifyViewController ()<YHFindNavBarStaticViewDelegate,PYSearchViewControllerDelegate>
  20. {
  21. }
  22. @property (nonatomic, strong) DRClassifyLeftView *leftView; //类别
  23. @property (nonatomic, strong) DRClassifyRightView *rightView;//详单
  24. @property (nonatomic, strong) NSMutableArray *dataArr;
  25. @property (nonatomic, strong) NSArray *secondDataArr;
  26. @property (nonatomic, strong) DRFindNavBarStaticView *navBarStaticView;
  27. @property (nonatomic, strong) NSMutableArray *everyoneSearchArr;
  28. @property (nonatomic, strong) NSString *classifyName;
  29. @property (nonatomic, strong) NSString *firstCategoryId;
  30. @end
  31. @implementation DRClassifyViewController
  32. - (void)viewWillDisappear:(BOOL)animated {
  33. [super viewWillDisappear:animated];
  34. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  35. [SVProgressHUD dismiss];
  36. }
  37. - (void)viewDidAppear:(BOOL)animated {
  38. [super viewDidAppear:animated];
  39. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  40. }
  41. - (void)viewWillAppear:(BOOL)animated {
  42. [super viewWillAppear:animated];
  43. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  44. }
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. [self creatNavBar];
  48. [self createView];
  49. [self requestData];
  50. [self requestEveryoneSearch];
  51. }
  52. - (void)creatNavBar {
  53. [self.navigationBar addSubview:self.navBarStaticView];
  54. self.view.backgroundColor = [UIColor whiteColor];
  55. self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
  56. self.navigationBar.backgroundColor = [UIColor changeColor];
  57. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSex) name:ChangeSex object:nil];
  58. }
  59. - (void)createView {
  60. [self createLeftView];
  61. [self createRightView];
  62. }
  63. - (void)changeSex {
  64. [self createLeftView];
  65. [self createRightView];
  66. [self refreshData];
  67. }
  68. - (void)createLeftView{
  69. self.leftView = [[DRClassifyLeftView alloc]initWithFrame:CGRectMake(0, NavBarHeight, 92*SCREEN_MUTI, SCREEN_HEIGHT - NavBarHeight - TabbarHeight)];
  70. __weak typeof(self) weakSelf = self;
  71. self.leftView.selectRowBlock = ^(NSInteger index) {
  72. DRCategoryModel *model = weakSelf.dataArr[index];
  73. weakSelf.secondDataArr = model.itemList;
  74. weakSelf.rightView.dataSource = model.itemList;
  75. weakSelf.classifyName = model.name;
  76. weakSelf.firstCategoryId = model.Id;
  77. };
  78. [self.view addSubview:self.leftView];
  79. }
  80. - (void)createRightView{
  81. self.rightView = [[DRClassifyRightView alloc]initWithFrame:CGRectMake(92*SCREEN_MUTI, NavBarHeight, SCREEN_WIDTH - 92*SCREEN_MUTI, SCREEN_HEIGHT -NavBarHeight -TabbarHeight)];
  82. __weak typeof(self) weakSelf = self;
  83. self.rightView.selectItemBlock = ^(NSInteger index) {
  84. DRClassifyListController *list = [[DRClassifyListController alloc] init];
  85. DRSecondCategoryModel *model = weakSelf.secondDataArr[index];
  86. list.firstCategoryId = weakSelf.firstCategoryId;
  87. list.cate_id = model.Id;
  88. list.name = model.name;
  89. [weakSelf.navigationController pushViewController:list animated:YES];
  90. NSString *tagStr = [NSString stringWithFormat:@"%@-%@",weakSelf.classifyName,model.name];
  91. [MobClick event:Classify label:tagStr];
  92. };
  93. [self.view addSubview:self.rightView];
  94. }
  95. - (void)requestData {
  96. [SVProgressHUD show];
  97. NSString *urlString = [NSString stringWithFormat:@"%@/api/v2/category/list", BaseURL];
  98. [DRCacheHttp get:urlString params:nil success:^(id json, BOOL isCache) {
  99. [self.dataArr removeAllObjects];
  100. NSArray *arr = [NSArray yy_modelArrayWithClass:[DRCategoryModel class] json:json[@"data"]];
  101. [self.dataArr addObjectsFromArray:arr];
  102. self.leftView.dataArray = arr;
  103. DRCategoryModel *model = arr.firstObject;
  104. self.classifyName = model.name;
  105. self.secondDataArr = model.itemList;
  106. self.rightView.dataSource = model.itemList;
  107. self.firstCategoryId = model.Id;
  108. [SVProgressHUD dismiss];
  109. } failure:^(NSError *error) {
  110. [SVProgressHUD dismiss];
  111. }];
  112. }
  113. - (void)requestEveryoneSearch {
  114. [DRFindRequestViewModel requestEveryoneSearchSuccess:^(NSArray *array) {
  115. if (array.count > 0) {
  116. for (NSDictionary *dict in array) {
  117. [self.everyoneSearchArr addObject:dict];
  118. }
  119. }
  120. } failure:^(NSError *error) {
  121. }];
  122. }
  123. - (void)refreshData {
  124. [self.dataArr removeAllObjects];
  125. [self requestData];
  126. }
  127. #pragma mark - navBarView
  128. - (void)yh_FindNavBarStaticViewClickSearch {
  129. [self createSearchViewControllerWith:nil];
  130. }
  131. #pragma mark - -------------search -----------
  132. - (void)createSearchViewControllerWith:(NSString *)text {
  133. PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:@[] searchBarPlaceholder:@"输入商品名或粘贴淘宝标题"];
  134. searchViewController.searchHistoryStyle = PYSearchHistoryStyleARCBorderTag;
  135. searchViewController.hotSearchStyle = PYHotSearchStyleARCBorderTag;
  136. searchViewController.searchTextField.font = [UIFont systemFontOfSize:13];
  137. searchViewController.delegate = self;
  138. searchViewController.searchBar.text = text;
  139. [self getHotSearchToSearchViewController:searchViewController];
  140. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
  141. [self presentViewController:nav animated:NO completion:^{
  142. for (int i = 0; i < searchViewController.hotSearchTags.count; i++) {
  143. UILabel *tag = searchViewController.hotSearchTags[i];
  144. NSDictionary *dic = self.everyoneSearchArr[i];
  145. if ([dic[@"color"] boolValue]) {
  146. tag.layer.borderColor = [UIColor homeRedColor].CGColor;
  147. tag.textColor = [UIColor homeRedColor];
  148. }
  149. }
  150. }];
  151. searchViewController.didSearchBlock = ^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
  152. [MobClick event:search_count label:Search_Normal];
  153. //处理搜索点击事件
  154. DRFindSearchResultViewController *resultVC = [[DRFindSearchResultViewController alloc] init];
  155. resultVC.searchBar = searchBar;
  156. resultVC.searchName = searchText;
  157. [nav pushViewController:resultVC animated:NO];
  158. };
  159. }
  160. /**
  161. 加载热搜词汇
  162. */
  163. - (void)getHotSearchToSearchViewController:(PYSearchViewController *)searchViewController {
  164. NSMutableArray *hotSearch = [NSMutableArray array];
  165. for (NSDictionary *dic in self.everyoneSearchArr) {
  166. [hotSearch addObject:dic[@"name"]];
  167. }
  168. searchViewController.hotSearches = hotSearch;
  169. }
  170. #pragma mark - PYSearchViewControllerDelegate
  171. /**
  172. 输入完成时触发
  173. */
  174. - (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText
  175. {
  176. if (searchText.length) {
  177. [DRHttp get:SearchAdvice params:@{@"name":searchText} success:^(id json) {
  178. NSArray *list = json[@"data"];
  179. searchViewController.searchSuggestions = list;
  180. } failure:^(NSError *error) {
  181. }];
  182. }
  183. }
  184. /**
  185. 点击建议
  186. */
  187. - (void)searchViewController:(PYSearchViewController *)searchViewController
  188. didSelectSearchSuggestionAtIndex:(NSInteger)index
  189. searchText:(NSString *)searchText {
  190. DRFindSearchResultViewController *resultVC = [[DRFindSearchResultViewController alloc] init];
  191. resultVC.searchName = searchText;
  192. resultVC.searchBar = searchViewController.searchBar;
  193. [searchViewController.navigationController pushViewController:resultVC animated:NO];
  194. }
  195. /**
  196. 点击热搜
  197. */
  198. - (void)searchViewController:(PYSearchViewController *)searchViewController
  199. didSelectHotSearchAtIndex:(NSInteger)index
  200. searchText:(NSString *)searchText {
  201. DRFindSearchResultViewController *resultVC = [[DRFindSearchResultViewController alloc] init];
  202. resultVC.searchName = searchText;
  203. resultVC.searchBar = searchViewController.searchBar;
  204. [searchViewController.navigationController pushViewController:resultVC animated:NO];
  205. [MobClick event:search_count label:Search_Hot_String];
  206. }
  207. /**
  208. 点击历史
  209. */
  210. - (void)searchViewController:(PYSearchViewController *)searchViewController
  211. didSelectSearchHistoryAtIndex:(NSInteger)index
  212. searchText:(NSString *)searchText {
  213. DRFindSearchResultViewController *resultVC = [[DRFindSearchResultViewController alloc] init];
  214. resultVC.searchName = searchText;
  215. resultVC.searchBar = searchViewController.searchBar;
  216. [searchViewController.navigationController pushViewController:resultVC animated:NO];
  217. [MobClick event:search_count label:Search_History_String];
  218. }
  219. #pragma mark -----
  220. - (DRFindNavBarStaticView *)navBarStaticView {
  221. if (!_navBarStaticView) {
  222. _navBarStaticView = [[DRFindNavBarStaticView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, NavBarHeight)];
  223. _navBarStaticView.backgroundColor = [UIColor whiteColor];
  224. _navBarStaticView.delegate = self;
  225. }
  226. return _navBarStaticView;
  227. }
  228. - (NSMutableArray *)dataArr {
  229. if (!_dataArr) {
  230. _dataArr = [NSMutableArray array];
  231. }
  232. return _dataArr;
  233. }
  234. - (NSMutableArray *)everyoneSearchArr {
  235. if (!_everyoneSearchArr) {
  236. _everyoneSearchArr = [NSMutableArray array];
  237. }
  238. return _everyoneSearchArr;
  239. }
  240. - (void)didReceiveMemoryWarning {
  241. [super didReceiveMemoryWarning];
  242. // Dispose of any resources that can be recreated.
  243. }
  244. /*
  245. #pragma mark - Navigation
  246. // In a storyboard-based application, you will often want to do a little preparation before navigation
  247. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  248. // Get the new view controller using [segue destinationViewController].
  249. // Pass the selected object to the new view controller.
  250. }
  251. */
  252. @end