// // FKProductAdviseController.m // FirstLink // // Created by ascii on 16/1/20. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKProductAdviseController.h" #import "FKProDetailController.h" #import "FKProductAdviseViewModel.h" #import "FKProductAdviseRequest.h" #import "FKProductAdviseReform.h" #import "FKProductAdviseItem.h" #import "FKProductQuestionCell.h" #import "FKProductAnswerCell.h" #import "FKProductAdviseEmptyView.h" #import "PdDetailKeyboardInputView.h" #import static NSString * const ProductQuestionCellIdentifier = @"ProductQuestionCellIdentifier"; static NSString * const ProductAnswerCellIdentifier = @"ProductAnswerCellIdentifier"; @interface FKProductAdviseController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) FKProductAdviseEmptyView *emptyView; @property (nonatomic, strong) UIButton *questionButton; @property (nonatomic, strong) PdDetailKeyboardInputView *boardInputView; @property (nonatomic, strong) UIView *coverView; @property (nonatomic, strong) FKProductAdviseViewModel *viewModel; @end @implementation FKProductAdviseController - (instancetype)initWithItemID:(NSString *)itemID { self = [super init]; if (self) { self.viewModel.itemID = itemID; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = UIColorFromRGB(0xffffff); [self.navigationController setNavigationBarHidden:NO animated:YES]; if (self.openURLPara) { self.viewModel.itemID = self.openURLPara[@"id"]; } [self addAllSubviews]; [self configNotifications]; [self requestNewData]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [IQKeyboardManager sharedManager].enable = FALSE; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [IQKeyboardManager sharedManager].enable = TRUE; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationItem.title = @"购买咨询"; [self.navigationController setNavigationBarHidden:NO animated:YES]; } #pragma mark - Request - (void)requestNewData { [self.hudView show:YES]; [FKProductAdviseRequest requestItems:FKProductAdviseRequestNew itemID:self.viewModel.itemID startRow:[FLStringHelper convertNumberToString:0] pageSize:[FLStringHelper convertNumberToString:PAGE_RECORD_COUNT] deleagate:self]; } - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction { [self.hudView show:YES]; if (direction == RefreshDirectionTop) { [self requestNewData]; self.refreshControl.bottomEnabled = YES; } else if (direction == RefreshDirectionBottom) { [FKProductAdviseRequest requestItems:FKProductAdviseRequestNextPage itemID:self.viewModel.itemID startRow:[FLStringHelper convertNumberToString:(int)self.viewModel.adviseArray.count] pageSize:[FLStringHelper convertNumberToString:PAGE_RECORD_COUNT] deleagate:self]; } } #pragma mark - Response - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header { [self.hudView hide:YES]; [self finishLoadingData]; if ([header.code intValue] == RESPONSE_MSG_NORMAL) { if (identify == FKProductAdviseRequestNew) { self.viewModel.adviseArray = [FKProductAdviseReform parseItems:response]; } if (identify == FKProductAdviseRequestNextPage) { [self.viewModel.adviseArray addObjectsFromArray:[FKProductAdviseReform parseItems:response]]; } if (identify == FKProductAdviseSubmitQuestion) { [self.tableView setContentOffset:CGPointZero animated:YES]; [self requestNewData]; } self.emptyView.hidden = self.viewModel.adviseArray.count; if (self.viewModel.adviseArray.count == 0 || self.viewModel.adviseArray.count % PAGE_RECORD_COUNT > 0) { self.refreshControl.bottomEnabled = FALSE; } [self.tableView reloadData]; } 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, UITableViewDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.viewModel.adviseArray.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { FKProductAdviseItem *item = [self.viewModel itemAtIndex:indexPath.row]; if (item.isExistAnswer) { return [FKProductAnswerCell heightWith:item.aContent question:item.qContent]; } return [FKProductQuestionCell height:item.qContent]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { FKProductAdviseItem *item = [self.viewModel itemAtIndex:indexPath.row]; FKProductQuestionCell *questionCell; if (item.isExistAnswer) { FKProductAnswerCell *answerCell = [tableView dequeueReusableCellWithIdentifier:ProductAnswerCellIdentifier]; answerCell.aContentLabel.text = item.aContent; questionCell = answerCell; } else { questionCell = [tableView dequeueReusableCellWithIdentifier:ProductQuestionCellIdentifier]; } questionCell.qContentLabel.text = item.qContent; questionCell.qNameLabel.text = item.qUsername; questionCell.qTimeLabel.text = [item.qTime stringByReplacingOccurrencesOfString:@"T" withString:@" "]; questionCell.selectionStyle = UITableViewCellSelectionStyleNone; return questionCell; } #pragma mark - - (void)keyboardWillChange:(NSNotification *)notification { if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) { [self showBoardViewWithNotification:notification]; } else if ([notification.name isEqualToString:UIKeyboardWillHideNotification]){ [self dismissBoardViewWithNotification:notification]; } else if ([notification.name isEqualToString:UIKeyboardDidHideNotification]){ self.boardInputView.textView.text = nil; } } - (void)showBoardViewWithNotification:(NSNotification *)notification { CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; CGRect endRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; UIViewAnimationOptions curve = (UIViewAnimationOptions)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; self.coverView.alpha = 0; WeakSelf(weakSelf); [UIView animateWithDuration:duration delay:0 options:curve animations:^{ weakSelf.boardInputView.frame = CGRectMake(endRect.origin.x , endRect.origin.y - 50 - 64 , CGRectGetWidth(self.boardInputView.bounds) , CGRectGetHeight(self.boardInputView.bounds)); weakSelf.coverView.alpha = 1.0f; } completion:^(BOOL finished) { }]; } - (void)dismissBoardViewWithNotification:(NSNotification *)notification { CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; CGRect endRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; UIViewAnimationOptions curve = (UIViewAnimationOptions)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; self.coverView.alpha = 1; WeakSelf(weakSelf); [UIView animateWithDuration:duration delay:0 options:curve animations:^{ weakSelf.coverView.alpha = 0; weakSelf.boardInputView.frame = CGRectMake(0 , endRect.origin.y , CGRectGetWidth(self.boardInputView.bounds) , CGRectGetHeight(self.boardInputView.bounds)); } completion:^(BOOL finished) { }]; } #pragma mark - Action - (IBAction)showSubmitQuestionView:(id)sender { if (![FKUserManager isUserLogin]) { [self showLoginActionMenu]; } else { [self.boardInputView.textView becomeFirstResponder]; } } - (void)clickQuestionCoverView:(id)sender { [self.boardInputView.textView resignFirstResponder]; self.boardInputView.textView.text = nil; } - (IBAction)clickSendQuestionButton:(id)sender { NSString *content = [self.boardInputView.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if (content.length > 0) { [FKProductAdviseRequest requestSubmitQuestion:FKProductAdviseSubmitQuestion itemID:self.viewModel.itemID content:content deleagate:self]; [self.boardInputView.textView resignFirstResponder]; self.boardInputView.textView.text = nil; } } #pragma mark - Method - (void)configNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardDidHideNotification object:nil]; } #pragma mark - Layout - (void)addAllSubviews { [self.view addSubview:self.questionButton]; [self.questionButton mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.view); make.height.mas_equalTo(48); }]; UIView *line = [UIView new]; line.backgroundColor = UIColorFromRGB(0xcccccc); [self.view addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.bottom.equalTo(self.questionButton.mas_top); make.height.mas_equalTo(0.5); }]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.equalTo(self.view); make.bottom.equalTo(line.mas_top); }]; [self.view addSubview:self.emptyView]; [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.equalTo(self.view); make.bottom.equalTo(line.mas_top); }]; [self.view addSubview:self.coverView]; [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.insets(UIEdgeInsetsZero); }]; [self.view addSubview:self.boardInputView]; self.boardInputView.frame = CGRectMake(0, UISCREENHEIGH, UISCREENWIDTH, 50); } #pragma mark - Property - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.allowsMultipleSelection = NO; _tableView.backgroundColor = UIColorFromRGB(0xf4f5f7); if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); } [self initRefreshControlWithTableView:_tableView]; [_tableView registerClass:[FKProductQuestionCell class] forCellReuseIdentifier:ProductQuestionCellIdentifier]; [_tableView registerClass:[FKProductAnswerCell class] forCellReuseIdentifier:ProductAnswerCellIdentifier]; } return _tableView; } - (FKProductAdviseEmptyView *)emptyView { if (!_emptyView) { _emptyView = [FKProductAdviseEmptyView new]; _emptyView.hidden = YES; } return _emptyView; } - (UIButton *)questionButton { if (!_questionButton) { _questionButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_questionButton.titleLabel setFont:[UIFont systemFontOfSize:15]]; [_questionButton setTitle:@"向帮主提问" forState:UIControlStateNormal]; [_questionButton setTitleColor:UIColorFromRGB(0x3e4246) forState:UIControlStateNormal]; [_questionButton setImage:[UIImage imageNamed:@"advise_submit_icon"] forState:UIControlStateNormal]; _questionButton.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 0); [_questionButton addTarget:self action:@selector(showSubmitQuestionView:) forControlEvents:UIControlEventTouchUpInside]; } return _questionButton; } - (PdDetailKeyboardInputView *)boardInputView { if (!_boardInputView) { _boardInputView = [[PdDetailKeyboardInputView alloc]init]; _boardInputView.bounds = CGRectMake(0, 0, UISCREENWIDTH, 50); _boardInputView.placeHolder = @"请输入提问内容..."; [_boardInputView.confirmButton addTarget:self action:@selector(clickSendQuestionButton:) forControlEvents:UIControlEventTouchUpInside]; } return _boardInputView; } - (UIView *)coverView { if (!_coverView) { _coverView = [UIView new]; _coverView.alpha = 0; _coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.15f]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickQuestionCoverView:)]; [_coverView addGestureRecognizer:tap]; } return _coverView; } - (FKProductAdviseViewModel *)viewModel { if (!_viewModel) { _viewModel = [FKProductAdviseViewModel new]; } return _viewModel; } @end