JXInviteAddressBookVC.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //
  2. // JXInviteAddressBookVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2019/3/30.
  6. // Copyright © 2019年 Reese. All rights reserved.
  7. //
  8. #import "JXInviteAddressBookVC.h"
  9. #import "QCheckBox.h"
  10. #import "BMChineseSort.h"
  11. #import "JXAddressBookCell.h"
  12. #import <MessageUI/MessageUI.h>
  13. @interface JXInviteAddressBookVC ()<JXAddressBookCellDelegate, QCheckBoxDelegate,MFMessageComposeViewControllerDelegate>
  14. @property(nonatomic,strong)NSMutableArray *array;
  15. //排序后的出现过的拼音首字母数组
  16. @property(nonatomic,strong)NSMutableArray *indexArray;
  17. //排序好的结果数组
  18. @property(nonatomic,strong)NSMutableArray *letterResultArr;
  19. @property (nonatomic, strong)NSMutableArray *abUreadArr;
  20. @property (nonatomic, assign) BOOL isShowSelect;
  21. @property (nonatomic, assign) BOOL isAllSelect; // YES:全选 // NO:全不选
  22. @property (nonatomic, strong) NSMutableArray *selectABs;
  23. @property (nonatomic, strong) UIView *doneBtn;
  24. @property (nonatomic, strong) UIButton *selectBtn;
  25. @property (nonatomic, strong) NSDictionary *phoneNumDict;
  26. @property (nonatomic, strong) NSMutableArray *headerCheckBoxs;
  27. @property (nonatomic, strong) NSMutableArray *allAbArr;
  28. @property (nonatomic, strong) JXUserObject *addressBookUser;
  29. @end
  30. @implementation JXInviteAddressBookVC
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. // Do any additional setup after loading the view.
  34. self.heightHeader = JX_SCREEN_TOP;
  35. self.heightFooter = 0;
  36. self.isGotoBack = YES;
  37. //self.view.frame = g_window.bounds;
  38. self.isShowFooterPull = NO;
  39. [self createHeadAndFoot];
  40. _phoneNumDict = [[JXAddressBook sharedInstance] getMyAddressBook];
  41. _headerCheckBoxs = [NSMutableArray array];
  42. self.title = Localized(@"JX_MobilePhoneContacts");
  43. _array = [NSMutableArray array];
  44. _indexArray = [NSMutableArray array];
  45. _letterResultArr = [NSMutableArray array];
  46. _selectABs = [NSMutableArray array];
  47. _allAbArr = [NSMutableArray array];
  48. self.isShowSelect = NO;
  49. _selectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  50. [_selectBtn setTitle:Localized(@"JX_BatchInvite") forState:UIControlStateNormal];
  51. [_selectBtn setTitleColor:THESIMPLESTYLE ? [UIColor blackColor] : [UIColor whiteColor] forState:UIControlStateNormal];
  52. _selectBtn.tintColor = [UIColor clearColor];
  53. _selectBtn.frame = CGRectMake(JX_SCREEN_WIDTH - 62-15, JX_SCREEN_TOP - 30, 62, 15);
  54. _selectBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  55. [_selectBtn addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  56. [self.tableHeader addSubview:_selectBtn];
  57. _doneBtn = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - JX_SCREEN_BOTTOM, JX_SCREEN_WIDTH, JX_SCREEN_BOTTOM)];
  58. _doneBtn.backgroundColor = HEXCOLOR(0xf0f0f0);
  59. _doneBtn.hidden = YES;
  60. [self.view addSubview:_doneBtn];
  61. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, JX_SCREEN_BOTTOM/2-49/2, JX_SCREEN_WIDTH, 49)];
  62. [btn setTitle:Localized(@"JX_Confirm") forState:UIControlStateNormal];
  63. [btn setTitleColor:HEXCOLOR(0x55BEB8) forState:UIControlStateNormal];
  64. [btn addTarget:self action:@selector(doneBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  65. btn.backgroundColor = HEXCOLOR(0xf0f0f0);
  66. [_doneBtn addSubview:btn];
  67. [self getServerData];
  68. [self createTableHeadView];
  69. }
  70. - (void)createTableHeadView {
  71. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 65)];
  72. [btn addTarget:self action:@selector(shareFriend) forControlEvents:UIControlEventTouchUpInside];
  73. self.tableView.tableHeaderView = btn;
  74. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(23, 23, 20, 20)];
  75. imageView.image = [UIImage imageNamed:@"ic_cs"];
  76. [btn addSubview:imageView];
  77. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10, 0, 200, btn.frame.size.height)];
  78. label.text = [NSString stringWithFormat:@"%@%@",Localized(@"JX_small_share"),APP_NAME];
  79. [btn addSubview:label];
  80. }
  81. - (void)shareFriend {
  82. NSString *testToShare = APP_NAME;
  83. // UIImage *imageToShare = [UIImage imageNamed:@"client"];
  84. NSURL *urlToShare = [NSURL URLWithString:g_config.website];
  85. NSArray *activityItems = @[testToShare,urlToShare];
  86. UIActivityViewController *activityVc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
  87. [self presentViewController:activityVc animated:YES completion:nil];
  88. activityVc.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
  89. if (completed) {
  90. NSLog(@"分享成功");
  91. }else{
  92. NSLog(@"分享取消");
  93. }
  94. };
  95. }
  96. - (void)didSelectedCheckBox:(QCheckBox *)checkbox checked:(BOOL)checked{
  97. [self addressBookCell:nil checkBoxSelectIndexNum:checkbox.tag isSelect:checked];
  98. }
  99. - (void)selectBtnAction:(UIButton *)btn {
  100. self.isShowSelect = YES;
  101. if (self.doneBtn.hidden == YES) {
  102. self.doneBtn.hidden = NO;
  103. self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.frame.size.height - JX_SCREEN_BOTTOM);
  104. [self.gotoBackBtn setBackgroundImage:nil forState:UIControlStateNormal];
  105. [self.gotoBackBtn setTitle:Localized(@"JX_Cencal") forState:UIControlStateNormal];
  106. [btn setTitle:Localized(@"JX_CheckAll") forState:UIControlStateNormal];
  107. btn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
  108. }else {
  109. self.isAllSelect = !self.isAllSelect;
  110. if (self.isAllSelect) {
  111. for (int i = 0 ; i < self.letterResultArr.count; i++) {
  112. NSArray *arr = [self.letterResultArr objectAtIndex:i];
  113. if (arr.count > 1) {
  114. for (JXAddressBook *ad in arr) {
  115. [_selectABs addObject:ad];
  116. }
  117. }else {
  118. [_selectABs addObject:[arr firstObject]];
  119. }
  120. }
  121. }else {
  122. [_selectABs removeAllObjects];
  123. }
  124. }
  125. [self.tableView reloadData];
  126. }
  127. - (void)actionQuit {
  128. if (self.isShowSelect) {
  129. self.isShowSelect = NO;
  130. self.isAllSelect = NO;
  131. [_selectABs removeAllObjects];
  132. _selectBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  133. [_selectBtn setTitle:Localized(@"JX_BatchInvite") forState:UIControlStateNormal];
  134. [self.gotoBackBtn setTitle:nil forState:UIControlStateNormal];
  135. [self.gotoBackBtn setBackgroundImage:[UIImage imageNamed:@"title_back_black_big"] forState:UIControlStateNormal];
  136. [_selectABs removeAllObjects];
  137. self.doneBtn.hidden = YES;
  138. self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self_height-self.heightHeader-self.heightFooter);
  139. [self.tableView reloadData];
  140. return;
  141. }
  142. [super actionQuit];
  143. }
  144. - (void)doneBtnAction:(UIButton *)btn {
  145. if (_selectABs.count <= 0) {
  146. [g_App showAlert:Localized(@"JX_PleaseSelectContacts")];
  147. return;
  148. }
  149. NSMutableArray *arr = [NSMutableArray array];
  150. for (NSInteger i = 0; i < _selectABs.count; i ++) {
  151. JXAddressBook *ab = _selectABs[i];
  152. [arr addObject:ab.toTelephone];
  153. }
  154. MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc]init];
  155. //设置短信内容
  156. vc.body = [NSString stringWithFormat:@"嗨,我正在使用%@。快来和我一起试试吧~ 下载地址:\n%@",APP_NAME,g_config.website];
  157. //设置收件人列表
  158. vc.recipients = arr;
  159. //设置代理
  160. vc.messageComposeDelegate = self;
  161. //显示控制器
  162. [self presentViewController:vc animated:YES completion:nil];
  163. }
  164. - (void)getServerData {
  165. [_array removeAllObjects];
  166. [_allAbArr removeAllObjects];
  167. NSMutableArray *arr = [[JXAddressBook sharedInstance] fetchAllAddressBook];
  168. for (NSString *key in _phoneNumDict.allKeys) {
  169. BOOL flag = NO;
  170. for (JXAddressBook *obj in arr) {
  171. if ([obj.toTelephone isEqualToString:key]) {
  172. flag = YES;
  173. break;
  174. }
  175. }
  176. if (!flag) {
  177. JXAddressBook *ab = [[JXAddressBook alloc] init];
  178. ab.toTelephone = key;
  179. ab.addressBookName = _phoneNumDict[key];
  180. [_array addObject:ab];
  181. }
  182. }
  183. //选择拼音 转换的 方法
  184. BMChineseSortSetting.share.sortMode = 2; // 1或2
  185. //排序 Person对象
  186. [BMChineseSort sortAndGroup:_array key:@"addressBookName" finish:^(bool isSuccess, NSMutableArray *unGroupArr, NSMutableArray *sectionTitleArr, NSMutableArray<NSMutableArray *> *sortedObjArr) {
  187. if (isSuccess) {
  188. self.indexArray = sectionTitleArr;
  189. self.letterResultArr = sortedObjArr;
  190. [_table reloadData];
  191. }
  192. }];
  193. // //根据Person对象的 name 属性 按中文 对 Person数组 排序
  194. // self.indexArray = [BMChineseSort IndexWithArray:_array Key:@"addressBookName"];
  195. // self.letterResultArr = [BMChineseSort sortObjectArray:_array Key:@"addressBookName"];
  196. }
  197. #pragma mark ---------tableView协议----------------
  198. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  199. {
  200. return [self.indexArray count];
  201. }
  202. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  203. return [self.indexArray objectAtIndex:section];
  204. }
  205. -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
  206. UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
  207. header.tintColor = HEXCOLOR(0xF2F2F2);
  208. [header.textLabel setTextColor:HEXCOLOR(0x999999)];
  209. [header.textLabel setFont:SYSFONT(15)];
  210. }
  211. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  212. {
  213. return [(NSArray *)[self.letterResultArr objectAtIndex:section] count];
  214. }
  215. -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
  216. return self.indexArray;
  217. }
  218. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
  219. return index;
  220. }
  221. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  222. {
  223. NSString *identifier = @"JXAddressBookCell";
  224. JXAddressBookCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  225. JXAddressBook *addressBook = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  226. if (!cell) {
  227. cell = [[JXAddressBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  228. }
  229. cell.isInvite = YES;
  230. cell.delegate = self;
  231. cell.index = indexPath.section * 1000 + indexPath.row;
  232. if (self.isAllSelect) {
  233. cell.isShowSelect = YES;
  234. cell.checkBox.selected = YES;
  235. }else {
  236. cell.isShowSelect = self.isShowSelect;
  237. if ([_selectABs containsObject:addressBook]) {
  238. cell.checkBox.selected = YES;
  239. }else {
  240. cell.checkBox.selected = NO;
  241. }
  242. }
  243. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  244. cell.addressBook = addressBook;
  245. cell.headImage.userInteractionEnabled = NO;
  246. if (indexPath.row == [(NSArray *)[self.letterResultArr objectAtIndex:indexPath.section] count]-1) {
  247. cell.lineView.frame = CGRectMake(cell.lineView.frame.origin.x, cell.lineView.frame.origin.y, cell.lineView.frame.size.width,0);
  248. }else {
  249. cell.lineView.frame = CGRectMake(cell.lineView.frame.origin.x, cell.lineView.frame.origin.y, cell.lineView.frame.size.width,LINE_WH);
  250. }
  251. return cell;
  252. }
  253. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  254. return 64;
  255. }
  256. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  257. {
  258. JXAddressBookCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  259. if (self.isShowSelect) {
  260. JXUserObject *user = [[JXUserObject sharedInstance] getUserById:cell.addressBook.toUserId];
  261. if ([user.status intValue] != 2) {
  262. cell.checkBox.selected = !cell.checkBox.selected;
  263. [self addressBookCell:cell checkBoxSelectIndexNum:cell.index isSelect:cell.checkBox.selected];
  264. }
  265. }else {
  266. // JXUserInfoVC* vc = [JXUserInfoVC alloc];
  267. // vc.userId = cell.addressBook.toUserId;
  268. // vc = [vc init];
  269. // [g_navigation pushViewController:vc animated:YES];
  270. }
  271. }
  272. - (void)addressBookCell:(JXAddressBookCell *)abCell checkBoxSelectIndexNum:(NSInteger)indexNum isSelect:(BOOL)isSelect {
  273. JXAddressBook *ab;
  274. if (abCell) {
  275. ab = [[self.letterResultArr objectAtIndex:abCell.index / 1000] objectAtIndex:abCell.index % 1000];
  276. }else {
  277. ab = self.abUreadArr[indexNum];
  278. }
  279. if (isSelect) {
  280. [_selectABs addObject:ab];
  281. }else {
  282. [_selectABs removeObject:ab];
  283. }
  284. }
  285. - (void)addressBookCell:(JXAddressBookCell *)abCell addBtnAction:(JXAddressBook *)addressBook {
  286. MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc]init];
  287. //设置短信内容
  288. vc.body = [NSString stringWithFormat:@"嗨,我正在使用%@。快来和我一起试试吧~ 下载地址:\n%@",APP_NAME,g_config.website];
  289. //设置收件人列表
  290. vc.recipients = @[addressBook.toTelephone];
  291. //设置代理
  292. vc.messageComposeDelegate = self;
  293. //显示控制器
  294. [self presentViewController:vc animated:YES completion:nil];
  295. }
  296. // 实现代理函数: 点击取消按钮会自动调用
  297. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
  298. [controller dismissViewControllerAnimated:YES completion:nil];
  299. }
  300. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  301. [_wait stop];
  302. if( [aDownload.action isEqualToString:act_FriendsAttentionBatchAdd] ){
  303. [g_notify postNotificationName:kFriendListRefresh object:nil];
  304. self.isShowSelect = NO;
  305. [_selectBtn setTitle:Localized(@"JX_BatchAddition") forState:UIControlStateNormal];
  306. [_selectABs removeAllObjects];
  307. self.doneBtn.hidden = YES;
  308. self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self_height-self.heightHeader-self.heightFooter);
  309. [self getServerData];
  310. // if (self.abUreadArr.count > 0) {
  311. // [self createHeaderView:self.abUreadArr];
  312. // }
  313. [g_App showAlert:Localized(@"JX_AddSuccess")];
  314. }
  315. }
  316. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  317. [_wait stop];
  318. return show_error;
  319. }
  320. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  321. [_wait stop];
  322. return show_error;
  323. }
  324. -(void) didServerConnectStart:(JXConnection*)aDownload{
  325. [_wait start];
  326. }
  327. /*
  328. #pragma mark - Navigation
  329. // In a storyboard-based application, you will often want to do a little preparation before navigation
  330. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  331. // Get the new view controller using [segue destinationViewController].
  332. // Pass the selected object to the new view controller.
  333. }
  334. */
  335. @end