| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- //
- // SocialDetailViewController.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/6/28.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "SocialDetailViewController.h"
- #import "SocialItemData.h"
- #import "SocialCommentData.h"
- #import "HttpRequest.h"
- #import "SocialDetailTableViewCell.h"
- #import "SocialCommentDetailTableViewCell.h"
- #import "UIView+NTES.h"
- #import "SVProgressHUD.h"
- #import "UIAlertView+NTESBlock.h"
- #import "User.h"
- @interface SocialDetailViewController ()<UIScrollViewDelegate, UITextFieldDelegate>
- @property (nonatomic, strong) NSMutableArray *likeArr;
- @property (nonatomic, strong) NSMutableArray *commentArr;
- @property (nonatomic, strong) SocialItemData *data;
- @property (nonatomic, strong) NSMutableArray *dataArr;
- @property (nonatomic, assign) float infoHeight;
- @property (nonatomic, assign) BOOL isFetching;
- @property (nonatomic, assign) BOOL flagRefresh;
- @property (nonatomic, strong) UIView *commentContainer;
- @property (nonatomic, strong) UITextField *commentTextField;
- @property (nonatomic, strong) UIButton *commentBtn;
- @property (nonatomic, strong) NSString *replyId;
- @end
- @implementation SocialDetailViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- // if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
- // self.edgesForExtendedLayout = UIRectEdgeNone;
-
- [self.view addSubview:[self tableView]];
-
- _flagRefresh = NO;
-
- _commentContainer = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height - 50, self.view.width, 50)];
- _commentContainer.backgroundColor = UIColor.whiteColor;
- [self.view addSubview:_commentContainer];
-
- _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _commentBtn.frame = CGRectMake(_commentContainer.width - 50, 0, 50, _commentContainer.height);
- [_commentBtn setImage:[UIImage imageNamed:@"朋友圈详情_07"] forState:UIControlStateNormal];
- [_commentBtn addTarget:self action:@selector(onTouchComment:) forControlEvents:UIControlEventTouchUpInside];
- [_commentContainer addSubview:_commentBtn];
-
- UIView *txtBg = [[UIView alloc] initWithFrame:CGRectMake(10, 8, _commentContainer.width - 50 - 10, _commentContainer.height-16)];
- txtBg.backgroundColor = UIColorFromRGB(0xeeeeee);
- txtBg.layer.cornerRadius = 3;
- [_commentContainer addSubview:txtBg];
-
- _commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, txtBg.width - 20, txtBg.height)];
- _commentTextField.font = [UIFont systemFontOfSize:15];
- _commentTextField.placeholder = @"我要评论";
- _commentTextField.delegate = self;
- [txtBg addSubview:_commentTextField];
-
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
-
- [self.navigationItem setTitle:@"详情"];
- }
- - (UITableView *)tableView
- {
- if (!self.isViewLoaded) {
- return nil;
- }
- if (!_tableView) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStylePlain];
- _tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
- _tableView.backgroundColor = [UIColor clearColor];
- _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- _tableView.dataSource = self;
- _tableView.delegate = self;
- }
- return _tableView;
- }
- - (void)setSocialId:(NSString *)socialId{
- [SVProgressHUD show];
- [[HttpRequest shared] socialData:socialId success:^(SocialItemData * _Nullable socialData) {
- [SVProgressHUD dismiss];
- [self setSocialData:socialData];
- } failure:^{
- [SVProgressHUD dismiss];
- }];
- }
- - (void)setSocialData:(SocialItemData *)socialData{
-
- _data = socialData;
-
- _dataArr = [[NSMutableArray alloc] init];
- [_dataArr addObject:_data];
-
- [self fetchCommentList];
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if(_dataArr)
- return _dataArr.count;
- return 0;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- NSObject *dataObj = [_dataArr objectAtIndex:indexPath.row];
-
- UITableViewCell *baseCell = nil;
-
- if([dataObj isKindOfClass:[SocialItemData class]])
- {
- NSString *cellId = @"social";
- SocialDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
-
- if(!cell)
- {
- cell = [[SocialDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
- [cell initWidth:self.view.frame.size.width];
- }
-
- [cell setSocialData:(SocialItemData *)dataObj];
- _infoHeight = cell.cellHeight;
-
- baseCell = cell;
- }
- else if([dataObj isKindOfClass:[SocialCommentData class]])
- {
- NSString *cellId = @"comment";
- SocialCommentDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
-
- if(!cell)
- {
- cell = [[SocialCommentDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
- [cell initWidth:self.view.frame.size.width];
- }
-
- [cell setSocialCommentData:(SocialCommentData *)dataObj];
- baseCell = cell;
- }
- return baseCell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- NSObject *dataObj = [_dataArr objectAtIndex:indexPath.row];
-
- if([dataObj isKindOfClass:[SocialCommentData class]])
- {
- SocialCommentData *commentData = (SocialCommentData *)dataObj;
- return commentData.cellHeight;
- }
-
- return _infoHeight;
- }
- - (void)fetchCommentList{
-
- if(_isFetching)
- return;
-
- _isFetching = YES;
-
- NSString *lastId = nil;
- if(self.commentArr && self.commentArr.count){
- SocialCommentData *lastData = [self.commentArr lastObject];
- lastId = lastData.commentId;
- }
-
-
- [[HttpRequest shared] socialCommentList:_data.socialId lastId:lastId success:^(NSMutableArray * _Nullable dataList) {
- if(!self.commentArr)
- self.commentArr = dataList;
- else
- [self.commentArr addObjectsFromArray:dataList];
-
- [self.dataArr addObjectsFromArray:dataList];
-
- [self.tableView reloadData];
-
- _isFetching = NO;
- } failure:^{
- _isFetching = NO;
- }];
- }
- - (void)deleteSocial:(NSString *)socialId{
-
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"是否删除该动态?" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
- [alert showAlertWithCompletionHandler:^(NSInteger index) {
- if(index == 1)
- {
- [SVProgressHUD show];
- [[HttpRequest shared] deleteSocialData:socialId success:^(NSString * _Nullable socialId) {
- [SVProgressHUD dismiss];
-
- [self.navigationController popViewControllerAnimated:YES];
- } failure:^{
- [SVProgressHUD dismiss];
- }];
- }
- }];
-
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
- //康海涛测试Ok的
- CGPoint offset1 = scrollView.contentOffset;
- CGRect bounds1 = scrollView.bounds;
- CGSize size1 = scrollView.contentSize;
- UIEdgeInsets inset1 = scrollView.contentInset;
- float y1 = offset1.y + bounds1.size.height - inset1.bottom;
- float h1 = size1.height;
-
- //NSLog(@"%f %f %i %f", y1, h1, _flagRefresh, bounds1.size.height);
-
- if(h1 < bounds1.size.height)
- return;
-
- if (y1 > h1 + 50) {
- _flagRefresh = YES;
- }
- else if (y1 == h1) {
- if(_flagRefresh)
- {
- _flagRefresh = NO;
- NSLog(@"下拉刷新");
- [self fetchCommentList];
- }
- }
- }
- - (void)onTouchComment:(int)sender{
-
- if([_commentTextField.text isEqualToString:@""])
- {
- if(_commentContainer.bottom == self.view.bottom)
- {
- [self beginComment:nil];
- }
- else
- {
- [_commentTextField resignFirstResponder];
- }
- }
- else
- {
- [self sendComment:_commentTextField.text replyId:_replyId];
- }
-
- }
- - (void)beginComment:(NSString *)replyId{
-
- _replyId = replyId;
- _commentTextField.text = @"";
- if(_replyId && _replyId.length > 0)
- {
- NSString *nick = [[User sharedInfo] getUserNameById:replyId];
- _commentTextField.placeholder = [NSString stringWithFormat:@"回复%@", nick];
- }
- else
- {
- _commentTextField.placeholder = @"我要评论";
- }
- [_commentTextField becomeFirstResponder];
-
- }
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
- {
- if ([string isEqualToString:@"\n"]) {
- [self sendComment:textField.text replyId:_replyId];
- return NO;
- }
- return YES;
- }
- - (void)sendComment:(NSString *)comment replyId:(NSString *)replyId{
-
- NSLog(@"sender %@", comment);
-
- if(comment && ![comment isEqualToString:@""])
- {
- [[HttpRequest shared] socialComment:_data.socialId replyId:replyId comment:comment success:^(SocialCommentData * _Nullable commentData, int commentCount) {
-
- _data.comments = commentCount;
- [self fetchCommentList];
-
- } failure:^{
-
- }];
- }
-
- _commentTextField.text = @"";
- [_commentTextField resignFirstResponder];
- }
- - (void)keyboardWillChangeFrame:(NSNotification *)notification
- {
- NSDictionary *userInfo = notification.userInfo;
- CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
- //CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
-
- float keyBoardFrameTop = endFrame.origin.y;
- _commentContainer.bottom = keyBoardFrameTop;
- }
- @end
|