
问题: - keyWindow 在 iOS 13+ 被废弃 - 使用 kWindow 会产生 deprecation warning - 不支持 multi-scene 应用 修复: - 添加 getKeyWindow 辅助方法 - iOS 13+: 使用 connectedScenes 获取活跃 window - iOS 13-: 使用旧的 keyWindow(suppress warning) - 确保兼容性和 multi-scene 支持 代码改进: - 使用 @available(iOS 13.0, *) 条件编译 - 使用 #pragma clang diagnostic 抑制旧 API 警告 - 遍历所有 scene 找到前台活跃的 window 现在可以在 iOS 13+ 上无警告编译和运行。
331 lines
11 KiB
Objective-C
331 lines
11 KiB
Objective-C
//
|
||
// NewMomentCell.m
|
||
// YuMi
|
||
//
|
||
// Created by AI on 2025-10-09.
|
||
// Copyright © 2025 YuMi. All rights reserved.
|
||
//
|
||
|
||
#import "NewMomentCell.h"
|
||
#import "MomentsInfoModel.h"
|
||
#import "AccountInfoStorage.h"
|
||
#import "Api+Moments.h"
|
||
#import <Masonry/Masonry.h>
|
||
|
||
@interface NewMomentCell ()
|
||
|
||
// MARK: - UI Components
|
||
|
||
/// 卡片容器
|
||
@property (nonatomic, strong) UIView *cardView;
|
||
|
||
/// 头像
|
||
@property (nonatomic, strong) UIImageView *avatarImageView;
|
||
|
||
/// 用户名
|
||
@property (nonatomic, strong) UILabel *nameLabel;
|
||
|
||
/// 时间标签
|
||
@property (nonatomic, strong) UILabel *timeLabel;
|
||
|
||
/// 内容标签
|
||
@property (nonatomic, strong) UILabel *contentLabel;
|
||
|
||
/// 图片容器(可选)
|
||
@property (nonatomic, strong) UIView *imagesContainer;
|
||
|
||
/// 底部操作栏
|
||
@property (nonatomic, strong) UIView *actionBar;
|
||
|
||
/// 点赞按钮
|
||
@property (nonatomic, strong) UIButton *likeButton;
|
||
|
||
/// 评论按钮
|
||
@property (nonatomic, strong) UIButton *commentButton;
|
||
|
||
/// 分享按钮
|
||
@property (nonatomic, strong) UIButton *shareButton;
|
||
|
||
/// 当前数据模型
|
||
@property (nonatomic, strong) MomentsInfoModel *currentModel;
|
||
|
||
@end
|
||
|
||
@implementation NewMomentCell
|
||
|
||
// MARK: - Lifecycle
|
||
|
||
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
||
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
|
||
self.selectionStyle = UITableViewCellSelectionStyleNone;
|
||
self.backgroundColor = [UIColor clearColor];
|
||
[self setupUI];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
// MARK: - Setup UI
|
||
|
||
- (void)setupUI {
|
||
// 卡片容器(圆角矩形 + 阴影)
|
||
[self.contentView addSubview:self.cardView];
|
||
[self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self.contentView).offset(15);
|
||
make.right.equalTo(self.contentView).offset(-15);
|
||
make.top.equalTo(self.contentView).offset(8);
|
||
make.bottom.equalTo(self.contentView).offset(-8);
|
||
}];
|
||
|
||
// 头像(圆角矩形,不是圆形!)
|
||
[self.cardView addSubview:self.avatarImageView];
|
||
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self.cardView).offset(15);
|
||
make.top.equalTo(self.cardView).offset(15);
|
||
make.size.mas_equalTo(CGSizeMake(40, 40));
|
||
}];
|
||
|
||
// 用户名
|
||
[self.cardView addSubview:self.nameLabel];
|
||
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self.avatarImageView.mas_right).offset(10);
|
||
make.top.equalTo(self.avatarImageView);
|
||
make.right.equalTo(self.cardView).offset(-15);
|
||
}];
|
||
|
||
// 时间
|
||
[self.cardView addSubview:self.timeLabel];
|
||
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self.nameLabel);
|
||
make.bottom.equalTo(self.avatarImageView);
|
||
make.right.equalTo(self.cardView).offset(-15);
|
||
}];
|
||
|
||
// 内容
|
||
[self.cardView addSubview:self.contentLabel];
|
||
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self.cardView).offset(15);
|
||
make.right.equalTo(self.cardView).offset(-15);
|
||
make.top.equalTo(self.avatarImageView.mas_bottom).offset(12);
|
||
}];
|
||
|
||
// 底部操作栏
|
||
[self.cardView addSubview:self.actionBar];
|
||
[self.actionBar mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.right.equalTo(self.cardView);
|
||
make.top.equalTo(self.contentLabel.mas_bottom).offset(15);
|
||
make.height.mas_equalTo(50);
|
||
make.bottom.equalTo(self.cardView).offset(-8);
|
||
}];
|
||
|
||
// 点赞按钮
|
||
[self.actionBar addSubview:self.likeButton];
|
||
[self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self.actionBar).offset(15);
|
||
make.centerY.equalTo(self.actionBar);
|
||
make.width.mas_greaterThanOrEqualTo(60);
|
||
}];
|
||
|
||
// 评论按钮
|
||
[self.actionBar addSubview:self.commentButton];
|
||
[self.commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.centerX.equalTo(self.actionBar);
|
||
make.centerY.equalTo(self.actionBar);
|
||
make.width.mas_greaterThanOrEqualTo(60);
|
||
}];
|
||
|
||
// 分享按钮
|
||
[self.actionBar addSubview:self.shareButton];
|
||
[self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.right.equalTo(self.actionBar).offset(-15);
|
||
make.centerY.equalTo(self.actionBar);
|
||
make.width.mas_greaterThanOrEqualTo(60);
|
||
}];
|
||
}
|
||
|
||
// MARK: - Public Methods
|
||
|
||
- (void)configureWithModel:(MomentsInfoModel *)model {
|
||
self.currentModel = model;
|
||
|
||
// 配置用户名
|
||
self.nameLabel.text = model.nick ?: @"匿名用户";
|
||
|
||
// 配置时间
|
||
self.timeLabel.text = model.publishTime;
|
||
|
||
// 配置内容
|
||
self.contentLabel.text = model.content ?: @"";
|
||
|
||
// 配置点赞数
|
||
[self.likeButton setTitle:[NSString stringWithFormat:@"👍 %ld", (long)model.likeCount] forState:UIControlStateNormal];
|
||
|
||
// 配置评论数
|
||
[self.commentButton setTitle:[NSString stringWithFormat:@"💬 %ld", (long)model.commentCount] forState:UIControlStateNormal];
|
||
|
||
// 配置分享
|
||
[self.shareButton setTitle:@"🔗 分享" forState:UIControlStateNormal];
|
||
|
||
// TODO: 加载头像图片
|
||
// [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
|
||
}
|
||
|
||
/// 格式化时间戳为相对时间
|
||
- (NSString *)formatTimeInterval:(NSInteger)timestamp {
|
||
if (timestamp <= 0) return @"刚刚";
|
||
|
||
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] - timestamp / 1000.0;
|
||
|
||
if (interval < 60) {
|
||
return @"刚刚";
|
||
} else if (interval < 3600) {
|
||
return [NSString stringWithFormat:@"%.0f分钟前", interval / 60];
|
||
} else if (interval < 86400) {
|
||
return [NSString stringWithFormat:@"%.0f小时前", interval / 3600];
|
||
} else if (interval < 604800) {
|
||
return [NSString stringWithFormat:@"%.0f天前", interval / 86400];
|
||
} else {
|
||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||
formatter.dateFormat = @"yyyy-MM-dd";
|
||
return [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timestamp / 1000.0]];
|
||
}
|
||
}
|
||
|
||
// MARK: - Actions
|
||
|
||
- (void)onLikeButtonTapped {
|
||
if (!self.currentModel) return;
|
||
|
||
NSLog(@"[NewMomentCell] 点赞动态: %@", self.currentModel.dynamicId);
|
||
|
||
NSString *uid = [[AccountInfoStorage instance] getUid];
|
||
NSString *dynamicId = self.currentModel.dynamicId;
|
||
NSString *status = self.currentModel.isLike ? @"0" : @"1"; // 0=取消,1=点赞
|
||
NSString *likedUid = self.currentModel.uid;
|
||
NSString *worldId = self.currentModel.worldId > 0 ? @(self.currentModel.worldId).stringValue : @"";
|
||
|
||
[Api momentsLike:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
|
||
if (code == 200) {
|
||
// 更新点赞状态
|
||
self.currentModel.isLike = !self.currentModel.isLike;
|
||
NSInteger likeCount = [self.currentModel.likeCount integerValue];
|
||
likeCount += self.currentModel.isLike ? 1 : -1;
|
||
self.currentModel.likeCount = @(likeCount).stringValue;
|
||
|
||
// 更新 UI
|
||
[self.likeButton setTitle:[NSString stringWithFormat:@"👍 %ld", (long)self.currentModel.likeCount] forState:UIControlStateNormal];
|
||
|
||
NSLog(@"[NewMomentCell] 点赞成功");
|
||
} else {
|
||
NSLog(@"[NewMomentCell] 点赞失败: %@", msg);
|
||
}
|
||
} dynamicId:dynamicId uid:uid status:status likedUid:likedUid worldId:worldId];
|
||
}
|
||
|
||
- (void)onCommentButtonTapped {
|
||
NSLog(@"[NewMomentCell] 评论");
|
||
// TODO: 实现评论逻辑
|
||
}
|
||
|
||
- (void)onShareButtonTapped {
|
||
NSLog(@"[NewMomentCell] 分享");
|
||
// TODO: 实现分享逻辑
|
||
}
|
||
|
||
// MARK: - Lazy Loading
|
||
|
||
- (UIView *)cardView {
|
||
if (!_cardView) {
|
||
_cardView = [[UIView alloc] init];
|
||
_cardView.backgroundColor = [UIColor whiteColor];
|
||
_cardView.layer.cornerRadius = 12; // 圆角
|
||
_cardView.layer.shadowColor = [UIColor blackColor].CGColor;
|
||
_cardView.layer.shadowOffset = CGSizeMake(0, 2);
|
||
_cardView.layer.shadowOpacity = 0.1;
|
||
_cardView.layer.shadowRadius = 8;
|
||
_cardView.layer.masksToBounds = NO;
|
||
}
|
||
return _cardView;
|
||
}
|
||
|
||
- (UIImageView *)avatarImageView {
|
||
if (!_avatarImageView) {
|
||
_avatarImageView = [[UIImageView alloc] init];
|
||
_avatarImageView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
|
||
_avatarImageView.layer.cornerRadius = 8; // 圆角矩形,不是圆形!
|
||
_avatarImageView.layer.masksToBounds = YES;
|
||
_avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
|
||
}
|
||
return _avatarImageView;
|
||
}
|
||
|
||
- (UILabel *)nameLabel {
|
||
if (!_nameLabel) {
|
||
_nameLabel = [[UILabel alloc] init];
|
||
_nameLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
|
||
_nameLabel.textColor = [UIColor colorWithWhite:0.2 alpha:1.0];
|
||
}
|
||
return _nameLabel;
|
||
}
|
||
|
||
- (UILabel *)timeLabel {
|
||
if (!_timeLabel) {
|
||
_timeLabel = [[UILabel alloc] init];
|
||
_timeLabel.font = [UIFont systemFontOfSize:12];
|
||
_timeLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
|
||
}
|
||
return _timeLabel;
|
||
}
|
||
|
||
- (UILabel *)contentLabel {
|
||
if (!_contentLabel) {
|
||
_contentLabel = [[UILabel alloc] init];
|
||
_contentLabel.font = [UIFont systemFontOfSize:15];
|
||
_contentLabel.textColor = [UIColor colorWithWhite:0.3 alpha:1.0];
|
||
_contentLabel.numberOfLines = 0;
|
||
_contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||
}
|
||
return _contentLabel;
|
||
}
|
||
|
||
- (UIView *)actionBar {
|
||
if (!_actionBar) {
|
||
_actionBar = [[UIView alloc] init];
|
||
_actionBar.backgroundColor = [UIColor colorWithWhite:0.98 alpha:1.0];
|
||
}
|
||
return _actionBar;
|
||
}
|
||
|
||
- (UIButton *)likeButton {
|
||
if (!_likeButton) {
|
||
_likeButton = [self createActionButtonWithTitle:@"👍 0"];
|
||
[_likeButton addTarget:self action:@selector(onLikeButtonTapped) forControlEvents:UIControlEventTouchUpInside];
|
||
}
|
||
return _likeButton;
|
||
}
|
||
|
||
- (UIButton *)commentButton {
|
||
if (!_commentButton) {
|
||
_commentButton = [self createActionButtonWithTitle:@"💬 0"];
|
||
[_commentButton addTarget:self action:@selector(onCommentButtonTapped) forControlEvents:UIControlEventTouchUpInside];
|
||
}
|
||
return _commentButton;
|
||
}
|
||
|
||
- (UIButton *)shareButton {
|
||
if (!_shareButton) {
|
||
_shareButton = [self createActionButtonWithTitle:@"🔗 分享"];
|
||
[_shareButton addTarget:self action:@selector(onShareButtonTapped) forControlEvents:UIControlEventTouchUpInside];
|
||
}
|
||
return _shareButton;
|
||
}
|
||
|
||
- (UIButton *)createActionButtonWithTitle:(NSString *)title {
|
||
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
|
||
[button setTitle:title forState:UIControlStateNormal];
|
||
button.titleLabel.font = [UIFont systemFontOfSize:13];
|
||
[button setTitleColor:[UIColor colorWithWhite:0.5 alpha:1.0] forState:UIControlStateNormal];
|
||
return button;
|
||
}
|
||
|
||
@end
|