// // MessageNotificationListViewController.m // Youth // // Created by ko1o on 2022/1/9. // #import "MessageNotificationListViewController.h" #import "MessageNotificationCell.h" @interface MessageNotificationListViewController () @end @implementation MessageNotificationListViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"互动通知"; [self.tableView registerClass:[MessageNotificationCell class] forCellReuseIdentifier:NSStringFromClass(MessageNotificationCell.class)]; [self startLoading]; } - (NSString *)emptyTips { return @"暂无通知,去别处玩玩吧"; } - (BOOL)mt_shouldSetupStateful { return YES; } - (BOOL)refreshEnable { return NO; } - (BOOL)loadMoreEnable { return YES; } - (Class)cellClass { return [MessageNotificationCell class]; } - (BOOL)mt_hasContentForStateful { return self.itemsM.count; } - (void)startLoading { [super startLoading]; self.itemsM = [NSMutableArray array]; [PYHTTPManager postWithPath:@"notice" params:@{ @"page_size": @(20), @"start_time": @(0) } callback:^(id _Nullable rsp, NSError * _Nullable error) { [self.tableView.mj_footer endRefreshing]; if (!error) { NSLog(@"%@", rsp); NSArray *items = [MessageNotification mj_objectArrayWithKeyValuesArray:rsp]; self.itemsM = [items mutableCopy]; self.totalCount = items.count < 20 ? self.itemsM.count : self.itemsM.count + 1; [self.tableView reloadData]; } [self endLoading:nil]; }]; } - (void)startLoadMore { [super startLoadMore]; MessageNotification *model = [self.itemsM lastObject]; [PYHTTPManager postWithPath:@"notice" params:@{ @"page_size": @(20), @"start_time": @(model.created_at) } callback:^(id _Nullable rsp, NSError * _Nullable error) { [self.tableView.mj_footer endRefreshing]; if (!error) { NSArray *items = [MessageNotification mj_objectArrayWithKeyValuesArray:rsp]; [self.itemsM addObjectsFromArray:items]; self.totalCount = items.count < 20 ? self.itemsM.count : self.itemsM.count + 1; [self.tableView reloadData]; } [self endLoading:nil]; }]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MessageNotificationCell *cell = (MessageNotificationCell *)[super tableView:tableView cellForRowAtIndexPath:indexPath]; cell.action = ^(UIButton * _Nonnull button) { MessageNotification *model = self.itemsM[indexPath.row]; if ([model.user_v_info isFollowing]) { [PYAppService chatWithUser:model.user]; } else { [UserService followUserWIthUserID:model.user.ID completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { // 刷新UI for (MessageNotification *item in self.itemsM) { if (item.user.ID == model.user.ID) { [item.user_v_info updateFollowState:YES]; } } [tableView reloadData]; } }]; } }; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.tableView fd_heightForCellWithIdentifier:NSStringFromClass(MessageNotificationCell.class) configuration:^(MessageNotificationCell *cell) { cell.model = self.itemsM[indexPath.row]; }]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; // MessageNotification *model = self.itemsM[indexPath.row]; // [PYAppService handleUrl:model.url]; } @end