WFCUChannelProfileViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // WFCUProfileTableViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/22.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUChannelProfileViewController.h"
  9. #import "SDWebImage.h"
  10. #import <WFChatClient/WFCChatClient.h>
  11. #import "WFCUMessageListViewController.h"
  12. #import "MBProgressHUD.h"
  13. #import "WFCUMyPortraitViewController.h"
  14. #import "WFCUVerifyRequestViewController.h"
  15. #import "WFCUGeneralModifyViewController.h"
  16. #import "UIView+Toast.h"
  17. #import "WFCUUtilities.h"
  18. @interface WFCUChannelProfileViewController () <UIActionSheetDelegate>
  19. @property (nonatomic, strong)UIImageView *channelPortrait;
  20. @property (nonatomic, strong)UILabel *channelName;
  21. @property (nonatomic, strong)UILabel *channelDesc;
  22. @end
  23. @implementation WFCUChannelProfileViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"..." style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
  27. CGFloat portraitWidth = 80;
  28. CGFloat top = kStatusBarAndNavigationBarHeight + 40;
  29. CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
  30. self.channelPortrait = [[UIImageView alloc] initWithFrame:CGRectMake((screenWidth - portraitWidth)/2, top, portraitWidth, portraitWidth)];
  31. [self.channelPortrait sd_setImageWithURL:[NSURL URLWithString:self.channelInfo.portrait] placeholderImage:[UIImage imageNamed:@"channel_default_portrait"]];
  32. top += portraitWidth;
  33. top += 20;
  34. self.channelName = [[UILabel alloc] initWithFrame:CGRectMake(40, top, screenWidth - 40 - 40, 18)];
  35. self.channelName.font = [UIFont systemFontOfSize:18];
  36. self.channelName.textAlignment = NSTextAlignmentCenter;
  37. self.channelName.text = self.channelInfo.name;
  38. top += 18;
  39. top += 20;
  40. NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:self.channelInfo.desc];
  41. UIFont *font = [UIFont systemFontOfSize:14];
  42. [attributeString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.channelInfo.desc.length)];
  43. NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
  44. CGRect rect = [attributeString boundingRectWithSize:CGSizeMake(screenWidth - 80, CGFLOAT_MAX) options:options context:nil];
  45. self.channelDesc = [[UILabel alloc] initWithFrame:CGRectMake(40, top, screenWidth - 80, rect.size.height)];
  46. self.channelDesc.font = [UIFont systemFontOfSize:14];
  47. self.channelDesc.textAlignment = NSTextAlignmentCenter;
  48. self.channelDesc.text = self.channelInfo.desc;
  49. self.channelDesc.numberOfLines = 0;
  50. [self.channelDesc sizeToFit];
  51. top += rect.size.height;
  52. top += 20;
  53. [self.view addSubview:self.channelPortrait];
  54. [self.view addSubview:self.channelName];
  55. [self.view addSubview:self.channelDesc];
  56. if(![[WFCCNetworkService sharedInstance].userId isEqualToString:self.channelInfo.owner]) {
  57. UIButton *btn;
  58. if ([[WFCCIMService sharedWFCIMService] isListenedChannel:self.channelInfo.channelId]) {
  59. btn = [[UIButton alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height - kTabbarSafeBottomMargin - 40 - 16, screenWidth - 40, 40)];
  60. [btn setTitle:@"发送消息" forState:UIControlStateNormal];
  61. [btn setBackgroundColor:[UIColor greenColor]];
  62. [btn addTarget:self action:@selector(onSendMessageBtn:) forControlEvents:UIControlEventTouchDown];
  63. } else {
  64. btn = [[UIButton alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height - kTabbarSafeBottomMargin - 40 - 16, screenWidth - 40, 40)];
  65. [btn setTitle:@"收听频道" forState:UIControlStateNormal];
  66. [btn setBackgroundColor:[UIColor greenColor]];
  67. [btn addTarget:self action:@selector(onSubscribeBtn:) forControlEvents:UIControlEventTouchDown];
  68. }
  69. btn.layer.cornerRadius = 5.f;
  70. btn.layer.masksToBounds = YES;
  71. [self.view addSubview:btn];
  72. } else {
  73. self.channelPortrait.userInteractionEnabled = YES;
  74. [self.channelPortrait addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(modifyChannelPortrait)]];
  75. self.channelName.userInteractionEnabled = YES;
  76. [self.channelName addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(modifyChannelName)]];
  77. self.channelDesc.userInteractionEnabled = YES;
  78. [self.channelDesc addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(modifyChannelDesc)]];
  79. }
  80. }
  81. - (void)onRightBtn:(id)sender {
  82. NSString *title;
  83. if ([self.channelInfo.owner isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  84. title = @"销毁频道";
  85. } else if ([[WFCCIMService sharedWFCIMService] isListenedChannel:self.channelInfo.channelId]) {
  86. title = @"取消收听";
  87. } else {
  88. title = @"收听频道";
  89. }
  90. UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:title otherButtonTitles:nil, nil];
  91. [actionSheet showInView:self.view];
  92. }
  93. - (void)onSendMessageBtn:(id)sender {
  94. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  95. mvc.conversation = [WFCCConversation conversationWithType:Channel_Type target:self.channelInfo.channelId line:0];
  96. for (UIViewController *vc in self.navigationController.viewControllers) {
  97. if ([vc isKindOfClass:[WFCUMessageListViewController class]]) {
  98. [self.navigationController popToViewController:vc animated:YES];
  99. return;
  100. }
  101. }
  102. [self.navigationController pushViewController:mvc animated:YES];
  103. }
  104. - (void)onSubscribeBtn:(id)sender {
  105. __weak typeof(self) ws = self;
  106. [[WFCCIMService sharedWFCIMService] listenChannel:self.channelInfo.channelId listen:YES success:^{
  107. dispatch_async(dispatch_get_main_queue(), ^{
  108. [ws.navigationController popViewControllerAnimated:YES];
  109. [ws onSubscribeBtn:sender];
  110. });
  111. } error:^(int errorCode) {
  112. }];
  113. }
  114. - (void)modifyChannelPortrait {
  115. UIActionSheet *actionSheet =
  116. [[UIActionSheet alloc] initWithTitle:@"修改头像"
  117. delegate:self
  118. cancelButtonTitle:@"取消"
  119. destructiveButtonTitle:@"拍照"
  120. otherButtonTitles:@"相册", nil];
  121. [actionSheet showInView:self.view];
  122. actionSheet.tag = 1;
  123. }
  124. - (void)modifyChannelName {
  125. WFCUGeneralModifyViewController *gmvc = [[WFCUGeneralModifyViewController alloc] init];
  126. gmvc.defaultValue = self.channelInfo.name;
  127. gmvc.titleText = @"修改频道名称";
  128. gmvc.canEmpty = NO;
  129. gmvc.tryModify = ^(NSString *newValue, void (^result)(BOOL success)) {
  130. [[WFCCIMService sharedWFCIMService] modifyChannelInfo:self.channelInfo.channelId type:Modify_Channel_Name newValue:newValue success:^{
  131. result(YES);
  132. } error:^(int error_code) {
  133. result(NO);
  134. }];
  135. };
  136. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:gmvc];
  137. [self.navigationController presentViewController:nav animated:YES completion:nil];
  138. }
  139. - (void)modifyChannelDesc {
  140. WFCUGeneralModifyViewController *gmvc = [[WFCUGeneralModifyViewController alloc] init];
  141. gmvc.defaultValue = self.channelInfo.desc;
  142. gmvc.titleText = @"修改频道描述";
  143. gmvc.canEmpty = NO;
  144. gmvc.tryModify = ^(NSString *newValue, void (^result)(BOOL success)) {
  145. [[WFCCIMService sharedWFCIMService] modifyChannelInfo:self.channelInfo.channelId type:Modify_Channel_Desc newValue:newValue success:^{
  146. result(YES);
  147. } error:^(int error_code) {
  148. result(NO);
  149. }];
  150. };
  151. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:gmvc];
  152. [self.navigationController presentViewController:nav animated:YES completion:nil];
  153. }
  154. - (void)didReceiveMemoryWarning {
  155. [super didReceiveMemoryWarning];
  156. // Dispose of any resources that can be recreated.
  157. }
  158. #pragma mark - UIActionSheetDelegate <NSObject>
  159. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  160. if(actionSheet.tag == 0) {
  161. if(buttonIndex == 0) {
  162. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  163. hud.label.text = @"处理中...";
  164. [hud showAnimated:YES];
  165. if ([self.channelInfo.owner isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  166. [[WFCCIMService sharedWFCIMService] destoryChannel:self.channelInfo.channelId success:^{
  167. dispatch_async(dispatch_get_main_queue(), ^{
  168. [hud hideAnimated:YES];
  169. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  170. hud.mode = MBProgressHUDModeText;
  171. hud.label.text = @"处理成功";
  172. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  173. [hud hideAnimated:YES afterDelay:1.f];
  174. });
  175. } error:^(int error_code) {
  176. dispatch_async(dispatch_get_main_queue(), ^{
  177. [hud hideAnimated:YES];
  178. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  179. hud.mode = MBProgressHUDModeText;
  180. hud.label.text = @"处理失败";
  181. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  182. [hud hideAnimated:YES afterDelay:1.f];
  183. });
  184. }];
  185. } else {
  186. BOOL isListen = ![[WFCCIMService sharedWFCIMService] isListenedChannel:self.channelInfo.channelId];
  187. [[WFCCIMService sharedWFCIMService] listenChannel:self.channelInfo.channelId listen:isListen success:^{
  188. dispatch_async(dispatch_get_main_queue(), ^{
  189. [hud hideAnimated:YES];
  190. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  191. hud.mode = MBProgressHUDModeText;
  192. hud.label.text = @"处理成功";
  193. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  194. [hud hideAnimated:YES afterDelay:1.f];
  195. });
  196. } error:^(int errorCode) {
  197. dispatch_async(dispatch_get_main_queue(), ^{
  198. [hud hideAnimated:YES];
  199. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  200. hud.mode = MBProgressHUDModeText;
  201. hud.label.text = @"处理失败";
  202. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  203. [hud hideAnimated:YES afterDelay:1.f];
  204. });
  205. }];
  206. }
  207. }
  208. } else if(actionSheet.tag == 1) {
  209. if(buttonIndex == 0) {
  210. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  211. picker.allowsEditing = YES;
  212. picker.delegate = self;
  213. if ([UIImagePickerController
  214. isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  215. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  216. } else {
  217. NSLog(@"无法连接相机");
  218. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  219. }
  220. [self presentViewController:picker animated:YES completion:nil];
  221. } else if (buttonIndex == 1) {
  222. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  223. picker.allowsEditing = YES;
  224. picker.delegate = self;
  225. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  226. [self presentViewController:picker animated:YES completion:nil];
  227. }
  228. }
  229. }
  230. #pragma mark - UIImagePickerControllerDelegate
  231. - (void)imagePickerController:(UIImagePickerController *)picker
  232. didFinishPickingMediaWithInfo:(NSDictionary *)info {
  233. [UIApplication sharedApplication].statusBarHidden = NO;
  234. NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  235. if ([mediaType isEqual:@"public.image"]) {
  236. UIImage *originImage =
  237. [info objectForKey:UIImagePickerControllerEditedImage];
  238. //获取截取区域的图像
  239. UIImage *captureImage = [WFCUUtilities thumbnailWithImage:originImage maxSize:CGSizeMake(60, 60)];
  240. [self uploadPortrait:captureImage];
  241. }
  242. [picker dismissViewControllerAnimated:YES completion:nil];
  243. }
  244. - (void)uploadPortrait:(UIImage *)portraitImage {
  245. NSData *portraitData = UIImageJPEGRepresentation(portraitImage, 0.70);
  246. __weak typeof(self) ws = self;
  247. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  248. hud.label.text = @"图片上传中...";
  249. [hud showAnimated:YES];
  250. [[WFCCIMService sharedWFCIMService] uploadMedia:portraitData mediaType:Media_Type_PORTRAIT success:^(NSString *remoteUrl) {
  251. dispatch_async(dispatch_get_main_queue(), ^{
  252. [hud hideAnimated:NO];
  253. if (remoteUrl.length) {
  254. [[WFCCIMService sharedWFCIMService] modifyChannelInfo:ws.channelInfo.channelId type:Modify_Channel_Portrait newValue:remoteUrl success:^{
  255. ;
  256. } error:^(int error_code) {
  257. [ws.view makeToast:@"创建修改头像失败"
  258. duration:2
  259. position:CSToastPositionCenter];
  260. }];
  261. }
  262. });
  263. }
  264. progress:^(long uploaded, long total) {
  265. }
  266. error:^(int error_code) {
  267. dispatch_async(dispatch_get_main_queue(), ^{
  268. [hud hideAnimated:NO];
  269. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  270. hud.mode = MBProgressHUDModeText;
  271. hud.label.text = @"上传失败";
  272. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  273. [hud hideAnimated:YES afterDelay:1.f];
  274. });
  275. }];
  276. }
  277. @end