123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- //
- // 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 <IQKeyboardManager.h>
- static NSString * const ProductQuestionCellIdentifier = @"ProductQuestionCellIdentifier";
- static NSString * const ProductAnswerCellIdentifier = @"ProductAnswerCellIdentifier";
- @interface FKProductAdviseController ()
- <UITableViewDataSource, UITableViewDelegate, FLNetworkDelegate>
- @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
|