123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- //
- // ClassTitleView.m
- // StudentDaya
- //
- // Created by Kyle on 2020/6/12.
- // Copyright © 2020 DayaMusic. All rights reserved.
- //
- #import "ClassTitleView.h"
- #import "ClassroomService.h"
- #import "Masonry.h"
- #import <AVFoundation/AVFoundation.h>
- #import "RTCService.h"
- #import "KSNormalAlertView.h"
- #define TSignalImageViewWidth (42)
- #define TButtonWidth (43)
- #define TButtonWidth (43)
- #define TButtonHeight (42)
- #define TButtonSpace (10)
- @interface ClassTitleView ()
- @property (nonatomic, strong) NSMutableArray *buttonArray;
- @property (nonatomic, strong) NSArray *buttonImageArray;
- @property (nonatomic, strong) NSArray *buttonHighlightedImageArray;
- @property (nonatomic, strong) NSArray *buttonCloseImageArray;
- @property (nonatomic, assign) NSInteger duration;
- @end
- @implementation ClassTitleView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
-
- self.backgroundColor = [UIColor clearColor];
- [self.buttonArray addObjectsFromArray:@[self.switchLineBtn,self.switchCameraBtn,self.microphoneBtn,self.cameraBtn,self.chatBtn,self.hangupBtn]];
- [self addSubviews];
- [self refreshTitleView];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveMessageNotification:) name:OnReceiveMessageNotification object:nil];
- }
- return self;
- }
- - (void)onReceiveMessageNotification:(NSNotification *)notification{
- NSDictionary *dic = notification.object;
- RCMessage *message = dic[@"message"];
- Classroom *currentRoom = [ClassroomService sharedService].currentRoom;
- if ([message.targetId isEqualToString:currentRoom.roomId] && [message.content isKindOfClass:[RCTextMessage class]]) {
- dispatch_main_async_safe(^{
- [self updateClassNewsButton];
- });
- }
- }
- - (void)updateClassNewsButton{
- UIButton *button = (UIButton *)[self viewWithTag:ClassTitleViewActionTagChat];
- if (!button.selected) {
- [button setBackgroundImage:[UIImage imageNamed:@"classnews_unreadStatus"] forState:UIControlStateNormal];
- [button setBackgroundImage:[UIImage imageNamed:@"classnews_unreadStatus"] forState:UIControlStateHighlighted];
- }else{
- [button setBackgroundImage:[UIImage imageNamed:@"classnews_nomal"] forState:UIControlStateNormal];
- [button setBackgroundImage:[UIImage imageNamed:@"classnews_nomal"] forState:UIControlStateHighlighted];
- }
- }
- - (void)addSubviews {
-
- UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"classroom_top"]];
- [self addSubview:bgImage];
- [bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.top.bottom.mas_equalTo(self);
- }];
-
- [self addSubview:self.signalImageView];
- [self addSubview:self.switchLineBtn];
- [self addSubview:self.switchCameraBtn];
- [self addSubview:self.microphoneBtn];
- [self addSubview:self.cameraBtn];
- [self addSubview:self.onShowLabel];
-
- [self addSubview:self.chatBtn];
- [self addSubview:self.hangupBtn];
-
- [self.signalImageView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.mas_left).offset(20);
- make.centerY.mas_equalTo(self.mas_centerY);
- make.width.mas_equalTo(TSignalImageViewWidth);
- make.height.mas_equalTo(TSignalImageViewWidth);
- }];
-
- CGFloat topOffset = (self.bounds.size.height - TButtonWidth) / 2.0;
-
- // 切换摄像头
- [self.switchCameraBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.signalImageView.mas_right).offset(TButtonSpace);
- make.width.mas_equalTo(TButtonWidth);
- make.height.mas_equalTo(TButtonHeight);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
- // 麦克风
- [self.microphoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.switchCameraBtn.mas_right).offset(TButtonSpace);
- make.width.mas_equalTo(TButtonWidth);
- make.height.mas_equalTo(TButtonHeight);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
- // 摄像头
- [self.cameraBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.microphoneBtn.mas_right).offset(TButtonSpace);
- make.width.mas_equalTo(TButtonWidth);
- make.height.mas_equalTo(TButtonHeight);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
-
- // 演示label
- [self.onShowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.cameraBtn.mas_right).offset(TButtonSpace * 2);
- make.width.mas_equalTo(120);
- make.height.mas_equalTo(TButtonWidth);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
-
-
- // 退出
- [self.hangupBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.mas_equalTo(self.mas_right).offset(-TButtonSpace);
- make.width.mas_equalTo(87);
- make.height.mas_equalTo(30);
- make.centerY.mas_equalTo(self.mas_centerY);
- }];
- // 切换线路
- [self.switchLineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.mas_equalTo(self.hangupBtn.mas_left).offset(-TButtonSpace);
- make.width.mas_equalTo(TButtonWidth);
- make.height.mas_equalTo(TButtonHeight);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
-
- // 聊天
- [self.chatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.mas_equalTo(self.switchLineBtn.mas_left).offset(-TButtonSpace);
- make.width.mas_equalTo(TButtonWidth);
- make.height.mas_equalTo(TButtonHeight);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
-
-
- self.isDisplay = YES;
- }
- - (void)clearUnreadMessage{
- Classroom *currentRoom = [ClassroomService sharedService].currentRoom;
- [[RCIMClient sharedRCIMClient] clearMessagesUnreadStatus:ConversationType_GROUP targetId:currentRoom.roomId];
- UIButton *button = (UIButton *)[self viewWithTag:ClassTitleViewActionTagChat];
- [button setBackgroundImage:[UIImage imageNamed:@"classnews_nomal"] forState:UIControlStateNormal];
- [button setBackgroundImage:[UIImage imageNamed:@"classnews_nomal"] forState:UIControlStateHighlighted];
- }
- - (void)refreshTitleView {
- [self setDefaultButtons];
- RoomMember *curMember = [ClassroomService sharedService].currentRoom.currentMember;
- // 判断显示状态
- [self isCameraAvailable:^(bool avilable) {
- if (avilable) {
- self.cameraBtn.selected = !curMember.cameraEnable;
- }else {
- self.cameraBtn.selected = YES;
- }
- }];
- [self isMicrophoneAvailable:^(bool avilable) {
- if (avilable) {
- self.microphoneBtn.selected = !curMember.microphoneEnable;
- }else {
- self.microphoneBtn.selected = YES;
- }
- }];
- }
- - (void)tapEvent:(UIButton *)btn {
-
- if (btn.tag == ClassTitleViewActionTagMicrophone) {
- [self isMicrophoneAvailable:^(bool avilable) {
- if (!avilable) {
- [KSNormalAlertView ks_showAlertWithTitle:NSLocalizedStringFromTable(@"microphoneAvailable", @"SealClass", nil) leftTitle:@"取消" rightTitle:@"设置" inView:self.superview cancel:^{
-
- } confirm:^{
- [self openSettingView];
- }];
- return;
- }else {
- if(self.delegate && [self.delegate respondsToSelector:@selector(classTitleView:didTapAtTag:)]) {
- [self.delegate classTitleView:btn didTapAtTag:btn.tag];
- }
-
- }
- }];
- }
- else if (btn.tag == ClassTitleViewActionTagCamera) {
- [self isCameraAvailable:^(bool avilable) {
- if (!avilable) {
- [KSNormalAlertView ks_showAlertWithTitle:NSLocalizedStringFromTable(@"cameraAvailable", @"SealClass", nil) leftTitle:@"取消" rightTitle:@"设置" inView:self.superview cancel:^{
-
- } confirm:^{
- [self openSettingView];
- }];
- return;
- }else {
- if(self.delegate && [self.delegate respondsToSelector:@selector(classTitleView:didTapAtTag:)]) {
- [self.delegate classTitleView:btn didTapAtTag:btn.tag];
- }
- }
- }];
- }
- else {
- if (btn.tag == ClassTitleViewActionTagChat) {
- [self clearUnreadMessage];
- }
- if(self.delegate && [self.delegate respondsToSelector:@selector(classTitleView:didTapAtTag:)]) {
- [self.delegate classTitleView:btn didTapAtTag:btn.tag];
- }
- }
- }
- - (void)setDefaultButtons {
- self.buttonImageArray = @[@"line_switch",@"camera_switch", @"userMic_on", @"userCamera_on", @"classnews_nomal", @"class_hangUp"];
- self.buttonCloseImageArray = @[@"line_switch",@"camera_switch", @"userMic_off", @"userCamera_off", @"classnews_nomal", @"class_hangUp"];
- for(int i = 0; i < self.buttonArray.count; i++) {
- UIButton *button = [self.buttonArray objectAtIndex:i];
- button.enabled = YES;
- [button setBackgroundImage:[UIImage imageNamed:[self.buttonImageArray objectAtIndex:i]] forState:UIControlStateNormal];
- [button setBackgroundImage:[UIImage imageNamed:[self.buttonCloseImageArray objectAtIndex:i]] forState:UIControlStateSelected];
- }
- }
- - (UIImageView *)signalImageView {
- if(!_signalImageView) {
- _signalImageView = [[UIImageView alloc] init];
- _signalImageView.image = [UIImage imageNamed:@"network_status1"];
- }
- return _signalImageView;
- }
- - (UIButton *)switchLineBtn {
- if (!_switchLineBtn) {
- _switchLineBtn = [[UIButton alloc] init];
- _switchLineBtn.enabled = YES;
- _switchLineBtn.tag = ClassTitleViewActionTagSwitchLine;
- [_switchLineBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _switchLineBtn;
- }
- - (UIButton *)switchCameraBtn {
- if(!_switchCameraBtn) {
- _switchCameraBtn = [[UIButton alloc] init];
- _switchCameraBtn.enabled = YES;
- _switchCameraBtn.tag = ClassTitleViewActionTagSwitchCamera;
- [_switchCameraBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
-
- }
- return _switchCameraBtn;
- }
- - (UIButton *)microphoneBtn {
- if(!_microphoneBtn) {
- _microphoneBtn = [[UIButton alloc] init];
- _microphoneBtn.enabled = YES;
- _microphoneBtn.tag = ClassTitleViewActionTagMicrophone;
- [_microphoneBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
-
- }
- return _microphoneBtn;
- }
- - (UIButton *)cameraBtn {
- if (!_cameraBtn) {
- _cameraBtn = [[UIButton alloc] init];
- _cameraBtn.enabled = YES;
- _cameraBtn.tag = ClassTitleViewActionTagCamera;
- [_cameraBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _cameraBtn;
- }
- - (UILabel *)onShowLabel {
- if (!_onShowLabel) {
- _onShowLabel = [[UILabel alloc] init];
- _onShowLabel.textColor = HexRGB(0xf9f9f9);
- _onShowLabel.font = [UIFont systemFontOfSize:14.0f];
- _onShowLabel.text = @"您已进入演示模式";
- }
- return _onShowLabel;
- }
- - (UIButton *)chatBtn {
- if (!_chatBtn) {
- _chatBtn = [[UIButton alloc] init];
- _chatBtn.enabled = YES;
- _chatBtn.tag = ClassTitleViewActionTagChat;
- [_chatBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _chatBtn;
- }
- - (UIButton *)hangupBtn {
- if(!_hangupBtn) {
- _hangupBtn = [[UIButton alloc] init];
- _hangupBtn.enabled = YES;
- _hangupBtn.tag = ClassTitleViewActionTagHangup;
- [_hangupBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
-
- }
- return _hangupBtn;
- }
- - (NSMutableArray *)buttonArray {
- if (!_buttonArray) {
- _buttonArray = [[NSMutableArray alloc] init];
- }
- return _buttonArray;
- }
- - (void)setNetStatus:(NetWorkingStatus)netStatus {
- _netStatus = netStatus;
- NSString *displayImg = @"network_status1";
- switch (netStatus) {
- case NetWorkingStatus_Good:
- displayImg = @"network_status1";
- break;
- case NetWorkingStatus_Well:
- displayImg = @"network_status2";
- break;
- case NetWorkingStatus_Bad:
- displayImg = @"network_status3";
- break;
- case NetWorkingStatus_Poor:
- displayImg = @"network_status4";
- break;
- default:
- break;
- }
- self.signalImageView.image = [UIImage imageNamed:displayImg];
- }
- #pragma mark ----- 显示和隐藏title
- - (void)displayView {
- CGRect frame = self.frame;
- if (frame.origin.y == 0) {
- return;
- }
-
- [UIView animateWithDuration:0.3f animations:^{
- CGRect frame = self.frame;
- frame.origin.y = 0;
- self.frame = frame;
- }];
- }
- - (void)hiddenView {
- CGRect frame = self.frame;
- if (frame.origin.y < 0) {
- return;
- }
- [UIView animateWithDuration:0.3f animations:^{
- CGRect frame = self.frame;
- frame.origin.y = -frame.size.height;
- self.frame = frame;
- }];
- }
- - (void)setIsDisplay:(BOOL)isDisplay {
- _isDisplay = isDisplay;
- if (isDisplay) {
- [self displayView];
- }
- }
- - (void)openSettingView {
- if (@available(iOS 10, *)) {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
- } else {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
- }
- }
- - (void)isCameraAvailable:(void (^)(bool avilable))successBlock {
- NSString *mediaType = AVMediaTypeVideo; //读取媒体类型
- AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; //读取设备授权状态
- if (AVAuthorizationStatusAuthorized == authStatus) {
- successBlock(YES);
- } else if(authStatus == AVAuthorizationStatusNotDetermined) {
- [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
- completionHandler:^(BOOL granted) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (granted) {
- successBlock(YES);
- } else {
- successBlock(NO);
- }
- });
- }];
- }else {
- successBlock(NO);
- }
- }
- -(void)isMicrophoneAvailable:(void (^)(bool avilable))successBlock {
- AVAudioSessionRecordPermission authStatus = [[AVAudioSession sharedInstance] recordPermission];
- if (AVAudioSessionRecordPermissionGranted == authStatus) {
- successBlock(YES);
- } else if(authStatus == AVAudioSessionRecordPermissionUndetermined){
- [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (granted) {
- successBlock(YES);
- } else {
- successBlock(NO);
-
- }
- });
- }];
- }else {
- successBlock(NO);
- }
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|