358 lines
13 KiB
Objective-C
358 lines
13 KiB
Objective-C
//
|
|
// YMRoomMusicLibraryViewController.m
|
|
// YUMI
|
|
//
|
|
// Created by YUMI on 2022/5/9.
|
|
//
|
|
|
|
#import "XPRoomMusicLibraryViewController.h"
|
|
///Third
|
|
#import <Masonry/Masonry.h>
|
|
///Tool
|
|
#import "XPCoreDataManager.h"
|
|
#import "YUMIConstant.h"
|
|
#import "RtcManager.h"
|
|
#import "YUMIMacroUitls.h"
|
|
#import "XPCoreDataManager.h"
|
|
#import "DJDKMIMOMColor.h"
|
|
#import "NSArray+Safe.h"
|
|
///Model
|
|
#import "Music+CoreDataClass.h"
|
|
///View
|
|
#import "XPRoomMusicLibraryTableViewCell.h"
|
|
#import "XPRoomMusicLibraryEmptyTableViewCell.h"
|
|
#import "XPMusicLibraryPlayView.h"
|
|
#import "XPRoomMusicVoiceSettingView.h"
|
|
#import "XPRoomTransferMusicViewController.h"
|
|
|
|
UIKIT_EXTERN NSString * kRoomBackMusicAudioMixingVolumeKey;
|
|
UIKIT_EXTERN NSString * kRoomBackMusicCaptureVolumeKey;
|
|
|
|
@interface XPRoomMusicLibraryViewController ()<UITableViewDelegate, UITableViewDataSource, XPRoomMusicLibraryEmptyTableViewCellDelegate, XPMusicLibraryPlayViewDelegate, XPRoomMusicVoiceSettingViewDelegate>
|
|
///列表
|
|
@property (nonatomic,strong) UITableView *tableView;
|
|
///底部播放器
|
|
@property (nonatomic,strong) XPMusicLibraryPlayView *playView;
|
|
///设置声音
|
|
@property (nonatomic,strong) XPRoomMusicVoiceSettingView *voiceSettingView;
|
|
@property(nonatomic,strong)NSFetchedResultsController *fetchedResultController;
|
|
///当前选中播放的音乐
|
|
@property (nonatomic,strong, nullable) Music *selectMusic;
|
|
///数据源
|
|
@property (nonatomic,strong) NSArray *datasource;
|
|
///数据的请求
|
|
@property (nonatomic,strong) NSFetchRequest *request;
|
|
///顶部视图
|
|
@property (nonatomic,strong) UIView * headerView;
|
|
///歌曲数量的
|
|
@property (nonatomic,strong) UILabel *numberMusicLabel;
|
|
@end
|
|
|
|
@implementation XPRoomMusicLibraryViewController
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
[super viewWillAppear:animated];
|
|
NSArray *data = [[XPCoreDataManager shareInstance].managedObjectContext executeFetchRequest:self.request error:nil];
|
|
self.datasource = data;
|
|
[self.tableView reloadData];
|
|
}
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
self.title = YMLocalizedString(@"XPRoomMusicLibraryViewController0");
|
|
[self initData];
|
|
[self initSubViews];
|
|
[self initSubViewConstraints];
|
|
}
|
|
|
|
#pragma mark - Private Method
|
|
- (void)initData {
|
|
self.playView.music = self.currentMusic;
|
|
self.playView.isPlaying = self.isPlaying;
|
|
|
|
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
|
|
NSInteger musicVolum = [defaults integerForKey:kRoomBackMusicAudioMixingVolumeKey];
|
|
musicVolum = musicVolum > 0 ? musicVolum : 50;
|
|
self.voiceSettingView.musicVolum = musicVolum;
|
|
|
|
NSInteger captureVolum = [defaults integerForKey:kRoomBackMusicCaptureVolumeKey];
|
|
captureVolum = captureVolum > 0 ? captureVolum : 50;
|
|
self.voiceSettingView.captureVolum = captureVolum;
|
|
}
|
|
|
|
- (void)initSubViews {
|
|
[self addNavigationItemWithImageNames:@[@"room_music_library_nav_add"] isLeft:NO target:self action:@selector(addMusicAction:) tags:nil];
|
|
[self.view addSubview:self.tableView];
|
|
[self.view addSubview:self.playView];
|
|
[self.view addSubview:self.voiceSettingView];
|
|
}
|
|
|
|
- (void)initSubViewConstraints {
|
|
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.top.right.mas_equalTo(self.view);
|
|
make.bottom.mas_equalTo(self.playView.mas_top);
|
|
}];
|
|
|
|
[self.playView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.height.mas_equalTo(55);
|
|
make.bottom.left.right.mas_equalTo(self.view);
|
|
}];
|
|
|
|
[self.voiceSettingView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.bottom.mas_equalTo(self.playView.mas_top);
|
|
make.left.right.mas_equalTo(self.view);
|
|
}];
|
|
}
|
|
#pragma mark - UITableViewDelegate And UITableViewDataSource
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
return 1;
|
|
}
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
return self.datasource.count > 0 ? self.datasource.count : 1;
|
|
}
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
if (self.datasource.count > 0) {
|
|
XPRoomMusicLibraryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPRoomMusicLibraryTableViewCell class])];
|
|
Music * musicInfo = [self.datasource safeObjectAtIndex1:indexPath.row];
|
|
if (musicInfo.musicId == self.selectMusic.musicId) {
|
|
cell.isSelect = YES;
|
|
} else {
|
|
cell.isSelect = NO;
|
|
}
|
|
cell.musicInfo = musicInfo;
|
|
return cell;
|
|
}
|
|
|
|
XPRoomMusicLibraryEmptyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPRoomMusicLibraryEmptyTableViewCell class])];
|
|
cell.delegate = self;
|
|
return cell;
|
|
}
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
return self.datasource.count > 0 ? 60 : (KScreenHeight - kNavigationHeight);
|
|
}
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
if (self.datasource.count > 0) {
|
|
Music * musicInfo = [self.datasource safeObjectAtIndex1:indexPath.row];
|
|
self.selectMusic = musicInfo;
|
|
self.playView.music = musicInfo;
|
|
[self.tableView reloadData];
|
|
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(xPRoomMusicLibraryViewController:musicInfo:)]) {
|
|
[self.delegate xPRoomMusicLibraryViewController:self musicInfo:self.selectMusic];
|
|
}
|
|
}
|
|
if (!self.voiceSettingView.hidden) {
|
|
self.voiceSettingView.hidden = YES;
|
|
}
|
|
}
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
|
|
if (self.datasource.count > 0) {
|
|
return 20;
|
|
}
|
|
return 0.01f;
|
|
}
|
|
|
|
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
|
|
if (self.datasource.count > 0) {
|
|
self.headerView.frame = CGRectMake(0, 0, KScreenWidth, 20);
|
|
if (!self.numberMusicLabel.superview) {
|
|
[self.headerView addSubview:self.numberMusicLabel];
|
|
[self.numberMusicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.mas_equalTo(self.headerView).offset(15);
|
|
make.centerY.mas_equalTo(self.headerView);
|
|
}];
|
|
}
|
|
|
|
self.numberMusicLabel.text = [NSString stringWithFormat:YMLocalizedString(@"XPRoomMusicLibraryViewController1"), self.datasource.count];
|
|
return self.headerView;
|
|
}
|
|
return [UIView new];
|
|
}
|
|
|
|
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
if (self.datasource.count > 0) {
|
|
return YES;
|
|
}
|
|
return NO;
|
|
}
|
|
|
|
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
return YMLocalizedString(@"XPRoomMusicLibraryViewController2");
|
|
}
|
|
|
|
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
if (editingStyle == UITableViewCellEditingStyleDelete) {
|
|
Music * music = [self.datasource safeObjectAtIndex1:indexPath.row];
|
|
[self showLoading];
|
|
if ([music.musicId isEqualToString:self.currentMusic.musicId]) {
|
|
self.playView.music = nil;
|
|
self.playView.isPlaying = NO;
|
|
self.selectMusic = nil;
|
|
self.isPlaying = NO;
|
|
[[RtcManager instance] changePlayState:BackMusicPlayState_Stop];
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(xPRoomMusicLibraryViewController:isPlaying:)]) {
|
|
[self.delegate xPRoomMusicLibraryViewController:self isPlaying:self.isPlaying];
|
|
}
|
|
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(xPRoomMusicLibraryViewController:musicInfo:)]) {
|
|
[self.delegate xPRoomMusicLibraryViewController:self musicInfo:self.selectMusic];
|
|
}
|
|
}
|
|
|
|
if (music.musicName) {
|
|
NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
|
|
documentsPath = [NSString stringWithFormat:@"%@/music/",documentsPath];
|
|
NSString * musicPath = [documentsPath stringByAppendingString:music.musicName];
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
if ([fileManager fileExistsAtPath:musicPath]) {
|
|
NSError * error;
|
|
[fileManager removeItemAtPath:musicPath error:&error];
|
|
}
|
|
}
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
[[XPCoreDataManager shareInstance].managedObjectContext deleteObject:music];
|
|
[[XPCoreDataManager shareInstance] save];
|
|
NSArray *data = [[XPCoreDataManager shareInstance].managedObjectContext executeFetchRequest:self.request error:nil];
|
|
self.datasource = data;
|
|
[self.tableView reloadData];
|
|
[self hideHUD];
|
|
});
|
|
}
|
|
}
|
|
|
|
#pragma mark - XPRoomMusicLibraryEmptyTableViewCellDelegate
|
|
- (void)xPRoomMusicLibraryEmptyTableViewCell:(XPRoomMusicLibraryEmptyTableViewCell *)cell didClickAdd:(UIButton *)sender {
|
|
XPRoomTransferMusicViewController * transferVC = [[XPRoomTransferMusicViewController alloc] init];
|
|
[self.navigationController pushViewController:transferVC animated:YES];
|
|
}
|
|
#pragma mark - XPMusicLibraryPlayViewDelegate
|
|
- (void)xPMusicLibraryPlayView:(XPMusicLibraryPlayView *)view didClickVoiceSetting:(UIButton *)sender {
|
|
self.voiceSettingView.hidden = !sender.selected;
|
|
}
|
|
|
|
- (void)xPMusicLibraryPlayView:(XPMusicLibraryPlayView *)view didClickPlayButton:(UIButton *)sender {
|
|
if (self.currentMusic) {
|
|
sender.selected = !sender.selected;
|
|
if (self.isPlaying) {
|
|
[[RtcManager instance] changePlayState:BackMusicPlayState_Pause];
|
|
sender.selected = NO;
|
|
self.isPlaying = NO;
|
|
} else {
|
|
[[RtcManager instance] changePlayState:BackMusicPlayState_Resume];
|
|
sender.selected = YES;
|
|
self.isPlaying = YES;
|
|
}
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(xPRoomMusicLibraryViewController:isPlaying:)]) {
|
|
[self.delegate xPRoomMusicLibraryViewController:self isPlaying:self.isPlaying];
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma mark - XPRoomMusicVoiceSettingViewDelegate
|
|
- (void)xPRoomMusicVoiceSettingView:(XPRoomMusicVoiceSettingView *)view didSliderValueChange:(UISlider *)slider {
|
|
switch (slider.tag) {
|
|
case MusicVoiceSettingType_User:
|
|
{
|
|
[[NSUserDefaults standardUserDefaults] setInteger:slider.value forKey:kRoomBackMusicCaptureVolumeKey];
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
[[RtcManager instance] updateUserSound:(int)slider.value];
|
|
}
|
|
break;
|
|
case MusicVoiceSettingType_Music:
|
|
{
|
|
[[NSUserDefaults standardUserDefaults] setInteger:slider.value forKey:kRoomBackMusicAudioMixingVolumeKey];
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
[[RtcManager instance] updateMusicSound:(int)slider.value];
|
|
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(xPRoomMusicLibraryViewController:backMusicVolum:)]) {
|
|
[self.delegate xPRoomMusicLibraryViewController:self backMusicVolum:slider.value];
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
#pragma mark - Event Response
|
|
- (void)addMusicAction:(UIButton *)sender {
|
|
XPRoomTransferMusicViewController * transferVC = [[XPRoomTransferMusicViewController alloc] init];
|
|
[self.navigationController pushViewController:transferVC animated:YES];
|
|
}
|
|
#pragma mark - Getters And Setters
|
|
- (void)setIsPlaying:(BOOL)isPlaying {
|
|
_isPlaying = isPlaying;
|
|
self.playView.isPlaying = _isPlaying;
|
|
}
|
|
|
|
- (void)setCurrentMusic:(Music *)currentMusic {
|
|
_currentMusic = currentMusic;
|
|
self.playView.music = _currentMusic;
|
|
self.selectMusic = _currentMusic;
|
|
[self.tableView reloadData];
|
|
}
|
|
|
|
- (UITableView *)tableView {
|
|
if (!_tableView) {
|
|
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
|
|
_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:[XPRoomMusicLibraryTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPRoomMusicLibraryTableViewCell class])];
|
|
[_tableView registerClass:[XPRoomMusicLibraryEmptyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPRoomMusicLibraryEmptyTableViewCell class])];
|
|
}
|
|
return _tableView;
|
|
}
|
|
|
|
- (XPMusicLibraryPlayView *)playView {
|
|
if (!_playView) {
|
|
_playView = [[XPMusicLibraryPlayView alloc] init];
|
|
_playView.delegate = self;
|
|
}
|
|
return _playView;
|
|
}
|
|
|
|
- (XPRoomMusicVoiceSettingView *)voiceSettingView {
|
|
if (!_voiceSettingView) {
|
|
_voiceSettingView = [[XPRoomMusicVoiceSettingView alloc] init];
|
|
_voiceSettingView.delegate = self;
|
|
_voiceSettingView.hidden = YES;
|
|
}
|
|
return _voiceSettingView;
|
|
}
|
|
|
|
- (NSFetchRequest *)request {
|
|
if (!_request) {
|
|
_request = [NSFetchRequest fetchRequestWithEntityName:[Music getMusicName]];
|
|
}
|
|
return _request;
|
|
}
|
|
|
|
- (UIView *)headerView {
|
|
if (!_headerView) {
|
|
_headerView = [[UIView alloc] init];
|
|
_headerView.backgroundColor = [UIColor clearColor];
|
|
}
|
|
return _headerView;
|
|
}
|
|
|
|
- (UILabel *)numberMusicLabel {
|
|
if (!_numberMusicLabel) {
|
|
_numberMusicLabel = [[UILabel alloc] init];
|
|
_numberMusicLabel.font = [UIFont systemFontOfSize:10];
|
|
_numberMusicLabel.textColor = [DJDKMIMOMColor secondTextColor];
|
|
}
|
|
return _numberMusicLabel;
|
|
}
|
|
|
|
|
|
@end
|