123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- //
- // FKWantBuyController.m
- // FirstLink
- //
- // Created by 施昌鹏 on 16/8/17.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKWantBuyController.h"
- #import "FKLinkView.h"
- #import "FKWantBuyFooterView.h"
- #import "FKUpdateCell.h"
- #import "FKWantBuyRequest.h"
- #import "FKWantBuyViewModel.h"
- #import "FKWantBuyReform.h"
- #import "FKWantBuyItem.h"
- #import "FKWantBuyProductCell.h"
- #import "FKProDetailController.h"
- #import "FKWantBuyEmptyView.h"
- #import "FKPushSettingViewModel.h"
- static NSString *UP_DATE_CELL_IDENTIFIER = @"UP_DATE_CELL_IDENTIFIER";
- static NSString *WANT_BUY_FOOTER_VIEW_IDENTIFIER = @"WANT_BUY_FOOTER_VIEW_IDENTIFIER";
- static NSString *WANT_BUY_PRODUCT_CELL_IDENTIFIER = @"WANT_BUY_PRODUCT_CELL_IDENTIFIER";
- @interface FKWantBuyController ()<UITableViewDelegate,UITableViewDataSource,FLNetworkDelegate,UITextFieldDelegate>
- @property (nonatomic, strong) FKLinkView *linkView;
- @property (nonatomic, strong) FKWantBuyEmptyView *emptyView;
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) FKWantBuyViewModel *viewModel;
- @end
- @implementation FKWantBuyController
- - (void)loadView {
- [super loadView];
-
- [self.view addSubview:self.linkView];
- [self.linkView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.left.right.equalTo(self.view);
- make.height.mas_equalTo(55);
- }];
-
- [self.view addSubview:self.tableView];
- [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.linkView.mas_bottom);
- make.left.right.bottom.equalTo(self.view);
- }];
-
- [self.view addSubview:self.emptyView];
- [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self.view);
- }];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- self.navigationItem.title = @"我要买";
- self.view.backgroundColor = [UIColor whiteColor];
-
- [self addRefreshControl];
- [self checkPasteboardOfProductLink];
-
- self.linkView.hidden = YES;
- self.tableView.hidden = YES;
- self.emptyView.hidden = YES;
- }
- -(void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- self.navigationController.navigationBarHidden = NO;
-
- [self.hudView show:YES];
- [self reqDetailWithStartRow:0 identify:REQ_WANT_BUY_DETAIL_IDENTIFY];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkPasteboardOfProductLink) name:UIApplicationDidBecomeActiveNotification object:nil];
- }
- - (void)viewDidDisappear:(BOOL)animated {
- [super viewDidDisappear:animated];
-
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark - Request
- - (void)addRefreshControl{
- [self initRefreshControlWithTableView:self.tableView];
- }
- - (void)finishLoadingData {
- if (self.refreshControl.refreshingDirection==RefreshingDirectionTop) {
- [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
- } else if (self.refreshControl.refreshingDirection==RefreshingDirectionBottom) {
- [self.refreshControl finishRefreshingDirection:RefreshDirectionBottom];
- }
- }
- - (void)reqDetailWithStartRow:(NSInteger)startRow identify:(int)indentify{
- self.refreshControl.bottomEnabled = (startRow == 0);
- [FKWantBuyRequest reqWantBuyDetailWithStartRow:startRow identify:indentify delegate:self];
- }
- - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
- if (direction==RefreshDirectionTop) {
- [self reqDetailWithStartRow:0 identify:REQ_WANT_BUY_DETAIL_IDENTIFY];
- } else if (direction==RefreshDirectionBottom) {
- [self reqDetailWithStartRow:self.viewModel.dataArray.count identify:REQ_WANT_BUY_NEXT_PAGE];
- }
- }
- #pragma mark - Response
- -(void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header userInfo:(NSDictionary *)userInfo {
- [self finishLoadingData];
- [self.hudView hide:NO];
-
- if (header.code.intValue == RESPONSE_MSG_NORMAL) {
- if (identify == REQ_WANT_BUY_DETAIL_IDENTIFY) {
- self.viewModel.dataArray = [FKWantBuyReform parserWantBuyDetailWithDict:response];
- } else if (identify == REQ_WANT_BUY_NEXT_PAGE) {
- NSArray *array = [FKWantBuyReform parserWantBuyDetailWithDict:response];
- [self.viewModel.dataArray addObjectsFromArray:array];
-
- self.refreshControl.bottomEnabled = (array.count > 0);
- } else if (identify == REQ_WANT_BUY_RETURN_DATA) {
- [self reqDetailWithStartRow:0 identify:REQ_WANT_BUY_DETAIL_IDENTIFY];
- self.linkView.textField.text = nil;
- self.emptyView.textField.text = nil;
-
- [self showWantBuyResultWith:[FKWantBuyReform parseStatus:response targetURL:userInfo[@"url"]]];
- } else if (identify == REQ_WANT_BUY_DELETE_DATA) {
- [self reqDetailWithStartRow:0 identify:REQ_WANT_BUY_DETAIL_IDENTIFY];
- }
- [self.tableView reloadData];
-
- if (self.viewModel.dataArray.count > 0) {
- self.linkView.hidden = NO;
- self.tableView.hidden = NO;
- self.emptyView.hidden = YES;
- } else {
- self.linkView.hidden = YES;
- self.tableView.hidden = YES;
- self.emptyView.hidden = NO;
- }
- } else {
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- }
- }
- -(void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header {
- [self.hudView hide:YES];
- [self finishLoadingData];
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- }
- #pragma mark - UITableViewDataSource
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return self.viewModel.dataArray.count;
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return 1;
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- FKWantBuyItem *item = [[FKWantBuyItem alloc] init];
- item = self.viewModel.dataArray[indexPath.section];
- if (item.product && [item getCatchState] == FKCatchSuccess) {
- FKWantBuyProductCell *cell = [tableView dequeueReusableCellWithIdentifier:WANT_BUY_PRODUCT_CELL_IDENTIFIER];
- [cell configWithViewModel:self.viewModel index:indexPath];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- return cell;
- } else {
- FKUpdateCell *cell = [tableView dequeueReusableCellWithIdentifier:UP_DATE_CELL_IDENTIFIER];
- [cell configWithViewModel:self.viewModel index:indexPath];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- return cell;
- }
- }
- -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
- FKWantBuyFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:WANT_BUY_FOOTER_VIEW_IDENTIFIER];
- return footerView;
- }
- #pragma mark - UITableViewDelegate
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- FKWantBuyItem *item = [[FKWantBuyItem alloc] init];
- item = self.viewModel.dataArray[indexPath.section];
- if (item.product) {
- return 168.5;
- }else{
- return 131;
- }
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- return CGFLOAT_MIN;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
- return 10;
- }
- -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
- editingStyle = UITableViewCellEditingStyleDelete;
- }
- -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
- FKWantBuyItem *item = self.viewModel.dataArray[indexPath.section];
- [FKWantBuyRequest reqWantBuyDeleteWithUrl:item.proUrl identify:REQ_WANT_BUY_DELETE_DATA delegate:self];
- [self.hudView show:YES];
- }];
- return @[deleteRowAction];
- }
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- FKWantBuyItem *item = self.viewModel.dataArray[indexPath.section];
- if (item.product.proId && [item getCatchState] == FKCatchSuccess) {
- FKProDetailController *proDetailController = [[FKProDetailController alloc] initWithProductID: item.product.proId];
- [self.navigationController pushViewController:proDetailController animated:YES];
-
- [[FKPageRouterUtil sharedInstance] pushPageRefer:PRPageWantBuy
- itemID:item.product.proId
- value:item.product.proTitle];
- }
- }
- #pragma mark - UITextFieldDelegate
- -(BOOL)textFieldShouldReturn:(UITextField *)textField {
- if (textField.text) {
- [FKWantBuyRequest reqWantBuyWithUrl:textField.text identify:REQ_WANT_BUY_RETURN_DATA delegate:self userInfo:@{@"url": textField.text}];
- }
- [self.view endEditing:YES];
- return YES;
- }
- #pragma mark - Action
- - (IBAction)clickBuyButtonAction:(id)sender {
- NSString *url = self.emptyView.textField.text;
- if (url.length > 0) {
- [self.hudView show:YES];
- [FKWantBuyRequest reqWantBuyWithUrl:url identify:REQ_WANT_BUY_RETURN_DATA delegate:self userInfo:@{@"url": url}];
- }
- }
- #pragma mark - Method
- -(void)checkPasteboardOfProductLink {
- NSString *paste = [UIPasteboard generalPasteboard].string;
- if (paste && [paste hasPrefix:@"http"]) {
- [self showWantBuyAlert:paste];
- [UIPasteboard generalPasteboard].string = @"";
- };
- }
- - (void)showWantBuyAlert:(NSString *)url {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"检测到您有以下链接,是否同步?" message:url preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
- UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"我要买" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
- [FKWantBuyRequest reqWantBuyWithUrl:url identify:REQ_WANT_BUY_RETURN_DATA delegate:self userInfo:@{@"url": url}];
- [self.hudView show:YES];
- }];
-
- [alert addAction:cancelAction];
- [alert addAction:defaultAction];
- [self presentViewController:alert animated:YES completion:nil];
- }
- - (void)showWantBuyResultWith:(NSString *)status {
- if ([status isEqualToString:@"0"] || [status isEqualToString:@"1"]) {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"系统正在为您抓取商品,成功后我们将以推送形式通知到您。" preferredStyle:UIAlertControllerStyleAlert];
- if ([FKPushSettingViewModel isPushEnable]) {
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil];
- [alert addAction:cancelAction];
- } else {
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"暂不" style:UIAlertActionStyleCancel handler:nil];
- [alert addAction:cancelAction];
-
- UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"开启通知" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
- NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
- [[UIApplication sharedApplication] openURL:url];
- }];
- [alert addAction:defaultAction];
- }
- [self presentViewController:alert animated:YES completion:nil];
- }
- }
- #pragma mark - Property
- -(FKWantBuyEmptyView *)emptyView {
- if (!_emptyView) {
- _emptyView = [[FKWantBuyEmptyView alloc] init];
- _emptyView.textField.delegate = self;
- [_emptyView.buyButton addTarget:self action:@selector(clickBuyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _emptyView;
- }
- -(FKLinkView *)linkView {
- if (_linkView == nil) {
- _linkView = [[FKLinkView alloc] init];
- _linkView.textField.delegate = self;
- }
- return _linkView;
- }
- -(UITableView *)tableView {
- if (_tableView == nil) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- _tableView.backgroundColor = [UIColor whiteColor];
-
- [_tableView registerClass:[FKUpdateCell class] forCellReuseIdentifier:UP_DATE_CELL_IDENTIFIER];
- [_tableView registerClass:[FKWantBuyProductCell class] forCellReuseIdentifier:WANT_BUY_PRODUCT_CELL_IDENTIFIER];
- [_tableView registerClass:[FKWantBuyFooterView class] forHeaderFooterViewReuseIdentifier:WANT_BUY_FOOTER_VIEW_IDENTIFIER];
- }
- return _tableView;
- }
- -(FKWantBuyViewModel *)viewModel {
- if (_viewModel == nil) {
- _viewModel = [[FKWantBuyViewModel alloc] init];
- }
- return _viewModel;
- }
- @end
|