説明なし

FKCirAllCommentController.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // FKCirAllCommentController.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/6/16.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKCirAllCommentController.h"
  9. #import "FKCirAllCommentViewModel.h"
  10. #import "PdDetailKeyboardInputView.h"
  11. #import "PindanDeleteCommentView.h"
  12. #import "FKCirAllCommentBottomView.h"
  13. #import "FKCirAllCommentReform.h"
  14. #import "FKCirAllCommentRequest.h"
  15. #define K_REQ_COMMENT_LIST 222
  16. #define K_REQ_COMMENT_LIST_MORE 223
  17. #define K_REQ_COMMENT 224
  18. #define K_REQ_COMMENT_REPLY 225
  19. #define K_REQ_REMOVE_COMMENT 226
  20. static NSString *CIRCLE_COMMENT_CELL_IDENTIFY = @"CIRCLE_COMMENT_CELL_IDENTIFY";
  21. @interface FKCirAllCommentController () <UITableViewDataSource, UITableViewDelegate, FLNetworkDelegate>
  22. @property (nonatomic, strong) FKCirAllCommentViewModel *viewModel;
  23. @property (nonatomic, strong) UITableView *tableView;
  24. @property (nonatomic, strong) UIView *coverView;
  25. @property (nonatomic, strong) PdDetailKeyboardInputView *boardInputView; // 键盘弹出辅助输入视图
  26. @property (nonatomic, strong) PindanDeleteCommentView *deleteCommentView; // 删除自己的评论
  27. @property (nonatomic, strong) FKCirAllCommentBottomView *bottomView;
  28. @end
  29. @implementation FKCirAllCommentController
  30. - (instancetype)initWithCircleID:(NSString *)circleID{
  31. if (self = [super init]) {
  32. self.viewModel.circleID = circleID;
  33. }
  34. return self;
  35. }
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. self.navigationItem.title = @"所有评论";
  39. [self addAllSubviews];
  40. [self addRefreshControl];
  41. [self registerNotifications];
  42. [self reqDataListWithHud:YES];
  43. }
  44. - (void)viewWillAppear:(BOOL)animated{
  45. [super viewWillAppear:animated];
  46. [self.navigationController setNavigationBarHidden:NO animated:YES];
  47. }
  48. - (void)dealloc {
  49. [[NSNotificationCenter defaultCenter] removeObserver:self];
  50. }
  51. - (void)addAllSubviews{
  52. [self.view addSubview:self.tableView];
  53. [self.view addSubview:self.bottomView];
  54. [self.view addSubview:self.coverView];
  55. [self.view addSubview:self.boardInputView];
  56. self.boardInputView.frame = CGRectMake(0, UISCREENHEIGH, UISCREENWIDTH, 50);
  57. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.top.right.left.equalTo(self.view);
  59. make.bottom.equalTo(self.bottomView.mas_top);
  60. }];
  61. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  62. make.left.right.bottom.equalTo(self.view);
  63. make.height.mas_equalTo(49);
  64. }];
  65. [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.edges.insets(UIEdgeInsetsZero);
  67. }];
  68. }
  69. - (void)reqDataListWithHud:(BOOL)hud{
  70. if (hud) [self.hudView show:YES];
  71. [FKCirAllCommentRequest reqCommentListWithID:self.viewModel.circleID
  72. startRow:0
  73. identify:K_REQ_COMMENT_LIST
  74. delegate:self];
  75. }
  76. - (void)reqMoreData{
  77. [FKCirAllCommentRequest reqCommentListWithID:self.viewModel.circleID
  78. startRow:self.viewModel.dataArray.count
  79. identify:K_REQ_COMMENT_LIST_MORE
  80. delegate:self];
  81. }
  82. #pragma mark - response
  83. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header{
  84. [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
  85. [self.refreshControl finishRefreshingDirection:RefreshDirectionBottom];
  86. if (header.code.intValue == RESPONSE_MSG_NORMAL){
  87. if (identify == K_REQ_COMMENT_LIST){
  88. [self.hudView hide:NO];
  89. self.viewModel.dataArray = [FKCirAllCommentReform parserCommentListWithDict:response];
  90. self.viewModel.pageItem = [FKCirAllCommentReform parserPageItemWithDict:response];
  91. self.viewModel.serveTime = [FKCirAllCommentReform parserServeTimeWithDict:response];
  92. }else if (identify == K_REQ_COMMENT_LIST_MORE){
  93. [self.hudView hide:NO];
  94. NSArray *dataArray = [FKCirAllCommentReform parserCommentListWithDict:response];
  95. self.viewModel.pageItem = [FKCirAllCommentReform parserPageItemWithDict:response];
  96. self.viewModel.serveTime = [FKCirAllCommentReform parserServeTimeWithDict:response];
  97. [self.viewModel appendMoreContent:dataArray];
  98. }else if (identify == K_REQ_COMMENT || identify == K_REQ_COMMENT_REPLY || identify == K_REQ_REMOVE_COMMENT){
  99. [self reqDataListWithHud:NO];
  100. return;
  101. }
  102. [self.tableView reloadData];
  103. [self resetBottomRefresh];
  104. }else{
  105. [FLProgressHUDHelper showText:header.msg inView:self.view];
  106. }
  107. }
  108. - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header{
  109. [self.hudView hide:NO];
  110. [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
  111. [self.refreshControl finishRefreshingDirection:RefreshDirectionBottom];
  112. [FLProgressHUDHelper showText:header.msg inView:self.view];
  113. }
  114. #pragma mark - tableView dataSource
  115. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  116. return self.viewModel.dataArray.count;
  117. }
  118. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  119. {
  120. FKCirDetailCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:CIRCLE_COMMENT_CELL_IDENTIFY];
  121. [cell fk_configWithViewModel:self.viewModel indexPath:indexPath];
  122. return cell;
  123. }
  124. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  125. FKCircleCommentItem *item = [self.viewModel commentItemAtIndex:indexPath.row];
  126. return [FKCirDetailCommentCell cellHeightForCommentText:item.content];
  127. }
  128. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  129. {
  130. FKCircleCommentItem *item = [self.viewModel commentItemAtIndex:indexPath.row];
  131. if (item) {
  132. self.boardInputView.tag = indexPath.row;
  133. if ([item isSelfComment]){
  134. // 删除自己的评论
  135. [self.deleteCommentView showInView:self.view animated:YES];
  136. return;
  137. }
  138. [self.boardInputView.textView becomeFirstResponder];
  139. }
  140. }
  141. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  142. if (direction == RefreshDirectionTop) {
  143. [self reqDataListWithHud:NO];
  144. } else if (direction==RefreshDirectionBottom) {
  145. [self reqMoreData];
  146. }
  147. }
  148. #pragma mark - keyboard observe
  149. - (void)keyboardWillChange:(NSNotification *)notification{
  150. // CGFloat duration = [notification.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
  151. if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) {
  152. // 键盘弹出
  153. [self showBoardViewWithNotification:notification];
  154. NSInteger row = [self.tableView indexPathForSelectedRow].row;
  155. self.boardInputView.placeHolder = [self.viewModel getCommentPlaceHolderWithRow:row];
  156. }else if ([notification.name isEqualToString:UIKeyboardWillHideNotification]){
  157. // 键盘退出
  158. [self dismissBoardViewWithNotification:notification];
  159. }else if ([notification.name isEqualToString:UIKeyboardDidShowNotification]){
  160. }else if ([notification.name isEqualToString:UIKeyboardDidHideNotification]){
  161. self.boardInputView.textView.text = nil;
  162. self.boardInputView.placeHolder = nil;
  163. }
  164. }
  165. #pragma mark - cover View show & dismiss
  166. - (void)showBoardViewWithNotification:(NSNotification *)notification;{
  167. CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
  168. CGRect endRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  169. CGRect realEnd = [self.view convertRect:endRect fromView:self.view.window];
  170. UIViewAnimationOptions curve = (UIViewAnimationOptions)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
  171. self.coverView.alpha = 0;
  172. WeakSelf(weakSelf);
  173. [UIView animateWithDuration:duration delay:0 options:curve animations:^{
  174. weakSelf.boardInputView.frame = CGRectMake(realEnd.origin.x, realEnd.origin.y - 50, CGRectGetWidth(weakSelf.boardInputView.bounds), CGRectGetHeight(weakSelf.boardInputView.bounds));
  175. weakSelf.coverView.alpha = 1.0f;
  176. [weakSelf.view bringSubviewToFront:weakSelf.boardInputView];
  177. } completion:^(BOOL finished) {
  178. }];
  179. }
  180. - (void)dismissBoardViewWithNotification:(NSNotification *)notification{
  181. CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
  182. CGRect endRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  183. CGRect curEndRect = [self.view convertRect:endRect fromView:self.navigationController.view];
  184. UIViewAnimationOptions curve = (UIViewAnimationOptions)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
  185. self.coverView.alpha = 1;
  186. WeakSelf(weakSelf);
  187. [UIView animateWithDuration:duration delay:0 options:curve animations:^{
  188. weakSelf.coverView.alpha = 0;
  189. weakSelf.boardInputView.frame = CGRectMake(curEndRect.origin.x, curEndRect.origin.y, CGRectGetWidth(self.boardInputView.bounds), CGRectGetHeight(self.boardInputView.bounds));
  190. } completion:^(BOOL finished) {
  191. }];
  192. }
  193. #pragma mark - method
  194. - (void)addRefreshControl{
  195. [self initRefreshControlWithTableView:self.tableView];
  196. }
  197. - (void)resetBottomRefresh{
  198. if (self.viewModel.dataArray.count >= self.viewModel.pageItem.total){
  199. [self.refreshControl setBottomEnabled:NO];
  200. return;
  201. }
  202. [self.refreshControl setBottomEnabled:YES];
  203. }
  204. // 注册通知
  205. - (void)registerNotifications
  206. {
  207. [[NSNotificationCenter defaultCenter] addObserver:self
  208. selector:@selector(keyboardWillChange:)
  209. name:UIKeyboardWillShowNotification
  210. object:nil];
  211. [[NSNotificationCenter defaultCenter] addObserver:self
  212. selector:@selector(keyboardWillChange:)
  213. name:UIKeyboardDidShowNotification
  214. object:nil];
  215. [[NSNotificationCenter defaultCenter] addObserver:self
  216. selector:@selector(keyboardWillChange:)
  217. name:UIKeyboardWillHideNotification
  218. object:nil];
  219. [[NSNotificationCenter defaultCenter] addObserver:self
  220. selector:@selector(keyboardWillChange:)
  221. name:UIKeyboardDidHideNotification
  222. object:nil];
  223. }
  224. #pragma mark - action
  225. - (void)coverViewTap{
  226. self.boardInputView.textView.text = nil;
  227. [self.boardInputView.textView resignFirstResponder];
  228. [self.boardInputView resignFirstResponder];
  229. }
  230. - (void)clickInputViewConfirmBtn{
  231. // 发送评论
  232. NSString *commentContent = self.boardInputView.textView.text;
  233. NSInteger commentIndex = self.boardInputView.tag;
  234. if (!commentContent || commentContent.length == 0){
  235. [FLProgressHUDHelper showText:@"评论内容不能为空" inView:self.view];
  236. return;
  237. }
  238. if (commentContent.length > 250){
  239. [FLProgressHUDHelper showText:@"评论内容不能超过250个字符" inView:self.view];
  240. return;
  241. }
  242. FKCircleCommentItem *item = [self.viewModel commentItemAtIndex:commentIndex];
  243. int reqTyep = commentIndex >= 0 ? K_REQ_COMMENT_REPLY : K_REQ_COMMENT;
  244. NSString *userID = commentIndex >= 0 ? item.userID : nil;
  245. [self.hudView show:YES];
  246. [FKCirAllCommentRequest reqCommentWithID:self.viewModel.circleID
  247. replyToUserID:userID
  248. content:commentContent
  249. identify:reqTyep
  250. delegate:self];
  251. self.boardInputView.textView.text = nil;
  252. [self.boardInputView.textView resignFirstResponder];
  253. [self.boardInputView resignFirstResponder];
  254. [self.view endEditing:YES];
  255. }
  256. - (void)clickDeleteCommentBtn{
  257. // 删除自己的评论
  258. FKCircleCommentItem *item = [self.viewModel commentItemAtIndex:self.boardInputView.tag];
  259. if (!item) return;
  260. [self.hudView show:YES];
  261. [FKCirAllCommentRequest reqRmoveCommentWithID:item.itemID
  262. identify:K_REQ_REMOVE_COMMENT
  263. delegate:self];
  264. [self.deleteCommentView dismissAnimation];
  265. }
  266. - (void)clickCommentBtn{
  267. self.boardInputView.tag = - 1;
  268. [self.boardInputView.textView becomeFirstResponder];
  269. }
  270. #pragma mark - getter & setter
  271. - (FKCirAllCommentViewModel *)viewModel{
  272. if (_viewModel == nil) {
  273. _viewModel = [[FKCirAllCommentViewModel alloc]init];
  274. }
  275. return _viewModel;
  276. }
  277. - (FKCirAllCommentBottomView *)bottomView{
  278. if (_bottomView == nil) {
  279. _bottomView = [[FKCirAllCommentBottomView alloc]init];
  280. [_bottomView.commentBtn addTarget:self
  281. action:@selector(clickCommentBtn)
  282. forControlEvents:UIControlEventTouchUpInside];
  283. }
  284. return _bottomView;
  285. }
  286. - (UITableView *)tableView{
  287. if (_tableView == nil) {
  288. _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
  289. _tableView.dataSource = self;
  290. _tableView.delegate = self;
  291. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  292. _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4);
  293. [_tableView registerClass:[FKCirDetailCommentCell class] forCellReuseIdentifier:CIRCLE_COMMENT_CELL_IDENTIFY];
  294. }
  295. return _tableView;
  296. }
  297. - (UIView *)coverView{
  298. if (_coverView == nil) {
  299. _coverView = [[UIView alloc]init];
  300. _coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
  301. _coverView.alpha = 0;
  302. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(coverViewTap)];
  303. [_coverView addGestureRecognizer:singleTap];
  304. }
  305. return _coverView;
  306. }
  307. - (PdDetailKeyboardInputView *)boardInputView{
  308. if (_boardInputView == nil) {
  309. _boardInputView = [[PdDetailKeyboardInputView alloc]init];
  310. _boardInputView.bounds = CGRectMake(0, 0, UISCREENWIDTH, 50);
  311. [_boardInputView.confirmButton addTarget:self
  312. action:@selector(clickInputViewConfirmBtn)
  313. forControlEvents:UIControlEventTouchUpInside];
  314. }
  315. return _boardInputView;
  316. }
  317. - (PindanDeleteCommentView *)deleteCommentView{
  318. if (_deleteCommentView == nil) {
  319. _deleteCommentView = [[PindanDeleteCommentView alloc]init];
  320. [_deleteCommentView.deleteButton addTarget:self action:@selector(clickDeleteCommentBtn) forControlEvents:UIControlEventTouchUpInside];
  321. }
  322. return _deleteCommentView;
  323. }
  324. @end