GroupInfoViewController.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "GroupInfoViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "SDWebImage.h"
  11. #import <WFChatUIKit/WFChatUIKit.h>
  12. @interface GroupInfoViewController ()
  13. @property (nonatomic, strong)WFCCGroupInfo *groupInfo;
  14. @property (nonatomic, strong)UIImageView *groupProtraitView;
  15. @property (nonatomic, strong)UILabel *groupNameLabel;
  16. @property (nonatomic, strong)NSArray<WFCCGroupMember *> *members;
  17. @property (nonatomic, strong)UIButton *btn;
  18. @property (nonatomic, assign)BOOL isJoined;
  19. @end
  20. @implementation GroupInfoViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. __weak typeof(self)ws = self;
  24. [[NSNotificationCenter defaultCenter] addObserverForName:kGroupInfoUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  25. if ([ws.groupId isEqualToString:note.object]) {
  26. WFCCGroupInfo *groupInfo = note.userInfo[@"groupInfo"];
  27. ws.groupInfo = groupInfo;
  28. }
  29. }];
  30. [[NSNotificationCenter defaultCenter] addObserverForName:kGroupMemberUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  31. if ([ws.groupId isEqualToString:note.object]) {
  32. ws.members = [[WFCCIMService sharedWFCIMService] getGroupMembers:ws.groupId forceUpdate:NO];
  33. }
  34. }];
  35. self.groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:self.groupId refresh:NO];
  36. self.view.backgroundColor = [UIColor whiteColor];
  37. self.members = [[WFCCIMService sharedWFCIMService] getGroupMembers:self.groupId forceUpdate:NO];
  38. }
  39. - (void)setGroupInfo:(WFCCGroupInfo *)groupInfo {
  40. _groupInfo = groupInfo;
  41. [self.groupProtraitView sd_setImageWithURL:[NSURL URLWithString:groupInfo.portrait] placeholderImage:[UIImage imageNamed:@""]];
  42. self.groupNameLabel.text = groupInfo.name;
  43. }
  44. - (void)setMembers:(NSArray<WFCCGroupMember *> *)members {
  45. _members = members;
  46. __block BOOL isContainMe = NO;
  47. [members enumerateObjectsUsingBlock:^(WFCCGroupMember * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  48. if ([obj.memberId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  49. *stop = YES;
  50. isContainMe = YES;
  51. }
  52. }];
  53. self.isJoined = isContainMe;
  54. }
  55. - (void)setIsJoined:(BOOL)isJoined {
  56. _isJoined = isJoined;
  57. if (isJoined) {
  58. [self.btn setTitle:@"进入聊天" forState:UIControlStateNormal];
  59. } else {
  60. [self.btn setTitle:@"加入聊天" forState:UIControlStateNormal];
  61. }
  62. }
  63. - (void)onButtonPressed:(id)sender {
  64. if (self.isJoined) {
  65. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  66. mvc.conversation = [[WFCCConversation alloc] init];
  67. mvc.conversation.type = Group_Type;
  68. mvc.conversation.target = self.groupId;
  69. mvc.conversation.line = 0;
  70. mvc.hidesBottomBarWhenPushed = YES;
  71. [self.navigationController pushViewController:mvc animated:YES];
  72. } else {
  73. __weak typeof(self) ws = self;
  74. [[WFCCIMService sharedWFCIMService] addMembers:@[[WFCCNetworkService sharedInstance].userId] toGroup:self.groupId notifyLines:@[@(0)] notifyContent:nil success:^{
  75. [[WFCCIMService sharedWFCIMService] getGroupMembers:ws.groupId forceUpdate:YES];
  76. ws.isJoined = YES;
  77. [ws onButtonPressed:nil];
  78. } error:^(int error_code) {
  79. }];
  80. }
  81. }
  82. - (UIButton *)btn {
  83. if (!_btn) {
  84. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  85. _btn = [[UIButton alloc] initWithFrame:CGRectMake(width/2 - 80, 280, 160, 44)];
  86. _btn.layer.masksToBounds = YES;
  87. _btn.layer.cornerRadius = 5.f;
  88. [self.view addSubview:_btn];
  89. [_btn setBackgroundColor:[UIColor colorWithRed:0.1 green:0.27 blue:0.9 alpha:0.9]];
  90. [_btn addTarget:self action:@selector(onButtonPressed:) forControlEvents:UIControlEventTouchDown];
  91. }
  92. return _btn;
  93. }
  94. - (UILabel *)groupNameLabel {
  95. if (!_groupNameLabel) {
  96. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  97. _groupNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(width/2 - 100, 200, 200, 24)];
  98. _groupNameLabel.textAlignment = NSTextAlignmentCenter;
  99. [self.view addSubview:_groupNameLabel];
  100. }
  101. return _groupNameLabel;
  102. }
  103. - (UIImageView *)groupProtraitView {
  104. if (!_groupProtraitView) {
  105. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  106. _groupProtraitView = [[UIImageView alloc] initWithFrame:CGRectMake(width/2 - 32, 120, 64, 64)];
  107. [self.view addSubview:_groupProtraitView];
  108. }
  109. return _groupProtraitView;
  110. }
  111. /*
  112. #pragma mark - Navigation
  113. // In a storyboard-based application, you will often want to do a little preparation before navigation
  114. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  115. // Get the new view controller using [segue destinationViewController].
  116. // Pass the selected object to the new view controller.
  117. }
  118. */
  119. - (void)dealloc {
  120. [[NSNotificationCenter defaultCenter] removeObserver:self];
  121. }
  122. @end