JXCourseListVC.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //
  2. // JXCourseListVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2017/10/20.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXCourseListVC.h"
  9. #import "JXCourseListCell.h"
  10. #import "JXChatViewController.h"
  11. #import "JXRelayVC.h"
  12. @interface JXCourseListVC ()<JXRelayVCDelegate>
  13. @property (nonatomic, strong) NSMutableArray *array;
  14. @property (nonatomic, assign) NSInteger selectIndex;
  15. @property (nonatomic, assign) NSInteger editIndex;
  16. @property (nonatomic, assign) BOOL isMultiselect;
  17. @property (nonatomic, strong) NSMutableArray *selArray;
  18. @property (nonatomic, strong) NSMutableArray *selCourseIds;
  19. @property (nonatomic, strong) NSMutableArray *allCourseArray;
  20. @property (nonatomic, strong) UIButton *sendBtn;
  21. @property (nonatomic, assign) int sendIndex;
  22. @property (nonatomic, strong) ATMHud *chatWait;
  23. @property (nonatomic, strong) NSTimer *timer;
  24. @property (nonatomic, strong) JXLabel *allLabel;
  25. @end
  26. @implementation JXCourseListVC
  27. - (void)dealloc {
  28. [g_notify removeObserver:self name:kUpdateCourseList object:nil];
  29. [_timer invalidate];
  30. _timer = nil;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.heightHeader = JX_SCREEN_TOP;
  35. self.heightFooter = 0;
  36. self.isGotoBack = YES;
  37. //self.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-JX_SCREEN_BOTTOM);
  38. [self createHeadAndFoot];
  39. self.isShowFooterPull = NO;
  40. self.title = Localized(@"JX_CourseList");
  41. _array = [NSMutableArray array];
  42. _selArray = [NSMutableArray array];
  43. _selCourseIds = [NSMutableArray array];
  44. _allCourseArray = [NSMutableArray array];
  45. _chatWait = [[ATMHud alloc] init];
  46. JXLabel *label = [[JXLabel alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH -32-15, JX_SCREEN_TOP - 30, 32, 15)];
  47. label.delegate = self;
  48. label.didTouch = @selector(multiselect:);
  49. label.text = Localized(@"JX_Multiselect");
  50. label.font = g_factory.font15;
  51. label.textColor = [UIColor blackColor];
  52. label.textAlignment = NSTextAlignmentRight;
  53. [self.tableHeader addSubview:label];
  54. self.isMultiselect = NO;
  55. _allLabel = label;
  56. _sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT - 48, JX_SCREEN_WIDTH, 48)];
  57. _sendBtn.backgroundColor = THEMECOLOR;
  58. [_sendBtn setTitle:Localized(@"JX_Send") forState:UIControlStateNormal];
  59. [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  60. _sendBtn.titleLabel.font = g_factory.font15;
  61. [_sendBtn addTarget:self action:@selector(sendCourseAction) forControlEvents:UIControlEventTouchUpInside];
  62. _sendBtn.hidden = YES;
  63. [self.view addSubview:_sendBtn];
  64. [g_notify addObserver:self selector:@selector(updateCourseList) name:kUpdateCourseList object:nil];
  65. [self getServerData];
  66. }
  67. - (void)updateCourseList {
  68. [_table reloadData];
  69. }
  70. - (void)multiselect:(JXLabel *)label {
  71. if ([label.text isEqualToString:Localized(@"JX_Cencal")]) {
  72. label.text = Localized(@"JX_Multiselect");
  73. self.isMultiselect = NO;
  74. _sendBtn.hidden = YES;
  75. [_table setFrame:CGRectMake(_table.frame.origin.x, _table.frame.origin.y, _table.frame.size.width, _table.frame.size.height + 48)];
  76. [self.selArray removeAllObjects];
  77. [self.selCourseIds removeAllObjects];
  78. self.sendIndex = 0;
  79. [self.allCourseArray removeAllObjects];
  80. }else {
  81. label.text = Localized(@"JX_Cencal");
  82. self.isMultiselect = YES;
  83. _sendBtn.hidden = NO;
  84. [_table setFrame:CGRectMake(_table.frame.origin.x, _table.frame.origin.y, _table.frame.size.width, _table.frame.size.height - 48)];
  85. }
  86. [_table reloadData];
  87. }
  88. - (void)sendCourseAction {
  89. if (self.selCourseIds.count > 0) {
  90. NSString *courseId = self.selCourseIds.firstObject;
  91. [_chatWait start:Localized(@"JX_Loading")];
  92. [g_server userCourseGetWithCourseId:courseId toView:self];
  93. }
  94. }
  95. -(void)getServerData{
  96. [g_server userCourseList:self];
  97. }
  98. #pragma mark ---------tableView协议----------------
  99. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  100. {
  101. NSString* cellName = [NSString stringWithFormat:@"courseListCell_%ld",indexPath.row];
  102. JXCourseListCell *courseListCell = [tableView dequeueReusableCellWithIdentifier:cellName];
  103. if (!courseListCell) {
  104. courseListCell = [[JXCourseListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
  105. }
  106. courseListCell.vc = self;
  107. courseListCell.indexNum = indexPath.row;
  108. courseListCell.isMultiselect = self.isMultiselect;
  109. NSDictionary *dict = _array[indexPath.row];
  110. [courseListCell setData:dict];
  111. courseListCell.selectionStyle = UITableViewCellSelectionStyleNone;
  112. return courseListCell;
  113. }
  114. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  115. {
  116. return _array.count;
  117. }
  118. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  119. {
  120. return 70;
  121. }
  122. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  123. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  124. if (self.isMultiselect) {
  125. JXCourseListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  126. NSInteger num = [self getSelNum:[cell.multiselectBtn.titleLabel.text integerValue] indexNum:indexPath.row];
  127. if (num > 0) {
  128. [cell.multiselectBtn setTitle:[NSString stringWithFormat:@"%ld",num] forState:UIControlStateNormal];
  129. }else {
  130. cell.multiselectBtn.titleLabel.text = @"";
  131. [cell.multiselectBtn setTitle:@"" forState:UIControlStateNormal];
  132. }
  133. return;
  134. }
  135. NSDictionary *dict = _array[indexPath.row];
  136. self.selectIndex = indexPath.row;
  137. [g_server userCourseGetWithCourseId:dict[@"courseId"] toView:self];
  138. }
  139. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  140. return YES;
  141. }
  142. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  143. self.editIndex = indexPath.row;
  144. UITableViewRowAction *deleteBtn = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:Localized(@"JX_Delete") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  145. NSDictionary *dict = _array[indexPath.row];
  146. [g_server userCourseDeleteWithCourseId:dict[@"courseId"] toView:self];
  147. [_array removeObject:dict];
  148. }];
  149. UITableViewRowAction *editBtn = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:Localized(@"JX_ModifyName") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  150. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Localized(@"JX_CourseName") message:nil delegate:self cancelButtonTitle:Localized(@"JX_Cencal") otherButtonTitles:Localized(@"JX_Confirm"), nil];
  151. alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
  152. [alertView show];
  153. }];
  154. return @[deleteBtn,editBtn];
  155. }
  156. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  157. if (buttonIndex == 1) {
  158. UITextField *tf = [alertView textFieldAtIndex:0];
  159. NSMutableDictionary *dict = _array[self.editIndex];
  160. if (tf.text.length <= 0) {
  161. [g_App showAlert:Localized(@"JX_InputCourseName")];
  162. return;
  163. }
  164. [g_server userCourseUpdateWithCourseId:dict[@"courseId"] MessageIds:nil CourseName:tf.text CourseMessageId:nil toView:self];
  165. dict[@"courseName"] = tf.text;
  166. }
  167. }
  168. - (NSInteger)getSelNum:(NSInteger)num indexNum:(NSInteger)indexNum{
  169. NSInteger index = 0;
  170. if (num > 0) {
  171. [self.selArray removeObject:[NSNumber numberWithInteger:num]];
  172. NSDictionary *dict = _array[indexNum];
  173. [self.selCourseIds removeObject:dict[@"courseId"]];
  174. }else {
  175. for (NSInteger i = 0; i < self.selArray.count; i ++) {
  176. NSInteger m = [self.selArray[i] integerValue];
  177. if (m != i + 1) {
  178. index = i + 1;
  179. break;
  180. }
  181. }
  182. if (index == 0) {
  183. [self.selArray addObject:[NSNumber numberWithInteger:self.selArray.count + 1]];
  184. NSDictionary *dict = _array[indexNum];
  185. [self.selCourseIds addObject:dict[@"courseId"]];
  186. index = self.selArray.count;
  187. }else {
  188. [self.selArray insertObject:[NSNumber numberWithInteger:index] atIndex:index - 1];
  189. NSDictionary *dict = _array[indexNum];
  190. [self.selCourseIds insertObject:dict[@"courseId"] atIndex:index - 1];
  191. }
  192. }
  193. if (self.selArray.count > 0) {
  194. if (_sendBtn.hidden) {
  195. _sendBtn.hidden = NO;
  196. [_table setFrame:CGRectMake(_table.frame.origin.x, _table.frame.origin.y, _table.frame.size.width, _table.frame.size.height - 48)];
  197. }
  198. }else {
  199. if (!_sendBtn.hidden) {
  200. _sendBtn.hidden = YES;
  201. [_table setFrame:CGRectMake(_table.frame.origin.x, _table.frame.origin.y, _table.frame.size.width, _table.frame.size.height + 48)];
  202. }
  203. }
  204. return index;
  205. }
  206. - (void)sendCourse:(NSTimer *) timer{
  207. JXMsgAndUserObject *obj = timer.userInfo;
  208. BOOL isRoom;
  209. if ([obj.user.roomFlag intValue] > 0 || obj.user.roomId.length > 0) {
  210. isRoom = YES;
  211. }else {
  212. isRoom = NO;
  213. }
  214. self.sendIndex ++;
  215. // [_chatWait start:[NSString stringWithFormat:@"正在发送:%d/%ld",self.sendIndex,_array.count] inView:g_window];
  216. [_chatWait setCaption:[NSString stringWithFormat:@"%@:%d/%ld",Localized(@"JX_SendNow"),self.sendIndex,_allCourseArray.count]];
  217. [_chatWait update];
  218. JXMessageObject *msg= _allCourseArray[self.sendIndex - 1];
  219. msg.messageId = nil;
  220. msg.timeSend = [NSDate date];
  221. msg.fromId = nil;
  222. msg.fromUserId = MY_USER_ID;
  223. if(isRoom){
  224. msg.toUserId = obj.user.userId;
  225. msg.isGroup = YES;
  226. msg.fromUserName = g_myself.userNickname;
  227. }
  228. else{
  229. msg.toUserId = obj.user.userId;
  230. msg.isGroup = NO;
  231. }
  232. // msg.content = relayMsg.content;
  233. // msg.type = relayMsg.type;
  234. msg.isSend = [NSNumber numberWithInt:transfer_status_ing];
  235. msg.isRead = [NSNumber numberWithBool:NO];
  236. msg.isReadDel = [NSNumber numberWithInt:NO];
  237. //发往哪里
  238. if (isRoom) {
  239. [msg insert:obj.user.userId];
  240. [g_xmpp sendMessage:msg roomName:obj.user.userId];//发送消息
  241. }else {
  242. [msg insert:nil];
  243. [g_xmpp sendMessage:msg roomName:nil];//发送消息
  244. }
  245. if (_allCourseArray.count == self.sendIndex) {
  246. [_chatWait stop];
  247. [_timer invalidate];
  248. _timer = nil;
  249. [JXMyTools showTipView:Localized(@"JXAlert_SendOK")];
  250. if (self.isMultiselect) {
  251. [self multiselect:_allLabel];
  252. }
  253. }
  254. }
  255. - (void)relay:(JXRelayVC *)relayVC MsgAndUserObject:(JXMsgAndUserObject *)obj {
  256. if (_allCourseArray.count <= 0) {
  257. return;
  258. }
  259. [_chatWait start:[NSString stringWithFormat:@"%@:1/%ld",Localized(@"JX_SendNow"),_allCourseArray.count] inView:g_window];
  260. _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(sendCourse:) userInfo:obj repeats:YES];
  261. }
  262. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  263. if ([aDownload.action isEqualToString:act_userCourseList]) {
  264. _array = [array1 mutableCopy];
  265. [self.tableView reloadData];
  266. }
  267. if ([aDownload.action isEqualToString:act_userCourseGet]) {
  268. NSMutableArray *array = [NSMutableArray array];
  269. for (NSInteger i = 0; i < array1.count; i ++) {
  270. NSDictionary *dict = array1[i];
  271. SBJsonParser * parser = [[SBJsonParser alloc] init] ;
  272. NSDictionary *content = [parser objectWithString:dict[@"message"]];
  273. JXMessageObject *msg = [[JXMessageObject alloc] init];
  274. [msg fromDictionary:content];
  275. msg.timeLen = [content objectForKey:@"fileTime"];
  276. msg.messageId = [(NSDictionary *)[content objectForKey:@"messageHead"] objectForKey:@"messageId"];
  277. msg.timeSend = [NSDate dateWithTimeIntervalSince1970:[[content objectForKey:kMESSAGE_TIMESEND] doubleValue] / 1000.0];
  278. msg.isNotUpdateHeight = YES;
  279. if(![msg isVisible] && [msg.type intValue]!=kWCMessageTypeIsRead)
  280. continue;
  281. [dict setValue:msg forKey:@"message"];
  282. [array addObject:dict];
  283. }
  284. if (self.isMultiselect) {
  285. for (NSDictionary *dict in array) {
  286. [_allCourseArray addObject:dict[@"message"]];
  287. }
  288. [self.selCourseIds removeObjectAtIndex:0];
  289. if (self.selCourseIds.count > 0) {
  290. NSString *courseId = self.selCourseIds.firstObject;
  291. [g_server userCourseGetWithCourseId:courseId toView:self];
  292. }else {
  293. [_chatWait stop];
  294. JXRelayVC *vc = [[JXRelayVC alloc] init];
  295. vc.isCourse = YES;
  296. vc.relayDelegate = self;
  297. // [g_window addSubview:vc.view];
  298. [g_navigation pushViewController:vc animated:YES];
  299. }
  300. }else {
  301. JXChatViewController *sendView=[JXChatViewController alloc];
  302. NSDictionary *dict = _array[self.selectIndex];
  303. sendView.title = Localized(@"JX_CourseDetails");
  304. sendView.courseArray = array;
  305. sendView.courseId = dict[@"courseId"];
  306. sendView = [sendView init];
  307. // [g_App.window addSubview:sendView.view];
  308. [g_navigation pushViewController:sendView animated:YES];
  309. }
  310. }
  311. if ([aDownload.action isEqualToString:act_userCourseUpdate]) {
  312. [JXMyTools showTipView:Localized(@"JXAlert_UpdateOK")];
  313. [self.tableView reloadData];
  314. }
  315. if ([aDownload.action isEqualToString:act_userCourseDelete]) {
  316. [JXMyTools showTipView:Localized(@"JXAlert_DeleteOK")];
  317. [self.tableView reloadData];
  318. }
  319. [_wait stop];
  320. }
  321. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  322. [_wait stop];
  323. return show_error;
  324. }
  325. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  326. [_wait stop];
  327. return show_error;
  328. }
  329. -(void) didServerConnectStart:(JXConnection*)aDownload{
  330. [_wait start];
  331. }
  332. - (void)didReceiveMemoryWarning {
  333. [super didReceiveMemoryWarning];
  334. // Dispose of any resources that can be recreated.
  335. }
  336. /*
  337. #pragma mark - Navigation
  338. // In a storyboard-based application, you will often want to do a little preparation before navigation
  339. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  340. // Get the new view controller using [segue destinationViewController].
  341. // Pass the selected object to the new view controller.
  342. }
  343. */
  344. @end