No Description

FKProductAdviseController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // FKProductAdviseController.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/1/20.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKProductAdviseController.h"
  9. #import "FKProDetailController.h"
  10. #import "FKProductAdviseViewModel.h"
  11. #import "FKProductAdviseRequest.h"
  12. #import "FKProductAdviseReform.h"
  13. #import "FKProductAdviseItem.h"
  14. #import "FKProductQuestionCell.h"
  15. #import "FKProductAnswerCell.h"
  16. #import "FKProductAdviseEmptyView.h"
  17. #import "PdDetailKeyboardInputView.h"
  18. #import <IQKeyboardManager.h>
  19. static NSString * const ProductQuestionCellIdentifier = @"ProductQuestionCellIdentifier";
  20. static NSString * const ProductAnswerCellIdentifier = @"ProductAnswerCellIdentifier";
  21. @interface FKProductAdviseController ()
  22. <UITableViewDataSource, UITableViewDelegate, FLNetworkDelegate>
  23. @property (nonatomic, strong) UITableView *tableView;
  24. @property (nonatomic, strong) FKProductAdviseEmptyView *emptyView;
  25. @property (nonatomic, strong) UIButton *questionButton;
  26. @property (nonatomic, strong) PdDetailKeyboardInputView *boardInputView;
  27. @property (nonatomic, strong) UIView *coverView;
  28. @property (nonatomic, strong) FKProductAdviseViewModel *viewModel;
  29. @end
  30. @implementation FKProductAdviseController
  31. - (instancetype)initWithItemID:(NSString *)itemID {
  32. self = [super init];
  33. if (self) {
  34. self.viewModel.itemID = itemID;
  35. }
  36. return self;
  37. }
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. self.view.backgroundColor = UIColorFromRGB(0xffffff);
  41. [self.navigationController setNavigationBarHidden:NO animated:YES];
  42. if (self.openURLPara) {
  43. self.viewModel.itemID = self.openURLPara[@"id"];
  44. }
  45. [self addAllSubviews];
  46. [self configNotifications];
  47. [self requestNewData];
  48. }
  49. - (void)viewDidAppear:(BOOL)animated {
  50. [super viewDidAppear:animated];
  51. [IQKeyboardManager sharedManager].enable = FALSE;
  52. }
  53. - (void)viewDidDisappear:(BOOL)animated {
  54. [super viewDidDisappear:animated];
  55. [IQKeyboardManager sharedManager].enable = TRUE;
  56. }
  57. - (void)dealloc {
  58. [[NSNotificationCenter defaultCenter] removeObserver:self];
  59. }
  60. - (void)viewWillAppear:(BOOL)animated {
  61. [super viewWillAppear:animated];
  62. self.navigationItem.title = @"购买咨询";
  63. [self.navigationController setNavigationBarHidden:NO animated:YES];
  64. }
  65. #pragma mark - Request
  66. - (void)requestNewData {
  67. [self.hudView show:YES];
  68. [FKProductAdviseRequest requestItems:FKProductAdviseRequestNew
  69. itemID:self.viewModel.itemID
  70. startRow:[FLStringHelper convertNumberToString:0]
  71. pageSize:[FLStringHelper convertNumberToString:PAGE_RECORD_COUNT]
  72. deleagate:self];
  73. }
  74. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  75. [self.hudView show:YES];
  76. if (direction == RefreshDirectionTop) {
  77. [self requestNewData];
  78. self.refreshControl.bottomEnabled = YES;
  79. } else if (direction == RefreshDirectionBottom) {
  80. [FKProductAdviseRequest requestItems:FKProductAdviseRequestNextPage
  81. itemID:self.viewModel.itemID
  82. startRow:[FLStringHelper convertNumberToString:(int)self.viewModel.adviseArray.count]
  83. pageSize:[FLStringHelper convertNumberToString:PAGE_RECORD_COUNT]
  84. deleagate:self];
  85. }
  86. }
  87. #pragma mark - Response
  88. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header {
  89. [self.hudView hide:YES];
  90. [self finishLoadingData];
  91. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  92. if (identify == FKProductAdviseRequestNew) {
  93. self.viewModel.adviseArray = [FKProductAdviseReform parseItems:response];
  94. }
  95. if (identify == FKProductAdviseRequestNextPage) {
  96. [self.viewModel.adviseArray addObjectsFromArray:[FKProductAdviseReform parseItems:response]];
  97. }
  98. if (identify == FKProductAdviseSubmitQuestion) {
  99. [self.tableView setContentOffset:CGPointZero animated:YES];
  100. [self requestNewData];
  101. }
  102. self.emptyView.hidden = self.viewModel.adviseArray.count;
  103. if (self.viewModel.adviseArray.count == 0
  104. || self.viewModel.adviseArray.count % PAGE_RECORD_COUNT > 0) {
  105. self.refreshControl.bottomEnabled = FALSE;
  106. }
  107. [self.tableView reloadData];
  108. } else {
  109. [FLProgressHUDHelper showText:header.msg inView:self.view];
  110. }
  111. }
  112. - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header {
  113. [self.hudView hide:YES];
  114. [self finishLoadingData];
  115. [FLProgressHUDHelper showText:header.msg inView:self.view];
  116. }
  117. #pragma mark - UITableViewDataSource, UITableViewDelegate
  118. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  119. return self.viewModel.adviseArray.count;
  120. }
  121. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  122. FKProductAdviseItem *item = [self.viewModel itemAtIndex:indexPath.row];
  123. if (item.isExistAnswer) {
  124. return [FKProductAnswerCell heightWith:item.aContent question:item.qContent];
  125. }
  126. return [FKProductQuestionCell height:item.qContent];
  127. }
  128. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  129. FKProductAdviseItem *item = [self.viewModel itemAtIndex:indexPath.row];
  130. FKProductQuestionCell *questionCell;
  131. if (item.isExistAnswer) {
  132. FKProductAnswerCell *answerCell = [tableView dequeueReusableCellWithIdentifier:ProductAnswerCellIdentifier];
  133. answerCell.aContentLabel.text = item.aContent;
  134. questionCell = answerCell;
  135. } else {
  136. questionCell = [tableView dequeueReusableCellWithIdentifier:ProductQuestionCellIdentifier];
  137. }
  138. questionCell.qContentLabel.text = item.qContent;
  139. questionCell.qNameLabel.text = item.qUsername;
  140. questionCell.qTimeLabel.text = [item.qTime stringByReplacingOccurrencesOfString:@"T"
  141. withString:@" "];
  142. questionCell.selectionStyle = UITableViewCellSelectionStyleNone;
  143. return questionCell;
  144. }
  145. #pragma mark -
  146. - (void)keyboardWillChange:(NSNotification *)notification {
  147. if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) {
  148. [self showBoardViewWithNotification:notification];
  149. } else if ([notification.name isEqualToString:UIKeyboardWillHideNotification]){
  150. [self dismissBoardViewWithNotification:notification];
  151. } else if ([notification.name isEqualToString:UIKeyboardDidHideNotification]){
  152. self.boardInputView.textView.text = nil;
  153. }
  154. }
  155. - (void)showBoardViewWithNotification:(NSNotification *)notification {
  156. CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
  157. CGRect endRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  158. UIViewAnimationOptions curve = (UIViewAnimationOptions)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
  159. self.coverView.alpha = 0;
  160. WeakSelf(weakSelf);
  161. [UIView animateWithDuration:duration delay:0 options:curve animations:^{
  162. weakSelf.boardInputView.frame = CGRectMake(endRect.origin.x
  163. , endRect.origin.y - 50 - 64
  164. , CGRectGetWidth(self.boardInputView.bounds)
  165. , CGRectGetHeight(self.boardInputView.bounds));
  166. weakSelf.coverView.alpha = 1.0f;
  167. } completion:^(BOOL finished) {
  168. }];
  169. }
  170. - (void)dismissBoardViewWithNotification:(NSNotification *)notification {
  171. CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
  172. CGRect endRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  173. UIViewAnimationOptions curve = (UIViewAnimationOptions)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
  174. self.coverView.alpha = 1;
  175. WeakSelf(weakSelf);
  176. [UIView animateWithDuration:duration delay:0 options:curve animations:^{
  177. weakSelf.coverView.alpha = 0;
  178. weakSelf.boardInputView.frame = CGRectMake(0
  179. , endRect.origin.y
  180. , CGRectGetWidth(self.boardInputView.bounds)
  181. , CGRectGetHeight(self.boardInputView.bounds));
  182. } completion:^(BOOL finished) {
  183. }];
  184. }
  185. #pragma mark - Action
  186. - (IBAction)showSubmitQuestionView:(id)sender {
  187. if (![FKUserManager isUserLogin]) {
  188. [self showLoginActionMenu];
  189. } else {
  190. [self.boardInputView.textView becomeFirstResponder];
  191. }
  192. }
  193. - (void)clickQuestionCoverView:(id)sender {
  194. [self.boardInputView.textView resignFirstResponder];
  195. self.boardInputView.textView.text = nil;
  196. }
  197. - (IBAction)clickSendQuestionButton:(id)sender {
  198. NSString *content = [self.boardInputView.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  199. if (content.length > 0) {
  200. [FKProductAdviseRequest requestSubmitQuestion:FKProductAdviseSubmitQuestion
  201. itemID:self.viewModel.itemID
  202. content:content
  203. deleagate:self];
  204. [self.boardInputView.textView resignFirstResponder];
  205. self.boardInputView.textView.text = nil;
  206. }
  207. }
  208. #pragma mark - Method
  209. - (void)configNotifications {
  210. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
  211. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardDidShowNotification object:nil];
  212. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillHideNotification object:nil];
  213. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardDidHideNotification object:nil];
  214. }
  215. #pragma mark - Layout
  216. - (void)addAllSubviews {
  217. [self.view addSubview:self.questionButton];
  218. [self.questionButton mas_makeConstraints:^(MASConstraintMaker *make) {
  219. make.left.right.bottom.equalTo(self.view);
  220. make.height.mas_equalTo(48);
  221. }];
  222. UIView *line = [UIView new];
  223. line.backgroundColor = UIColorFromRGB(0xcccccc);
  224. [self.view addSubview:line];
  225. [line mas_makeConstraints:^(MASConstraintMaker *make) {
  226. make.left.right.equalTo(self.view);
  227. make.bottom.equalTo(self.questionButton.mas_top);
  228. make.height.mas_equalTo(0.5);
  229. }];
  230. [self.view addSubview:self.tableView];
  231. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  232. make.left.right.top.equalTo(self.view);
  233. make.bottom.equalTo(line.mas_top);
  234. }];
  235. [self.view addSubview:self.emptyView];
  236. [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
  237. make.left.right.top.equalTo(self.view);
  238. make.bottom.equalTo(line.mas_top);
  239. }];
  240. [self.view addSubview:self.coverView];
  241. [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
  242. make.edges.insets(UIEdgeInsetsZero);
  243. }];
  244. [self.view addSubview:self.boardInputView];
  245. self.boardInputView.frame = CGRectMake(0, UISCREENHEIGH, UISCREENWIDTH, 50);
  246. }
  247. #pragma mark - Property
  248. - (UITableView *)tableView {
  249. if (!_tableView) {
  250. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  251. _tableView.dataSource = self;
  252. _tableView.delegate = self;
  253. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  254. _tableView.allowsMultipleSelection = NO;
  255. _tableView.backgroundColor = UIColorFromRGB(0xf4f5f7);
  256. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
  257. _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  258. }
  259. [self initRefreshControlWithTableView:_tableView];
  260. [_tableView registerClass:[FKProductQuestionCell class] forCellReuseIdentifier:ProductQuestionCellIdentifier];
  261. [_tableView registerClass:[FKProductAnswerCell class] forCellReuseIdentifier:ProductAnswerCellIdentifier];
  262. }
  263. return _tableView;
  264. }
  265. - (FKProductAdviseEmptyView *)emptyView {
  266. if (!_emptyView) {
  267. _emptyView = [FKProductAdviseEmptyView new];
  268. _emptyView.hidden = YES;
  269. }
  270. return _emptyView;
  271. }
  272. - (UIButton *)questionButton {
  273. if (!_questionButton) {
  274. _questionButton = [UIButton buttonWithType:UIButtonTypeCustom];
  275. [_questionButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
  276. [_questionButton setTitle:@"向帮主提问" forState:UIControlStateNormal];
  277. [_questionButton setTitleColor:UIColorFromRGB(0x3e4246) forState:UIControlStateNormal];
  278. [_questionButton setImage:[UIImage imageNamed:@"advise_submit_icon"] forState:UIControlStateNormal];
  279. _questionButton.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 0);
  280. [_questionButton addTarget:self action:@selector(showSubmitQuestionView:) forControlEvents:UIControlEventTouchUpInside];
  281. }
  282. return _questionButton;
  283. }
  284. - (PdDetailKeyboardInputView *)boardInputView {
  285. if (!_boardInputView) {
  286. _boardInputView = [[PdDetailKeyboardInputView alloc]init];
  287. _boardInputView.bounds = CGRectMake(0, 0, UISCREENWIDTH, 50);
  288. _boardInputView.placeHolder = @"请输入提问内容...";
  289. [_boardInputView.confirmButton addTarget:self action:@selector(clickSendQuestionButton:) forControlEvents:UIControlEventTouchUpInside];
  290. }
  291. return _boardInputView;
  292. }
  293. - (UIView *)coverView {
  294. if (!_coverView) {
  295. _coverView = [UIView new];
  296. _coverView.alpha = 0;
  297. _coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.15f];
  298. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickQuestionCoverView:)];
  299. [_coverView addGestureRecognizer:tap];
  300. }
  301. return _coverView;
  302. }
  303. - (FKProductAdviseViewModel *)viewModel {
  304. if (!_viewModel) {
  305. _viewModel = [FKProductAdviseViewModel new];
  306. }
  307. return _viewModel;
  308. }
  309. @end