WFCUGroupAnnouncementViewController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.h"
  11. #import "WFCUConfigManager.h"
  12. #import "MBProgressHUD.h"
  13. @interface WFCUGroupAnnouncementViewController () <UITextViewDelegate>
  14. @property(nonatomic, strong)UIImageView *portraitView;
  15. @property(nonatomic, strong)UILabel *nameLabel;
  16. @property(nonatomic, strong)UILabel *timeLabel;
  17. @property(nonatomic, strong)UITextView *textView;
  18. @end
  19. @implementation WFCUGroupAnnouncementViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  23. int offset = 0;
  24. if (self.announcement.author.length && self.announcement.text.length) {
  25. self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(16, kStatusBarAndNavigationBarHeight + 16, 48, 48)];
  26. WFCCUserInfo *author = [[WFCCIMService sharedWFCIMService] getUserInfo:self.announcement.author refresh:NO];
  27. [self.portraitView sd_setImageWithURL:[NSURL URLWithString:author.portrait] placeholderImage: [UIImage imageNamed:@"PersonalChat"]];
  28. self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, kStatusBarAndNavigationBarHeight + 16, self.view.bounds.size.width - 80 - 16, 20)];
  29. self.nameLabel.text = author.displayName;
  30. self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, kStatusBarAndNavigationBarHeight + 48, self.view.bounds.size.width - 80 - 16, 14)];
  31. NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.announcement.timestamp/1000];
  32. self.timeLabel.text = date.description;
  33. self.timeLabel.font = [UIFont systemFontOfSize:14];
  34. self.timeLabel.textColor = [UIColor grayColor];
  35. [self.view addSubview:self.portraitView];
  36. [self.view addSubview:self.nameLabel];
  37. [self.view addSubview:self.timeLabel];
  38. offset = 80;
  39. } else {
  40. offset = 16;
  41. }
  42. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(16, kStatusBarAndNavigationBarHeight + offset, self.view.bounds.size.width - 32, 0.5)];
  43. line.backgroundColor = [UIColor grayColor];
  44. [self.view addSubview:line];
  45. CGFloat hintSize = 0;
  46. if (!self.isManager) {
  47. line = [[UIView alloc] initWithFrame:CGRectMake(16, self.view.bounds.size.height - kTabbarSafeBottomMargin - 40, self.view.bounds.size.width - 32, 0.5)];
  48. line.backgroundColor = [UIColor grayColor];
  49. [self.view addSubview:line];
  50. UILabel *hint = [[UILabel alloc] initWithFrame:CGRectMake(16, self.view.bounds.size.height - kTabbarSafeBottomMargin - 36, self.view.bounds.size.width - 32, 16)];
  51. hint.textAlignment = NSTextAlignmentCenter;
  52. hint.text = @"仅群主和管理员可编辑";
  53. hint.textColor = [UIColor grayColor];
  54. hint.font = [UIFont systemFontOfSize:12];
  55. [self.view addSubview:hint];
  56. hintSize = 40;
  57. } else {
  58. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(onEditBtn:)];
  59. }
  60. self.textView = [[UITextView alloc] initWithFrame:CGRectMake(16, kStatusBarAndNavigationBarHeight + offset + 1, self.view.bounds.size.width - 31, self.view.bounds.size.height - 81 - kStatusBarAndNavigationBarHeight - kTabbarSafeBottomMargin - hintSize)];
  61. self.textView.editable = NO;
  62. self.textView.text = self.announcement.text;
  63. self.textView.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
  64. self.textView.delegate = self;
  65. [self.view addSubview:self.textView];
  66. }
  67. /*
  68. #pragma mark - Navigation
  69. // In a storyboard-based application, you will often want to do a little preparation before navigation
  70. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  71. // Get the new view controller using [segue destinationViewController].
  72. // Pass the selected object to the new view controller.
  73. }
  74. */
  75. - (void)onEditBtn:(id)sender {
  76. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(onSaveBtn:)];
  77. self.navigationItem.rightBarButtonItem.enabled = NO;
  78. self.textView.editable = YES;
  79. [self.textView becomeFirstResponder];
  80. self.textView.selectedRange = NSMakeRange(self.announcement.text.length, 0);
  81. }
  82. - (void)onSaveBtn:(id)sender {
  83. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  84. hud.label.text = @"保存中...";
  85. [hud showAnimated:YES];
  86. [[WFCUConfigManager globalManager].appServiceProvider updateGroup:self.announcement.groupId announcement:self.textView.text success:^(long timestamp) {
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  89. hud.mode = MBProgressHUDModeText;
  90. hud.label.text = @"保存成功";
  91. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  92. [hud hideAnimated:YES afterDelay:1.f];
  93. self.announcement.author = [WFCCNetworkService sharedInstance].userId;
  94. self.announcement.text = self.textView.text;
  95. self.announcement.timestamp = timestamp;
  96. [self.navigationController popViewControllerAnimated:YES];
  97. });
  98. } error:^(int error_code) {
  99. dispatch_async(dispatch_get_main_queue(), ^{
  100. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  101. hud.mode = MBProgressHUDModeText;
  102. hud.label.text = @"保存失败";
  103. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  104. [hud hideAnimated:YES afterDelay:1.f];
  105. });
  106. }];
  107. }
  108. #pragma mark - UITextViewDelegate
  109. - (void)textViewDidChange:(UITextView *)textView {
  110. if ([self.announcement.text isEqualToString:textView.text]) {
  111. self.navigationItem.rightBarButtonItem.enabled = NO;
  112. } else {
  113. self.navigationItem.rightBarButtonItem.enabled = YES;
  114. }
  115. }
  116. @end