WFCUGroupInfoViewController.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // GroupInfoViewController.m
  3. // WildFireChat
  4. //
  5. // Created by heavyrain lee on 2019/3/3.
  6. // Copyright © 2019 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUGroupInfoViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import <SDWebImage/SDWebImage.h>
  11. #import "WFCUConfigManager.h"
  12. #import "WFCUImage.h"
  13. #import "WFCUMessageListViewController.h"
  14. #import "WFCUUtilities.h"
  15. @interface WFCUGroupInfoViewController ()
  16. @property (nonatomic, strong)WFCCGroupInfo *groupInfo;
  17. @property (nonatomic, strong)UIImageView *groupProtraitView;
  18. @property (nonatomic, strong)UILabel *groupNameLabel;
  19. @property (nonatomic, strong)NSArray<WFCCGroupMember *> *members;
  20. @property (nonatomic, strong)UIButton *btn;
  21. @property (nonatomic, assign)BOOL isJoined;
  22. @end
  23. @implementation WFCUGroupInfoViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. __weak typeof(self)ws = self;
  27. [[NSNotificationCenter defaultCenter] addObserverForName:kGroupInfoUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  28. NSArray<WFCCGroupInfo *> *groupInfoList = note.userInfo[@"groupInfoList"];
  29. for (WFCCGroupInfo *groupInfo in groupInfoList) {
  30. if ([ws.groupId isEqualToString:groupInfo.target]) {
  31. ws.groupInfo = groupInfo;
  32. break;
  33. }
  34. }
  35. }];
  36. [[NSNotificationCenter defaultCenter] addObserverForName:kGroupMemberUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  37. if ([ws.groupId isEqualToString:note.object]) {
  38. ws.members = [[WFCCIMService sharedWFCIMService] getGroupMembers:ws.groupId forceUpdate:NO];
  39. }
  40. }];
  41. self.groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:self.groupId refresh:NO];
  42. self.view.backgroundColor = [UIColor whiteColor];
  43. self.members = [[WFCCIMService sharedWFCIMService] getGroupMembers:self.groupId forceUpdate:NO];
  44. }
  45. - (void)setGroupInfo:(WFCCGroupInfo *)groupInfo {
  46. _groupInfo = groupInfo;
  47. if(groupInfo) {
  48. if(groupInfo.portrait.length) {
  49. [self.groupProtraitView sd_setImageWithURL:[NSURL URLWithString:groupInfo.portrait] placeholderImage:[WFCUImage imageNamed:@""]];
  50. }
  51. self.groupNameLabel.text = [NSString stringWithFormat:@"%@(%ld)", groupInfo.displayName, groupInfo.memberCount];
  52. }
  53. }
  54. - (void)setMembers:(NSArray<WFCCGroupMember *> *)members {
  55. _members = members;
  56. __block BOOL isContainMe = NO;
  57. [members enumerateObjectsUsingBlock:^(WFCCGroupMember * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  58. if ([obj.memberId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  59. *stop = YES;
  60. isContainMe = YES;
  61. }
  62. }];
  63. if(!isContainMe) {
  64. __weak typeof(self)ws = self;
  65. [[WFCUConfigManager globalManager].appServiceProvider getGroupMembersForPortrait:self.groupId success:^(NSArray<NSDictionary<NSString *, NSString *> *> *groupMembers) {
  66. [ws onGetGroupMember:groupMembers];
  67. } error:^(int error_code) {
  68. NSLog(@"error");
  69. }];
  70. }
  71. self.isJoined = isContainMe;
  72. }
  73. - (void)onGetGroupMember:(NSArray<NSDictionary<NSString *, NSString *> *> *)groupMembers {
  74. if(!self.groupInfo.portrait.length) {
  75. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  76. NSString *imagePath = [WFCCUtilities getGroupGridPortrait:self.groupId memberPortraits:groupMembers width:50 defaultUserPortrait:^UIImage *(NSString *userId) {
  77. return [WFCUImage imageNamed:@"PersonalChat"];
  78. }];
  79. dispatch_async(dispatch_get_main_queue(), ^{
  80. self.groupProtraitView.image = [UIImage imageWithContentsOfFile:imagePath];
  81. });
  82. });
  83. }
  84. }
  85. - (void)setIsJoined:(BOOL)isJoined {
  86. _isJoined = isJoined;
  87. if (isJoined) {
  88. [self.btn setTitle:WFCString(@"StartChat") forState:UIControlStateNormal];
  89. } else {
  90. [self.btn setTitle:WFCString(@"StartChat") forState:UIControlStateNormal];
  91. }
  92. }
  93. - (void)onButtonPressed:(id)sender {
  94. if (self.isJoined) {
  95. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  96. mvc.conversation = [[WFCCConversation alloc] init];
  97. mvc.conversation.type = Group_Type;
  98. mvc.conversation.target = self.groupId;
  99. mvc.conversation.line = 0;
  100. mvc.hidesBottomBarWhenPushed = YES;
  101. [self.navigationController pushViewController:mvc animated:YES];
  102. } else {
  103. __weak typeof(self) ws = self;
  104. NSString *memberExtra = nil;
  105. if(self.sourceType) {
  106. memberExtra = [WFCCUtilities getGroupMemberExtra:self.sourceType sourceTargetId:self.sourceTargetId];
  107. }
  108. [[WFCCIMService sharedWFCIMService] addMembers:@[[WFCCNetworkService sharedInstance].userId] toGroup:self.groupId memberExtra:memberExtra notifyLines:@[@(0)] notifyContent:nil success:^{
  109. [[WFCCIMService sharedWFCIMService] getGroupMembers:ws.groupId forceUpdate:YES];
  110. ws.isJoined = YES;
  111. [ws onButtonPressed:nil];
  112. } error:^(int error_code) {
  113. }];
  114. }
  115. }
  116. - (UIButton *)btn {
  117. if (!_btn) {
  118. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  119. _btn = [[UIButton alloc] initWithFrame:CGRectMake(width/2 - 80, 280, 160, 44)];
  120. _btn.layer.masksToBounds = YES;
  121. _btn.layer.cornerRadius = 5.f;
  122. [self.view addSubview:_btn];
  123. [_btn setBackgroundColor:[UIColor colorWithRed:0.1 green:0.27 blue:0.9 alpha:0.9]];
  124. [_btn addTarget:self action:@selector(onButtonPressed:) forControlEvents:UIControlEventTouchDown];
  125. }
  126. return _btn;
  127. }
  128. - (UILabel *)groupNameLabel {
  129. if (!_groupNameLabel) {
  130. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  131. _groupNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(width/2 - 100, 200, 200, 24)];
  132. _groupNameLabel.textAlignment = NSTextAlignmentCenter;
  133. [self.view addSubview:_groupNameLabel];
  134. }
  135. return _groupNameLabel;
  136. }
  137. - (UIImageView *)groupProtraitView {
  138. if (!_groupProtraitView) {
  139. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  140. _groupProtraitView = [[UIImageView alloc] initWithFrame:CGRectMake(width/2 - 32, 120, 64, 64)];
  141. [self.view addSubview:_groupProtraitView];
  142. }
  143. return _groupProtraitView;
  144. }
  145. /*
  146. #pragma mark - Navigation
  147. // In a storyboard-based application, you will often want to do a little preparation before navigation
  148. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  149. // Get the new view controller using [segue destinationViewController].
  150. // Pass the selected object to the new view controller.
  151. }
  152. */
  153. - (void)dealloc {
  154. [[NSNotificationCenter defaultCenter] removeObserver:self];
  155. }
  156. @end