WFCUCreateChannelViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.h"
  13. #import "UIView+Toast.h"
  14. @interface WFCUCreateChannelViewController () <UIImagePickerControllerDelegate, UIActionSheetDelegate, UINavigationControllerDelegate>
  15. @property(nonatomic, strong)UIImageView *portraitView;
  16. @property(nonatomic, strong)UILabel *nameLabel;
  17. @property(nonatomic, strong)UITextField *nameField;
  18. @property(nonatomic, strong)UILabel *descLabel;
  19. @property(nonatomic, strong)UITextField *descField;
  20. @property(nonatomic, strong)UILabel *openLabel;
  21. @property(nonatomic, strong)UISwitch *openSwitch;
  22. @property(nonatomic, strong)NSString *portraitUrl;
  23. @end
  24. #define PortraitWidth 80
  25. @implementation WFCUCreateChannelViewController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. [self.view setBackgroundColor:[UIColor whiteColor]];
  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 = [UIImage 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 = @"频道名称:";
  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:@"请输入频道名称"];
  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 = @"频道说明:";
  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:@"请输入频道描述"];
  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.openLabel = [[UILabel alloc] initWithFrame:CGRectMake(namePadding, top, labelWidth, 25)];
  76. self.openLabel.text = @"是否公开:";
  77. self.openLabel.font = [UIFont systemFontOfSize:14];
  78. [self.view addSubview:self.openLabel];
  79. self.openSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(namePadding + labelWidth + labelFeildPadding, top, 60, 24)];
  80. self.openSwitch.on = YES;
  81. [self.view addSubview:self.openSwitch];
  82. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"创建" style:UIBarButtonItemStyleDone target:self action:@selector(onDone:)];
  83. [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onResetKeyBoard:)]];
  84. }
  85. - (void)onResetKeyBoard:(id)sender {
  86. [self.nameField resignFirstResponder];
  87. [self.descField resignFirstResponder];
  88. }
  89. - (void)onSelectPortrait:(id)sender {
  90. UIActionSheet *actionSheet =
  91. [[UIActionSheet alloc] initWithTitle:@"修改头像"
  92. delegate:self
  93. cancelButtonTitle:@"取消"
  94. destructiveButtonTitle:@"拍照"
  95. otherButtonTitles:@"相册", nil];
  96. [actionSheet showInView:self.view];
  97. }
  98. #pragma mark - UIActionSheetDelegate <NSObject>
  99. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  100. if(buttonIndex == 0) {
  101. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  102. picker.allowsEditing = YES;
  103. picker.delegate = self;
  104. if ([UIImagePickerController
  105. isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  106. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  107. } else {
  108. NSLog(@"无法连接相机");
  109. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  110. }
  111. [self presentViewController:picker animated:YES completion:nil];
  112. } else if (buttonIndex == 1) {
  113. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  114. picker.allowsEditing = YES;
  115. picker.delegate = self;
  116. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  117. [self presentViewController:picker animated:YES completion:nil];
  118. }
  119. }
  120. #pragma mark - UIImagePickerControllerDelegate
  121. - (void)imagePickerController:(UIImagePickerController *)picker
  122. didFinishPickingMediaWithInfo:(NSDictionary *)info {
  123. [UIApplication sharedApplication].statusBarHidden = NO;
  124. NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  125. if ([mediaType isEqual:@"public.image"]) {
  126. UIImage *originImage =
  127. [info objectForKey:UIImagePickerControllerEditedImage];
  128. //获取截取区域的图像
  129. UIImage *captureImage = [WFCUUtilities thumbnailWithImage:originImage maxSize:CGSizeMake(60, 60)];
  130. self.portraitView.image = captureImage;
  131. self.portraitView.hidden = NO;
  132. }
  133. [picker dismissViewControllerAnimated:YES completion:nil];
  134. }
  135. - (void)uploadPortrait:(UIImage *)portraitImage {
  136. NSData *portraitData = UIImageJPEGRepresentation(portraitImage, 0.70);
  137. __weak typeof(self) ws = self;
  138. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  139. hud.label.text = @"图片上传中...";
  140. [hud showAnimated:YES];
  141. [[WFCCIMService sharedWFCIMService] uploadMedia:portraitData mediaType:Media_Type_PORTRAIT success:^(NSString *remoteUrl) {
  142. dispatch_async(dispatch_get_main_queue(), ^{
  143. [hud hideAnimated:NO];
  144. ws.portraitUrl = remoteUrl;
  145. NSString *name = ws.nameField.text;
  146. [ws createChannel:ws.portraitUrl];
  147. });
  148. }
  149. progress:^(long uploaded, long total) {
  150. }
  151. error:^(int error_code) {
  152. dispatch_async(dispatch_get_main_queue(), ^{
  153. [hud hideAnimated:NO];
  154. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  155. hud.mode = MBProgressHUDModeText;
  156. hud.label.text = @"上传失败";
  157. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  158. [hud hideAnimated:YES afterDelay:1.f];
  159. });
  160. }];
  161. }
  162. - (void)didReceiveMemoryWarning {
  163. [super didReceiveMemoryWarning];
  164. // Dispose of any resources that can be recreated.
  165. }
  166. - (UIImage *)getPortraitImage {
  167. return self.portraitView.image;
  168. }
  169. - (void)onDone:(id)sender {
  170. [self uploadPortrait:[self getPortraitImage]];
  171. }
  172. - (void)createChannel:(NSString *)portraitUrl {
  173. __weak typeof(self) ws = self;
  174. [[WFCCIMService sharedWFCIMService] createChannel:self.nameField.text portrait:portraitUrl status:self.openSwitch.on ? 0 : 1 desc:self.descField.text extra:nil success:^(WFCCChannelInfo *channelInfo) {
  175. NSLog(@"create channel done");
  176. WFCCTipNotificationContent *tip = [[WFCCTipNotificationContent alloc] init];
  177. tip.tip = @"频道创建成功";
  178. [[WFCCIMService sharedWFCIMService] send:[WFCCConversation conversationWithType:Channel_Type target:channelInfo.channelId line:0] content:tip success:^(long long messageUid, long long timestamp) {
  179. NSLog(@"send channel msg done");
  180. } error:^(int error_code) {
  181. NSLog(@"send channel msg failure");
  182. }];
  183. [ws.view makeToast:@"创建频道失败"
  184. duration:2
  185. position:CSToastPositionCenter];
  186. [ws.navigationController popViewControllerAnimated:YES];
  187. } error:^(int error_code) {
  188. NSLog(@"create channel error%d", error_code);
  189. }];
  190. }
  191. - (void)modifyGroup:(NSString *)groupId portrait:(NSString *)portraitUrl {
  192. __weak typeof(self) ws = self;
  193. [[WFCCIMService sharedWFCIMService] modifyGroupInfo:groupId type:Modify_Group_Portrait newValue:portraitUrl notifyLines:@[@(0)] notifyContent:nil success:^{
  194. dispatch_async(dispatch_get_main_queue(), ^{
  195. [ws.navigationController popViewControllerAnimated:YES];
  196. });
  197. } error:^(int error_code) {
  198. }];
  199. }
  200. @end