SocialDetailViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // SocialDetailViewController.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/6/28.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "SocialDetailViewController.h"
  9. #import "SocialItemData.h"
  10. #import "SocialCommentData.h"
  11. #import "HttpRequest.h"
  12. #import "SocialDetailTableViewCell.h"
  13. #import "SocialCommentDetailTableViewCell.h"
  14. #import "UIView+NTES.h"
  15. #import "SVProgressHUD.h"
  16. #import "UIAlertView+NTESBlock.h"
  17. #import "User.h"
  18. @interface SocialDetailViewController ()<UIScrollViewDelegate, UITextFieldDelegate>
  19. @property (nonatomic, strong) NSMutableArray *likeArr;
  20. @property (nonatomic, strong) NSMutableArray *commentArr;
  21. @property (nonatomic, strong) SocialItemData *data;
  22. @property (nonatomic, strong) NSMutableArray *dataArr;
  23. @property (nonatomic, assign) float infoHeight;
  24. @property (nonatomic, assign) BOOL isFetching;
  25. @property (nonatomic, assign) BOOL flagRefresh;
  26. @property (nonatomic, strong) UIView *commentContainer;
  27. @property (nonatomic, strong) UITextField *commentTextField;
  28. @property (nonatomic, strong) UIButton *commentBtn;
  29. @property (nonatomic, strong) NSString *replyId;
  30. @end
  31. @implementation SocialDetailViewController
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
  35. // self.edgesForExtendedLayout = UIRectEdgeNone;
  36. [self.view addSubview:[self tableView]];
  37. _flagRefresh = NO;
  38. _commentContainer = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height - 50, self.view.width, 50)];
  39. _commentContainer.backgroundColor = UIColor.whiteColor;
  40. [self.view addSubview:_commentContainer];
  41. _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  42. _commentBtn.frame = CGRectMake(_commentContainer.width - 50, 0, 50, _commentContainer.height);
  43. [_commentBtn setImage:[UIImage imageNamed:@"朋友圈详情_07"] forState:UIControlStateNormal];
  44. [_commentBtn addTarget:self action:@selector(onTouchComment:) forControlEvents:UIControlEventTouchUpInside];
  45. [_commentContainer addSubview:_commentBtn];
  46. UIView *txtBg = [[UIView alloc] initWithFrame:CGRectMake(10, 8, _commentContainer.width - 50 - 10, _commentContainer.height-16)];
  47. txtBg.backgroundColor = UIColorFromRGB(0xeeeeee);
  48. txtBg.layer.cornerRadius = 3;
  49. [_commentContainer addSubview:txtBg];
  50. _commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, txtBg.width - 20, txtBg.height)];
  51. _commentTextField.font = [UIFont systemFontOfSize:15];
  52. _commentTextField.placeholder = @"我要评论";
  53. _commentTextField.delegate = self;
  54. [txtBg addSubview:_commentTextField];
  55. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  56. }
  57. - (void)didReceiveMemoryWarning {
  58. [super didReceiveMemoryWarning];
  59. // Dispose of any resources that can be recreated.
  60. }
  61. - (void)viewWillAppear:(BOOL)animated{
  62. [super viewWillAppear:animated];
  63. [self.navigationItem setTitle:@"详情"];
  64. }
  65. - (UITableView *)tableView
  66. {
  67. if (!self.isViewLoaded) {
  68. return nil;
  69. }
  70. if (!_tableView) {
  71. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStylePlain];
  72. _tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
  73. _tableView.backgroundColor = [UIColor clearColor];
  74. _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  75. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  76. _tableView.dataSource = self;
  77. _tableView.delegate = self;
  78. }
  79. return _tableView;
  80. }
  81. - (void)setSocialId:(NSString *)socialId{
  82. [SVProgressHUD show];
  83. [[HttpRequest shared] socialData:socialId success:^(SocialItemData * _Nullable socialData) {
  84. [SVProgressHUD dismiss];
  85. [self setSocialData:socialData];
  86. } failure:^{
  87. [SVProgressHUD dismiss];
  88. }];
  89. }
  90. - (void)setSocialData:(SocialItemData *)socialData{
  91. _data = socialData;
  92. _dataArr = [[NSMutableArray alloc] init];
  93. [_dataArr addObject:_data];
  94. [self fetchCommentList];
  95. }
  96. #pragma mark - Table view data source
  97. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  98. return 1;
  99. }
  100. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  101. if(_dataArr)
  102. return _dataArr.count;
  103. return 0;
  104. }
  105. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  106. NSObject *dataObj = [_dataArr objectAtIndex:indexPath.row];
  107. UITableViewCell *baseCell = nil;
  108. if([dataObj isKindOfClass:[SocialItemData class]])
  109. {
  110. NSString *cellId = @"social";
  111. SocialDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  112. if(!cell)
  113. {
  114. cell = [[SocialDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
  115. [cell initWidth:self.view.frame.size.width];
  116. }
  117. [cell setSocialData:(SocialItemData *)dataObj];
  118. _infoHeight = cell.cellHeight;
  119. baseCell = cell;
  120. }
  121. else if([dataObj isKindOfClass:[SocialCommentData class]])
  122. {
  123. NSString *cellId = @"comment";
  124. SocialCommentDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  125. if(!cell)
  126. {
  127. cell = [[SocialCommentDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
  128. [cell initWidth:self.view.frame.size.width];
  129. }
  130. [cell setSocialCommentData:(SocialCommentData *)dataObj];
  131. baseCell = cell;
  132. }
  133. return baseCell;
  134. }
  135. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  136. NSObject *dataObj = [_dataArr objectAtIndex:indexPath.row];
  137. if([dataObj isKindOfClass:[SocialCommentData class]])
  138. {
  139. SocialCommentData *commentData = (SocialCommentData *)dataObj;
  140. return commentData.cellHeight;
  141. }
  142. return _infoHeight;
  143. }
  144. - (void)fetchCommentList{
  145. if(_isFetching)
  146. return;
  147. _isFetching = YES;
  148. NSString *lastId = nil;
  149. if(self.commentArr && self.commentArr.count){
  150. SocialCommentData *lastData = [self.commentArr lastObject];
  151. lastId = lastData.commentId;
  152. }
  153. [[HttpRequest shared] socialCommentList:_data.socialId lastId:lastId success:^(NSMutableArray * _Nullable dataList) {
  154. if(!self.commentArr)
  155. self.commentArr = dataList;
  156. else
  157. [self.commentArr addObjectsFromArray:dataList];
  158. [self.dataArr addObjectsFromArray:dataList];
  159. [self.tableView reloadData];
  160. _isFetching = NO;
  161. } failure:^{
  162. _isFetching = NO;
  163. }];
  164. }
  165. - (void)deleteSocial:(NSString *)socialId{
  166. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"是否删除该动态?" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  167. [alert showAlertWithCompletionHandler:^(NSInteger index) {
  168. if(index == 1)
  169. {
  170. [SVProgressHUD show];
  171. [[HttpRequest shared] deleteSocialData:socialId success:^(NSString * _Nullable socialId) {
  172. [SVProgressHUD dismiss];
  173. [self.navigationController popViewControllerAnimated:YES];
  174. } failure:^{
  175. [SVProgressHUD dismiss];
  176. }];
  177. }
  178. }];
  179. }
  180. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  181. //康海涛测试Ok的
  182. CGPoint offset1 = scrollView.contentOffset;
  183. CGRect bounds1 = scrollView.bounds;
  184. CGSize size1 = scrollView.contentSize;
  185. UIEdgeInsets inset1 = scrollView.contentInset;
  186. float y1 = offset1.y + bounds1.size.height - inset1.bottom;
  187. float h1 = size1.height;
  188. //NSLog(@"%f %f %i %f", y1, h1, _flagRefresh, bounds1.size.height);
  189. if(h1 < bounds1.size.height)
  190. return;
  191. if (y1 > h1 + 50) {
  192. _flagRefresh = YES;
  193. }
  194. else if (y1 == h1) {
  195. if(_flagRefresh)
  196. {
  197. _flagRefresh = NO;
  198. NSLog(@"下拉刷新");
  199. [self fetchCommentList];
  200. }
  201. }
  202. }
  203. - (void)onTouchComment:(int)sender{
  204. if([_commentTextField.text isEqualToString:@""])
  205. {
  206. if(_commentContainer.bottom == self.view.bottom)
  207. {
  208. [self beginComment:nil];
  209. }
  210. else
  211. {
  212. [_commentTextField resignFirstResponder];
  213. }
  214. }
  215. else
  216. {
  217. [self sendComment:_commentTextField.text replyId:_replyId];
  218. }
  219. }
  220. - (void)beginComment:(NSString *)replyId{
  221. _replyId = replyId;
  222. _commentTextField.text = @"";
  223. if(_replyId && _replyId.length > 0)
  224. {
  225. NSString *nick = [[User sharedInfo] getUserNameById:replyId];
  226. _commentTextField.placeholder = [NSString stringWithFormat:@"回复%@", nick];
  227. }
  228. else
  229. {
  230. _commentTextField.placeholder = @"我要评论";
  231. }
  232. [_commentTextField becomeFirstResponder];
  233. }
  234. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
  235. {
  236. if ([string isEqualToString:@"\n"]) {
  237. [self sendComment:textField.text replyId:_replyId];
  238. return NO;
  239. }
  240. return YES;
  241. }
  242. - (void)sendComment:(NSString *)comment replyId:(NSString *)replyId{
  243. NSLog(@"sender %@", comment);
  244. if(comment && ![comment isEqualToString:@""])
  245. {
  246. [[HttpRequest shared] socialComment:_data.socialId replyId:replyId comment:comment success:^(SocialCommentData * _Nullable commentData, int commentCount) {
  247. _data.comments = commentCount;
  248. [self fetchCommentList];
  249. } failure:^{
  250. }];
  251. }
  252. _commentTextField.text = @"";
  253. [_commentTextField resignFirstResponder];
  254. }
  255. - (void)keyboardWillChangeFrame:(NSNotification *)notification
  256. {
  257. NSDictionary *userInfo = notification.userInfo;
  258. CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  259. //CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  260. float keyBoardFrameTop = endFrame.origin.y;
  261. _commentContainer.bottom = keyBoardFrameTop;
  262. }
  263. @end