WFCUChannelProfileViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // WFCUChannelProfileViewController.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/SDWebImage.h>
  10. #import <WFChatClient/WFCChatClient.h>
  11. #import "WFCUMessageListViewController.h"
  12. #import "MBProgressHUD.h"
  13. #import "WFCUMyPortraitViewController.h"
  14. #import "WFCUGeneralModifyViewController.h"
  15. #import "UIView+Toast.h"
  16. #import "WFCUUtilities.h"
  17. #import "WFCUImage.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 = [WFCUUtilities wf_navigationFullHeight] + 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 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage 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 - [WFCUUtilities wf_safeDistanceBottom] - 40 - 16, screenWidth - 40, 40)];
  60. [btn setTitle:WFCString(@"SendMessage") 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 - [WFCUUtilities wf_safeDistanceBottom] - 40 - 16, screenWidth - 40, 40)];
  65. [btn setTitle:WFCString(@"SubscribeChannel") 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 = WFCString(@"DestroyChannel");
  85. } else if ([[WFCCIMService sharedWFCIMService] isListenedChannel:self.channelInfo.channelId]) {
  86. title = WFCString(@"UnscribeChannel");
  87. } else {
  88. title = WFCString(@"SubscribeChannel");
  89. }
  90. UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:WFCString(@"Cancel") 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. });
  110. } error:^(int errorCode) {
  111. }];
  112. }
  113. - (void)modifyChannelPortrait {
  114. UIActionSheet *actionSheet =
  115. [[UIActionSheet alloc] initWithTitle:WFCString(@"ChangePortrait")
  116. delegate:self
  117. cancelButtonTitle:WFCString(@"Cancel")
  118. destructiveButtonTitle:WFCString(@"TakePhotos")
  119. otherButtonTitles:WFCString(@"Album"), nil];
  120. [actionSheet showInView:self.view];
  121. actionSheet.tag = 1;
  122. }
  123. - (void)modifyChannelName {
  124. WFCUGeneralModifyViewController *gmvc = [[WFCUGeneralModifyViewController alloc] init];
  125. gmvc.defaultValue = self.channelInfo.name;
  126. gmvc.titleText = WFCString(@"ModifyChannelName");
  127. gmvc.canEmpty = NO;
  128. gmvc.tryModify = ^(NSString *newValue, void (^result)(BOOL success)) {
  129. [[WFCCIMService sharedWFCIMService] modifyChannelInfo:self.channelInfo.channelId type:Modify_Channel_Name newValue:newValue success:^{
  130. result(YES);
  131. } error:^(int error_code) {
  132. result(NO);
  133. }];
  134. };
  135. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:gmvc];
  136. [self.navigationController presentViewController:nav animated:YES completion:nil];
  137. }
  138. - (void)modifyChannelDesc {
  139. WFCUGeneralModifyViewController *gmvc = [[WFCUGeneralModifyViewController alloc] init];
  140. gmvc.defaultValue = self.channelInfo.desc;
  141. gmvc.titleText = WFCString(@"ModifyChannelDesc");
  142. gmvc.canEmpty = NO;
  143. gmvc.tryModify = ^(NSString *newValue, void (^result)(BOOL success)) {
  144. [[WFCCIMService sharedWFCIMService] modifyChannelInfo:self.channelInfo.channelId type:Modify_Channel_Desc newValue:newValue success:^{
  145. result(YES);
  146. } error:^(int error_code) {
  147. result(NO);
  148. }];
  149. };
  150. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:gmvc];
  151. [self.navigationController presentViewController:nav animated:YES completion:nil];
  152. }
  153. - (void)didReceiveMemoryWarning {
  154. [super didReceiveMemoryWarning];
  155. // Dispose of any resources that can be recreated.
  156. }
  157. #pragma mark - UIActionSheetDelegate <NSObject>
  158. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  159. if(actionSheet.tag == 0) {
  160. if(buttonIndex == 0) {
  161. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  162. hud.label.text = WFCString(@"Updating");
  163. [hud showAnimated:YES];
  164. if ([self.channelInfo.owner isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  165. [[WFCCIMService sharedWFCIMService] destoryChannel:self.channelInfo.channelId success:^{
  166. dispatch_async(dispatch_get_main_queue(), ^{
  167. [hud hideAnimated:YES];
  168. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  169. hud.mode = MBProgressHUDModeText;
  170. hud.label.text = WFCString(@"UpdateDone");
  171. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  172. [hud hideAnimated:YES afterDelay:1.f];
  173. });
  174. } error:^(int error_code) {
  175. dispatch_async(dispatch_get_main_queue(), ^{
  176. [hud hideAnimated:YES];
  177. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  178. hud.mode = MBProgressHUDModeText;
  179. hud.label.text = WFCString(@"UpdateFailure");
  180. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  181. [hud hideAnimated:YES afterDelay:1.f];
  182. });
  183. }];
  184. } else {
  185. BOOL isListen = ![[WFCCIMService sharedWFCIMService] isListenedChannel:self.channelInfo.channelId];
  186. [[WFCCIMService sharedWFCIMService] listenChannel:self.channelInfo.channelId listen:isListen success:^{
  187. dispatch_async(dispatch_get_main_queue(), ^{
  188. [hud hideAnimated:YES];
  189. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  190. hud.mode = MBProgressHUDModeText;
  191. hud.label.text = WFCString(@"UpdateDone");
  192. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  193. [hud hideAnimated:YES afterDelay:1.f];
  194. });
  195. } error:^(int errorCode) {
  196. dispatch_async(dispatch_get_main_queue(), ^{
  197. [hud hideAnimated:YES];
  198. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  199. hud.mode = MBProgressHUDModeText;
  200. hud.label.text = WFCString(@"UpdateFailure");
  201. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  202. [hud hideAnimated:YES afterDelay:1.f];
  203. });
  204. }];
  205. }
  206. }
  207. } else if(actionSheet.tag == 1) {
  208. if(buttonIndex == 0) {
  209. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  210. picker.allowsEditing = YES;
  211. picker.delegate = self;
  212. if ([UIImagePickerController
  213. isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  214. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  215. } else {
  216. NSLog(@"无法连接相机");
  217. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  218. }
  219. [self presentViewController:picker animated:YES completion:nil];
  220. } else if (buttonIndex == 1) {
  221. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  222. picker.allowsEditing = YES;
  223. picker.delegate = self;
  224. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  225. [self presentViewController:picker animated:YES completion:nil];
  226. }
  227. }
  228. }
  229. #pragma mark - UIImagePickerControllerDelegate
  230. - (void)imagePickerController:(UIImagePickerController *)picker
  231. didFinishPickingMediaWithInfo:(NSDictionary *)info {
  232. [UIApplication sharedApplication].statusBarHidden = NO;
  233. NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  234. if ([mediaType isEqual:@"public.image"]) {
  235. UIImage *originImage =
  236. [info objectForKey:UIImagePickerControllerEditedImage];
  237. //获取截取区域的图像
  238. UIImage *captureImage = [WFCUUtilities thumbnailWithImage:originImage maxSize:CGSizeMake(60, 60)];
  239. [self uploadPortrait:captureImage];
  240. }
  241. [picker dismissViewControllerAnimated:YES completion:nil];
  242. }
  243. - (void)uploadPortrait:(UIImage *)portraitImage {
  244. NSData *portraitData = UIImageJPEGRepresentation(portraitImage, 0.70);
  245. __weak typeof(self) ws = self;
  246. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  247. hud.label.text = WFCString(@"PhotoUploading");
  248. [hud showAnimated:YES];
  249. [[WFCCIMService sharedWFCIMService] uploadMedia:nil mediaData:portraitData mediaType:Media_Type_PORTRAIT success:^(NSString *remoteUrl) {
  250. dispatch_async(dispatch_get_main_queue(), ^{
  251. [hud hideAnimated:NO];
  252. if (remoteUrl.length) {
  253. [[WFCCIMService sharedWFCIMService] modifyChannelInfo:ws.channelInfo.channelId type:Modify_Channel_Portrait newValue:remoteUrl success:^{
  254. ;
  255. } error:^(int error_code) {
  256. [ws.view makeToast:WFCString(@"ModifyPortraitFailure")
  257. duration:2
  258. position:CSToastPositionCenter];
  259. }];
  260. }
  261. });
  262. }
  263. progress:^(long uploaded, long total) {
  264. }
  265. error:^(int error_code) {
  266. dispatch_async(dispatch_get_main_queue(), ^{
  267. [hud hideAnimated:NO];
  268. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  269. hud.mode = MBProgressHUDModeText;
  270. hud.label.text = WFCString(@"UploadFailure");
  271. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  272. [hud hideAnimated:YES afterDelay:1.f];
  273. });
  274. }];
  275. }
  276. @end