Files
yuyin_ios/SweetParty/主类/Dynamic/Controller/SPTrendDetailVC.m
2025-08-08 11:05:33 +08:00

272 lines
8.9 KiB
Objective-C
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SPTrendDetailVC.m
// SweetParty
//
// Created by bj_szd on 2022/6/6.
//
#import "SPTrendDetailVC.h"
#import "SPTrendListCell.h"
#import "SPTrendCommentCell.h"
#import "SPTrendInsertView.h"
@interface SPTrendDetailVC ()
@property (nonatomic, strong) SPTrendInsertView *insertView;
@property (strong, nonatomic) SPTrendListModel *model;
@end
@implementation SPTrendDetailVC
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self showNaviBarWithTitle:@"动态详情"];
[self createUI];
[self fetchArticleData];
//监听键盘
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardWillShow:(NSNotification *)noti {
CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect frame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:duration animations:^{
self.insertView.transform = CGAffineTransformMakeTranslation(0, -frame.size.height+SAFE_AREA_INSERTS_BOTTOM);
}];
}
-(void)keyboardWillHide:(NSNotification *)noti {
CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:duration animations:^{
self.insertView.transform = CGAffineTransformIdentity;
}];
}
-(void)createUI {
UIImageView *bgImgV = [[UIImageView alloc] initWithImage:ImageNamed(@"home_bg")];
[self.view addSubview:bgImgV];
[self.view sendSubviewToBack:bgImgV];
[bgImgV mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self.view);
make.height.mas_equalTo(ScreenWidth/375*812);
}];
[self.view addSubview:self.insertView];
[self.insertView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.bottom.equalTo(self.view);
make.height.mas_equalTo(52+SAFE_AREA_INSERTS_BOTTOM);
}];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(TOP_BAR_HEIGHT);
make.left.right.equalTo(self.view);
make.bottom.equalTo(self.insertView.mas_top);
}];
[self.tableView registerNib:[UINib nibWithNibName:@"SPTrendListCell" bundle:nil] forCellReuseIdentifier:@"SPTrendListCell"];
[self.tableView registerNib:[UINib nibWithNibName:@"SPTrendCommentCell" bundle:nil] forCellReuseIdentifier:@"SPTrendCommentCell"];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 152;
[self showPullToRefresh];
[self showLoadMoreRefresh];
}
- (void)fetchArticleData {
NSDictionary *params = @{@"zid":C_string(self.zid)};
[AFNetworkRequset.shared postRequestWithParams:params Path:@"api/user_zone/get_zone_info" Loading:NO Hud:NO Success:^(id _Nonnull responseDic) {
SPTrendListModel *model = [SPTrendListModel mj_objectWithKeyValues:responseDic[@"data"]];
self.model = model;
[self fetchData];
[self onUpdateBottomUI];
} Failure:^(id _Nonnull errorData) {
}];
}
- (void)onUpdateBottomUI {
[self.insertView.commentBtn setTitle:self.model.comment_num forState:UIControlStateNormal];
self.insertView.zanBtn.selected = self.model.is_praise;
[self.insertView.zanBtn setTitle:[NSString stringWithFormat:@"%ld", self.model.praise_num] forState:UIControlStateNormal];
}
- (void)fetchData {
NSDictionary *params = @{@"page":@(self.page), @"page_limit":@(10), @"zid":C_string(self.zid)};
[AFNetworkRequset.shared postRequestWithParams:params Path:@"api/User_Zone/get_comment_list" Loading:NO Hud:NO Success:^(id _Nonnull responseDic) {
if (self.page == 1) {
[self.dataArray removeAllObjects];
[self.tableView reloadData];
}
[self endRefresh];
NSArray *arr = [SPTrendCommentModel mj_objectArrayWithKeyValuesArray:responseDic[@"data"]];
[self.dataArray addObjectsFromArray:arr];
[self.tableView reloadData];
if (arr.count > 0) {
[self endFooterRefreshWithMore];
}else {
[self endFooterRefreshWithNoMore];
}
// if (self.dataArray.count <= 0) {
// [self showNoContentView];
// } else {
// [self hideNoContentView];
// }
} Failure:^(id _Nonnull errorData) {
}];
}
- (void)refreshFetchData {
self.page = 1;
[self fetchArticleData];
}
- (void)fetchMoreData {
self.page ++;
[self fetchData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (0 == section) {
return 1;
}else {
return self.dataArray.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (0 == indexPath.section) {
SPTrendListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SPTrendListCell" forIndexPath:indexPath];
cell.selectionStyle = NO;
cell.model = self.model;
WEAK_SELF
cell.onDeleteBlock = ^{
[weakSelf onDelete];
};
return cell;
}else {
SPTrendCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SPTrendCommentCell" forIndexPath:indexPath];
cell.selectionStyle = NO;
SPTrendCommentModel *model = self.dataArray[indexPath.row];
cell.model = model;
return cell;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (1 == section) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 52)];
view.backgroundColor = [UIColor clearColor];
UILabel *titleLabel = [ControlCreator createLabel:view rect:CGRectMake(14, 16, 150, 16) text:@"全部评论" font:YBBoldFont(16) color:HEXCOLOR(0x212121) backguoundColor:nil align:NSTextAlignmentLeft lines:1];
titleLabel.text = [NSString stringWithFormat:@"全部评论(%@",self.model.comment_num];
return view;
}else {
return nil;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (1 == section) {
return 52;
}else {
return 0;
}
}
-(void)onDelete {
WEAK_SELF
NSDictionary *params = @{@"zid":C_string(self.model.zid)};
[[AFNetworkRequset shared] postRequestWithParams:params Path:@"api/User_Zone/delete_zone" Loading:YES Hud:YES Success:^(id _Nonnull responseDic) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (weakSelf.onDeleteBlock) {
weakSelf.onDeleteBlock(weakSelf.model.zid);
}
[weakSelf.navigationController popViewControllerAnimated:YES];
});
} Failure:^(id _Nonnull errorData) {
}];
}
- (void)onSend {
NSString *insertStr = self.insertView.insertTF.text;
if (insertStr.length <= 0) {
[HelpPageDefine showMessage:@"请输入评论内容"];
return;
}
NSDictionary *params = @{@"zid":C_string(self.model.zid), @"content":insertStr};
[AFNetworkRequset.shared postRequestWithParams:params Path:@"api/User_Zone/comment_zone" Loading:YES Hud:YES Success:^(id _Nonnull responseDic) {
[self refreshFetchData];
self.insertView.insertTF.text = nil;
} Failure:^(id _Nonnull errorData) {
}];
}
- (SPTrendInsertView *)insertView {
if (!_insertView) {
_insertView = [[NSBundle mainBundle] loadNibNamed:@"SPTrendInsertView" owner:self options:nil].firstObject;
WEAK_SELF
_insertView.onSendBlock = ^{
[weakSelf onSend];
};
[_insertView.zanBtn buttonAddTaget:^(UIButton *btn) {
[weakSelf onZan:nil];
} forControlEvents:UIControlEventTouchUpInside];
}
return _insertView;
}
- (IBAction)onZan:(id)sender {
NSDictionary *params = @{@"zid":C_string(self.model.zid)};
[[AFNetworkRequset shared] postRequestWithParams:params Path:@"api/User_Zone/praise_zone" Loading:YES Hud:YES Success:^(id _Nonnull responseDic) {
SPTrendListModel *model = self.model;
model.is_praise = !model.is_praise;
if (model.is_praise) {
model.praise_num += 1;
}else {
model.praise_num -= 1;
}
self.model = model;
[self onUpdateBottomUI];
} Failure:^(id _Nonnull errorData) {
}];
}
@end