2
0

WFCUCreateGroupViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // CreateGroupViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/14.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUCreateGroupViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUUtilities.h"
  11. #import "MBProgressHUD.h"
  12. #import "SDWebImage.h"
  13. #import "UIView+Toast.h"
  14. @interface WFCUCreateGroupViewController () <UIImagePickerControllerDelegate, UIActionSheetDelegate, UINavigationControllerDelegate>
  15. @property(nonatomic, strong)UIImageView *portraitView;
  16. @property(nonatomic, strong)UITextField *nameField;
  17. @property(nonatomic, strong)NSString *portraitUrl;
  18. @property(nonatomic, strong)UIButton *resetBtn;
  19. @property(nonatomic, strong)UIView *combineHeadView;
  20. @end
  21. #define PortraitWidth 120
  22. @implementation WFCUCreateGroupViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. [self.view setBackgroundColor:[UIColor whiteColor]];
  26. CGRect bound = self.view.bounds;
  27. CGFloat portraitWidth = PortraitWidth;
  28. self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake((bound.size.width - portraitWidth)/2, 100, portraitWidth, portraitWidth)];
  29. self.portraitView.image = [UIImage imageNamed:@"group_default_portrait"];
  30. self.portraitView.userInteractionEnabled = YES;
  31. UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectPortrait:)];
  32. [self.portraitView addGestureRecognizer:tap];
  33. self.combineHeadView = [[UIView alloc] initWithFrame:CGRectMake((bound.size.width - portraitWidth)/2, 100, portraitWidth, portraitWidth)];
  34. [self.combineHeadView addGestureRecognizer:tap];
  35. [self.combineHeadView setBackgroundColor:[UIColor grayColor]];
  36. self.portraitView.hidden = YES;
  37. [self.view addSubview:self.combineHeadView];
  38. [self.view addSubview:self.portraitView];
  39. if(self.isModifyPortrait) {
  40. CGFloat btnWidth = 60;
  41. self.resetBtn = [[UIButton alloc] initWithFrame:CGRectMake((bound.size.width - btnWidth)/2, 80 + portraitWidth + 60, btnWidth, 40)];
  42. [self.resetBtn setTitle:@"重置" forState:UIControlStateNormal];
  43. [self.resetBtn addTarget:self action:@selector(onResetPortrait:) forControlEvents:UIControlEventTouchUpInside];
  44. [self.resetBtn setBackgroundColor:[UIColor greenColor]];
  45. self.resetBtn.layer.cornerRadius = 4.f;
  46. self.resetBtn.layer.masksToBounds = YES;
  47. [self.view addSubview:self.resetBtn];
  48. } else {
  49. CGFloat namePadding = 60;
  50. self.nameField = [[UITextField alloc] initWithFrame:CGRectMake(namePadding, 80 + portraitWidth + 60, bound.size.width - namePadding - namePadding, 24)];
  51. [self.nameField setPlaceholder:@"请输入群名称(可选)"];
  52. [self.nameField setFont:[UIFont systemFontOfSize:21]];
  53. self.nameField.textAlignment = NSTextAlignmentCenter;
  54. [self.view addSubview:self.nameField];
  55. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(namePadding, 80 + portraitWidth + 60 + 24, bound.size.width - namePadding - namePadding, 2)];
  56. [line setBackgroundColor:[UIColor grayColor]];
  57. [self.view addSubview:line];
  58. }
  59. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(onDone:)];
  60. [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onResetKeyBoard:)]];
  61. if (!_isModifyPortrait) {
  62. [self loadCombineView];
  63. } else {
  64. UIImageView *portraitView = [[UIImageView alloc] initWithFrame:self.combineHeadView.bounds];
  65. WFCCGroupInfo *groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:self.groupId refresh:NO];
  66. [portraitView sd_setImageWithURL:[NSURL URLWithString:groupInfo.portrait] placeholderImage:[UIImage imageNamed:@"PersonalChat"]];
  67. [self.combineHeadView addSubview:portraitView];
  68. }
  69. }
  70. - (void)loadCombineView {
  71. NSMutableArray *users = [[NSMutableArray alloc] init];
  72. if (![self.memberIds containsObject:[WFCCNetworkService sharedInstance].userId]) {
  73. WFCCUserInfo *user = [[WFCCIMService sharedWFCIMService] getUserInfo:[WFCCNetworkService sharedInstance].userId refresh:NO];
  74. [users addObject:user];
  75. }
  76. for (UIView *subView in self.combineHeadView.subviews) {
  77. [subView removeFromSuperview];
  78. }
  79. for (NSString *userId in self.memberIds) {
  80. WFCCUserInfo *user = [[WFCCIMService sharedWFCIMService] getUserInfo:userId refresh:NO];
  81. if (user != nil) {
  82. [users addObject:user];
  83. }
  84. if (users.count >= 9) {
  85. break;
  86. }
  87. }
  88. CGFloat padding = 5;
  89. int numPerRow = 3;
  90. if (users.count <= 4) {
  91. numPerRow = 2;
  92. }
  93. int row = (int)(users.count - 1) / numPerRow + 1;
  94. int column = numPerRow;
  95. int firstCol = (int)(users.count - (row - 1)*column);
  96. CGFloat width = (PortraitWidth - padding) / numPerRow - padding;
  97. CGFloat Y = (PortraitWidth - (row * (width + padding) + padding))/2;
  98. for (int i = 0; i < row; i++) {
  99. int c = column;
  100. if (i == 0) {
  101. c = firstCol;
  102. }
  103. CGFloat X = (PortraitWidth - (c * (width + padding) + padding))/2;
  104. for (int j = 0; j < c; j++) {
  105. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(X + j *(width + padding) + padding, Y + i * (width + padding) + padding, width, width)];
  106. int index;
  107. if (i == 0) {
  108. index = j;
  109. } else {
  110. index = j + (i-1)*column + firstCol;
  111. }
  112. WFCCUserInfo *user = [users objectAtIndex:index];
  113. [imageView sd_setImageWithURL:[NSURL URLWithString:user.portrait] placeholderImage:[UIImage imageNamed:@"PersonalChat"]];
  114. [self.combineHeadView addSubview:imageView];
  115. }
  116. }
  117. }
  118. - (void)onResetKeyBoard:(id)sender {
  119. [self.nameField resignFirstResponder];
  120. }
  121. - (void)onSelectPortrait:(id)sender {
  122. UIActionSheet *actionSheet =
  123. [[UIActionSheet alloc] initWithTitle:@"修改头像"
  124. delegate:self
  125. cancelButtonTitle:@"取消"
  126. destructiveButtonTitle:@"拍照"
  127. otherButtonTitles:@"相册", nil];
  128. [actionSheet showInView:self.view];
  129. }
  130. #pragma mark - UIActionSheetDelegate <NSObject>
  131. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  132. if(buttonIndex == 0) {
  133. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  134. picker.allowsEditing = YES;
  135. picker.delegate = self;
  136. if ([UIImagePickerController
  137. isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  138. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  139. } else {
  140. NSLog(@"无法连接相机");
  141. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  142. }
  143. [self presentViewController:picker animated:YES completion:nil];
  144. } else if (buttonIndex == 1) {
  145. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  146. picker.allowsEditing = YES;
  147. picker.delegate = self;
  148. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  149. [self presentViewController:picker animated:YES completion:nil];
  150. }
  151. }
  152. #pragma mark - UIImagePickerControllerDelegate
  153. - (void)imagePickerController:(UIImagePickerController *)picker
  154. didFinishPickingMediaWithInfo:(NSDictionary *)info {
  155. [UIApplication sharedApplication].statusBarHidden = NO;
  156. NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  157. if ([mediaType isEqual:@"public.image"]) {
  158. UIImage *originImage =
  159. [info objectForKey:UIImagePickerControllerEditedImage];
  160. //获取截取区域的图像
  161. UIImage *captureImage = [WFCUUtilities thumbnailWithImage:originImage maxSize:CGSizeMake(60, 60)];
  162. self.portraitView.image = captureImage;
  163. self.portraitView.hidden = NO;
  164. self.combineHeadView.hidden = YES;
  165. }
  166. [picker dismissViewControllerAnimated:YES completion:nil];
  167. }
  168. - (void)uploadPortrait:(UIImage *)portraitImage createGroup:(BOOL)createGroup {
  169. NSData *portraitData = UIImageJPEGRepresentation(portraitImage, 0.70);
  170. __weak typeof(self) ws = self;
  171. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  172. hud.label.text = @"图片上传中...";
  173. [hud showAnimated:YES];
  174. [[WFCCIMService sharedWFCIMService] uploadMedia:portraitData mediaType:Media_Type_PORTRAIT success:^(NSString *remoteUrl) {
  175. dispatch_async(dispatch_get_main_queue(), ^{
  176. [hud hideAnimated:NO];
  177. if (createGroup) {
  178. ws.portraitUrl = remoteUrl;
  179. NSString *name = ws.nameField.text;
  180. if (name.length == 0) {
  181. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[self.memberIds objectAtIndex:0] refresh:NO];
  182. if (userInfo.displayName.length > 0) {
  183. name = [name stringByAppendingString:userInfo.displayName];
  184. }
  185. for (int i = 1; i < 8 && i < self.memberIds.count; i++) {
  186. userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[self.memberIds objectAtIndex:i] refresh:NO];
  187. if (userInfo.displayName.length > 0) {
  188. if (name.length + userInfo.displayName.length + 1 > 16) {
  189. name = [name stringByAppendingString:@"等"];
  190. break;
  191. }
  192. name = [name stringByAppendingFormat:@",%@", userInfo.displayName];
  193. }
  194. }
  195. if (name.length == 0) {
  196. name = @"群聊";
  197. }
  198. }
  199. [ws createGroup:name portrait:ws.portraitUrl members:ws.memberIds];
  200. } else {
  201. [ws modifyGroup:ws.groupId portrait:remoteUrl];
  202. }
  203. });
  204. }
  205. progress:^(long uploaded, long total) {
  206. }
  207. error:^(int error_code) {
  208. dispatch_async(dispatch_get_main_queue(), ^{
  209. [hud hideAnimated:NO];
  210. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  211. hud.mode = MBProgressHUDModeText;
  212. hud.label.text = @"上传失败";
  213. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  214. [hud hideAnimated:YES afterDelay:1.f];
  215. });
  216. }];
  217. }
  218. - (void)didReceiveMemoryWarning {
  219. [super didReceiveMemoryWarning];
  220. // Dispose of any resources that can be recreated.
  221. }
  222. - (UIImage *)getPortraitImage {
  223. if (self.portraitView.hidden == NO) {
  224. return self.portraitView.image;
  225. } else {
  226. UIGraphicsBeginImageContextWithOptions(self.combineHeadView.frame.size, NO, 2.0);
  227. [self.combineHeadView.layer renderInContext:UIGraphicsGetCurrentContext()];
  228. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  229. UIGraphicsEndImageContext();
  230. return image;
  231. }
  232. }
  233. - (void)onDone:(id)sender {
  234. if (self.isModifyPortrait) {
  235. [self uploadPortrait:[self getPortraitImage] createGroup:NO];
  236. } else {
  237. [self uploadPortrait:[self getPortraitImage] createGroup:YES];
  238. }
  239. }
  240. - (void)onResetPortrait:(id)sender {
  241. [self loadCombineView];
  242. }
  243. - (void)createGroup:(NSString *)groupName portrait:(NSString *)portraitUrl members:(NSArray<NSString *> *)memberIds {
  244. __weak typeof(self) ws = self;
  245. [[WFCCIMService sharedWFCIMService] createGroup:nil name:groupName portrait:portraitUrl members:memberIds notifyLines:@[@(0)] notifyContent:nil success:^(NSString *groupId) {
  246. NSLog(@"create group success");
  247. } error:^(int error_code) {
  248. NSLog(@"create group failure");
  249. [ws.view makeToast:@"创建群组失败"
  250. duration:2
  251. position:CSToastPositionCenter];
  252. }];
  253. [self.navigationController popViewControllerAnimated:YES];
  254. }
  255. - (void)modifyGroup:(NSString *)groupId portrait:(NSString *)portraitUrl {
  256. __weak typeof(self) ws = self;
  257. [[WFCCIMService sharedWFCIMService] modifyGroupInfo:groupId type:Modify_Group_Portrait newValue:portraitUrl notifyLines:@[@(0)] notifyContent:nil success:^{
  258. dispatch_async(dispatch_get_main_queue(), ^{
  259. [ws.navigationController popViewControllerAnimated:YES];
  260. });
  261. } error:^(int error_code) {
  262. }];
  263. }
  264. @end