WFCUCreateChannelViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "WFCUCreateChannelViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUUtilities.h"
  11. #import "MBProgressHUD.h"
  12. #import <SDWebImage/SDWebImage.h>
  13. #import "UIView+Toast.h"
  14. #import "WFCUConfigManager.h"
  15. #import "WFCUImage.h"
  16. @interface WFCUCreateChannelViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
  17. @property(nonatomic, strong)UIImageView *portraitView;
  18. @property(nonatomic, strong)UILabel *nameLabel;
  19. @property(nonatomic, strong)UITextField *nameField;
  20. @property(nonatomic, strong)UILabel *descLabel;
  21. @property(nonatomic, strong)UITextField *descField;
  22. @property(nonatomic, strong)NSString *portraitUrl;
  23. @end
  24. #define PortraitWidth 80
  25. @implementation WFCUCreateChannelViewController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  29. CGRect bound = self.view.bounds;
  30. CGFloat portraitWidth = PortraitWidth;
  31. CGFloat top = 100;
  32. self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake((bound.size.width - portraitWidth)/2, top, portraitWidth, portraitWidth)];
  33. self.portraitView.image = [WFCUImage imageNamed:@"channel_default_portrait"];
  34. self.portraitView.userInteractionEnabled = YES;
  35. self.portraitView.layer.borderWidth = 0.5;
  36. self.portraitView.layer.borderColor = [UIColor colorWithRed:0.1 green:0.27 blue:0.9 alpha:0.9].CGColor;
  37. self.portraitView.layer.cornerRadius = 3;
  38. self.portraitView.layer.masksToBounds = YES;
  39. UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectPortrait:)];
  40. [self.portraitView addGestureRecognizer:tap];
  41. [self.view addSubview:self.portraitView];
  42. CGFloat namePadding = 36;
  43. CGFloat labelWidth = 72;
  44. CGFloat labelFeildPadding = 4;
  45. top += portraitWidth;
  46. top += 40;
  47. self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(namePadding, top, labelWidth, 25)];
  48. self.nameLabel.text = WFCString(@"ChannelName");
  49. self.nameLabel.font = [UIFont systemFontOfSize:14];
  50. [self.view addSubview:self.nameLabel];
  51. self.nameField = [[UITextField alloc] initWithFrame:CGRectMake(namePadding + labelWidth + labelFeildPadding, top, bound.size.width - namePadding - labelWidth - labelFeildPadding - namePadding, 24)];
  52. [self.nameField setPlaceholder:WFCString(@"InputChannelNameHint")];
  53. [self.nameField setFont:[UIFont systemFontOfSize:14]];
  54. self.nameField.textAlignment = NSTextAlignmentLeft;
  55. [self.view addSubview:self.nameField];
  56. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(namePadding + labelWidth + labelFeildPadding, top + 24, bound.size.width - namePadding - labelWidth - labelFeildPadding - namePadding, 1)];
  57. [line setBackgroundColor:[UIColor grayColor]];
  58. [self.view addSubview:line];
  59. top += 25;
  60. top += 16;
  61. self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(namePadding, top, labelWidth, 25)];
  62. self.descLabel.text = WFCString(@"ChannelDescription");
  63. self.descLabel.font = [UIFont systemFontOfSize:14];
  64. [self.view addSubview:self.descLabel];
  65. self.descField = [[UITextField alloc] initWithFrame:CGRectMake(namePadding + labelWidth + labelFeildPadding, top, bound.size.width - namePadding - labelWidth - labelFeildPadding - namePadding, 24)];
  66. [self.descField setPlaceholder:WFCString(@"InputChannelDescHint")];
  67. [self.descField setFont:[UIFont systemFontOfSize:14]];
  68. self.descField.textAlignment = NSTextAlignmentLeft;
  69. [self.view addSubview:self.descField];
  70. line = [[UIView alloc] initWithFrame:CGRectMake(namePadding + labelWidth + labelFeildPadding, top + 24, bound.size.width - namePadding - labelWidth - labelFeildPadding - namePadding, 1)];
  71. [line setBackgroundColor:[UIColor grayColor]];
  72. [self.view addSubview:line];
  73. top += 25;
  74. top += 16;
  75. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Create") style:UIBarButtonItemStyleDone target:self action:@selector(onDone:)];
  76. [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onResetKeyBoard:)]];
  77. }
  78. - (void)onResetKeyBoard:(id)sender {
  79. [self.nameField resignFirstResponder];
  80. [self.descField resignFirstResponder];
  81. }
  82. - (void)onSelectPortrait:(id)sender {
  83. __weak typeof(self)ws = self;
  84. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:WFCString(@"ChangePortrait") message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  85. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:WFCString(@"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  86. }];
  87. UIAlertAction *actionCamera = [UIAlertAction actionWithTitle:WFCString(@"TakePhotos") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  88. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  89. picker.allowsEditing = YES;
  90. picker.delegate = ws;
  91. if ([UIImagePickerController
  92. isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  93. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  94. } else {
  95. NSLog(@"无法连接相机");
  96. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  97. }
  98. [ws presentViewController:picker animated:YES completion:nil];
  99. }];
  100. UIAlertAction *actionAlubum = [UIAlertAction actionWithTitle:WFCString(@"Album") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  101. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  102. picker.allowsEditing = YES;
  103. picker.delegate = ws;
  104. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  105. [ws presentViewController:picker animated:YES completion:nil];
  106. }];
  107. //把action添加到actionSheet里
  108. [actionSheet addAction:actionCamera];
  109. [actionSheet addAction:actionAlubum];
  110. [actionSheet addAction:actionCancel];
  111. //相当于之前的[actionSheet show];
  112. [self presentViewController:actionSheet animated:YES completion:nil];
  113. }
  114. #pragma mark - UIImagePickerControllerDelegate
  115. - (void)imagePickerController:(UIImagePickerController *)picker
  116. didFinishPickingMediaWithInfo:(NSDictionary *)info {
  117. [UIApplication sharedApplication].statusBarHidden = NO;
  118. NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  119. if ([mediaType isEqual:@"public.image"]) {
  120. UIImage *originImage =
  121. [info objectForKey:UIImagePickerControllerEditedImage];
  122. //获取截取区域的图像
  123. UIImage *captureImage = [WFCUUtilities thumbnailWithImage:originImage maxSize:CGSizeMake(60, 60)];
  124. self.portraitView.image = captureImage;
  125. self.portraitView.hidden = NO;
  126. }
  127. [picker dismissViewControllerAnimated:YES completion:nil];
  128. }
  129. - (void)uploadPortrait:(UIImage *)portraitImage {
  130. NSData *portraitData = UIImageJPEGRepresentation(portraitImage, 0.70);
  131. __weak typeof(self) ws = self;
  132. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  133. hud.label.text = WFCString(@"PhotoUploading");
  134. [hud showAnimated:YES];
  135. [[WFCCIMService sharedWFCIMService] uploadMedia:nil mediaData:portraitData mediaType:Media_Type_PORTRAIT success:^(NSString *remoteUrl) {
  136. dispatch_async(dispatch_get_main_queue(), ^{
  137. [hud hideAnimated:NO];
  138. ws.portraitUrl = remoteUrl;
  139. NSString *name = ws.nameField.text;
  140. [ws createChannel:ws.portraitUrl];
  141. });
  142. }
  143. progress:^(long uploaded, long total) {
  144. }
  145. error:^(int error_code) {
  146. dispatch_async(dispatch_get_main_queue(), ^{
  147. [hud hideAnimated:NO];
  148. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  149. hud.mode = MBProgressHUDModeText;
  150. hud.label.text = WFCString(@"UploadFailure");
  151. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  152. [hud hideAnimated:YES afterDelay:1.f];
  153. });
  154. }];
  155. }
  156. - (void)didReceiveMemoryWarning {
  157. [super didReceiveMemoryWarning];
  158. // Dispose of any resources that can be recreated.
  159. }
  160. - (UIImage *)getPortraitImage {
  161. return self.portraitView.image;
  162. }
  163. - (void)onDone:(id)sender {
  164. [self uploadPortrait:[self getPortraitImage]];
  165. }
  166. - (void)createChannel:(NSString *)portraitUrl {
  167. __weak typeof(self) ws = self;
  168. [[WFCCIMService sharedWFCIMService] createChannel:self.nameField.text portrait:portraitUrl desc:self.descField.text extra:nil success:^(WFCCChannelInfo *channelInfo) {
  169. NSLog(@"create channel done");
  170. WFCCTipNotificationContent *tip = [[WFCCTipNotificationContent alloc] init];
  171. tip.tip = WFCString(@"ChannelCreated");
  172. [[WFCCIMService sharedWFCIMService] send:[WFCCConversation conversationWithType:Channel_Type target:channelInfo.channelId line:0] content:tip success:^(long long messageUid, long long timestamp) {
  173. NSLog(@"send channel msg done");
  174. } error:^(int error_code) {
  175. NSLog(@"send channel msg failure");
  176. }];
  177. [ws.navigationController popViewControllerAnimated:YES];
  178. } error:^(int error_code) {
  179. NSLog(@"create channel error%d", error_code);
  180. [ws.view makeToast:WFCString(@"ChannelCreateFailure")
  181. duration:2
  182. position:CSToastPositionCenter];
  183. }];
  184. }
  185. - (void)modifyGroup:(NSString *)groupId portrait:(NSString *)portraitUrl {
  186. __weak typeof(self) ws = self;
  187. [[WFCCIMService sharedWFCIMService] modifyGroupInfo:groupId type:Modify_Group_Portrait newValue:portraitUrl notifyLines:@[@(0)] notifyContent:nil success:^{
  188. dispatch_async(dispatch_get_main_queue(), ^{
  189. [ws.navigationController popViewControllerAnimated:YES];
  190. });
  191. } error:^(int error_code) {
  192. }];
  193. }
  194. @end