ClassTitleView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. //
  2. // ClassTitleView.m
  3. // TeacherDaya
  4. //
  5. // Created by Kyle on 2020/4/21.
  6. // Copyright © 2020 DayaMusic. All rights reserved.
  7. //
  8. #import "ClassTitleView.h"
  9. #import "ClassroomService.h"
  10. #import "Masonry.h"
  11. #import <AVFoundation/AVFoundation.h>
  12. #import "RTCService.h"
  13. #import "KSNormalAlertView.h"
  14. #define TSignalImageViewWidth (42)
  15. #define StateButtonWidth (34)
  16. #define TButtonWidth (43)
  17. #define TButtonHeight (42)
  18. #define TButtonSpace (10)
  19. @interface ClassTitleView ()
  20. @property (nonatomic, strong) NSMutableArray *buttonArray;
  21. @property (nonatomic, strong) NSArray *buttonImageArray;
  22. @property (nonatomic, strong) NSArray *buttonHighlightedImageArray;
  23. @property (nonatomic, strong) NSArray *buttonCloseImageArray;
  24. @end
  25. @implementation ClassTitleView
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. {
  28. self = [super initWithFrame:frame];
  29. if (self) {
  30. self.backgroundColor = [UIColor clearColor];
  31. [self.buttonArray addObjectsFromArray:@[self.switchCameraBtn, self.micBtn,self.cameraBtn,self.muteBtn,self.squareBtn, self.chatBtn,self.memberBtn,self.switchLineBtn,self.closeButton,self.hangupBtn]];
  32. [self addSubviews];
  33. [self refreshTitleView];
  34. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveMessageNotification:) name:OnReceiveMessageNotification object:nil];
  35. }
  36. return self;
  37. }
  38. - (void)onReceiveMessageNotification:(NSNotification *)notification{
  39. NSDictionary *dic = notification.object;
  40. RCMessage *message = dic[@"message"];
  41. Classroom *currentRoom = [ClassroomService sharedService].currentRoom;
  42. if ([message.targetId isEqualToString:currentRoom.roomId] && [message.content isKindOfClass:[RCTextMessage class]]) {
  43. dispatch_main_async_safe(^{
  44. [self updateClassNewsButton];
  45. });
  46. }
  47. }
  48. - (void)updateClassNewsButton{
  49. UIButton *button = (UIButton *)[self viewWithTag:ClassTitleViewActionTagChat];
  50. if (!button.selected) {
  51. [button setBackgroundImage:[UIImage imageNamed:@"classnews_unreadStatus"] forState:UIControlStateNormal];
  52. [button setBackgroundImage:[UIImage imageNamed:@"classnews_unreadStatus"] forState:UIControlStateHighlighted];
  53. }else{
  54. [button setBackgroundImage:[UIImage imageNamed:@"classnews_nomal"] forState:UIControlStateNormal];
  55. [button setBackgroundImage:[UIImage imageNamed:@"classnews_nomal"] forState:UIControlStateHighlighted];
  56. }
  57. }
  58. - (void)addSubviews {
  59. UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"classroom_top"]];
  60. [self addSubview:bgImage];
  61. [bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
  62. make.left.right.top.bottom.mas_equalTo(self);
  63. }];
  64. [self addSubview:self.signalImageView];
  65. [self addSubview:self.switchCameraBtn];
  66. [self addSubview:self.micBtn];
  67. [self addSubview:self.cameraBtn];
  68. [self addSubview:self.muteBtn];
  69. [self addSubview:self.squareBtn];
  70. [self addSubview:self.chatBtn];
  71. [self addSubview:self.memberBtn];
  72. [self addSubview:self.switchLineBtn];
  73. [self addSubview:self.closeButton];
  74. [self addSubview:self.hangupBtn];
  75. [self.signalImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.left.mas_equalTo(self.mas_left).offset(20);
  77. make.centerY.mas_equalTo(self.mas_centerY);
  78. make.width.mas_equalTo(TSignalImageViewWidth);
  79. make.height.mas_equalTo(TSignalImageViewWidth);
  80. }];
  81. CGFloat topOffset = (self.bounds.size.height - TButtonWidth) / 2.0;
  82. // 切换摄像头
  83. [self.switchCameraBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.left.mas_equalTo(self.signalImageView.mas_right).offset(TButtonSpace);
  85. make.width.mas_equalTo(TButtonWidth);
  86. make.height.mas_equalTo(TButtonHeight);
  87. make.top.mas_equalTo(self.mas_top).offset(topOffset);
  88. }];
  89. // 麦克风
  90. [self.micBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.left.mas_equalTo(self.switchCameraBtn.mas_right).offset(TButtonSpace);
  92. make.width.mas_equalTo(TButtonWidth);
  93. make.height.mas_equalTo(TButtonHeight);
  94. make.top.mas_equalTo(self.mas_top).offset(topOffset);
  95. }];
  96. // 摄像头
  97. [self.cameraBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  98. make.left.mas_equalTo(self.micBtn.mas_right).offset(TButtonSpace);
  99. make.width.mas_equalTo(TButtonWidth);
  100. make.height.mas_equalTo(TButtonHeight);
  101. make.top.mas_equalTo(self.mas_top).offset(topOffset);
  102. }];
  103. // 扬声器
  104. [self.muteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  105. make.left.mas_equalTo(self.cameraBtn.mas_right).offset(TButtonSpace);
  106. make.width.mas_equalTo(TButtonWidth);
  107. make.height.mas_equalTo(TButtonHeight);
  108. make.top.mas_equalTo(self.mas_top).offset(topOffset);
  109. }];
  110. [self.squareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  111. make.left.mas_equalTo(self.muteBtn.mas_right).offset(TButtonSpace);
  112. make.width.mas_equalTo(TButtonWidth);
  113. make.height.mas_equalTo(TButtonHeight);
  114. make.top.mas_equalTo(self.mas_top).offset(topOffset);
  115. }];
  116. // 退出
  117. [self.hangupBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  118. make.right.mas_equalTo(self.mas_right).offset(-TButtonSpace);
  119. make.width.mas_equalTo(87);
  120. make.height.mas_equalTo(30);
  121. make.centerY.mas_equalTo(self.mas_centerY);
  122. }];
  123. // 切换线路
  124. [self.switchLineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  125. make.right.mas_equalTo(self.hangupBtn.mas_left).offset(-TButtonSpace);
  126. make.width.mas_equalTo(TButtonWidth);
  127. make.height.mas_equalTo(TButtonHeight);
  128. make.top.mas_equalTo(self.mas_top).offset(topOffset);
  129. }];
  130. // 成员
  131. [self.memberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  132. make.right.mas_equalTo(self.switchLineBtn.mas_left).offset(-TButtonSpace);
  133. make.width.mas_equalTo(TButtonWidth);
  134. make.height.mas_equalTo(TButtonHeight);
  135. make.top.mas_equalTo(self.mas_top).offset(topOffset);
  136. }];
  137. // 聊天
  138. [self.chatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  139. make.right.mas_equalTo(self.memberBtn.mas_left).offset(-TButtonSpace);
  140. make.width.mas_equalTo(TButtonWidth);
  141. make.height.mas_equalTo(TButtonHeight);
  142. make.top.mas_equalTo(self.mas_top).offset(topOffset);
  143. }];
  144. self.isDisplay = YES;
  145. }
  146. - (void)clearUnreadMessage{
  147. Classroom *currentRoom = [ClassroomService sharedService].currentRoom;
  148. [[RCIMClient sharedRCIMClient] clearMessagesUnreadStatus:ConversationType_GROUP targetId:currentRoom.roomId];
  149. UIButton *button = (UIButton *)[self viewWithTag:ClassTitleViewActionTagChat];
  150. [button setBackgroundImage:[UIImage imageNamed:@"classnews_nomal"] forState:UIControlStateNormal];
  151. [button setBackgroundImage:[UIImage imageNamed:@"classnews_nomal"] forState:UIControlStateHighlighted];
  152. }
  153. - (void)refreshTitleView {
  154. [self setDefaultButtons];
  155. RoomMember *curMember = [ClassroomService sharedService].currentRoom.currentMember;
  156. // 判断显示状态
  157. [self isCameraAvailable:^(bool avilable) {
  158. if (avilable) {
  159. self.cameraBtn.selected = !curMember.cameraEnable;
  160. }else {
  161. self.cameraBtn.selected = YES;
  162. }
  163. }];
  164. [self isMicrophoneAvailable:^(bool avilable) {
  165. if (avilable) {
  166. self.micBtn.selected = !curMember.microphoneEnable;
  167. }else {
  168. self.micBtn.selected = YES;
  169. }
  170. }];
  171. }
  172. - (void)tapEvent:(UIButton *)btn {
  173. if (btn.tag == ClassTitleViewActionTagMic) {
  174. [self isMicrophoneAvailable:^(bool avilable) {
  175. if (!avilable) {
  176. [KSNormalAlertView ks_showAlertWithTitle:NSLocalizedStringFromTable(@"microphoneAvailable", @"SealClass", nil) leftTitle:@"取消" rightTitle:@"设置" inView:self.superview cancel:^{
  177. } confirm:^{
  178. [self openSettingView];
  179. }];
  180. return;
  181. }else {
  182. if(self.delegate && [self.delegate respondsToSelector:@selector(classTitleView:didTapAtTag:)]) {
  183. [self.delegate classTitleView:btn didTapAtTag:btn.tag];
  184. }
  185. }
  186. }];
  187. }
  188. else if (btn.tag == ClassTitleViewActionTagCamera) {
  189. [self isCameraAvailable:^(bool avilable) {
  190. if (!avilable) {
  191. [KSNormalAlertView ks_showAlertWithTitle:NSLocalizedStringFromTable(@"cameraAvailable", @"SealClass", nil) leftTitle:@"取消" rightTitle:@"设置" inView:self.superview cancel:^{
  192. } confirm:^{
  193. [self openSettingView];
  194. }];
  195. }else {
  196. if(self.delegate && [self.delegate respondsToSelector:@selector(classTitleView:didTapAtTag:)]) {
  197. [self.delegate classTitleView:btn didTapAtTag:btn.tag];
  198. }
  199. }
  200. }];
  201. }
  202. else {
  203. if (btn.tag == ClassTitleViewActionTagChat) {
  204. [self clearUnreadMessage];
  205. }
  206. if(self.delegate && [self.delegate respondsToSelector:@selector(classTitleView:didTapAtTag:)]) {
  207. [self.delegate classTitleView:btn didTapAtTag:btn.tag];
  208. }
  209. }
  210. }
  211. - (void)setDefaultButtons {
  212. self.buttonImageArray = @[@"camera_switch",@"userMic_on",@"userCamera_on", @"horn_on", @"square_button",@"classnews_nomal",@"member_setting",@"line_switch",@"class_close",@"class_hangUp"];
  213. self.buttonCloseImageArray = @[@"camera_switch",@"userMic_off",@"userCamera_off", @"horn_close", @"square_button", @"classnews_nomal",@"member_setting",@"line_switch",@"class_close",@"class_hangUp"];
  214. for(int i = 0; i < self.buttonArray.count; i++) {
  215. UIButton *button = [self.buttonArray objectAtIndex:i];
  216. button.enabled = YES;
  217. [button setBackgroundImage:[UIImage imageNamed:[self.buttonImageArray objectAtIndex:i]] forState:UIControlStateNormal];
  218. [button setBackgroundImage:[UIImage imageNamed:[self.buttonCloseImageArray objectAtIndex:i]] forState:UIControlStateSelected];
  219. }
  220. }
  221. - (UIImageView *)signalImageView {
  222. if(!_signalImageView) {
  223. _signalImageView = [[UIImageView alloc] init];
  224. _signalImageView.image = [UIImage imageNamed:@"network_status1"];
  225. }
  226. return _signalImageView;
  227. }
  228. - (UIButton *)switchLineBtn {
  229. if (!_switchLineBtn) {
  230. _switchLineBtn = [[UIButton alloc] init];
  231. _switchLineBtn.enabled = YES;
  232. _switchLineBtn.tag = ClassTitleViewActionTagSwitchLine;
  233. [_switchLineBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
  234. }
  235. return _switchLineBtn;
  236. }
  237. - (UIButton *)switchCameraBtn {
  238. if(!_switchCameraBtn) {
  239. _switchCameraBtn = [[UIButton alloc] init];
  240. _switchCameraBtn.enabled = YES;
  241. _switchCameraBtn.tag = ClassTitleViewActionTagSwitchCamera;
  242. [_switchCameraBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
  243. }
  244. return _switchCameraBtn;
  245. }
  246. - (UIButton *)micBtn {
  247. if (!_micBtn) {
  248. _micBtn = [[UIButton alloc] init];
  249. _micBtn.enabled = YES;
  250. _micBtn.tag = ClassTitleViewActionTagMic;
  251. [_micBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
  252. }
  253. return _micBtn;
  254. }
  255. - (UIButton *)cameraBtn {
  256. if (!_cameraBtn) {
  257. _cameraBtn = [[UIButton alloc] init];
  258. _cameraBtn.enabled = YES;
  259. _cameraBtn.tag = ClassTitleViewActionTagCamera;
  260. [_cameraBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
  261. }
  262. return _cameraBtn;
  263. }
  264. - (UIButton *)muteBtn {
  265. if(!_muteBtn) {
  266. _muteBtn = [[UIButton alloc] init];
  267. _muteBtn.enabled = YES;
  268. _muteBtn.tag = ClassTitleViewActionTagMute;
  269. [_muteBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
  270. }
  271. return _muteBtn;
  272. }
  273. - (UIButton *)squareBtn {
  274. if (!_squareBtn) {
  275. _squareBtn = [[UIButton alloc] init];
  276. _squareBtn.enabled = YES;
  277. _squareBtn.tag = ClassTitleViewActionTagSquare;
  278. [_squareBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
  279. }
  280. return _squareBtn;
  281. }
  282. - (UIButton *)chatBtn {
  283. if (!_chatBtn) {
  284. _chatBtn = [[UIButton alloc] init];
  285. _chatBtn.enabled = YES;
  286. _chatBtn.tag = ClassTitleViewActionTagChat;
  287. [_chatBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
  288. }
  289. return _chatBtn;
  290. }
  291. - (UIButton *)memberBtn {
  292. if (!_memberBtn) {
  293. _memberBtn = [[UIButton alloc] init];
  294. _memberBtn.enabled = YES;
  295. _memberBtn.tag = ClassTitleViewActionTagMember;
  296. [_memberBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
  297. }
  298. return _memberBtn;
  299. }
  300. - (UIButton *)closeButton {
  301. if (!_closeButton) {
  302. _closeButton = [[UIButton alloc] init];
  303. _closeButton.enabled = YES;
  304. _closeButton.tag = ClassTitleViewActionTagClose;
  305. [_closeButton addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
  306. }
  307. return _closeButton;
  308. }
  309. - (UIButton *)hangupBtn {
  310. if(!_hangupBtn) {
  311. _hangupBtn = [[UIButton alloc] init];
  312. _hangupBtn.enabled = YES;
  313. _hangupBtn.tag = ClassTitleViewActionTagHangup;
  314. [_hangupBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
  315. }
  316. return _hangupBtn;
  317. }
  318. - (NSMutableArray *)buttonArray {
  319. if (!_buttonArray) {
  320. _buttonArray = [[NSMutableArray alloc] init];
  321. }
  322. return _buttonArray;
  323. }
  324. - (void)setNetStatus:(NetWorkingStatus)netStatus {
  325. _netStatus = netStatus;
  326. NSString *displayImg = @"network_status1";
  327. switch (netStatus) {
  328. case NetWorkingStatus_Good:
  329. displayImg = @"network_status1";
  330. break;
  331. case NetWorkingStatus_Well:
  332. displayImg = @"network_status2";
  333. break;
  334. case NetWorkingStatus_Bad:
  335. displayImg = @"network_status3";
  336. break;
  337. case NetWorkingStatus_Poor:
  338. displayImg = @"network_status4";
  339. break;
  340. default:
  341. break;
  342. }
  343. self.signalImageView.image = [UIImage imageNamed:displayImg];
  344. }
  345. #pragma mark ----- 显示和隐藏title
  346. - (void)displayView {
  347. CGRect frame = self.frame;
  348. if (frame.origin.y == 0) {
  349. return;
  350. }
  351. [self.superview bringSubviewToFront:self];
  352. [UIView animateWithDuration:0.3f animations:^{
  353. CGRect frame = self.frame;
  354. frame.origin.y = 0;
  355. self.frame = frame;
  356. }];
  357. }
  358. - (void)hiddenView {
  359. CGRect frame = self.frame;
  360. if (frame.origin.y < 0) {
  361. return;
  362. }
  363. [UIView animateWithDuration:0.3f animations:^{
  364. CGRect frame = self.frame;
  365. frame.origin.y = -frame.size.height;
  366. self.frame = frame;
  367. }];
  368. }
  369. - (void)setIsDisplay:(BOOL)isDisplay {
  370. _isDisplay = isDisplay;
  371. if (isDisplay) {
  372. [self displayView];
  373. }
  374. }
  375. - (void)openSettingView {
  376. if (@available(iOS 10, *)) {
  377. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
  378. } else {
  379. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
  380. }
  381. }
  382. - (void)isCameraAvailable:(void (^)(bool avilable))successBlock {
  383. NSString *mediaType = AVMediaTypeVideo; //读取媒体类型
  384. AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; //读取设备授权状态
  385. if (AVAuthorizationStatusAuthorized == authStatus) {
  386. successBlock(YES);
  387. } else if(authStatus == AVAuthorizationStatusNotDetermined) {
  388. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
  389. completionHandler:^(BOOL granted) {
  390. dispatch_async(dispatch_get_main_queue(), ^{
  391. if (granted) {
  392. successBlock(YES);
  393. } else {
  394. successBlock(NO);
  395. }
  396. });
  397. }];
  398. }else {
  399. successBlock(NO);
  400. }
  401. }
  402. -(void)isMicrophoneAvailable:(void (^)(bool avilable))successBlock {
  403. AVAudioSessionRecordPermission authStatus = [[AVAudioSession sharedInstance] recordPermission];
  404. if (AVAudioSessionRecordPermissionGranted == authStatus) {
  405. successBlock(YES);
  406. } else if(authStatus == AVAudioSessionRecordPermissionUndetermined){
  407. [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
  408. dispatch_async(dispatch_get_main_queue(), ^{
  409. if (granted) {
  410. successBlock(YES);
  411. } else {
  412. successBlock(NO);
  413. }
  414. });
  415. }];
  416. }else {
  417. successBlock(NO);
  418. }
  419. }
  420. - (void)dealloc {
  421. [[NSNotificationCenter defaultCenter] removeObserver:self];
  422. }
  423. /*
  424. // Only override drawRect: if you perform custom drawing.
  425. // An empty implementation adversely affects performance during animation.
  426. - (void)drawRect:(CGRect)rect {
  427. // Drawing code
  428. }
  429. */
  430. @end