Files
yinmeng-ios/xplan-ios/Main/Message/View/Session/Content/MessageContentGuildView.m
2022-05-13 16:12:18 +08:00

410 lines
15 KiB
Objective-C

//
// MessageContentGuildView.m
// xplan-ios
//
// Created by 冯硕 on 2022/4/18.
//
#import "MessageContentGuildView.h"
///Third
#import <Masonry/Masonry.h>
#import <YYText/YYText.h>
#import "HttpRequestHelper.h"
#import "AttachMentModel.h"
#import "GuildMessageModel.h"
#import "NSObject+MJExtension.h"
#import "ThemeColor.h"
#import "XCHUDTool.h"
#import "AccountInfoStorage.h"
#import "XPMineGuildViewController.h"
#import "XCCurrentVCStackManager.h"
#define MESSAGE_MAX_WIDTH 230
#define MESSAGE_TEXT_PADDING 10
@interface MessageContentGuildView ()
///总的stackView
@property (nonatomic,strong) UIStackView *stackView;
///内容的view
@property (nonatomic,strong) UIView * contentView;
///显示标题
@property (nonatomic,strong) UILabel *titleLabel;
///显示内容
@property (nonatomic,strong) YYLabel *contentLabel;
///显示操作按钮
@property (nonatomic,strong) UIStackView *controlStackView;
///拒绝
@property (nonatomic,strong) UIButton *rejectButton;
///状态
@property (nonatomic,strong) UIButton *resultButton;
///同意
@property (nonatomic,strong) UIButton *agreeButton;
///消息的信息
@property (nonatomic,strong) GuildMessageModel *messageInfo;
@property (nonatomic,strong) AttachmentModel *attachment;
@property (nonatomic,strong) NIMMessage *message;
@end
@implementation MessageContentGuildView
+ (CGFloat)measureHeight:(NIMMessage *)message {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
AttachmentModel * attach = obj.attachment;
GuildMessageModel * info = [GuildMessageModel modelWithDictionary:attach.data];
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]init];
GuildMessageLayoutModel *layout = info.layout;
for (GuildMessageLayoutInfoModel *params in layout.contents) {
if (params.content.length > 0) {
if ([params.content containsString:@"/r/n"]) {
params.content = @"\r\n";
}
NSMutableAttributedString *subAttr = [[NSMutableAttributedString alloc]initWithString:params.content];
if (params.fontSize > 0) {
[subAttr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:params.fontSize weight:params.fontBold?UIFontWeightBold:UIFontWeightRegular] range:NSMakeRange(0, subAttr.length)];
}
if (params.fontColor.length > 0) {
[subAttr addAttribute:NSForegroundColorAttributeName value:[ThemeColor colorWithHexString:params.fontColor] range:NSMakeRange(0, subAttr.length)];
}
[attr appendAttributedString:subAttr];
}
}
attr.yy_lineSpacing = 5;
CGSize maxSize = CGSizeMake(MESSAGE_MAX_WIDTH - MESSAGE_TEXT_PADDING * 2, CGFLOAT_MAX);
YYTextLayout * contentLayout = [YYTextLayout layoutWithContainerSize:maxSize text:attr];
///顶部的距离 20 title的高度15 title到content的高度
CGFloat contentHeight = contentLayout.textBoundingSize.height + 5 + 20 + 15 + 15 + MESSAGE_TEXT_PADDING;
///44 底部操作栏的高度
BOOL isCommonNotice = (attach.second == Custom_Message_Sub_Hall_Notice || attach.second == Custom_Message_Sub_Hall_Become_Hall);
if (!isCommonNotice) {
contentHeight += (10+44);
}
return contentHeight + CONTENT_PADDING_V_TOTAL;
}
- (void)initSubViews {
[super initSubViews];
[self addSubview:self.backView];
[self.backView addSubview:self.stackView];
[self.stackView addArrangedSubview:self.contentView];
[self.stackView addArrangedSubview:self.controlStackView];
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.contentLabel];
///操作按钮
[self.controlStackView addArrangedSubview:self.rejectButton];
[self.controlStackView addArrangedSubview:self.resultButton];
[self.controlStackView addArrangedSubview:self.agreeButton];
}
- (void)initSubViewConstraints {
[super initSubViewConstraints];
[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(MESSAGE_MAX_WIDTH);
make.bottom.mas_equalTo(self.stackView).offset(MESSAGE_TEXT_PADDING);
}];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(self.backView);
}];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(MESSAGE_MAX_WIDTH);
make.height.mas_equalTo(100);
}];
[self.controlStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(44);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.contentView);
make.top.mas_equalTo(self.contentView).offset(20);
}];
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.left.mas_equalTo(self.contentView).inset(MESSAGE_TEXT_PADDING);
make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(MESSAGE_TEXT_PADDING);
make.bottom.mas_equalTo(self.contentView);
}];
CGFloat itemWidth = (MESSAGE_MAX_WIDTH - 15 * 2 - 10) / 2.0;
[self.agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(itemWidth);
make.height.mas_equalTo(30);
}];
[self.rejectButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(self.agreeButton);
}];
[self.resultButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(30);
}];
}
- (void)render:(NIMMessage *)message {
self.message = message;
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
AttachmentModel * attach = obj.attachment;
self.attachment = attach;
GuildMessageModel * info;
if (message.localExt) {
info = [GuildMessageModel modelWithDictionary:message.localExt];
} else {
info = [GuildMessageModel modelWithDictionary:attach.data];
}
self.messageInfo = info;
self.titleLabel.text = info.layout.title.content;
self.titleLabel.font = [UIFont systemFontOfSize:info.layout.title.fontSize weight:info.layout.title.fontBold?UIFontWeightBold:UIFontWeightRegular];
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]init];
GuildMessageLayoutModel *layout = info.layout;
for (GuildMessageLayoutInfoModel *params in layout.contents) {
if (params.content.length > 0) {
if ([params.content containsString:@"/r/n"]) {
params.content = @"\r\n";
}
NSMutableAttributedString *subAttr = [[NSMutableAttributedString alloc]initWithString:params.content];
if (params.fontSize > 0) {
[subAttr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:params.fontSize weight:params.fontBold?UIFontWeightBold:UIFontWeightRegular] range:NSMakeRange(0, subAttr.length)];
}
if (params.fontColor.length > 0) {
[subAttr addAttribute:NSForegroundColorAttributeName value:[ThemeColor colorWithHexString:params.fontColor] range:NSMakeRange(0, subAttr.length)];
}
if (params.routertype > 0) {
}
[attr appendAttributedString:subAttr];
}
}
attr.yy_lineSpacing = 5;
self.contentLabel.attributedText = attr;
self.contentLabel.textAlignment = NSTextAlignmentCenter;
CGSize maxSize = CGSizeMake(MESSAGE_MAX_WIDTH - MESSAGE_TEXT_PADDING * 2, CGFLOAT_MAX);
YYTextLayout * contentLayout = [YYTextLayout layoutWithContainerSize:maxSize text:attr];
CGFloat contentHeight = contentLayout.textBoundingSize.height + 5;
///顶部的距离 20 title的高度15 title到content的高度15
[self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(contentHeight + 20+ 15 + 15);
}];
BOOL isCommonNotice = (attach.second == Custom_Message_Sub_Hall_Notice || attach.second == Custom_Message_Sub_Hall_Become_Hall);
if (!isCommonNotice) {
self.controlStackView.hidden = NO;
if (info.msgStatus == 0) {
info.msgStatus = MessageGuildState_Untreated;
}
[self handletResultControll:info second:attach.second];
} else {
self.controlStackView.hidden = YES;
[self.rejectButton setAttributedTitle:nil forState:UIControlStateNormal];
}
}
- (void)handletResultControll:(GuildMessageModel *)info second:(Custom_Noti_Sub_HALL)second {
self.rejectButton.hidden = YES;
self.agreeButton.hidden = YES;
self.resultButton.hidden= YES;
MessageGuildState state = info.msgStatus;
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]init];
switch (state) {
case MessageGuildState_Agree:
{
self.resultButton.hidden = NO;
[attr appendAttributedString:[self creatResultAttribute:@"已同意" textColor:UIColorRGBAlpha(0x09BB07, 0.6)]];
}
break;
case MessageGuildState_Reject:
{
self.resultButton.hidden= NO;
[attr appendAttributedString:[self creatResultAttribute:@"已拒绝" textColor:UIColorRGBAlpha(0xFF3852, 0.6)]];
}
break;
case MessageGuildState_OutData:
{
self.resultButton.hidden= NO;
[attr appendAttributedString:[self creatResultAttribute:@"消息已过期" textColor:UIColorRGBAlpha(0x333333, 0.6)]];
}
break;
case MessageGuildState_Processed:
{
self.resultButton.hidden= NO;
[attr appendAttributedString:[self creatResultAttribute:@"消息已处理" textColor:UIColorFromRGB(0xC6C6E9)]];
}
break;
case MessageGuildState_Untreated:
{
self.agreeButton.hidden = NO;
self.rejectButton.hidden = second == Custom_Message_Sub_Hall_Apply_Exit;
}
break;
default:
break;
}
if (attr.length > 0) {
[self.resultButton setAttributedTitle:attr forState:UIControlStateNormal];
}
}
- (NSAttributedString *)creatResultAttribute:(NSString *)message textColor:(UIColor *)textColor {
if (message.length <=0) {
message = @"";
}
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:message];
[attr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:textColor} range:NSMakeRange(0, attr.length)];
return attr;
}
#pragma mark - Event Response
- (void)controllButtonAction:(UIButton *)sender {
NSRange recordRange = [self.messageInfo.url rangeOfString:@"recordId="];
NSString *recordId;
if (!NSEqualRanges(recordRange, NSMakeRange(NSNotFound, 0))) {
if (self.messageInfo.url.length > recordRange.location+recordRange.length) {
recordId = [self.messageInfo.url substringFromIndex:recordRange.location+recordRange.length];
}
}
if (recordId.length > 0) {
NSMutableDictionary * params = [NSMutableDictionary dictionary];
[params setObject:recordId forKey:@"recordId"];
[params setObject:[AccountInfoStorage instance].getUid forKey:@"uid"];
///0拒绝 1同意
NSString * type = sender.tag == 1000? @"0" : @"1";
if (self.attachment.second == Custom_Message_Sub_Hall_Apply_Join || self.attachment.second == Custom_Message_Sub_Hall_Manager_Invite || self.attachment.second == Custom_Message_Sub_Hall_Apply_Exit) {
if (self.attachment.second != Custom_Message_Sub_Hall_Apply_Exit) {
[params setObject:type forKey:@"type"];
}
[XCHUDTool showLoading];
[HttpRequestHelper POST:self.messageInfo.url params:params success:^(BaseModel * _Nonnull data) {
[XCHUDTool hideHUD];
NSString *code = [NSString stringWithFormat:@"%@", data.data[@"code"]];
BOOL isExpired = [code isEqualToString:@"90121"];
BOOL isHandled = [code isEqualToString:@"90122"];
MessageGuildState state = MessageGuildState_Untreated;
if (isExpired) {
state = MessageGuildState_OutData;
} else if(isHandled) {
state = MessageGuildState_Processed;
} else {
state = sender.tag == 1000 ? MessageGuildState_Reject : MessageGuildState_Agree;
}
self.messageInfo.msgStatus = state;
self.message.localExt = [self.messageInfo model2dictionary];
[[NIMSDK sharedSDK].conversationManager updateMessage:self.message forSession:self.message.session completion:^(NSError * _Nullable error) {
if (self.delegate && [self.delegate respondsToSelector:@selector(updateMessageSuccess:)]) {
[self.delegate updateMessageSuccess:self.message];
}
}];
} failure:^(NSInteger resCode, NSString * _Nonnull message) {
[XCHUDTool hideHUD];
[XCHUDTool showErrorWithMessage:message];
}];
}
}
}
- (void)contentLableTapRecognizer {
if (self.messageInfo.routerType == 22 && self.messageInfo.routerValue > 0) {
XPMineGuildViewController * guildVC= [[XPMineGuildViewController alloc] init];
guildVC.ownerUid = [AccountInfoStorage instance].getUid;
guildVC.guildId = [NSString stringWithFormat:@"%ld", self.messageInfo.routerValue];
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:guildVC animated:YES];
}
}
#pragma mark - Getters And Setters
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisVertical;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentCenter;
_stackView.spacing = 10;
}
return _stackView;
}
- (UIView *)contentView {
if (!_contentView) {
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = [UIColor clearColor];
}
return _contentView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.textAlignment= NSTextAlignmentCenter;
_titleLabel.font = [UIFont systemFontOfSize:14];
_titleLabel.textColor = [ThemeColor mainTextColor];
}
return _titleLabel;
}
- (YYLabel *)contentLabel {
if (!_contentLabel) {
_contentLabel = [[YYLabel alloc] init];
_contentLabel.numberOfLines = 0;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentLableTapRecognizer)];
[_contentLabel addGestureRecognizer:tap];
}
return _contentLabel;
}
- (UIStackView *)controlStackView {
if (!_controlStackView) {
_controlStackView = [[UIStackView alloc] init];
_controlStackView.axis = UILayoutConstraintAxisHorizontal;
_controlStackView.distribution = UIStackViewDistributionFill;
_controlStackView.alignment = UIStackViewAlignmentCenter;
_controlStackView.spacing = 10;
}
return _controlStackView;
}
- (UIButton *)rejectButton {
if (!_rejectButton) {
_rejectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_rejectButton setTitle:@"拒绝" forState:UIControlStateNormal];
_rejectButton.titleLabel.font = [UIFont systemFontOfSize:13];
[_rejectButton setTitleColor:[ThemeColor appMainColor] forState:UIControlStateNormal];
_rejectButton.layer.masksToBounds = YES;
_rejectButton.layer.cornerRadius = 15;
_rejectButton.layer.borderColor = [ThemeColor appMainColor].CGColor;
_rejectButton.layer.borderWidth = 1;
[_rejectButton setBackgroundColor:[ThemeColor appCellBackgroundColor]];
_rejectButton.tag = 1000;
[_rejectButton addTarget:self action:@selector(controllButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _rejectButton;
}
- (UIButton *)resultButton {
if (!_resultButton) {
_resultButton = [UIButton buttonWithType:UIButtonTypeCustom];
}
return _resultButton;
}
- (UIButton *)agreeButton {
if (!_agreeButton) {
_agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_agreeButton setTitle:@"同意" forState:UIControlStateNormal];
_agreeButton.titleLabel.font = [UIFont systemFontOfSize:13];
[_agreeButton setTitleColor:[ThemeColor mainTextColor] forState:UIControlStateNormal];
_agreeButton.layer.masksToBounds = YES;
_agreeButton.layer.cornerRadius = 15;
[_agreeButton setBackgroundColor:[ThemeColor appMainColor]];
_agreeButton.tag = 1001;
[_agreeButton addTarget:self action:@selector(controllButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _agreeButton;
}
@end