WFCUGroupAnnouncementViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // WFCUGroupAnnouncementViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by Heavyrain Lee on 2019/10/22.
  6. // Copyright © 2019 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUGroupAnnouncementViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import <SDWebImage/SDWebImage.h>
  11. #import "WFCUConfigManager.h"
  12. #import "MBProgressHUD.h"
  13. #import "WFCUImage.h"
  14. #import "WFCUUtilities.h"
  15. #import "UIView+Toast.h"
  16. @interface WFCUGroupAnnouncementViewController () <UITextViewDelegate>
  17. @property(nonatomic, strong)UIImageView *portraitView;
  18. @property(nonatomic, strong)UILabel *nameLabel;
  19. @property(nonatomic, strong)UILabel *timeLabel;
  20. @property(nonatomic, strong)UILabel *maxLengthLabel;
  21. @property(nonatomic, strong)UITextView *textView;
  22. @end
  23. #define MAX_TEXT_LENGTH 2000
  24. @implementation WFCUGroupAnnouncementViewController
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  28. int offset = 0;
  29. CGFloat headHeight;
  30. if (self.announcement.author.length && self.announcement.text.length) {
  31. UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, [WFCUUtilities wf_navigationFullHeight], self.view.bounds.size.width, 80)];
  32. self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 16, 48, 48)];
  33. WFCCUserInfo *author = [[WFCCIMService sharedWFCIMService] getUserInfo:self.announcement.author refresh:NO];
  34. [self.portraitView sd_setImageWithURL:[NSURL URLWithString:author.portrait] placeholderImage: [WFCUImage imageNamed:@"PersonalChat"]];
  35. self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 16, self.view.bounds.size.width - 80 - 16, 20)];
  36. self.nameLabel.text = author.displayName;
  37. self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 48, self.view.bounds.size.width - 80 - 16, 14)];
  38. NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.announcement.timestamp/1000];
  39. self.timeLabel.text = date.description;
  40. self.timeLabel.font = [UIFont systemFontOfSize:14];
  41. self.timeLabel.textColor = [UIColor grayColor];
  42. [headView addSubview:self.portraitView];
  43. [headView addSubview:self.nameLabel];
  44. [headView addSubview:self.timeLabel];
  45. [self.view addSubview:headView];
  46. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapHeaderView:)];
  47. [headView addGestureRecognizer:tap];
  48. offset = 80;
  49. headHeight = offset;
  50. } else {
  51. offset = 16;
  52. headHeight = offset;
  53. }
  54. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(16, [WFCUUtilities wf_navigationFullHeight] + offset, self.view.bounds.size.width - 32, 0.5)];
  55. line.backgroundColor = [UIColor grayColor];
  56. [self.view addSubview:line];
  57. CGFloat hintSize = 0;
  58. if (!self.isManager) {
  59. line = [[UIView alloc] initWithFrame:CGRectMake(16, self.view.bounds.size.height - [WFCUUtilities wf_safeDistanceBottom] - 40, self.view.bounds.size.width - 32, 0.5)];
  60. line.backgroundColor = [UIColor grayColor];
  61. [self.view addSubview:line];
  62. UILabel *hint = [[UILabel alloc] initWithFrame:CGRectMake(16, self.view.bounds.size.height - [WFCUUtilities wf_safeDistanceBottom] - 36, self.view.bounds.size.width - 32, 16)];
  63. hint.textAlignment = NSTextAlignmentCenter;
  64. hint.text = @"仅群主和管理员可编辑";
  65. hint.textColor = [UIColor grayColor];
  66. hint.font = [UIFont systemFontOfSize:12];
  67. [self.view addSubview:hint];
  68. hintSize = 40;
  69. } else {
  70. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Edit") style:UIBarButtonItemStyleDone target:self action:@selector(onEditBtn:)];
  71. }
  72. CGFloat maxLenLabelHeight = 24;
  73. CGFloat textHeigh = self.view.bounds.size.height - headHeight - [WFCUUtilities wf_navigationFullHeight] - [WFCUUtilities wf_safeDistanceBottom] - hintSize - maxLenLabelHeight;
  74. self.textView = [[UITextView alloc] initWithFrame:CGRectMake(16, [WFCUUtilities wf_navigationFullHeight] + offset + 1, self.view.bounds.size.width - 32, textHeigh)];
  75. self.textView.editable = NO;
  76. self.textView.text = self.announcement.text;
  77. self.textView.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
  78. self.textView.delegate = self;
  79. [self.view addSubview:self.textView];
  80. offset += textHeigh;
  81. offset += 4;
  82. self.maxLengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 80 - 16, [WFCUUtilities wf_navigationFullHeight] + offset, 80, maxLenLabelHeight - 4)];
  83. self.maxLengthLabel.font = [UIFont systemFontOfSize:12];
  84. self.maxLengthLabel.textColor = [UIColor grayColor];
  85. self.maxLengthLabel.textAlignment = NSTextAlignmentRight;
  86. [self.view addSubview:self.maxLengthLabel];
  87. [self updateMaxLengthLabel];
  88. self.maxLengthLabel.hidden = YES;
  89. }
  90. - (void)updateMaxLengthLabel {
  91. self.maxLengthLabel.text = [NSString stringWithFormat:@"%d/%d", self.textView.text.length, MAX_TEXT_LENGTH];
  92. }
  93. - (void)onTapHeaderView:(id)sender {
  94. if([self.textView isFirstResponder]) {
  95. [self.textView resignFirstResponder];
  96. }
  97. }
  98. - (void)onEditBtn:(id)sender {
  99. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Save") style:UIBarButtonItemStyleDone target:self action:@selector(onSaveBtn:)];
  100. self.navigationItem.rightBarButtonItem.enabled = NO;
  101. self.textView.editable = YES;
  102. [self.textView becomeFirstResponder];
  103. self.textView.selectedRange = NSMakeRange(self.announcement.text.length, 0);
  104. self.maxLengthLabel.hidden = NO;
  105. [[NSNotificationCenter defaultCenter] addObserver:self
  106. selector:@selector(keyboardWillShow:)
  107. name:UIKeyboardWillShowNotification
  108. object:nil];
  109. [[NSNotificationCenter defaultCenter] addObserver:self
  110. selector:@selector(keyboardWillHide:)
  111. name:UIKeyboardWillHideNotification
  112. object:nil];
  113. }
  114. - (void)keyboardWillShow:(NSNotification *)notification {
  115. if (![self.textView isFirstResponder]) {
  116. return;
  117. }
  118. NSDictionary *userInfo = [notification userInfo];
  119. NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  120. CGRect keyboardRect = [value CGRectValue];
  121. CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  122. int offset = 0;
  123. CGFloat headHeight;
  124. if (self.announcement.author.length && self.announcement.text.length) {
  125. offset = 80;
  126. headHeight = offset;
  127. } else {
  128. offset = 16;
  129. headHeight = offset;
  130. }
  131. CGFloat hintSize = self.isManager?0:40;
  132. CGFloat maxLenLabelHeight = 24;
  133. CGFloat textHeigh = self.view.bounds.size.height - headHeight - [WFCUUtilities wf_navigationFullHeight] - [WFCUUtilities wf_safeDistanceBottom] - hintSize - maxLenLabelHeight - keyboardRect.size.height;
  134. CGRect textViewFrame = self.textView.frame;
  135. textViewFrame.size.height = textHeigh;
  136. offset += textHeigh;
  137. offset += 4;
  138. CGRect maxLenFrame = self.maxLengthLabel.frame;
  139. maxLenFrame.origin.y = [WFCUUtilities wf_navigationFullHeight] + offset + 1;
  140. [UIView animateWithDuration:duration animations:^{
  141. self.textView.frame = textViewFrame;
  142. self.maxLengthLabel.frame = maxLenFrame;
  143. }];
  144. }
  145. - (void)keyboardWillHide:(NSNotification *)notification {
  146. NSDictionary *userInfo = [notification userInfo];
  147. CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  148. int offset = 0;
  149. CGFloat headHeight;
  150. if (self.announcement.author.length && self.announcement.text.length) {
  151. offset = 80;
  152. headHeight = offset;
  153. } else {
  154. offset = 16;
  155. headHeight = offset;
  156. }
  157. CGFloat hintSize = self.isManager?0:40;
  158. CGFloat maxLenLabelHeight = 24;
  159. CGFloat textHeigh = self.view.bounds.size.height - headHeight - [WFCUUtilities wf_navigationFullHeight] - [WFCUUtilities wf_safeDistanceBottom] - hintSize - maxLenLabelHeight;
  160. CGRect textViewFrame = self.textView.frame;
  161. textViewFrame.size.height = textHeigh;
  162. offset += textHeigh;
  163. offset += 4;
  164. CGRect maxLenFrame = self.maxLengthLabel.frame;
  165. maxLenFrame.origin.y = [WFCUUtilities wf_navigationFullHeight] + offset;
  166. [UIView animateWithDuration:duration animations:^{
  167. self.textView.frame = textViewFrame;
  168. self.maxLengthLabel.frame = maxLenFrame;
  169. }];
  170. }
  171. - (void)onSaveBtn:(id)sender {
  172. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  173. hud.label.text = @"保存中...";
  174. [hud showAnimated:YES];
  175. [[WFCUConfigManager globalManager].appServiceProvider updateGroup:self.announcement.groupId announcement:self.textView.text success:^(long timestamp) {
  176. dispatch_async(dispatch_get_main_queue(), ^{
  177. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  178. hud.mode = MBProgressHUDModeText;
  179. hud.label.text = @"保存成功";
  180. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  181. [hud hideAnimated:YES afterDelay:1.f];
  182. self.announcement.author = [WFCCNetworkService sharedInstance].userId;
  183. self.announcement.text = self.textView.text;
  184. self.announcement.timestamp = timestamp;
  185. [self.navigationController popViewControllerAnimated:YES];
  186. });
  187. } error:^(int error_code) {
  188. dispatch_async(dispatch_get_main_queue(), ^{
  189. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  190. hud.mode = MBProgressHUDModeText;
  191. hud.label.text = @"保存失败";
  192. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  193. [hud hideAnimated:YES afterDelay:1.f];
  194. });
  195. }];
  196. }
  197. #pragma mark - UITextViewDelegate
  198. - (void)textViewDidChange:(UITextView *)textView {
  199. [self updateMaxLengthLabel];
  200. if ([self.announcement.text isEqualToString:textView.text]) {
  201. self.navigationItem.rightBarButtonItem.enabled = NO;
  202. } else {
  203. self.navigationItem.rightBarButtonItem.enabled = YES;
  204. }
  205. }
  206. -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  207. NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
  208. if(newText.length > MAX_TEXT_LENGTH) {
  209. [self.view makeToast:@"超过大小限制"];
  210. return NO;
  211. }
  212. return YES;
  213. }
  214. - (void)dealloc {
  215. [[NSNotificationCenter defaultCenter] removeObserver:self];
  216. }
  217. @end