//
// hotAskContentCell.m
// ThePaperHD
//
// Created by scar1900 on 15/1/28.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "messageAskContentCell.h"
@interface messageAskContentCell() {
CGFloat answerNumWidth;
}
@property(nonatomic, strong)UILabel *markLabel;
@property(nonatomic, strong)UILabel *timeLabel;
@property(nonatomic, strong)RTLabel *askNumsLabel;
@property(nonatomic, strong) UIView *line;
@end
@implementation messageAskContentCell
@synthesize commentBO = _commentBO;
@synthesize isHaveMenu = _isHaveMenu;
@synthesize delegate;
//需要呼出自定义菜单
- (BOOL)canBecomeFirstResponder{
[super canBecomeFirstResponder];
return YES;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor colorWithHexString:CardBackGroundColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.clipsToBounds = YES;
self.headPicImgView.hidden = YES;
self.vipImg.hidden = YES;
self.dashedLineView.hidden = YES;
[self.contentView addSubview:self.askNumsLabel];
[self.contentView addSubview:self.menuButton];
[self.contentView addSubview:self.markLabel];
[self.contentView addSubview:self.timeLabel];
[self.contentView addSubview:self.line];
}
return self;
}
- (void)setIsHaveMenu:(BOOL)isHaveMenu {
if (isHaveMenu) {
self.menuButton.hidden = NO;
}else {
self.menuButton.hidden= YES;
}
}
- (UILabel*)markLabel {
if (!_markLabel) {
_markLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_markLabel.backgroundColor = [UIColor colorWithHexString:@"0xc32128"];
_markLabel.layer.cornerRadius = 11/2;
_markLabel.clipsToBounds = YES;
_markLabel.textAlignment = NSTextAlignmentCenter;
_markLabel.textColor = [UIColor whiteColor];
_markLabel.font = appFont(7, NO);
_markLabel.hidden = YES;
}
return _markLabel;
}
-(UILabel *)timeLabel{
if (!_timeLabel) {
_timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_timeLabel.font = appFont(11, NO);
_timeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY];
_timeLabel.backgroundColor = [UIColor clearColor];
}
return _timeLabel;
}
-(UIView *)line{
if (!_line) {
_line = [[UIView alloc] initWithFrame:CGRectZero];
_line.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _line;
}
-(RTLabel *)askNumsLabel{
if (!_askNumsLabel) {
_askNumsLabel = [[RTLabel alloc]initWithFrame:CGRectZero];
_askNumsLabel.font = appFont(11, NO);
_askNumsLabel.backgroundColor = [UIColor clearColor];
_askNumsLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY];
_askNumsLabel.textAlignment = RTTextAlignmentRight;
_askNumsLabel.lineSpacing = 10;
_askNumsLabel.lineBreakMode = RTTextLineBreakModeWordWrapping;
}
return _askNumsLabel;
}
-(void)setLineHidden:(BOOL)hidden{
self.line.hidden = hidden;
}
-(void)setTimeHidden:(BOOL)hidden{
self.timeLabel.hidden = hidden;
}
- (void)setCommentBO:(commentObjectVO *)data {
_commentBO = data;
if (_commentBO.answerList.count >0) {
self.line.hidden = YES;
}else{
self.line.hidden = NO;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
userBO *user = setJsonDicToDataModel(data.userInfo, [userBO class]);
NSString *nameStr;
if([user.userId intValue] == [[TPUserDefault instance].userBO.userId intValue]){
nameStr = @"我:";
}else{
nameStr = user.sname?[NSString stringWithFormat:@"%@:",user.sname]:@"";
}
self.askNameHeight = heightForString(nameStr, self.askNameLabel.font, 400, self.askNameLabel.lineBreakMode);
NSString *answerNumStr = data.answerNums?[NSString stringWithFormat:@"(%@个回答)",data.answerNums]:@"";
self.askContentHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(data.content, 10, appFont(interactionFontSize, NO)), messagePopSize.width-30, appFont(interactionFontSize, NO));
answerNumWidth = widthForString(answerNumStr, self.answerNumLabel.font, self.askNameHeight, self.answerNumLabel.lineBreakMode);
self.timeLabel.text = _commentBO.pubTime;
NSString *newNum = data.unNums;
dispatch_async(dispatch_get_main_queue(), ^{
self.askNameLabel.text = nameStr;
self.askNumsLabel.text = answerNumStr;
self.askContentLabel.attributedText = getLineSpaceAttributedString(data.content, 10, appFont(interactionFontSize, NO));
if ([newNum intValue] >0 ) {
if ([newNum integerValue] >9) {
self.markLabel.text = @"9+";
}else{
self.markLabel.text = newNum;
}
self.markLabel.hidden = NO;
}else {
self.markLabel.hidden = YES;
self.markLabel.text = @"0";
}
[self layoutSubviews];
});
});
}
- (UIButton*)menuButton {
if (!_menuButton) {
_menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
_menuButton.backgroundColor = [UIColor clearColor];
[_menuButton addTarget:self action:@selector(menuTap:) forControlEvents:UIControlEventTouchUpInside];
}
return _menuButton;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (self.isEditing) {
[self sendSubviewToBack:self.contentView];
}
self.askNameLabel.frame = CGRectMake(15,15,
400,
self.askNameHeight);
self.timeLabel.frame = CGRectMake(CGRectGetMinX(self.askNameLabel.frame), CGRectGetMaxY(self.askNameLabel.frame), 400, 16);
if (self.timeLabel.hidden) {
self.askNumsLabel.frame = CGRectMake(messagePopSize.width-15-answerNumWidth,
CGRectGetMinY(self.askNameLabel.frame),
answerNumWidth,
CGRectGetHeight(self.askNameLabel.frame));
self.askContentLabel.frame = CGRectMake(CGRectGetMinX(self.askNameLabel.frame),
CGRectGetMaxY(self.askNameLabel.frame)+10,
messagePopSize.width-30,
self.askContentHeight);
}else{
self.askNumsLabel.frame = CGRectMake(messagePopSize.width-15-answerNumWidth,
CGRectGetMinY(self.timeLabel.frame),
answerNumWidth,
CGRectGetHeight(self.timeLabel.frame));
self.askContentLabel.frame = CGRectMake(CGRectGetMinX(self.askNameLabel.frame),
CGRectGetMaxY(self.timeLabel.frame)+10,
messagePopSize.width-30,
self.askContentHeight);
}
self.menuButton.frame = CGRectMake(CGRectGetMaxX(self.headPicImgView.frame),
0,
CGRectGetWidth(self.bounds)-CGRectGetMaxX(self.headPicImgView.frame),
CGRectGetHeight(self.bounds));
self.markLabel.frame = CGRectMake(CGRectGetMaxX(self.askNumsLabel.frame)-5, CGRectGetMinY(self.askNumsLabel.frame)-5, 11, 11);
self.pushInfo.frame = self.headPicImgView.frame;
for (UIView *subview in self.subviews) {
for (UIView *subview2 in subview.subviews) {
if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) { // move delete confirmation view
[subview bringSubviewToFront:subview2];
}
}
}
self.line.frame = CGRectMake(15, CGRectGetHeight(self.contentView.bounds)-1, messagePopSize.width -30, 1);
}
#pragma mark - menu tap handler
- (void)menuTap:(UIButton*)btn {
[self becomeFirstResponder];
UIMenuItem *commentBack = [[UIMenuItem alloc] initWithTitle:@"回答"action:@selector(commentBack:)];
UIMenuItem *copy = [[UIMenuItem alloc] initWithTitle:@"复制"action:@selector(copyText:)];
UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:@"删除"action:@selector(deleteComment:)];
// UIMenuItem *praise = [[UIMenuItem alloc] initWithTitle:@"点赞"action:@selector(praise:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
if (menu.menuVisible) {
[menu setMenuVisible:NO animated:YES];
return;
}
NSDictionary *userInfo = self.commentBO.userInfo;
userBO *user = setJsonDicToDataModel(userInfo, [userBO class]);
if ([user.userId longLongValue] == [[TPUserDefault instance].userBO.userId longLongValue]) {
[menu setMenuItems:[NSArray arrayWithObjects:delete,commentBack, copy, nil]];
}else {
[menu setMenuItems:[NSArray arrayWithObjects:commentBack, copy, nil]];
}
[menu setTargetRect:self.askContentLabel.frame inView:self];
[menu setMenuVisible:YES animated:YES];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
[super canPerformAction:action withSender:sender];
if ( action == @selector(commentBack:) || action == @selector(copyText:) || action == @selector(deleteComment:))
{
return YES;
}
else
{
return NO;
}
}
- (void)copyText:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = self.askContentLabel.text;
ShowMessage(@"复制成功", YES);
[MobClick event:@"22"];
}
- (void)commentBack:(id)sender {
if ([self.delegate respondsToSelector:@selector(menuClickAnswer: closeHidden:)]) {
[self.delegate menuClickAnswer:self.commentBO closeHidden:YES];
}
}
- (void)deleteComment:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否确认删除此发言?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除",nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != 0) {
NSDictionary *dic = @{@"commentIds":self.commentBO.commentId,@"commentType":@"1"};
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:2001 requestUrl:removeCommentmspURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[MobClick event:@"27"];
if ([Self.delegate respondsToSelector:@selector(deleteAskSuccess)]) {
[Self.delegate deleteAskSuccess];
}
ShowMessage(@"删除成功",YES);
}else {
ShowMessage(@"删除失败", NO);
}
}];
}
}
@end