ClassTitleView.m 16 KB

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