// // XPMineUserDataViewController.m // xplan-ios // // Created by 冯硕 on 2022/4/14. // #import "XPMineUserDataViewController.h" ///Third #import ///Tool #import "AccountInfoStorage.h" #import "XPMomentsLayoutConfig.h" ///View #import "XPMineDataGiftTableViewCell.h" #import "XPMomentsTableViewCell.h" #import "XPMomentsEmptyTableViewCell.h" ///Model #import "UserInfoModel.h" #import "MineSkillCardListInfoModel.h" ///P #import "XPMineUserDataPresenter.h" #import "XPMineUserDataProtocol.h" #import "XPMomentsMineProtocol.h" ///View #import "XPMineUserInfoGiftWallViewController.h" #import "XPMineGuildViewController.h" #import "XPMomentsDetailViewController.h" #import "XPMineUserInfoAlbumViewController.h" #import "XPMineDataGiftTableViewCell.h" #import "XPMineAlbumTableViewCell.h" #import "XPMineMedalsTableViewCell.h" #import "XPMineGiftsTableViewCell.h" #import "XPMineDataGameMateTableViewCell.h" #import "XPMineGameMateOrderView.h" #import "MedalModel.h" #import "XPMineGamePartnerInfoModel.h" typedef enum : NSUInteger { GameMate = 0, Album = 1, Medal = 2, Gifts = 3, LuckyGifts = 4, SectionTypeCount = 5 } MineUserInfoPageSectionType; @interface XPMineUserSectionHeader : UIView @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *num; @property (nonatomic, assign) BOOL hasMore; @property (nonatomic, copy) void(^didTapMore)(void); @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *numLabel; @property (nonatomic, strong) UIButton *arrowButton; @end @implementation XPMineUserSectionHeader - (instancetype)init { if (self = [super init]) { self.frame = CGRectMake(0, 0, KScreenWidth, 20); _titleLabel = [UILabel labelInitWithText:@"" font:[UIFont systemFontOfSize:16 weight:UIFontWeightBold] textColor:UIColorFromRGB(0x191919)]; [self addSubview:_titleLabel]; [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.centerY.mas_equalTo(self); }]; _numLabel = [UILabel labelInitWithText:@"" font:[UIFont systemFontOfSize:16 weight:UIFontWeightBold] textColor:UIColorFromRGB(0x666666)]; [self addSubview:_numLabel]; [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self); make.leading.mas_equalTo(self.titleLabel.mas_trailing).offset(4); }]; _arrowButton = [UIButton buttonWithType:UIButtonTypeCustom]; if (isMSRTL()) { _arrowButton.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft; } if (isMSRTL()) { [_arrowButton setBackgroundImage:[[UIImage imageNamed:@"skillCard_arrow"] ms_SetImageForRTL] forState:UIControlStateNormal]; } else { [_arrowButton setBackgroundImage:[UIImage imageNamed:@"skillCard_arrow"] forState:UIControlStateNormal]; } [_arrowButton addTarget:self action:@selector(didTapArrowButton) forControlEvents:UIControlEventTouchUpInside]; [_arrowButton setEnlargeEdgeWithTop:5 right:10 bottom:10 left:10]; [self addSubview:_arrowButton]; [_arrowButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self); make.size.mas_equalTo(CGSizeMake(20, 20)); make.trailing.mas_equalTo(self).offset(0); }]; } return self; } - (void)setTitle:(NSString *)title { _titleLabel.text = title; } - (void)setNum:(NSString *)num { _numLabel.text = num; } - (void)setHasMore:(BOOL)hasMore { _arrowButton.hidden = !hasMore; } - (void)didTapArrowButton { if (self.didTapMore) { self.didTapMore(); } } @end @interface XPMineUserDataViewController () ///列表 @property (nonatomic,strong) UITableView *tableView; ///是否折叠 @property (nonatomic,assign) BOOL isFold; @property (nonatomic,assign) BOOL isGiftsSectionExpand; @property (nonatomic,assign) BOOL isLuckyGiftsSectionExpand; @property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView); ///数据源 @property (nonatomic,strong) NSMutableArray *datasource; //@property (nonatomic,strong) UIView *headView; @property (nonatomic, strong) UIButton *giftsExpandButton; @property (nonatomic, strong) UIButton *luckyGiftsExpandButton; /// @property (nonatomic,assign) BOOL isOpen; @property (nonatomic, strong) XPMineUserSectionHeader *gameMateHeader; @property (nonatomic, strong) XPMineUserSectionHeader *albumHeader; @property (nonatomic, strong) XPMineUserSectionHeader *medalHeader; @property (nonatomic, strong) XPMineUserSectionHeader *giftsHeader; @property (nonatomic, strong) XPMineUserSectionHeader *luckyGiftsHeader; @property (nonatomic, copy) NSArray *gameInfos; @end @implementation XPMineUserDataViewController - (BOOL)isHiddenNavBar { return YES; } - (__kindof id)createPresenter { return [[XPMineUserDataPresenter alloc] init]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self initSubViews]; [self initSubViewConstraints]; } #pragma mark - Private Method - (void)initSubViews { [self.view addSubview:self.tableView]; } - (void)initSubViewConstraints { [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.view).offset(14); make.bottom.mas_equalTo(self.view).offset(-14); make.leading.mas_equalTo(self.view).offset(14); make.trailing.mas_equalTo(self.view).offset(-14); }]; } - (void)didTapGiftsSectionFoldButton { self.isGiftsSectionExpand = !self.isGiftsSectionExpand; self.giftsExpandButton.transform = self.isGiftsSectionExpand ? CGAffineTransformMakeRotation(M_PI) : CGAffineTransformIdentity; [self.tableView reloadData]; } - (void)didTapLuckyGiftsSectionFoldButton { self.isLuckyGiftsSectionExpand = !self.isLuckyGiftsSectionExpand; self.luckyGiftsExpandButton.transform = self.isLuckyGiftsSectionExpand ? CGAffineTransformMakeRotation(M_PI) : CGAffineTransformIdentity; [self.tableView reloadData]; } - (void)submitGameOrder:(XPMineGamePartnerInfoModel *)model round:(NSInteger)round { @kWeakify(self); [self showLoading]; [self.presenter submitOrder:^{ @kStrongify(self); [self showSuccessToast:YMLocalizedString(@"GameOrderContent_18")]; if (self.delegate) { [self.delegate dismissGameOrderView]; } } failure:^(NSString * _Nonnull msg) { @kStrongify(self); [self showErrorToast:msg]; } gameId:model.gameId gameUid:model.uid inning:round]; } #pragma mark - UITableViewDelegate And UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return SectionTypeCount; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch (section) { case GameMate: return self.gameInfos.count; break; case Album: return 1; break; case Medal: return self.medalInfo.medalCount == 0 ? 0 : 1; break; case Gifts: return 1; break; case LuckyGifts: return 1; break; default: return 0; break; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case GameMate: return 78.5; break; case Album: return 90; break; case Medal: return 140; break; case Gifts: return [XPMineGiftsTableViewCell cellHeight:self.isGiftsSectionExpand source:self.giftWall]; break; case LuckyGifts: return [XPMineGiftsTableViewCell cellHeight:self.isLuckyGiftsSectionExpand source:self.luckyGiftWall]; break; default: return 0; break; } return KScreenHeight - kNavigationHeight - 49 - kSafeAreaBottomHeight; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ switch (section) { case GameMate: return nil; break; case Album: return nil; break; case Medal: return nil; break; case Gifts: return self.giftWall.count > 4 ? self.giftsExpandButton : nil; break; case LuckyGifts: return self.luckyGiftWall.count > 4 ? self.luckyGiftsExpandButton : nil; break; default: return 0; break; } } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ switch (section) { case GameMate: return 0; break; case Album: return 0; break; case Medal: return 0; break; case Gifts: return self.giftWall.count > 4 ? 20 : 0; break; case LuckyGifts: return self.luckyGiftWall.count > 4 ? 20 : 0; break; default: return 0; break; } } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ switch (section) { case GameMate: return self.gameInfos.count > 0 ? 40 : 0; break; case Album: return 40; break; case Medal: return self.medalInfo.medalCount == 0 ? 0 : 30; break; case Gifts: return 50; break; case LuckyGifts: return 50; break; default: return 40; break; } } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ switch (section) { case GameMate: return self.gameInfos.count > 0 ? self.gameMateHeader : nil; case Album: return self.albumHeader; break; case Medal: return self.medalInfo.medalCount == 0 ? [UIView new] : self.medalHeader; break; case Gifts: return self.giftsHeader; break; case LuckyGifts: return self.luckyGiftsHeader; break; default: return [UIView new]; break; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case GameMate: { XPMineDataGameMateTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineDataGameMateTableViewCell class]) forIndexPath:indexPath]; cell.cellModel = [self.gameInfos xpSafeObjectAtIndex:indexPath.row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; @kWeakify(self); [cell setDidTapOrder:^(XPMineGamePartnerInfoModel *model) { @kStrongify(self); if (self.delegate) { [self.delegate showGameOrderView:model]; } }]; return cell; } break; case Album: { XPMineAlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineAlbumTableViewCell class]) forIndexPath:indexPath]; cell.albumDataSource = self.userInfo.privatePhoto; return cell; } break; case Medal: { XPMineMedalsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineMedalsTableViewCell class]) forIndexPath:indexPath]; cell.medalsDataSource = self.medalInfo.userMedals; return cell; } break; case Gifts:{ XPMineGiftsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineGiftsTableViewCell class]) forIndexPath:indexPath]; cell.giftsDataSource = self.giftWall; cell.isLucky = NO; return cell; } break; case LuckyGifts:{ XPMineGiftsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineGiftsTableViewCell class]) forIndexPath:indexPath]; cell.giftsDataSource = self.luckyGiftWall; cell.isLucky = YES; return cell; } break; default: return [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMomentsEmptyTableViewCell class])]; break; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.section == 0)return; [tableView deselectRowAtIndexPath:indexPath animated:YES]; if (self.datasource.count > 0) { XPMomentsDetailViewController * detailVC = [[XPMomentsDetailViewController alloc] init]; MomentsInfoModel * monentsInfo = [self.datasource xpSafeObjectAtIndex:indexPath.row]; if(monentsInfo.dynamicId == nil){ return; } detailVC.momentsInfo = monentsInfo; detailVC.delegate = self; [self.navigationController pushViewController:detailVC animated:YES]; } } #pragma mark - XPMomentsTableViewCellDelegate - (void)XPMomentsTableViewCell:(XPMomentsTableViewCell *)view didClickLike:(MomentsInfoModel *)monentsInfo { if(monentsInfo.dynamicId == nil){ [self showErrorToast:YMLocalizedString(@"XPMineUserDataViewController2")]; return; } [self.presenter likeMonent:monentsInfo.dynamicId status:!monentsInfo.isLike likedUid:monentsInfo.uid worldId:[NSString stringWithFormat:@"%ld", monentsInfo.worldId]]; } - (void)XPMomentsTableViewCell:(XPMomentsTableViewCell *)view didClicCommon:(MomentsInfoModel *)monentsInfo{ if(monentsInfo.dynamicId == nil){ [self showErrorToast:YMLocalizedString(@"XPMineUserDataViewController3")]; return; } XPMomentsDetailViewController * detailVC = [[XPMomentsDetailViewController alloc] init]; detailVC.momentsInfo = monentsInfo; detailVC.delegate = self; [self.navigationController pushViewController:detailVC animated:YES]; } - (void)XPMomentsTableViewCell:(XPMomentsTableViewCell *)view didClicDelete:(MomentsInfoModel *)monentsInfo { [TTPopup alertWithMessage:YMLocalizedString(@"XPMonentsMineViewController0") confirmHandler:^{ [self.presenter deleteMonents:monentsInfo.dynamicId worldId:[NSString stringWithFormat:@"%ld", monentsInfo.worldId]]; } cancelHandler:^{ }]; } - (void)XPMomentsTableViewCell:(XPMomentsTableViewCell *)view didClicShielding:(nonnull MomentsInfoModel *)monentsInfo{ [self showLoading]; [self.presenter requesstShieldingWtihType:@"0" objId:monentsInfo.dynamicId]; } - (void)XPMomentsTableViewCell:(XPMomentsTableViewCell *)view didClicFold:(MomentsInfoModel *)monentsInfo { __block MomentsInfoModel * monentsInfos; [self.datasource enumerateObjectsUsingBlock:^(MomentsInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.dynamicId.integerValue == monentsInfo.dynamicId.integerValue) { monentsInfos = obj; *stop = YES; } }]; if (monentsInfos) { NSInteger section = [self.datasource indexOfObject:monentsInfo]; [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:section inSection:1]] withRowAnimation:UITableViewRowAnimationNone]; } } #pragma mark - XPMomentsDetailViewControllerDelegate - (void)XPMomentsDetailViewController:(XPMomentsDetailViewController *)view deleteMoments:(NSString *)dynamicId { __block MomentsInfoModel * deleteInfo; [self.datasource enumerateObjectsUsingBlock:^(MomentsInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.dynamicId.integerValue == dynamicId.integerValue) { deleteInfo = obj; } }]; if (deleteInfo) { [self.datasource removeObject:deleteInfo]; [self.tableView reloadData]; } } #pragma mark - XPMomentsMineProtocol - (void)requesstShieldingSuccess:(NSString *)monentsInfo{ [self hideHUD]; [self showSuccessToast:YMLocalizedString(@"XPMomentsMineViewController2")]; __block MomentsInfoModel * deleteInfo; [self.datasource enumerateObjectsUsingBlock:^(MomentsInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.dynamicId.integerValue == monentsInfo.integerValue) { deleteInfo = obj; } }]; if (deleteInfo) { [self.datasource removeObject:deleteInfo]; [self.tableView reloadData]; } } - (void)likeMomentsSuccess:(NSString *)dynamicId status:(BOOL)status { [self.datasource enumerateObjectsUsingBlock:^(MomentsInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj.dynamicId isEqualToString:dynamicId]) { NSInteger likeCount = obj.likeCount.integerValue; if (status) { likeCount += 1; obj.isLike += 1; } else { likeCount -= 1; obj.isLike -= 1; } obj.likeCount = @(likeCount).stringValue; *stop = YES; } }]; [self.tableView reloadData]; } - (void)deleteMomentsSuccess:(NSString *)monentsInfo { [self showSuccessToast:YMLocalizedString(@"XPMomentsMineViewController1")]; __block MomentsInfoModel * deleteInfo; [self.datasource enumerateObjectsUsingBlock:^(MomentsInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.dynamicId.integerValue == monentsInfo.integerValue) { deleteInfo = obj; } }]; if (deleteInfo) { [self.datasource removeObject:deleteInfo]; [self.tableView reloadData]; } } #pragma mark - XPMineDataGiftTableViewCellDelegate - (void)xPMineDataGiftTableViewCell:(XPMineDataGiftTableViewCell *)view didClickMore:(UIButton *)sender { XPMineUserInfoGiftWallViewController * giftWallVC = [[XPMineUserInfoGiftWallViewController alloc] init]; giftWallVC.userUid = self.userUid; [self.navigationController pushViewController:giftWallVC animated:YES]; } #pragma mark - JXPagingViewListViewDelegate - (UIView *)listView { return self.view; } - (UIScrollView *)listScrollView { return self.tableView; } - (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback { self.scrollCallback = callback; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.scrollCallback(scrollView); } #pragma mark - Getters And Setters - (void)setUserInfo:(UserInfoModel *)userInfo { _userInfo = userInfo; self.albumHeader.num = [NSString stringWithFormat:@"(%ld)", (long)userInfo.privatePhoto.count]; [self.tableView reloadData]; if (userInfo.uid != [[AccountInfoStorage instance].getUid integerValue]) { self.albumHeader.hasMore = NO; } @kWeakify(self); [self.presenter loadGamePartnerInfoList:^(NSArray * _Nonnull infos) { @kStrongify(self); self.gameInfos = infos; [self.tableView reloadData]; } uid:userInfo.uid]; } - (void)setDynamicInfo:(NSArray *)dynamicInfo { [self.datasource removeAllObjects]; [self.datasource addObjectsFromArray:dynamicInfo]; [self.tableView reloadData]; } - (void)setMedalInfo:(MedalModel *)medalInfo { _medalInfo = medalInfo; self.medalHeader.num = [NSString stringWithFormat:@"(%ld)", (long)medalInfo.medalCount]; [self.tableView reloadData]; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = [UIColor whiteColor]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } [_tableView registerClass:[XPMineDataGameMateTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMineDataGameMateTableViewCell class])]; [_tableView registerClass:[XPMineAlbumTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMineAlbumTableViewCell class])]; [_tableView registerClass:[XPMineMedalsTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMineMedalsTableViewCell class])]; [_tableView registerClass:[XPMineGiftsTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMineGiftsTableViewCell class])]; [_tableView registerClass:[XPMomentsEmptyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMomentsEmptyTableViewCell class])]; [_tableView registerClass:[XPMineDataGiftTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMineDataGiftTableViewCell class])]; if (isMSRTL()) { _tableView.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft; } } return _tableView; } - (NSMutableArray *)datasource { if (!_datasource) { _datasource = [NSMutableArray array]; } return _datasource; } - (XPMineUserSectionHeader *)gameMateHeader { if (!_gameMateHeader) { _gameMateHeader = [[XPMineUserSectionHeader alloc] init]; _gameMateHeader.title = YMLocalizedString(@"GameOrderContent_0"); _gameMateHeader.hasMore = NO; } return _gameMateHeader; } - (XPMineUserSectionHeader *)albumHeader { if (!_albumHeader) { _albumHeader = [[XPMineUserSectionHeader alloc] init]; _albumHeader.title = YMLocalizedString(@"QinputPhotoView1"); _albumHeader.hasMore = YES; @kWeakify(self); _albumHeader.didTapMore = ^{ @kStrongify(self); XPMineUserInfoAlbumViewController * albumVC = [[XPMineUserInfoAlbumViewController alloc] init]; [self.navigationController pushViewController:albumVC animated:YES]; }; } return _albumHeader; } - (XPMineUserSectionHeader *)medalHeader { if (!_medalHeader) { _medalHeader = [[XPMineUserSectionHeader alloc] init]; _medalHeader.title = YMLocalizedString(@"XPMineDataGiftTableViewCell2"); _medalHeader.num = @"(0)"; _medalHeader.hasMore = NO; } return _medalHeader; } - (XPMineUserSectionHeader *)giftsHeader { if (!_giftsHeader) { _giftsHeader = [[XPMineUserSectionHeader alloc] init]; _giftsHeader.title = YMLocalizedString(@"XPMineDataGiftTableViewCell0"); _giftsHeader.hasMore = NO; } return _giftsHeader; } - (XPMineUserSectionHeader *)luckyGiftsHeader { if (!_luckyGiftsHeader) { _luckyGiftsHeader = [[XPMineUserSectionHeader alloc] init]; _luckyGiftsHeader.title = YMLocalizedString(@"XPMineDataGiftTableViewCell1"); _luckyGiftsHeader.hasMore = NO; } return _luckyGiftsHeader; } - (UIButton *)giftsExpandButton { if (!_giftsExpandButton) { _giftsExpandButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_giftsExpandButton setImage:[UIImage imageNamed:@"common_down_arrow"] forState:UIControlStateNormal]; [_giftsExpandButton addTarget:self action:@selector(didTapGiftsSectionFoldButton) forControlEvents:UIControlEventTouchUpInside]; _giftsExpandButton.selected = YES; [_giftsExpandButton setEnlargeEdgeWithTop:5 right:10 bottom:10 left:10]; } return _giftsExpandButton; } - (UIButton *)luckyGiftsExpandButton { if (!_luckyGiftsExpandButton) { _luckyGiftsExpandButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_luckyGiftsExpandButton setImage:[UIImage imageNamed:@"common_down_arrow"] forState:UIControlStateNormal]; [_luckyGiftsExpandButton addTarget:self action:@selector(didTapLuckyGiftsSectionFoldButton) forControlEvents:UIControlEventTouchUpInside]; _luckyGiftsExpandButton.selected = YES; [_luckyGiftsExpandButton setEnlargeEdgeWithTop:5 right:10 bottom:10 left:10]; } return _luckyGiftsExpandButton; } @end