// // MinePageMienBodyView.m // KulexiuForTeacher // // Created by Kyle on 2022/3/30. // #import "MinePageMienBodyView.h" #import "MineIntroduceCell.h" #import "MineVideoCell.h" #import "MineFansGroupCell.h" #import "StyleVideoModel.h" #import "WMPlayer.h" #import "MinePageGroupModel.h" #import "KSChatConversationViewController.h" #import "MineEmptyGroupCell.h" #import "MineEmptyIntroduceCell.h" #import "MineEmptyVideoCell.h" #import "MyStyleViewController.h" #import "CreateFansGroupViewController.h" @interface MinePageMienBodyView () { WMPlayer *_wmPlayer; CGRect _playerFrame; } @property (nonatomic, strong) UIView *bgView; @property (nonatomic, assign) BOOL isRatation; @property (nonatomic, strong) NSMutableArray *fansGroupArray; @property (nonatomic, strong) NSString *content; @property (nonatomic, strong) NSMutableArray *videoArray; @end @implementation MinePageMienBodyView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = HexRGB(0xf6f8f9); self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStyleGrouped]; self.tableView.backgroundColor = HexRGB(0xf6f8f9); self.tableView.showsVerticalScrollIndicator = NO; self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 200.0f; self.tableView.dataSource = self; self.tableView.delegate = self; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self addSubview:self.tableView]; UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)]; bottomView.backgroundColor = HexRGB(0xf6f8f9); self.tableView.tableFooterView = bottomView; [self.tableView registerNib:[UINib nibWithNibName:@"MineIntroduceCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MineIntroduceCell"]; [self.tableView registerNib:[UINib nibWithNibName:@"MineVideoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MineVideoCell"]; [self.tableView registerNib:[UINib nibWithNibName:@"MineFansGroupCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MineFansGroupCell"]; [self.tableView registerNib:[UINib nibWithNibName:@"MineEmptyIntroduceCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MineEmptyIntroduceCell"]; [self.tableView registerNib:[UINib nibWithNibName:@"MineEmptyVideoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MineEmptyVideoCell"]; [self.tableView registerNib:[UINib nibWithNibName:@"MineEmptyGroupCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MineEmptyGroupCell"]; MJWeakSelf; self.tableView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{ [weakSelf requestData]; [weakSelf requestTeacherGroup]; }]; } return self; } - (void)requestData { [KSNetworkingManager queryTeacherStyleRequest:KS_GET success:^(NSDictionary * _Nonnull dic) { [self endRefresh]; if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) { NSDictionary *result = [dic dictionaryValueForKey:@"data"]; self.content = [result stringValueForKey:@"introduction"]; NSArray *videoList = [result arrayValueForKey:@"styleVideo"]; [self.videoArray removeAllObjects]; for (NSDictionary *parm in videoList) { StyleVideoModel *model = [[StyleVideoModel alloc] initWithDictionary:parm]; [self.videoArray addObject:model]; } } else { [self MBPShow:MESSAGEKEY]; } [self.tableView reloadData]; } faliure:^(NSError * _Nonnull error) { [self endRefresh]; }]; } - (void)requestTeacherGroup { [KSNetworkingManager queryTeacherGroupRequest:KS_POST success:^(NSDictionary * _Nonnull dic) { if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) { NSArray *sourceArray = [dic arrayValueForKey:@"data"]; NSMutableArray *groupArray = [NSMutableArray array]; for (NSDictionary *parm in sourceArray) { MinePageGroupModel *model = [[MinePageGroupModel alloc] initWithDictionary:parm]; [groupArray addObject:model]; } self.fansGroupArray = [NSMutableArray arrayWithArray:groupArray]; } else { [self MBPShow:MESSAGEKEY]; } [self.tableView reloadData]; } faliure:^(NSError * _Nonnull error) { }]; } - (void)endRefresh { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.tableView.mj_header endRefreshing]; }); } - (void)beginRefreshImmediately { [self.tableView.mj_header beginRefreshing]; } - (void)beginFirstRefresh { if (!self.isHeaderRefreshed) { [self beginRefreshImmediately]; } } - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath { if (self.lastSelectedIndexPath == indexPath) { return; } if (self.lastSelectedIndexPath != nil) { UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.lastSelectedIndexPath]; [cell setSelected:NO animated:NO]; } UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; [cell setSelected:YES animated:NO]; self.lastSelectedIndexPath = indexPath; } - (void)layoutSubviews { [super layoutSubviews]; self.tableView.frame = self.bounds; } #pragma mark --- table data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 2) { if (self.fansGroupArray.count == 0) { return 1; } return self.fansGroupArray.count; } return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { if ([NSString isEmptyString:self.content]) { MineEmptyIntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineEmptyIntroduceCell"]; MJWeakSelf; [cell emptyIntroduceCallback:^{ [weakSelf createStyleAction]; }]; return cell; } else { MineIntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineIntroduceCell"]; [cell configIntroduce:self.content]; return cell; } } else if (indexPath.section == 1) { if (self.videoArray.count > 0) { MineVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineVideoCell"]; MJWeakSelf; [cell configWithSource:self.videoArray]; [cell opreationCallback:^(UIView * _Nullable containerView) { NSInteger index = containerView.tag - 1000; if (weakSelf.videoArray.count > index) { StyleVideoModel *model = weakSelf.videoArray[index]; [weakSelf playVideoWithUrl:model.videoUrl]; } }]; return cell; } else { MineEmptyVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineEmptyVideoCell"]; MJWeakSelf; [cell emptyVideoCallback:^{ [weakSelf createStyleAction]; }]; return cell; } } else { if (self.fansGroupArray.count == 0) { MineEmptyGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineEmptyGroupCell"]; MJWeakSelf; [cell emptyGroupCallback:^{ [weakSelf createGroup]; }]; return cell; } else { MinePageGroupModel *model = self.fansGroupArray[indexPath.row]; MineFansGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineFansGroupCell"]; MJWeakSelf; [cell configWithSource:model chatAction:^(NSString *groupName, NSString *groupId) { [weakSelf chatAction:groupId groupName:groupName]; }]; return cell; } } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2 && self.fansGroupArray.count != 0) { MinePageGroupModel *model = self.fansGroupArray[indexPath.row]; [self chatAction:model.internalBaseClassIdentifier groupName:model.name]; } } - (void)createStyleAction { MyStyleViewController *style = [[MyStyleViewController alloc] init]; [self.naviController pushViewController:style animated:YES]; } - (void)createGroup { CreateFansGroupViewController *ctrl = [[CreateFansGroupViewController alloc] init]; [self.naviController pushViewController:ctrl animated:YES]; } - (void)chatAction:(NSString *)groupId groupName:(NSString *)groupName { KSChatConversationViewController *conversationVC = [[KSChatConversationViewController alloc] init]; conversationVC.targetId = groupId; conversationVC.title = groupName; conversationVC.conversationType = ConversationType_GROUP; [self.naviController pushViewController:conversationVC animated:YES]; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section == 2) { UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 43.0f)]; headView.backgroundColor = [UIColor clearColor]; CGRect frame = CGRectMake(14, 0, kScreenWidth - 28, 43.0f); UIView *displayView = [[UIView alloc] initWithFrame:frame]; displayView.backgroundColor = [UIColor whiteColor]; [headView addSubview:displayView]; if (@available(iOS 11.0, *)) { displayView.layer.cornerRadius = 10; displayView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner; // 左上圆角 } else { UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:displayView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = frame; maskLayer.path = path.CGPath; displayView.layer.mask = maskLayer; } UIImageView *iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mine_fans"]]; [displayView addSubview:iconImage]; [iconImage mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(displayView.mas_left).offset(12); make.top.mas_equalTo(displayView.mas_top).offset(17); make.width.mas_equalTo(24); make.height.mas_equalTo(24); }]; UILabel *tipsLable = [[UILabel alloc] init]; tipsLable.text = @"粉丝群"; tipsLable.font = [UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium]; tipsLable.textColor = HexRGB(0x333333); [displayView addSubview:tipsLable]; [tipsLable mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(iconImage.mas_right).offset(4); make.centerY.mas_equalTo(iconImage.mas_centerY); make.height.mas_equalTo(30); make.width.mas_equalTo(100); }]; return headView; } return [UIView new]; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { if (section == 2) { UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15.0f)]; bottomView.backgroundColor = [UIColor clearColor]; CGRect frame = CGRectMake(14, 0, kScreenWidth - 28, 15); UIView *displayView = [[UIView alloc] initWithFrame:frame]; displayView.backgroundColor = [UIColor whiteColor]; [bottomView addSubview:displayView]; if (@available(iOS 11.0, *)) { displayView.layer.cornerRadius = 10; displayView.layer.maskedCorners = kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner; } else { UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:displayView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = frame; maskLayer.path = path.CGPath; displayView.layer.mask = maskLayer; } return bottomView; } return [UIView new]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 2) { return 43.0f; } return CGFLOAT_MIN; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { if (section == 2) { return 15.0f; } return CGFLOAT_MIN; } - (NSMutableArray *)videoArray { if (!_videoArray) { _videoArray = [NSMutableArray array]; } return _videoArray; } #pragma mark ------ WMPlayer - (void)playVideoWithUrl:(NSString *)fileUrl { fileUrl = [fileUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; _playerFrame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin); _wmPlayer = [[WMPlayer alloc] initWithFrame:_playerFrame]; WMPlayerModel *playModel = [[WMPlayerModel alloc] init]; playModel.videoURL = [NSURL URLWithString:fileUrl]; _wmPlayer.playerModel = playModel; _wmPlayer.delegate = self; _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]; _bgView.backgroundColor = [UIColor blackColor]; [[UIApplication sharedApplication].keyWindow addSubview:_bgView]; [[UIApplication sharedApplication].keyWindow addSubview:_wmPlayer]; [[UIApplication sharedApplication].keyWindow bringSubviewToFront:_wmPlayer]; [_wmPlayer play]; } - (void)wmplayer:(WMPlayer *)wmplayer clickedCloseButton:(UIButton *)backBtn { [wmplayer removePlayer]; [_bgView removeFromSuperview]; [self.naviController setNeedsStatusBarAppearanceUpdate]; } - (void)wmplayer:(WMPlayer *)wmplayer clickedFullScreenButton:(UIButton *)fullScreenBtn { self.isRatation = !self.isRatation; if (self.isRatation) { [wmplayer removeFromSuperview]; [UIView animateWithDuration:1.0f animations:^{ wmplayer.transform = CGAffineTransformMakeRotation(M_PI_2); } completion:^(BOOL finished) { wmplayer.frame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin); [[UIApplication sharedApplication].keyWindow addSubview:wmplayer]; [[UIApplication sharedApplication].keyWindow bringSubviewToFront:wmplayer]; }]; } else { [wmplayer removeFromSuperview]; [UIView animateWithDuration:1.0f animations:^{ // 复原 wmplayer.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { wmplayer.frame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin); [[UIApplication sharedApplication].keyWindow addSubview:wmplayer]; [[UIApplication sharedApplication].keyWindow bringSubviewToFront:wmplayer]; }]; } } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end