JXCommonService.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // JXCommonService.m
  3. // shiku_im
  4. //
  5. // Created by p on 2017/11/9.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXCommonService.h"
  9. @interface JXCommonService()
  10. @property (nonatomic, assign) NSInteger sendIndex;
  11. @property (nonatomic, strong) NSArray *courseArray;
  12. @property (nonatomic, assign) NSInteger timeIndex;
  13. @property (nonatomic, strong) UILabel *sendLabel;
  14. @property (nonatomic, assign) CGRect subWindowFrame;
  15. @end
  16. @implementation JXCommonService
  17. // 发送课程
  18. - (void)sendCourse:(JXMsgAndUserObject *)obj Array:(NSArray *)array {
  19. [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
  20. g_subWindow.frame = CGRectMake(JX_SCREEN_WIDTH - 80 - 10, 50, 80, 100);
  21. g_subWindow.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  22. g_subWindow.layer.masksToBounds = YES;
  23. g_subWindow.layer.cornerRadius = 5.f;
  24. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
  25. [g_subWindow addGestureRecognizer:pan];
  26. self.sendLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, g_subWindow.frame.size.width, g_subWindow.frame.size.height - 20)];
  27. self.sendLabel.textColor = [UIColor whiteColor];
  28. self.sendLabel.font = g_factory.font12;
  29. self.sendLabel.numberOfLines = 0;
  30. self.sendLabel.textAlignment = NSTextAlignmentCenter;
  31. self.sendLabel.text = [NSString stringWithFormat:@"%@:\n1/%ld",Localized(@"JX_SendingCourses"),array.count];
  32. [g_subWindow addSubview:self.sendLabel];
  33. _courseArray = array;
  34. _sendIndex = 0;
  35. _timeIndex = 0;
  36. _courseTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sendCourseTimerAction:) userInfo:obj repeats:YES];
  37. [[NSRunLoop currentRunLoop] run];//已经将nstimer添加到NSRunloop中了
  38. }
  39. - (void)sendCourseTimerAction:(NSTimer *)timer {
  40. // _timeIndex ++;
  41. JXMessageObject *msg= _courseArray[self.sendIndex];
  42. self.sendLabel.text = [NSString stringWithFormat:@"%@:\n%ld/%ld",Localized(@"JX_SendingCourses"),self.sendIndex + 1,_courseArray.count];
  43. // NSInteger index = 0;
  44. // if ([msg.type integerValue] == kWCMessageTypeText) {
  45. //
  46. // index = msg.content.length / 2;
  47. //
  48. // }else if([msg.type integerValue] == kWCMessageTypeVoice) {
  49. //
  50. // index = [msg.timeLen integerValue];
  51. //
  52. // }else {
  53. // index = 0;
  54. // }
  55. // index += 3;
  56. // if (_timeIndex < index && self.sendIndex != 0) {
  57. // return;
  58. // }
  59. // _timeIndex = 0;
  60. JXMsgAndUserObject *obj = timer.userInfo;
  61. BOOL isRoom;
  62. if ([obj.user.roomFlag intValue] > 0 || obj.user.roomId.length > 0) {
  63. isRoom = YES;
  64. }else {
  65. isRoom = NO;
  66. }
  67. msg.messageId = nil;
  68. msg.timeSend = [NSDate date];
  69. msg.fromId = nil;
  70. msg.fromUserId = MY_USER_ID;
  71. if(isRoom){
  72. msg.toUserId = obj.user.userId;
  73. msg.isGroup = YES;
  74. msg.fromUserName = g_myself.userNickname;
  75. }
  76. else{
  77. msg.toUserId = obj.user.userId;
  78. msg.isGroup = NO;
  79. }
  80. // msg.content = relayMsg.content;
  81. // msg.type = relayMsg.type;
  82. msg.isSend = [NSNumber numberWithInt:transfer_status_ing];
  83. msg.isRead = [NSNumber numberWithBool:NO];
  84. msg.isReadDel = [NSNumber numberWithInt:NO];
  85. //发往哪里
  86. dispatch_async(dispatch_get_main_queue(), ^{
  87. if (isRoom) {
  88. [msg insert:obj.user.userId];
  89. [g_xmpp sendMessage:msg roomName:obj.user.userId];//发送消息
  90. }else {
  91. [msg insert:nil];
  92. [g_xmpp sendMessage:msg roomName:nil];//发送消息
  93. }
  94. });
  95. [g_notify postNotificationName:kSendCourseMsg object:msg];
  96. self.sendIndex ++;
  97. if (_courseArray.count == self.sendIndex) {
  98. [_courseTimer invalidate];
  99. _courseTimer = nil;
  100. [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
  101. g_subWindow.hidden = YES;
  102. // [g_subWindow resignKeyWindow];
  103. [g_subWindow removeFromSuperview];
  104. g_subWindow = nil;
  105. self.sendLabel = nil;
  106. [JXMyTools showTipView:Localized(@"JXAlert_SendOK")];
  107. }
  108. }
  109. - (void)panAction:(UIPanGestureRecognizer *)pan {
  110. if (pan.state == UIGestureRecognizerStateBegan) {
  111. self.subWindowFrame = g_subWindow.frame;
  112. }
  113. CGPoint offset = [pan translationInView:g_App.window];
  114. CGPoint offset1 = [pan translationInView:g_subWindow];
  115. NSLog(@"pan - offset = %@, offset1 = %@", NSStringFromCGPoint(offset), NSStringFromCGPoint(offset1));
  116. CGRect frame = self.subWindowFrame;
  117. frame.origin.x += offset.x;
  118. frame.origin.y += offset.y;
  119. g_subWindow.frame = frame;
  120. if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
  121. if (frame.origin.x <= JX_SCREEN_WIDTH / 2) {
  122. frame.origin.x = 10;
  123. }else {
  124. frame.origin.x = JX_SCREEN_WIDTH - frame.size.width - 10;
  125. }
  126. [UIView animateWithDuration:0.5 animations:^{
  127. g_subWindow.frame = frame;
  128. }];
  129. }
  130. }
  131. @end