ChatListCell.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // ChatListCell.m
  3. // AIIM
  4. //
  5. // Created by gan on 2025/4/6.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "ChatListCell.h"
  9. #import "CryptoAES.h"
  10. #import <SDWebImage/UIImageView+WebCache.h>
  11. #import "config.h"
  12. @interface ChatListCell()
  13. @property (weak, nonatomic) IBOutlet UILabel *name;
  14. @property (weak, nonatomic) IBOutlet UILabel *time;
  15. @property (weak, nonatomic) IBOutlet UILabel *lastMsg;
  16. @property (weak, nonatomic) IBOutlet UILabel *countText;
  17. @property (weak, nonatomic) IBOutlet UIImageView *avatar;
  18. @end
  19. @implementation ChatListCell
  20. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  21. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  22. if (self) {
  23. }
  24. return self;
  25. }
  26. - (void)fillWithData:( NSDictionary*)data {
  27. // NSLog(@"fillWithData:%@",data);
  28. NSString *avatar =data[@"avatar"];
  29. if([avatar isKindOfClass:[NSString class]]){
  30. }
  31. else{
  32. avatar = @"";
  33. }
  34. [self.avatar sd_setImageWithURL:[NSURL URLWithString:avatar] placeholderImage:[UIImage imageNamed:@"Avatar"]];
  35. self.name.text = data[@"name"];
  36. NSString *dcontent = data[@"lastMessage"];//[CryptoAES.shareInstance decryptDataL:data[@"lastMessage"]];
  37. self.lastMsg.text = dcontent;
  38. _countText.layer.cornerRadius = 10.0f; // 设置圆角
  39. _countText.layer.masksToBounds = YES; // 防止子视图超出边界
  40. _countText.backgroundColor = globalColor(GCTypeOrangeR);
  41. self.avatar.layer.cornerRadius = 22.f;
  42. NSString *count = data[@"unreadCount"];
  43. NSLog(@"count:%@",count);
  44. if(count.intValue>99){
  45. _countText.text=@"99+";
  46. _countText.alpha = 1;
  47. }
  48. else if(count.intValue==0){
  49. _countText.alpha = 0;
  50. }
  51. else{
  52. _countText.text=[NSString stringWithFormat:@"%@",count];
  53. _countText.alpha = 1;
  54. }
  55. //NSLog(@"lastTime:%@",data[@"lastTime"]);
  56. NSString *timestr =data[@"lastTime"];
  57. if([timestr isKindOfClass:[NSNumber class]]){
  58. _time.text=[self timeF:timestr.longLongValue];
  59. }
  60. else if([timestr isKindOfClass:[NSString class]]){
  61. _time.text=[self timeF:timestr.longLongValue];
  62. }
  63. else{
  64. _time.text=@"";
  65. }
  66. // tell constraints they need updating
  67. [self setNeedsUpdateConstraints];
  68. // update constraints now so we can animate the change
  69. [self updateConstraintsIfNeeded];
  70. [self layoutIfNeeded];
  71. }
  72. -(NSString *)timeF:(NSInteger)time{
  73. //NSLog(@"time:%ld",(long)time);
  74. if(time<=0){
  75. return @"";
  76. }
  77. // 创建日期格式器
  78. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  79. [formatter setDateFormat:@"dd"]; // 设置你想要的日期格式
  80. long long timestamp =(long)time/1000; // 例如:2021-10-01 00:00:00 UTC
  81. // 转换为NSDate
  82. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  83. NSString *dateString = [formatter stringFromDate:date];
  84. NSDate *now = [NSDate date];
  85. NSTimeInterval trt = [now timeIntervalSince1970];
  86. date = [NSDate dateWithTimeIntervalSince1970:trt];
  87. NSString *tdateString = [formatter stringFromDate:date];
  88. NSInteger shijiancha = trt-timestamp;
  89. if(shijiancha<60){
  90. return NSLocalizedString(@"time-oneminint", @"");
  91. }
  92. if(shijiancha>24*3600||dateString.intValue!=tdateString.intValue){
  93. // 假设这是你的时间戳(以秒为单位)
  94. long long timestamp =(long)time/1000; // 例如:2021-10-01 00:00:00 UTC
  95. // 转换为NSDate
  96. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  97. // 创建日期格式器
  98. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  99. [formatter setDateFormat:@"yyyy-MM-dd"]; // 设置你想要的日期格式
  100. // 转换为字符串
  101. NSString *dateString = [formatter stringFromDate:date];
  102. return dateString;
  103. }
  104. else{
  105. NSInteger xiaoshi = shijiancha/3600;
  106. NSInteger fenzhong = shijiancha/60;
  107. if(xiaoshi>0){
  108. // 假设这是你的时间戳(以秒为单位)
  109. long long timestamp =(long)time/1000; // 例如:2021-10-01 00:00:00 UTC
  110. // 转换为NSDate
  111. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  112. // 创建日期格式器
  113. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  114. [formatter setDateFormat:@"HH:mm"]; // 设置你想要的日期格式
  115. // 转换为字符串
  116. NSString *dateString = [formatter stringFromDate:date];
  117. return dateString;
  118. }
  119. else{
  120. return [NSString stringWithFormat:@"%ld%@",(long)fenzhong,NSLocalizedString(@"time-outmin", @"")];
  121. }
  122. }
  123. return @"";
  124. }
  125. @end