Files
yinmeng-ios/xplan-ios/Main/Mine/View/SkillCard/View/XPSkillCardViewController.m

324 lines
13 KiB
Objective-C

//
// XPSkillCardViewController.m
// xplan-ios
//
// Created by GreenLand on 2022/1/19.
//
#import "XPSkillCardViewController.h"
///P
#import "XPSkillCardProtocol.h"
#import "XPSkillCardPresenter.h"
// View
#import "XPSkillCardHeadView.h"
#import "XPSkillCardListCell.h"
#import "XPSkillCardTypeView.h"
#import "XPSkillCardEditViewController.h"
#import "XPVoiceCardViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <AFNetworking.h>
///Tool
#import "XPMacro.h"
#import "AccountInfoStorage.h"
#import "TTPopup.h"
#import "ThemeColor.h"
#import "XPSkillCardPlayerManager.h"
@interface XPSkillCardViewController ()
<UITableViewDelegate,
UITableViewDataSource,
XPSkillCardProtocol,
XPSkillCardHeadViewDelegate,
XPSkillCardTypeViewDelegate,
XPSkillCardListCellDelegate
>
@property (nonatomic,strong) UITableView *tableView;
///头部加号
@property (nonatomic, strong) XPSkillCardHeadView *headView;
///数据源
@property (nonatomic, strong) NSMutableArray <XPSkillCardModel *> *dataSource;
///是否播放中
@property (nonatomic, assign) BOOL isPlaying;
@property (nonatomic, strong) UIButton *playButton;
@end
@implementation XPSkillCardViewController
- (XPSkillCardPresenter *)createPresenter {
return [[XPSkillCardPresenter alloc] init];;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initSubViewConstraints];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.presenter getSkillCardInfoWithUid:[NSString stringWithFormat:@"%zd", self.uid]];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.playButton.selected = NO;
self.isPlaying = NO;
[[XPSkillCardPlayerManager shareInstance] stopMusicIsNeedCompletion:NO];
}
#pragma mark - Private Method
- (void)initSubViews {
self.title = @"技能卡";
[self.view addSubview:self.tableView];
}
- (void)initSubViewConstraints {
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view);
make.left.right.bottom.mas_equalTo(self.view);
}];
}
#pragma mark - XPSkillCardProtocol
///成功获取当前添加的技能卡列表
- (void)onGetSkillCardListSuccess:(NSArray<XPSkillCardModel *> *)array {
self.dataSource = [NSMutableArray arrayWithArray:array];
self.headView.hadSkillCard = self.dataSource.count;
if (self.uid == [[AccountInfoStorage instance].getUid integerValue]) {//主态
if (self.dataSource.count) {///有技能卡
CGFloat height = (KScreenWidth - 30) * 64 / 345.0 + 16;
self.headView.frame = CGRectMake(0, 0, KScreenWidth, height);
} else {
CGFloat height = (KScreenWidth - 30) * 180 / 345.0 + 16;
self.headView.frame = CGRectMake(0, 0, KScreenWidth, height);
}
self.tableView.tableHeaderView = self.headView;
} else {
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
}
[self.tableView reloadData];
}
///未添加的技能卡类型列表
- (void)onGetSkillCardTypeListSuccess:(NSArray<XPSkillCardTypeModel *> *)array {
XPSkillCardTypeView *view = [[XPSkillCardTypeView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kSafeAreaBottomHeight + 310)];
view.array = array;
view.delegate = self;
[TTPopup popupView:view style:TTPopupStyleActionSheet];
}
///删除技能卡成功
- (void)onDeleteSkillCardSuccess {
[self showErrorToast:@"删除成功"];
[self.presenter getSkillCardInfoWithUid:[NSString stringWithFormat:@"%zd", self.uid]];
[[XPSkillCardPlayerManager shareInstance] stopMusic];
}
#pragma mark - XPSkillCardTypeViewDelegate
///选择了添加的技能卡类型
- (void)xPSkillCardTypeViewDidSelectItem:(XPSkillCardTypeModel *)item {
[TTPopup dismiss];
if ([item.name isEqualToString:@"声音秀"]) {
XPVoiceCardViewController *vc = [[XPVoiceCardViewController alloc] init];
vc.cardId = item.cardId;
[self.navigationController pushViewController:vc animated:YES];
} else {
XPSkillCardEditViewController *vc = [[XPSkillCardEditViewController alloc] init];
vc.cardId = item.cardId;
[self.navigationController pushViewController:vc animated:YES];
}
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
XPSkillCardListCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPSkillCardListCell class])];
if (cell == nil) {
cell = [[XPSkillCardListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPSkillCardListCell class])];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
XPSkillCardModel * item = [self.dataSource objectAtIndex:indexPath.row];
item.isGuestPage = self.uid != [AccountInfoStorage instance].getUid.integerValue;
cell.model = item;
cell.delegate = self;
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectZero];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
XPSkillCardModel * item = [self.dataSource objectAtIndex:indexPath.row];
return [self layoutCellHeightWithItem:item];
}
- (CGFloat)layoutCellHeightWithItem:(XPSkillCardModel *)item {
CGFloat height = 56 + 8 + 28;
if (item.type == SkillCardTypeVoice) {
height = 56 + 8 + 120;
return height;
}
for (int i = 0; i<item.propRecordVo.count; i++) {
XPSkillCardRecordPropModel *propItem = item.propRecordVo[i];
if (propItem.state == 0) {
height += 56;
} else {
CGRect rect = [propItem.parentVal boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 22) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.f]} context:nil];
CGFloat contentW = KScreenWidth - rect.size.width - 30 - 16 - 15 - 15;
CGRect contentRect = [propItem.propVal boundingRectWithSize:CGSizeMake(contentW, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.f weight:UIFontWeightMedium]} context:nil];
height += (contentRect.size.height + 34 > 56 ? contentRect.size.height + 40 : 56);
}
}
return height;
}
#pragma mark - XPSkillCardHeadViewDelegate
///点击添加技能卡按钮
- (void)onSkillCardHeadViewAddButtonClick {
[self.presenter getSkillCardTypeList];
}
#pragma mark - XPSkillCardListCellDelegate
///编辑技能卡
- (void)xPSkillCardListCellEditButtonClick:(XPSkillCardListCell *)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
XPSkillCardModel * item = [self.dataSource objectAtIndex:indexPath.row];
XPSkillCardEditViewController *vc = [[XPSkillCardEditViewController alloc] init];
vc.cardId = item.cardId;
vc.cardRecordId = item.cardRecordId;
[self.navigationController pushViewController:vc animated:YES];
}
///删除声音秀
- (void)xPSkillCardListCellDeleteVoiceButtonClick:(XPSkillCardListCell *)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
XPSkillCardModel * item = [self.dataSource objectAtIndex:indexPath.row];
[TTPopup alertWithMessage:@"确定要删除该声音吗?" confirmHandler:^{
[self.presenter deleteSkillCard:[NSString stringWithFormat:@"%zd", item.cardRecordId]];
} cancelHandler:^{
}];
}
///重新录制
- (void)xPSkillCardListCellRerecordButtonClick:(XPSkillCardListCell *)cell {
TTAlertConfig *config = [[TTAlertConfig alloc] init];
config.cancelButtonConfig.title = @"";
config.confirmButtonConfig.title = @"";
config.message = @"确定要重新录制吗?";
@kWeakify(self);
[TTPopup alertWithConfig:config confirmHandler:^{
@kStrongify(self);
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
XPSkillCardModel * item = [self.dataSource objectAtIndex:indexPath.row];
XPVoiceCardViewController *vc = [[XPVoiceCardViewController alloc] init];
vc.cardId = item.cardId;
vc.cardRecordId = item.cardRecordId;
[self.navigationController pushViewController:vc animated:YES];
} cancelHandler:^{
}];
}
///播放、暂停
- (void)xPSkillCardListCellPlayButtonClick:(XPSkillCardListCell *)cell button:(UIButton *)button {
self.playButton = button;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
XPSkillCardModel * item = [self.dataSource objectAtIndex:indexPath.row];
if (!self.isPlaying) {
if(item.propRecordVo.count) {
XPSkillCardRecordPropModel *voiceItem = item.propRecordVo[0];
NSString *fileName = [[voiceItem.propVal componentsSeparatedByString:@"/"] lastObject];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"mineSkillCardVoice"];
NSString *fullPath = [filePath stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
self.isPlaying = YES;
button.selected = YES;
[[XPSkillCardPlayerManager shareInstance] playerVoiceWithPath:fullPath completionBlock:^{
self.isPlaying = NO;
button.selected = NO;
[[XPSkillCardPlayerManager shareInstance] stopMusic];
}];
} else {
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSFileManager *fileMgr = [[NSFileManager alloc] init];
[fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
[self downloadAudioWithFileName:fileName musicUrl:voiceItem.propVal completion:^(BOOL isSuccess, NSString *editAudioPath) {
self.isPlaying = YES;
button.selected = YES;
[[XPSkillCardPlayerManager shareInstance] playerVoiceWithPath:editAudioPath completionBlock:^{
self.isPlaying = NO;
button.selected = NO;
[[XPSkillCardPlayerManager shareInstance] stopMusic];
}];
}];
}
}
} else {
self.isPlaying = NO;
button.selected = NO;
[[XPSkillCardPlayerManager shareInstance] stopMusic];
}
}
#pragma mark - private
- (void)downloadAudioWithFileName:(NSString *)fileName musicUrl:(NSString *)musicUrl completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:musicUrl]];
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSString *filePath = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"mineSkillCardVoice"] stringByAppendingPathComponent:fileName];
return [NSURL fileURLWithPath:filePath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (!error) {
completion(YES, filePath.path);
} else {
completion(NO, nil);
}
}];
[download resume];
}
#pragma mark - Getters And Setters
- (void)setUid:(NSInteger)uid {
_uid = uid;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableFooterView = [UIView new];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[_tableView registerClass:[XPSkillCardListCell class] forCellReuseIdentifier:NSStringFromClass([XPSkillCardListCell class])];
}
return _tableView;
}
- (XPSkillCardHeadView *)headView {
if (!_headView) {
_headView = [[XPSkillCardHeadView alloc] init];
_headView.delegate = self;
}
return _headView;
}
@end