JXNewRoomVC.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. //
  2. // JXNewRoomVC.m
  3. // shiku_im
  4. //
  5. // Created by flyeagleTang on 14-6-10.
  6. // Copyright (c) 2014年 Reese. All rights reserved.
  7. //
  8. #import "JXNewRoomVC.h"
  9. //#import "selectTreeVC.h"
  10. #import "selectValueVC.h"
  11. #import "selectProvinceVC.h"
  12. #import "ImageResize.h"
  13. #import "roomData.h"
  14. #import "JXUserInfoVC.h"
  15. #import "JXSelFriendVC.h"
  16. #import "JXRoomObject.h"
  17. #import "JXChatViewController.h"
  18. #import "JXRoomObject.h"
  19. #import "JXRoomPool.h"
  20. #import "JXSelectFriendsVC.h"
  21. #define HEIGHT 56
  22. #define IMGSIZE 170
  23. @interface JXNewRoomVC ()<UITextFieldDelegate,JXRoomObjectDelegate>
  24. @property (nonatomic, assign) NSInteger roomNameLength;
  25. @property (nonatomic, assign) NSInteger descLength;
  26. @property (nonatomic, strong) JXImageView *GroupValidationBtn;
  27. @property (nonatomic, strong) UISwitch *GroupValidationSwitch;
  28. @property (nonatomic, strong) UISwitch *showGroupMembersSwitch;
  29. @property (nonatomic, strong) UISwitch *sendCardSwitch;
  30. @end
  31. @implementation JXNewRoomVC
  32. @synthesize chatRoom;
  33. - (id)init
  34. {
  35. self = [super init];
  36. if (self) {
  37. self.isGotoBack = YES;
  38. self.title = Localized(@"JXNewRoomVC_CreatRoom");
  39. self.heightFooter = 0;
  40. self.heightHeader = JX_SCREEN_TOP;
  41. //self.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  42. [self createHeadAndFoot];
  43. self.tableBody.backgroundColor = HEXCOLOR(0xF2F2F2);
  44. self.tableBody.scrollEnabled = YES;
  45. self.tableBody.showsVerticalScrollIndicator = YES;
  46. int h = 0;
  47. _room = [[roomData alloc] init];
  48. _room.maxCount = 10000;
  49. JXImageView* iv;
  50. iv = [[JXImageView alloc]init];
  51. iv.frame = self.tableBody.bounds;
  52. iv.delegate = self;
  53. iv.didTouch = @selector(hideKeyboard);
  54. [self.tableBody addSubview:iv];
  55. // [iv release];
  56. iv = [self createButton:Localized(@"JX_RoomName") drawTop:NO drawBottom:YES must:NO click:nil];
  57. iv.frame = CGRectMake(0, h, JX_SCREEN_WIDTH, HEIGHT);
  58. _roomName = [self createTextField:iv default:_room.name hint:Localized(@"JX_InputRoomName") type:1];
  59. h+=iv.frame.size.height;
  60. iv = [self createButton:Localized(@"JX_RoomExplain") drawTop:NO drawBottom:YES must:NO click:nil];
  61. iv.frame = CGRectMake(0, h, JX_SCREEN_WIDTH, HEIGHT);
  62. _desc = [self createTextField:iv default:_room.desc hint:Localized(@"JXNewRoomVC_InputExplain") type:0];
  63. h+=iv.frame.size.height;
  64. iv = [self createButton:Localized(@"JXRoomMemberVC_CreatPer") drawTop:NO drawBottom:NO must:NO click:nil];
  65. iv.frame = CGRectMake(0, h, JX_SCREEN_WIDTH, HEIGHT);
  66. _userName = [self createLabel:iv default:g_myself.userNickname];
  67. h+=iv.frame.size.height;
  68. #ifdef IS_MsgEncrypt
  69. iv = [self createButton:@"是否开启私密群组" drawTop:YES drawBottom:NO must:NO click:nil];
  70. iv.frame = CGRectMake(0, h, JX_SCREEN_WIDTH, HEIGHT);
  71. _secretSwitch = [[UISwitch alloc] init];
  72. _secretSwitch.frame = CGRectMake(JX_SCREEN_WIDTH-5-51,0,0,0);
  73. _secretSwitch.onTintColor = THEMECOLOR;
  74. _secretSwitch.center = CGPointMake(_secretSwitch.center.x, iv.frame.size.height/2);
  75. [_secretSwitch setOn:NO];
  76. [_secretSwitch addTarget:self action:@selector(secretSwitchAction:) forControlEvents:UIControlEventValueChanged];
  77. [iv addSubview:_secretSwitch];
  78. h+=iv.frame.size.height;
  79. #endif
  80. // iv = [self createButton:Localized(@"JXRoomMemberVC_PerCount") drawTop:NO drawBottom:YES must:NO click:nil];
  81. // iv.frame = CGRectMake(0, h, JX_SCREEN_WIDTH, HEIGHT);
  82. // _size = [self createLabel:iv default:[NSString stringWithFormat:@"%ld/%d",_room.curCount,_room.maxCount]];
  83. // h+=iv.frame.size.height;
  84. // iv = [self createButton:Localized(@"JX_DisplayGroupMemberList") drawTop:NO drawBottom:YES must:NO click:nil];
  85. // iv.frame = CGRectMake(0, h, JX_SCREEN_WIDTH, HEIGHT);
  86. // _showGroupMembersSwitch = [[UISwitch alloc] init];
  87. // _showGroupMembersSwitch.frame = CGRectMake(JX_SCREEN_WIDTH-5-51,0,0,0);
  88. // _showGroupMembersSwitch.center = CGPointMake(_showGroupMembersSwitch.center.x, iv.frame.size.height/2);
  89. // [_showGroupMembersSwitch setOn:YES];
  90. // [iv addSubview:_showGroupMembersSwitch];
  91. // h+=iv.frame.size.height;
  92. // iv = [self createButton:@"允许群成员在群组内发送名片" drawTop:NO drawBottom:YES must:NO click:nil];
  93. // iv.frame = CGRectMake(0, h, JX_SCREEN_WIDTH, HEIGHT);
  94. // _sendCardSwitch = [[UISwitch alloc] init];
  95. // _sendCardSwitch.frame = CGRectMake(JX_SCREEN_WIDTH-5-51,0,0,0);
  96. // _sendCardSwitch.center = CGPointMake(_sendCardSwitch.center.x, iv.frame.size.height/2);
  97. // [_sendCardSwitch setOn:YES];
  98. // [iv addSubview:_sendCardSwitch];
  99. // h+=iv.frame.size.height;
  100. // iv = [self createButton:Localized(@"JX_RoomShowRead") drawTop:NO drawBottom:YES must:NO click:nil];
  101. // iv.frame = CGRectMake(0, h, JX_SCREEN_WIDTH, HEIGHT);
  102. // _readSwitch = [[UISwitch alloc] init];
  103. // _readSwitch.frame = CGRectMake(JX_SCREEN_WIDTH-5-51,0,0,0);
  104. // _readSwitch.center = CGPointMake(_readSwitch.center.x, iv.frame.size.height/2);
  105. // [iv addSubview:_readSwitch];
  106. // h+=iv.frame.size.height;
  107. // if ([g_config.isOpenRoomSearch boolValue]) {
  108. // iv = [self createButton:Localized(@"JX_PrivateGroups") drawTop:NO drawBottom:YES must:NO click:nil];
  109. // iv.frame = CGRectMake(0, h, JX_SCREEN_WIDTH, HEIGHT);
  110. // _publicSwitch = [[UISwitch alloc] init];
  111. // _publicSwitch.frame = CGRectMake(JX_SCREEN_WIDTH-5-51,0,0,0);
  112. // _publicSwitch.onTintColor = THEMECOLOR;
  113. // _publicSwitch.center = CGPointMake(_publicSwitch.center.x, iv.frame.size.height/2);
  114. // [_publicSwitch setOn:YES];
  115. // // _publicSwitch.userInteractionEnabled = NO;
  116. // [_publicSwitch addTarget:self action:@selector(publicSwitchAction:) forControlEvents:UIControlEventValueChanged];
  117. // [iv addSubview:_publicSwitch];
  118. // h+=iv.frame.size.height;
  119. // }
  120. // self.GroupValidationBtn = [self createButton:Localized(@"JX_OpenGroupValidation") drawTop:NO drawBottom:YES must:NO click:nil];
  121. // self.GroupValidationBtn.frame = CGRectMake(0, h, JX_SCREEN_WIDTH, HEIGHT);
  122. // self.GroupValidationSwitch = [[UISwitch alloc] init];
  123. // self.GroupValidationSwitch.frame = CGRectMake(JX_SCREEN_WIDTH-5-51,0,0,0);
  124. // self.GroupValidationSwitch.center = CGPointMake(self.GroupValidationSwitch.center.x, self.GroupValidationBtn.frame.size.height/2);
  125. // [self.GroupValidationBtn addSubview:self.GroupValidationSwitch];
  126. // h+=self.GroupValidationBtn.frame.size.height;
  127. h+=20;
  128. UIButton* _btn;
  129. _btn = [UIFactory createCommonButton:Localized(@"JXNewRoomVC_CreatRoom") target:self action:@selector(onInsert)];
  130. _btn.custom_acceptEventInterval = .25f;
  131. _btn.layer.cornerRadius = 7;
  132. _btn.clipsToBounds = YES;
  133. _btn.frame = CGRectMake(15,h,JX_SCREEN_WIDTH-30,40);
  134. [self.tableBody addSubview:_btn];
  135. }
  136. return self;
  137. }
  138. -(void)dealloc{
  139. // NSLog(@"JXNewRoomVC.dealloc");
  140. // [_room release];
  141. // [super dealloc];
  142. }
  143. - (void)viewDidLoad
  144. {
  145. [super viewDidLoad];
  146. // Do any additional setup after loading the view.
  147. }
  148. - (void)didReceiveMemoryWarning
  149. {
  150. [super didReceiveMemoryWarning];
  151. // Dispose of any resources that can be recreated.
  152. }
  153. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
  154. return YES;
  155. }
  156. -(JXImageView*)createButton:(NSString*)title drawTop:(BOOL)drawTop drawBottom:(BOOL)drawBottom must:(BOOL)must click:(SEL)click{
  157. JXImageView* btn = [[JXImageView alloc] init];
  158. btn.backgroundColor = [UIColor whiteColor];
  159. btn.userInteractionEnabled = YES;
  160. if(click)
  161. btn.didTouch = click;
  162. else
  163. btn.didTouch = @selector(hideKeyboard);
  164. btn.delegate = self;
  165. [self.tableBody addSubview:btn];
  166. if(must){
  167. UILabel* p = [[UILabel alloc] initWithFrame:CGRectMake(INSETS, 5, 20, HEIGHT-5)];
  168. p.text = @"*";
  169. p.font = g_factory.font16;
  170. p.backgroundColor = [UIColor clearColor];
  171. p.textColor = [UIColor redColor];
  172. p.textAlignment = NSTextAlignmentCenter;
  173. [btn addSubview:p];
  174. }
  175. JXLabel* p = [[JXLabel alloc] initWithFrame:CGRectMake(15, 0, 230, HEIGHT)];
  176. p.text = title;
  177. p.font = g_factory.font16;
  178. p.backgroundColor = [UIColor clearColor];
  179. p.textColor = [UIColor blackColor];
  180. [btn addSubview:p];
  181. if(drawTop){
  182. UIView* line = [[UIView alloc] initWithFrame:CGRectMake(15,0,JX_SCREEN_WIDTH-15,LINE_WH)];
  183. line.backgroundColor = THE_LINE_COLOR;
  184. [btn addSubview:line];
  185. }
  186. if(drawBottom){
  187. UIView* line = [[UIView alloc]initWithFrame:CGRectMake(15,HEIGHT-LINE_WH,JX_SCREEN_WIDTH-15,LINE_WH)];
  188. line.backgroundColor = THE_LINE_COLOR;
  189. [btn addSubview:line];
  190. }
  191. if(click){
  192. UIImageView* iv;
  193. iv = [[UIImageView alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH-15-7, (HEIGHT-13)/2, 7, 13)];
  194. iv.image = [UIImage imageNamed:@"new_icon_>"];
  195. [btn addSubview:iv];
  196. }
  197. return btn;
  198. }
  199. -(UITextField*)createTextField:(UIView*)parent default:(NSString*)s hint:(NSString*)hint type:(BOOL)name{
  200. UITextField* p = [[UITextField alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH/2,INSETS,JX_SCREEN_WIDTH/2-15,HEIGHT-INSETS*2)];
  201. p.delegate = self;
  202. p.autocorrectionType = UITextAutocorrectionTypeNo;
  203. p.autocapitalizationType = UITextAutocapitalizationTypeNone;
  204. p.enablesReturnKeyAutomatically = YES;
  205. p.borderStyle = UITextBorderStyleNone;
  206. p.returnKeyType = UIReturnKeyDone;
  207. // p.clearButtonMode = UITextFieldViewModeAlways;
  208. p.textAlignment = NSTextAlignmentRight;
  209. p.userInteractionEnabled = YES;
  210. p.text = s;
  211. p.placeholder = hint;
  212. p.font = g_factory.font16;
  213. if (name) {
  214. [p addTarget:self action:@selector(textLong12:) forControlEvents:UIControlEventEditingChanged];
  215. }else{
  216. [p addTarget:self action:@selector(textLong32:) forControlEvents:UIControlEventEditingChanged];
  217. }
  218. [parent addSubview:p];
  219. // [p release];
  220. return p;
  221. }
  222. - (void)textLong12:(UITextField *)textField
  223. {
  224. NSInteger length = [self getTextLength:textField.text];
  225. if (length > 20) {
  226. textField.text = [textField.text substringToIndex:_roomNameLength];
  227. [JXMyTools showTipView:Localized(@"JX_CannotEnterMore")];
  228. }
  229. _roomNameLength = textField.text.length;
  230. }
  231. - (void)textLong32:(UITextField *)textField
  232. {
  233. NSInteger length = [self getTextLength:textField.text];
  234. if (length > 100) {
  235. textField.text = [textField.text substringToIndex:_descLength];
  236. [JXMyTools showTipView:Localized(@"JX_CannotEnterMore")];
  237. }
  238. _descLength = textField.text.length;
  239. }
  240. - (NSInteger) getTextLength:(NSString *)text {
  241. NSInteger length = [text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  242. NSInteger num = (length - text.length) / 2;
  243. length = length - num;
  244. return length;
  245. }
  246. -(UILabel*)createLabel:(UIView*)parent default:(NSString*)s{
  247. UILabel* p = [[UILabel alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH/2,INSETS,JX_SCREEN_WIDTH/2 - 15,HEIGHT-INSETS*2)];
  248. p.userInteractionEnabled = NO;
  249. p.text = s;
  250. p.font = g_factory.font16;
  251. p.textAlignment = NSTextAlignmentRight;
  252. [parent addSubview:p];
  253. // [p release];
  254. return p;
  255. }
  256. - (void)publicSwitchAction:(UISwitch *)publicSwitch {
  257. // if (publicSwitch.on) {
  258. // self.GroupValidationBtn.hidden = YES;
  259. // self.GroupValidationSwitch.on = NO;
  260. // }else {
  261. // self.GroupValidationBtn.hidden = NO;
  262. // }
  263. }
  264. - (void)secretSwitchAction:(UISwitch *)secretSwitch {
  265. }
  266. -(void)onUpdate{
  267. if(![self getInputValue])
  268. return;
  269. }
  270. -(BOOL)getInputValue{
  271. if([_roomName.text length]<=0){
  272. [g_App showAlert:Localized(@"JX_InputName")];
  273. return NO;
  274. }
  275. return YES;
  276. }
  277. -(BOOL)hideKeyboard{
  278. BOOL b = _roomName.editing || _desc.editing;
  279. [self.view endEditing:YES];
  280. return b;
  281. }
  282. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  283. {
  284. [self.view endEditing:YES];
  285. return YES;
  286. }
  287. -(void)onInsert{
  288. if ([_roomName.text isEqualToString:@""]) {
  289. [g_App showAlert:Localized(@"JX_InputRoomName")];
  290. }
  291. // else if ([_desc.text isEqualToString:@""]){
  292. // [g_App showAlert:Localized(@"JXNewRoomVC_InputExplain")];
  293. // }
  294. else{
  295. #ifdef IS_MsgEncrypt
  296. if (!g_msgUtil.rsaPublicKey || g_msgUtil.rsaPublicKey.length <= 0) {
  297. if (_secretSwitch.isOn) {
  298. [JXMyTools showTipView:@"不支持创建私密群组"];
  299. return;
  300. }
  301. }
  302. #endif
  303. _roomName.text = [_roomName.text stringByReplacingOccurrencesOfString:@" " withString:@""];
  304. if (_roomName.text.length <= 0) {
  305. [JXMyTools showTipView:@"请输入有效的群组名称"];
  306. return;
  307. }
  308. NSString* s = [NSUUID UUID].UUIDString;
  309. s = [[s stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
  310. _room.roomJid= s;
  311. _room.name = _roomName.text;
  312. _room.desc = _desc.text;
  313. _room.userId = [g_myself.userId longLongValue];
  314. _room.userNickName = _userName.text;
  315. _room.showRead = NO;
  316. _room.showMember = YES;
  317. _room.allowSendCard = YES;
  318. _room.isNeedVerify = NO;
  319. _room.allowInviteFriend = YES;
  320. _room.allowUploadFile = YES;
  321. _room.allowConference = YES;
  322. _room.allowSpeakCourse = YES;
  323. _room.isSecretGroup = _secretSwitch.isOn;
  324. NSInteger category = 0;
  325. if (self.isAddressBook) {
  326. category = 510;
  327. }
  328. _chatRoom = [[JXXMPP sharedInstance].roomPool createRoom:s title:_roomName.text];
  329. _chatRoom.delegate = self;
  330. [g_server addRoom:_room isPublic:_publicSwitch.on isNeedVerify:self.GroupValidationSwitch.on category:category toView:self];
  331. //
  332. // _chatRoom = [[JXXMPP sharedInstance].roomPool createRoom:s title:_roomName.text];
  333. // _chatRoom.delegate = self;
  334. [_wait start:Localized(@"JXAlert_CreatRoomIng") delay:30];
  335. }
  336. }
  337. -(void)xmppRoomDidCreate{
  338. NSInteger category = 0;
  339. if (self.isAddressBook) {
  340. category = 510;
  341. }
  342. if (_room.isSecretGroup) {
  343. NSString *g = [NSUUID UUID].UUIDString;
  344. g = [[g stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
  345. _room.chatKeyGroup = g;
  346. }
  347. [g_server addRoom:_room isPublic:_publicSwitch.on isNeedVerify:self.GroupValidationSwitch.on category:category toView:self];
  348. _chatRoom.delegate = nil;
  349. }
  350. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  351. [_wait stop];
  352. if( [aDownload.action isEqualToString:act_roomAdd] ){
  353. _chatRoom = [[JXXMPP sharedInstance].roomPool createRoom:_room.roomJid title:_roomName.text];
  354. _chatRoom.delegate = self;
  355. _room.roomId = [dict objectForKey:@"id"];
  356. // _room.call = [NSString stringWithFormat:@"%@",[dict objectForKey:@"call"]];
  357. [self insertRoom];
  358. memberData *member = [[memberData alloc] init];
  359. member.userId = [g_myself.userId longLongValue];
  360. member.userNickName = MY_USER_NAME;
  361. member.role = @1;
  362. [_room.members addObject:member];
  363. JXSelectFriendsVC *vc = [JXSelectFriendsVC alloc];
  364. vc.chatRoom = _chatRoom;
  365. vc.room = _room;
  366. vc.isNewRoom = YES;
  367. vc.maxSize = [[dict objectForKey:@"maxUserSize"] intValue] - 1;
  368. if (self.isAddressBook) {
  369. NSMutableArray *arr = [NSMutableArray array];
  370. NSMutableSet *existSet = [NSMutableSet set];
  371. for (NSInteger i = 0; i < self.addressBookArr.count; i ++) {
  372. JXAddressBook *ab = self.addressBookArr[i];
  373. JXUserObject *user = [[JXUserObject alloc] init];
  374. user.userId = ab.toUserId;
  375. user.userNickname = ab.toUserName;
  376. [arr addObject:user];
  377. [existSet addObject:ab.toUserId];
  378. }
  379. vc.existSet = [existSet copy];
  380. vc.addressBookArr = arr;
  381. }
  382. vc = [vc init];
  383. [g_navigation pushViewController:vc animated:YES];
  384. // JXSelFriendVC* vc = [JXSelFriendVC alloc];
  385. // vc.chatRoom = _chatRoom;
  386. // vc.room = _room;
  387. // vc.isNewRoom = YES;
  388. // vc = [vc init];
  389. //// [g_window addSubview:vc.view];
  390. // [g_navigation pushViewController:vc animated:YES];
  391. [g_notify postNotificationName:kUpdateUserNotifaction object:nil];
  392. [self actionQuit];
  393. // _pSelf = nil;
  394. }
  395. }
  396. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  397. [_wait stop];
  398. return show_error;
  399. }
  400. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  401. [_wait stop];
  402. return show_error;
  403. }
  404. -(void) didServerConnectStart:(JXConnection*)aDownload{
  405. [_wait start];
  406. }
  407. -(void)insertRoom{
  408. JXUserObject* user = [[JXUserObject alloc]init];
  409. user.userNickname = _room.name;
  410. user.userId = _room.roomJid;
  411. user.userDescription = _room.desc;
  412. user.roomId = _room.roomId;
  413. user.content = Localized(@"JX_WelcomeGroupChat");
  414. user.showRead = [NSNumber numberWithBool:_room.showRead];
  415. user.showMember = [NSNumber numberWithBool:_room.showMember];
  416. user.allowSendCard = [NSNumber numberWithBool:_room.allowSendCard];
  417. user.allowInviteFriend = [NSNumber numberWithBool:_room.allowInviteFriend];
  418. user.allowUploadFile = [NSNumber numberWithBool:_room.allowUploadFile];
  419. user.allowSpeakCourse = [NSNumber numberWithBool:_room.allowSpeakCourse];
  420. user.isNeedVerify = [NSNumber numberWithBool:_room.isNeedVerify];
  421. user.createUserId = [NSString stringWithFormat:@"%ld",_room.userId];
  422. if (self.isAddressBook) {
  423. user.category = [NSNumber numberWithInteger:510];
  424. }
  425. #ifdef IS_MsgEncrypt
  426. user.chatKeyGroup = [g_msgUtil encryptRoomMsgKey:_room.roomId randomKey:_room.chatKeyGroup];
  427. #endif
  428. [user insertRoom];
  429. }
  430. @end