Няма описание

FKWantBuyController.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // FKWantBuyController.m
  3. // FirstLink
  4. //
  5. // Created by 施昌鹏 on 16/8/17.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKWantBuyController.h"
  9. #import "FKLinkView.h"
  10. #import "FKWantBuyFooterView.h"
  11. #import "FKUpdateCell.h"
  12. #import "FKWantBuyRequest.h"
  13. #import "FKWantBuyViewModel.h"
  14. #import "FKWantBuyReform.h"
  15. #import "FKWantBuyItem.h"
  16. #import "FKWantBuyProductCell.h"
  17. #import "FKProDetailController.h"
  18. #import "FKWantBuyEmptyView.h"
  19. #import "FKPushSettingViewModel.h"
  20. static NSString *UP_DATE_CELL_IDENTIFIER = @"UP_DATE_CELL_IDENTIFIER";
  21. static NSString *WANT_BUY_FOOTER_VIEW_IDENTIFIER = @"WANT_BUY_FOOTER_VIEW_IDENTIFIER";
  22. static NSString *WANT_BUY_PRODUCT_CELL_IDENTIFIER = @"WANT_BUY_PRODUCT_CELL_IDENTIFIER";
  23. @interface FKWantBuyController ()<UITableViewDelegate,UITableViewDataSource,FLNetworkDelegate,UITextFieldDelegate>
  24. @property (nonatomic, strong) FKLinkView *linkView;
  25. @property (nonatomic, strong) FKWantBuyEmptyView *emptyView;
  26. @property (nonatomic, strong) UITableView *tableView;
  27. @property (nonatomic, strong) FKWantBuyViewModel *viewModel;
  28. @end
  29. @implementation FKWantBuyController
  30. - (void)loadView {
  31. [super loadView];
  32. [self.view addSubview:self.linkView];
  33. [self.linkView mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.top.left.right.equalTo(self.view);
  35. make.height.mas_equalTo(55);
  36. }];
  37. [self.view addSubview:self.tableView];
  38. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.top.equalTo(self.linkView.mas_bottom);
  40. make.left.right.bottom.equalTo(self.view);
  41. }];
  42. [self.view addSubview:self.emptyView];
  43. [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.edges.equalTo(self.view);
  45. }];
  46. }
  47. - (void)viewDidLoad {
  48. [super viewDidLoad];
  49. self.navigationItem.title = @"我要买";
  50. self.view.backgroundColor = [UIColor whiteColor];
  51. [self addRefreshControl];
  52. [self checkPasteboardOfProductLink];
  53. self.linkView.hidden = YES;
  54. self.tableView.hidden = YES;
  55. self.emptyView.hidden = YES;
  56. }
  57. -(void)viewWillAppear:(BOOL)animated {
  58. [super viewWillAppear:animated];
  59. self.navigationController.navigationBarHidden = NO;
  60. [self.hudView show:YES];
  61. [self reqDetailWithStartRow:0 identify:REQ_WANT_BUY_DETAIL_IDENTIFY];
  62. }
  63. - (void)viewDidAppear:(BOOL)animated {
  64. [super viewDidAppear:animated];
  65. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkPasteboardOfProductLink) name:UIApplicationDidBecomeActiveNotification object:nil];
  66. }
  67. - (void)viewDidDisappear:(BOOL)animated {
  68. [super viewDidDisappear:animated];
  69. [[NSNotificationCenter defaultCenter] removeObserver:self];
  70. }
  71. #pragma mark - Request
  72. - (void)addRefreshControl{
  73. [self initRefreshControlWithTableView:self.tableView];
  74. }
  75. - (void)finishLoadingData {
  76. if (self.refreshControl.refreshingDirection==RefreshingDirectionTop) {
  77. [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
  78. } else if (self.refreshControl.refreshingDirection==RefreshingDirectionBottom) {
  79. [self.refreshControl finishRefreshingDirection:RefreshDirectionBottom];
  80. }
  81. }
  82. - (void)reqDetailWithStartRow:(NSInteger)startRow identify:(int)indentify{
  83. self.refreshControl.bottomEnabled = (startRow == 0);
  84. [FKWantBuyRequest reqWantBuyDetailWithStartRow:startRow identify:indentify delegate:self];
  85. }
  86. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  87. if (direction==RefreshDirectionTop) {
  88. [self reqDetailWithStartRow:0 identify:REQ_WANT_BUY_DETAIL_IDENTIFY];
  89. } else if (direction==RefreshDirectionBottom) {
  90. [self reqDetailWithStartRow:self.viewModel.dataArray.count identify:REQ_WANT_BUY_NEXT_PAGE];
  91. }
  92. }
  93. #pragma mark - Response
  94. -(void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header userInfo:(NSDictionary *)userInfo {
  95. [self finishLoadingData];
  96. [self.hudView hide:NO];
  97. if (header.code.intValue == RESPONSE_MSG_NORMAL) {
  98. if (identify == REQ_WANT_BUY_DETAIL_IDENTIFY) {
  99. self.viewModel.dataArray = [FKWantBuyReform parserWantBuyDetailWithDict:response];
  100. } else if (identify == REQ_WANT_BUY_NEXT_PAGE) {
  101. NSArray *array = [FKWantBuyReform parserWantBuyDetailWithDict:response];
  102. [self.viewModel.dataArray addObjectsFromArray:array];
  103. self.refreshControl.bottomEnabled = (array.count > 0);
  104. } else if (identify == REQ_WANT_BUY_RETURN_DATA) {
  105. [self reqDetailWithStartRow:0 identify:REQ_WANT_BUY_DETAIL_IDENTIFY];
  106. self.linkView.textField.text = nil;
  107. self.emptyView.textField.text = nil;
  108. [self showWantBuyResultWith:[FKWantBuyReform parseStatus:response targetURL:userInfo[@"url"]]];
  109. } else if (identify == REQ_WANT_BUY_DELETE_DATA) {
  110. [self reqDetailWithStartRow:0 identify:REQ_WANT_BUY_DETAIL_IDENTIFY];
  111. }
  112. [self.tableView reloadData];
  113. if (self.viewModel.dataArray.count > 0) {
  114. self.linkView.hidden = NO;
  115. self.tableView.hidden = NO;
  116. self.emptyView.hidden = YES;
  117. } else {
  118. self.linkView.hidden = YES;
  119. self.tableView.hidden = YES;
  120. self.emptyView.hidden = NO;
  121. }
  122. } else {
  123. [FLProgressHUDHelper showText:header.msg inView:self.view];
  124. }
  125. }
  126. -(void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header {
  127. [self.hudView hide:YES];
  128. [self finishLoadingData];
  129. [FLProgressHUDHelper showText:header.msg inView:self.view];
  130. }
  131. #pragma mark - UITableViewDataSource
  132. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  133. return self.viewModel.dataArray.count;
  134. }
  135. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  136. return 1;
  137. }
  138. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  139. FKWantBuyItem *item = [[FKWantBuyItem alloc] init];
  140. item = self.viewModel.dataArray[indexPath.section];
  141. if (item.product && [item getCatchState] == FKCatchSuccess) {
  142. FKWantBuyProductCell *cell = [tableView dequeueReusableCellWithIdentifier:WANT_BUY_PRODUCT_CELL_IDENTIFIER];
  143. [cell configWithViewModel:self.viewModel index:indexPath];
  144. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  145. return cell;
  146. } else {
  147. FKUpdateCell *cell = [tableView dequeueReusableCellWithIdentifier:UP_DATE_CELL_IDENTIFIER];
  148. [cell configWithViewModel:self.viewModel index:indexPath];
  149. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  150. return cell;
  151. }
  152. }
  153. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  154. FKWantBuyFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:WANT_BUY_FOOTER_VIEW_IDENTIFIER];
  155. return footerView;
  156. }
  157. #pragma mark - UITableViewDelegate
  158. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  159. FKWantBuyItem *item = [[FKWantBuyItem alloc] init];
  160. item = self.viewModel.dataArray[indexPath.section];
  161. if (item.product) {
  162. return 168.5;
  163. }else{
  164. return 131;
  165. }
  166. }
  167. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  168. return CGFLOAT_MIN;
  169. }
  170. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  171. return 10;
  172. }
  173. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  174. editingStyle = UITableViewCellEditingStyleDelete;
  175. }
  176. -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  177. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  178. FKWantBuyItem *item = self.viewModel.dataArray[indexPath.section];
  179. [FKWantBuyRequest reqWantBuyDeleteWithUrl:item.proUrl identify:REQ_WANT_BUY_DELETE_DATA delegate:self];
  180. [self.hudView show:YES];
  181. }];
  182. return @[deleteRowAction];
  183. }
  184. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  185. FKWantBuyItem *item = self.viewModel.dataArray[indexPath.section];
  186. if (item.product.proId && [item getCatchState] == FKCatchSuccess) {
  187. FKProDetailController *proDetailController = [[FKProDetailController alloc] initWithProductID: item.product.proId];
  188. [self.navigationController pushViewController:proDetailController animated:YES];
  189. [[FKPageRouterUtil sharedInstance] pushPageRefer:PRPageWantBuy
  190. itemID:item.product.proId
  191. value:item.product.proTitle];
  192. }
  193. }
  194. #pragma mark - UITextFieldDelegate
  195. -(BOOL)textFieldShouldReturn:(UITextField *)textField {
  196. if (textField.text) {
  197. [FKWantBuyRequest reqWantBuyWithUrl:textField.text identify:REQ_WANT_BUY_RETURN_DATA delegate:self userInfo:@{@"url": textField.text}];
  198. }
  199. [self.view endEditing:YES];
  200. return YES;
  201. }
  202. #pragma mark - Action
  203. - (IBAction)clickBuyButtonAction:(id)sender {
  204. NSString *url = self.emptyView.textField.text;
  205. if (url.length > 0) {
  206. [self.hudView show:YES];
  207. [FKWantBuyRequest reqWantBuyWithUrl:url identify:REQ_WANT_BUY_RETURN_DATA delegate:self userInfo:@{@"url": url}];
  208. }
  209. }
  210. #pragma mark - Method
  211. -(void)checkPasteboardOfProductLink {
  212. NSString *paste = [UIPasteboard generalPasteboard].string;
  213. if (paste && [paste hasPrefix:@"http"]) {
  214. [self showWantBuyAlert:paste];
  215. [UIPasteboard generalPasteboard].string = @"";
  216. };
  217. }
  218. - (void)showWantBuyAlert:(NSString *)url {
  219. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"检测到您有以下链接,是否同步?" message:url preferredStyle:UIAlertControllerStyleAlert];
  220. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  221. UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"我要买" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  222. [FKWantBuyRequest reqWantBuyWithUrl:url identify:REQ_WANT_BUY_RETURN_DATA delegate:self userInfo:@{@"url": url}];
  223. [self.hudView show:YES];
  224. }];
  225. [alert addAction:cancelAction];
  226. [alert addAction:defaultAction];
  227. [self presentViewController:alert animated:YES completion:nil];
  228. }
  229. - (void)showWantBuyResultWith:(NSString *)status {
  230. if ([status isEqualToString:@"0"] || [status isEqualToString:@"1"]) {
  231. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"系统正在为您抓取商品,成功后我们将以推送形式通知到您。" preferredStyle:UIAlertControllerStyleAlert];
  232. if ([FKPushSettingViewModel isPushEnable]) {
  233. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil];
  234. [alert addAction:cancelAction];
  235. } else {
  236. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"暂不" style:UIAlertActionStyleCancel handler:nil];
  237. [alert addAction:cancelAction];
  238. UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"开启通知" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  239. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  240. [[UIApplication sharedApplication] openURL:url];
  241. }];
  242. [alert addAction:defaultAction];
  243. }
  244. [self presentViewController:alert animated:YES completion:nil];
  245. }
  246. }
  247. #pragma mark - Property
  248. -(FKWantBuyEmptyView *)emptyView {
  249. if (!_emptyView) {
  250. _emptyView = [[FKWantBuyEmptyView alloc] init];
  251. _emptyView.textField.delegate = self;
  252. [_emptyView.buyButton addTarget:self action:@selector(clickBuyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  253. }
  254. return _emptyView;
  255. }
  256. -(FKLinkView *)linkView {
  257. if (_linkView == nil) {
  258. _linkView = [[FKLinkView alloc] init];
  259. _linkView.textField.delegate = self;
  260. }
  261. return _linkView;
  262. }
  263. -(UITableView *)tableView {
  264. if (_tableView == nil) {
  265. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  266. _tableView.delegate = self;
  267. _tableView.dataSource = self;
  268. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  269. _tableView.backgroundColor = [UIColor whiteColor];
  270. [_tableView registerClass:[FKUpdateCell class] forCellReuseIdentifier:UP_DATE_CELL_IDENTIFIER];
  271. [_tableView registerClass:[FKWantBuyProductCell class] forCellReuseIdentifier:WANT_BUY_PRODUCT_CELL_IDENTIFIER];
  272. [_tableView registerClass:[FKWantBuyFooterView class] forHeaderFooterViewReuseIdentifier:WANT_BUY_FOOTER_VIEW_IDENTIFIER];
  273. }
  274. return _tableView;
  275. }
  276. -(FKWantBuyViewModel *)viewModel {
  277. if (_viewModel == nil) {
  278. _viewModel = [[FKWantBuyViewModel alloc] init];
  279. }
  280. return _viewModel;
  281. }
  282. @end