Files
peko-ios/YuMi/Modules/YMMessage/View/Session/Content/MessageContentTweetView.m
2024-02-21 10:18:59 +08:00

247 lines
9.8 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MessageContentTweetView.m
// YUMI
//
// Created by YUMI on 2022/4/28.
//
#import "MessageContentTweetView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "DJDKMIMOMColor.h"
#import "NetImageView.h"
#import "NSObject+MJExtension.h"
#import "XCCurrentVCStackManager.h"
#import "YUMIConstant.h"
///Model
#import "MessageTweetModel.h"
#import "ContentTweetModel.h"
///View
#import "XPWebViewController.h"
#import "XPRoomViewController.h"
@interface MessageContentTweetView ()
///显示图片
@property (nonatomic,strong) NetImageView *logoImageView;
///显示名字
@property (nonatomic,strong) UILabel *titleLabel;
///分割线
@property (nonatomic,strong) UIView * lineView;
///显示副标题
@property (nonatomic,strong) UILabel *subTitleLabel;
///查看详情
@property (nonatomic,strong) UIButton *checkButton;
///是否为新消息
@property (nonatomic, strong) UIImageView *isNewImageView;
///新闻的模型
@property (nonatomic,strong) ContentTweetModel *tweetModel;
///消息ID用于记录是否已读
@property (nonatomic, copy) NSString *messageId;
@end
@implementation MessageContentTweetView
- (void)render:(MessageBaseModel *)model1 {
MessageTweetModel *obj = (MessageTweetModel *)model1;
ContentTweetModel * model = obj.tweetInfo;
self.tweetModel = model;
self.messageId = model1.message.messageId;
self.logoImageView.imageUrl = model.picUrl;
self.titleLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:model.title attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:16] color:[DJDKMIMOMColor mainTextColor]]];
self.subTitleLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:model.desc attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:13] color:[DJDKMIMOMColor secondTextColor]]];
CGFloat oneHeight = [YMLocalizedString(@"MessageContentTweetView0") boundingRectWithSize:CGSizeMake(240, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:16] color:[DJDKMIMOMColor mainTextColor]] context:nil].size.height + 2.5;
CGFloat titleHeight = [model.title boundingRectWithSize:CGSizeMake(240, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:16] color:[DJDKMIMOMColor mainTextColor]] context:nil].size.height;
if (titleHeight <= oneHeight * 2) {
[self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(titleHeight + 5);
}];
} else{
[self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(oneHeight * 2);
}];
}
CGFloat desHeight = [model.desc boundingRectWithSize:CGSizeMake(240, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:13] color:[DJDKMIMOMColor mainTextColor]] context:nil].size.height + 10;
[self.subTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(desHeight);
}];
NSArray *redPacketHistory = [[NSUserDefaults standardUserDefaults] objectForKey:kTuWenMessageHistory];
if ( [redPacketHistory containsObject: model1.message.messageId]) {
self.isNewImageView.hidden = YES;
[self.isNewImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.backView);
make.centerY.equalTo(self.titleLabel);
make.size.mas_equalTo(CGSizeMake(0, 10));
}];
} else {
self.isNewImageView.hidden = NO;
[self.isNewImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.backView);
make.centerY.equalTo(self.titleLabel);
make.size.mas_equalTo(CGSizeMake(24, 10));
}];
}
}
- (NSDictionary<NSAttributedStringKey, id> *)messageTextAttibutes:(UIFont *)font color:(UIColor *)color {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:2.5];
return @{
NSFontAttributeName:font,
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName:color
};
}
- (void)initSubViews {
[super initSubViews];
[self.backView addSubview:self.titleLabel];
[self.backView addSubview:self.logoImageView];
[self.backView addSubview:self.lineView];
[self.backView addSubview:self.subTitleLabel];
[self.backView addSubview:self.checkButton];
[self.backView addSubview:self.isNewImageView];
}
- (void)initSubViewConstraints {
[super initSubViewConstraints];
[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(280);
make.bottom.mas_equalTo(self.checkButton);
}];
[self.isNewImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.backView);
make.centerY.equalTo(self.titleLabel);
make.size.mas_equalTo(CGSizeMake(24, 10));
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.backView);
make.right.equalTo(self.isNewImageView.mas_left);
make.top.mas_equalTo(self.backView);
make.height.mas_equalTo(18);
}];
[self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.backView);
make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
make.height.mas_equalTo(87);
}];
[self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.backView);
make.top.mas_equalTo(self.logoImageView.mas_bottom).offset(10);
make.height.mas_equalTo(18);
}];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.backView);
make.height.mas_equalTo(0.5);
make.top.mas_equalTo(self.subTitleLabel.mas_bottom).offset(5);
}];
[self.checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.backView);
make.top.mas_equalTo(self.lineView.mas_bottom).offset(10);
make.height.mas_equalTo(20);
}];
}
#pragma mark - Event Response
- (void)checkButtonAction:(UIButton *)sender {
// 如果打开红包成功 则记录当前红包状态
NSArray *history = [[NSUserDefaults standardUserDefaults] objectForKey:kTuWenMessageHistory];
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:history];
if (![tempArray containsObject:self.messageId]) {
[tempArray addObject:self.messageId];
[[NSUserDefaults standardUserDefaults] setObject:tempArray forKey:kTuWenMessageHistory];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if (self.tweetModel.routerType == 1) {
[XPRoomViewController openRoom:self.tweetModel.routerValue viewController:[XCCurrentVCStackManager shareManager].getCurrentVC];
} else if (self.tweetModel.routerType == 2) {
XPWebViewController * webVC = [[XPWebViewController alloc] init];
webVC.url = self.tweetModel.routerValue;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:webVC animated:YES];
} else {
if ([self.tweetModel.webUrl hasPrefix:@"http"]) {
XPWebViewController * webVC = [[XPWebViewController alloc] init];
webVC.url = self.tweetModel.webUrl;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:webVC animated:YES];
}
}
}
#pragma mark - Getters And Setters
- (NetImageView *)logoImageView {
if (!_logoImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_logoImageView = [[NetImageView alloc] initWithConfig:config];
_logoImageView.layer.masksToBounds = YES;
_logoImageView.layer.cornerRadius = 10;
_logoImageView.contentMode = UIViewContentModeScaleAspectFill;
_logoImageView.layer.masksToBounds = YES;
}
return _logoImageView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.numberOfLines = 2;
_titleLabel.preferredMaxLayoutWidth = 240;
}
return _titleLabel;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = [DJDKMIMOMColor dividerColor];
}
return _lineView;
}
- (UILabel *)subTitleLabel {
if (!_subTitleLabel) {
_subTitleLabel = [[UILabel alloc] init];
_subTitleLabel.numberOfLines = 0;
_subTitleLabel.preferredMaxLayoutWidth = 240;
}
return _subTitleLabel;
}
- (UIButton *)checkButton {
if (!_checkButton) {
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setTitle:YMLocalizedString(@"MessageContentTweetView1") forState:UIControlStateNormal];
[_checkButton setTitleColor:UIColorFromRGB(0x4C5AF1) forState:UIControlStateNormal];
_checkButton.titleLabel.font = [UIFont systemFontOfSize:14];
[_checkButton addTarget:self action:@selector(checkButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _checkButton;
}
- (UIImageView *)isNewImageView {
if (!_isNewImageView) {
_isNewImageView = [[UIImageView alloc] init];
_isNewImageView.image = [UIImage imageNamed:@"gift_type_newIcon"];
_isNewImageView.hidden = YES;
}
return _isNewImageView;
}
@end