WFCUFriendRequestTableViewCell.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // FriendRequestTableViewCell.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/23.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUFriendRequestTableViewCell.h"
  9. #import <SDWebImage/SDWebImage.h>
  10. #import <WFChatClient/WFCChatClient.h>
  11. #import "WFCUConfigManager.h"
  12. #import "UIFont+YH.h"
  13. #import "UIColor+YH.h"
  14. #import "WFCUImage.h"
  15. @interface WFCUFriendRequestTableViewCell()
  16. @property (nonatomic, strong)UIImageView *portraitView;
  17. @property (nonatomic, strong)UILabel *nameLabel;
  18. @property (nonatomic, strong)UILabel *reasonLabel;
  19. @property (nonatomic, strong)UIButton *acceptBtn;
  20. @end
  21. @implementation WFCUFriendRequestTableViewCell
  22. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  23. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  24. if (self) {
  25. [self initSubViews];
  26. }
  27. return self;
  28. }
  29. - (void)awakeFromNib {
  30. [super awakeFromNib];
  31. // Initialization code
  32. [self initSubViews];
  33. }
  34. - (void)initSubViews {
  35. for (UIView *view in self.contentView.subviews) {
  36. [view removeFromSuperview];
  37. }
  38. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  39. self.separatorInset = UIEdgeInsetsMake(0, 76, 0, 0);
  40. self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 10, 40, 40)];
  41. self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(16 + 40 + 20,11, width - 128, 16)];
  42. self.nameLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:15];
  43. self.nameLabel.textColor = [UIColor colorWithHexString:@"0x1d1d1d"];
  44. self.reasonLabel = [[UILabel alloc] initWithFrame:CGRectMake(16 + 40 + 20, 11 + 15 + 6, width - 128, 14)];
  45. self.reasonLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:12];
  46. self.reasonLabel.textColor = [UIColor colorWithHexString:@"0xb3b3b3"];
  47. self.acceptBtn = [[UIButton alloc] initWithFrame:CGRectMake(width - (46 + 16), 16, 46, 28)];
  48. [self.acceptBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  49. [self.acceptBtn setTitle:WFCString(@"Accept") forState:UIControlStateNormal];
  50. [self.acceptBtn setBackgroundColor:[UIColor colorWithHexString:@"0x4764DC"]];
  51. self.acceptBtn.layer.cornerRadius = 4.f;
  52. self.acceptBtn.layer.masksToBounds = YES;
  53. [self.acceptBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  54. self.acceptBtn.titleLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:12];
  55. [self.contentView addSubview:self.portraitView];
  56. [self.contentView addSubview:self.nameLabel];
  57. [self.contentView addSubview:self.reasonLabel];
  58. [self.contentView addSubview:self.acceptBtn];
  59. [self.acceptBtn addTarget:self action:@selector(onAddBtn:) forControlEvents:UIControlEventTouchDown];
  60. self.selectionStyle = UITableViewCellSelectionStyleNone;
  61. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  62. }
  63. - (void)onAddBtn:(id)sender {
  64. [self.delegate onAcceptBtn:self.friendRequest.target];
  65. }
  66. - (void)onUserInfoUpdated:(NSNotification *)notification {
  67. NSArray<WFCCUserInfo *> *userInfoList = notification.userInfo[@"userInfoList"];
  68. for (WFCCUserInfo *userInfo in userInfoList) {
  69. if ([self.friendRequest.target isEqualToString:userInfo.userId]) {
  70. self.friendRequest = _friendRequest;
  71. break;
  72. }
  73. }
  74. }
  75. - (void)setFriendRequest:(WFCCFriendRequest *)friendRequest {
  76. _friendRequest = friendRequest;
  77. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:friendRequest.target refresh:NO];
  78. [self.portraitView sd_setImageWithURL:[NSURL URLWithString:[userInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage: [WFCUImage imageNamed:@"PersonalChat"]];
  79. self.nameLabel.text = userInfo.displayName;
  80. if([WFCCUtilities isExternalTarget:friendRequest.target]) {
  81. NSString *domainId = [WFCCUtilities getExternalDomain:friendRequest.target];
  82. self.nameLabel.attributedText = [WFCCUtilities getExternal:domainId withName:self.nameLabel.text withColor:[WFCUConfigManager globalManager].externalNameColor withSize:12];
  83. }
  84. self.reasonLabel.text = friendRequest.reason;
  85. BOOL expired = NO;
  86. NSDate *date = [[NSDate alloc] init];
  87. if (date.timeIntervalSince1970*1000 - friendRequest.timestamp > 7 * 24 * 60 * 60 * 1000) {
  88. expired = YES;
  89. }
  90. if (friendRequest.status == 0 && !expired) {
  91. [self.acceptBtn setTitle:WFCString(@"Accept") forState:UIControlStateNormal];
  92. [self.acceptBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  93. [self.acceptBtn setBackgroundColor:[UIColor colorWithHexString:@"0x4764DC"]];
  94. [self.acceptBtn setEnabled:YES];
  95. } else if (friendRequest.status == 1) {
  96. [self.acceptBtn setTitle:WFCString(@"Accepted") forState:UIControlStateNormal];
  97. [self.acceptBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  98. [self.acceptBtn setBackgroundColor:self.backgroundColor];
  99. [self.acceptBtn setEnabled:NO];
  100. } else if (friendRequest.status == 2) {
  101. [self.acceptBtn setTitle:WFCString(@"Rejected") forState:UIControlStateNormal];
  102. [self.acceptBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  103. [self.acceptBtn setBackgroundColor:self.backgroundColor];
  104. [self.acceptBtn setEnabled:NO];
  105. } else { //expired
  106. [self.acceptBtn setTitle:WFCString(@"Expired") forState:UIControlStateNormal];
  107. [self.acceptBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  108. [self.acceptBtn setBackgroundColor:self.backgroundColor];
  109. [self.acceptBtn setEnabled:NO];
  110. }
  111. }
  112. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  113. [super setSelected:selected animated:animated];
  114. // Configure the view for the selected state
  115. }
  116. - (void)dealloc {
  117. [[NSNotificationCenter defaultCenter] removeObserver:self];
  118. }
  119. @end