|
//
// writeCommentAndAskController.m
// ThePaperHD
//
// Created by scar1900 on 15/3/4.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "writeCommentAndAskController.h"
#import "KGModal.h"
@interface writeCommentAndAskController ()<UITextViewDelegate> {
CGFloat keyboardOffset;
}
@property(nonatomic, strong)UIButton *closeBtn;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIButton *confirmBtn;
@property(nonatomic, strong)UIView *writeContentView;
@property(nonatomic, strong)UITextView *writeTextView;
@property(nonatomic, assign)NSInteger limitTextNum;
@property(nonatomic, strong)UILabel *placeHolderLabel;
@property(nonatomic, strong)NSString *commentContent;
@end
@implementation writeCommentAndAskController
@synthesize isPush;
@synthesize type = _type;
@synthesize commentBO;
@synthesize limitTextNum;
@synthesize nodeId;
@synthesize commentContent;
@synthesize delegate;
@synthesize commentOT;
@synthesize placeHoldText;
@synthesize isMessage = _isMessage;
- (id)init {
self = [super init];
if (self) {
self.isPush = NO;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.closeBtn];
[self.view addSubview:self.confirmBtn];
[self.view addSubview:self.titleLabel];
[self setPlaceHolderOfTextView];
[self.view addSubview:self.writeContentView];
[self.writeContentView addSubview:self.writeTextView];
[self.writeContentView addSubview:self.placeHolderLabel];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.writeTextView becomeFirstResponder];
[self disablePopBackTap];
[KGModal sharedInstance].tapOutsideToDismiss = NO;
__weak typeof(self) Self = self;
[[KGModal sharedInstance] setTapHandler:^{
[Self closeKeyBoard];
}];
self.writeContentView.frame = CGRectMake(0,
CGRectGetMaxY(self.titleLabel.frame),
CGRectGetWidth(self.view.bounds),
570/2);
self.writeTextView.frame =CGRectMake(10,
10,
CGRectGetWidth(self.writeContentView.bounds)-20,
CGRectGetHeight(self.writeContentView.bounds)-20);
CGFloat height = heightForString(self.placeHolderLabel.text, self.placeHolderLabel.font, CGRectGetWidth(self.writeContentView.bounds) -40, self.placeHolderLabel.lineBreakMode);
self.placeHolderLabel.frame = CGRectMake(20, 20, CGRectGetWidth(self.writeContentView.bounds) -40, height);
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.closeBtn.frame = CGRectMake(0, 0, 50, 80);
self.titleLabel.frame = CGRectMake(50,
0,
CGRectGetWidth(self.view.bounds)-100,
80);
self.confirmBtn.frame = CGRectMake(CGRectGetMaxX(self.titleLabel.frame), 0, 50, 80);
self.writeContentView.frame = CGRectMake(0,
CGRectGetMaxY(self.titleLabel.frame),
CGRectGetWidth(self.view.bounds),
570/2);
[self.closeBtn setImageEdgeInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
}
- (UIView*)writeContentView {
if (!_writeContentView) {
_writeContentView = [[UIView alloc]initWithFrame:CGRectMake(0,
CGRectGetMaxY(self.titleLabel.frame),
CGRectGetWidth(self.view.bounds),
570/2)];
_writeContentView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
_writeContentView.clipsToBounds = YES;
}
return _writeContentView;
}
- (UIButton*)closeBtn {
if (!_closeBtn) {
_closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
if (self.isPush) {
[_closeBtn setImage:Image(@"login/popUpBack.png") forState:UIControlStateNormal];
[_closeBtn setImage:Image(@"login/popUpBack_h.png") forState:UIControlStateHighlighted];
}else {
[_closeBtn setImage:Image(@"login/popUpCloseBtn.png") forState:UIControlStateNormal];
[_closeBtn setImage:Image(@"login/popUpCloseBtn_s.png") forState:UIControlStateHighlighted];
}
[_closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _closeBtn;
}
- (UIButton*)confirmBtn {
if (!_confirmBtn) {
_confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_confirmBtn setImage:Image(@"Button/popConfirmBtn.png") forState:UIControlStateNormal];
[_confirmBtn setImage:Image(@"Button/popConfirmBtn_h.png") forState:UIControlStateHighlighted];
[_confirmBtn addTarget:self action:@selector(confirmEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmBtn;
}
- (UILabel*)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.textAlignment = NSTextAlignmentCenter;
// _titleLabel.text = @"登录";
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = appFont(30, NO);
}
return _titleLabel;
}
- (UITextView*)writeTextView {
if (!_writeTextView) {
_writeTextView = [[UITextView alloc]initWithFrame:CGRectMake(10,
10,
messagePopSize.width-20,
CGRectGetHeight(self.writeContentView.bounds)-20)];
_writeTextView.backgroundColor = [UIColor clearColor];
_writeTextView.scrollEnabled = YES;
_writeTextView.delegate =self;
_writeTextView.textAlignment = NSTextAlignmentLeft;
_writeTextView.textColor = [UIColor colorWithHexString:TextBlack];
_writeTextView.font = appFont(18, NO);
_writeTextView.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_writeTextView.keyboardAppearance = UIKeyboardAppearanceDark;
}
}
return _writeTextView;
}
- (UILabel*)placeHolderLabel {
if (!_placeHolderLabel) {
_placeHolderLabel = [UILabel new];
_placeHolderLabel.textAlignment = NSTextAlignmentLeft;
_placeHolderLabel.textColor = [UIColor colorWithHexString:LINECOLOR];
_placeHolderLabel.font = appFont(18, NO);
_placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
_placeHolderLabel.numberOfLines = 0;
_placeHolderLabel.backgroundColor = [UIColor clearColor];
}
return _placeHolderLabel;
}
- (void)closeEvent:(UIButton*)btn {
if (!isBlankString(self.commentContent)) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"确定取消已编辑的内容吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
[alert show];
return;
}
if (self.isPush) {
if ([self.delegate respondsToSelector:@selector(writeConetentPopBack)]) {
[self.delegate writeConetentPopBack];
}
}else {
[self dismissControllerAnimated:YES completion:nil];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != 0) {
if (self.isPush) {
if ([self.delegate respondsToSelector:@selector(writeConetentPopBack)]) {
[self.delegate writeConetentPopBack];
}
}else {
[self dismissControllerAnimated:YES completion:nil];
}
}
}
- (void)setPlaceHolderOfTextView {
NSString *placeHolder = @"";
if (!self.placeHoldText && isBlankString(self.placeHoldText)) {
userBO *user = setJsonDicToDataModel(self.commentBO.userInfo, [userBO class]);
if (self.type == commentType) {
placeHolder = [NSString stringWithFormat:@"评论(内容最多%ld字)",(long)self.limitTextNum];
}else if (self.type == commentForUserType) {
placeHolder = [NSString stringWithFormat:@"回复%@(内容最多%ld字)",user.sname,(long)self.limitTextNum];
}else if (self.type == askType) {
placeHolder = [NSString stringWithFormat:@"提问(内容最多%ld字)",(long)self.limitTextNum];
}else if (self.type == askForUserType) {
placeHolder = [NSString stringWithFormat:@"回复%@(内容最多%ld字)",user.sname,(long)self.limitTextNum];
}else if (self.type == aswerType) {//【需求】问答详情页(bug:4552) 回答改为回复
placeHolder = [NSString stringWithFormat:@"回复(内容最多%ld字)",(long)self.limitTextNum];
}else if (self.type == interactionCommentType) {
placeHolder = @"每个问题背后都是一份翘首以待的热情,如何回复,你懂的!";
}else if (self.type == interactionAskType){
placeHolder = @"新颖、大胆、专业、有趣的好问题更有机会获得回复,开始提问吧!";
}
CGFloat height = heightForString(placeHolder, self.placeHolderLabel.font,CGRectGetWidth(self.writeContentView.bounds) -40, self.placeHolderLabel.lineBreakMode);
self.placeHolderLabel.frame = CGRectMake(20, 20,CGRectGetWidth(self.writeContentView.bounds) -40, height);
self.placeHolderLabel.text = placeHolder;
}else {
self.placeHolderLabel.text = self.placeHoldText;
}
}
- (void)setType:(commentAndAskType)coType {
_type = coType;
if (coType == commentType) {
self.limitTextNum = 800;
self.titleLabel.text = @"评论";
}else if (coType == commentForUserType) {
self.limitTextNum = 800;
self.titleLabel.text = @"回复";
}else if (coType == askType) {
self.limitTextNum = 140;
self.titleLabel.text = @"提问";
}else if (coType == askForUserType) {
self.limitTextNum = 800;
self.titleLabel.text = @"回复";
}else if (coType == aswerType) {
self.limitTextNum = 800;
self.titleLabel.text = @"回复";
}else if (coType == interactionAskType){
self.limitTextNum = 140;
self.titleLabel.text = @"提问";
}else if (coType == interactionCommentType) {
self.limitTextNum = 800;
self.titleLabel.text = @"回复";
}
}
#pragma mark - keyBoard event
- (void)closeKeyBoard {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait
|| orientation ==UIInterfaceOrientationPortraitUpsideDown) {
return;
}
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
if (isIOS8) {
keyboardOffset = keyboardRect.size.height;
}else {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight
|| orientation ==UIInterfaceOrientationLandscapeLeft) {
keyboardOffset = keyboardRect.size.width;
}else {
keyboardOffset = keyboardRect.size.height;
}
}
CGRect orginRect = self.view.frame;
if (orginRect.origin.y > 20) {
orginRect.origin.y = orginRect.origin.y - 100;
self.view.frame = orginRect;
}
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait
|| orientation ==UIInterfaceOrientationPortraitUpsideDown) {
return;
}
CGRect orginRect = self.view.frame;
if (orginRect.origin.y < 20) {
orginRect.origin.y = orginRect.origin.y + 100;
self.view.frame = orginRect;
}
}
#pragma mark - button handler
- (void)confirmEvent:(UIButton*)btn {
if (isBlankString(self.commentContent)) {
[self closeKeyBoard];
ShowTextMessageInPop((@"内容不能为空"), self.writeTextView);
return;
}
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
if (self.commentOT && !isBlankString(self.commentOT)) {
[dic setValue:self.commentOT forKey:@"ot"];
}else {
[dic setValue:@"1" forKey:@"ot"];
}
[dic setValue:self.nodeId forKey:@"c"];
// self.commentContent = [self.commentContent stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
// self.commentContent = [self.commentContent stringByReplacingOccurrencesOfString:@">" withString:@">"];
[dic setValue:self.commentContent forKey:@"content"];
if (self.type == commentType) {
[dic setValue:@"1" forKey:@"commentType"];
if(![dic[@"ot"] isEqualToString:@"3"]){
[MobClick event:@"19"];
}
}else if (self.type == commentForUserType) {
if(![dic[@"ot"] isEqualToString:@"3"]){
[MobClick event:@"19"];
}
[dic setValue:@"1" forKey:@"commentType"];
if (self.commentBO) {
[dic setValue:self.commentBO.commentId forKey:@"parentId"];
}
}else if (self.type == askType) {
if(![dic[@"ot"] isEqualToString:@"3"]){
[MobClick event:@"20"];
}
[dic setValue:@"2" forKey:@"commentType"];
}else if (self.type == askForUserType) {
if(![dic[@"ot"] isEqualToString:@"3"]){
[MobClick event:@"21"];
}
[dic setValue:@"3" forKey:@"commentType"];
if (self.commentBO) {
if (!isBlankString(self.commentBO.parentId)) {
[dic setValue:self.commentBO.parentId forKey:@"parentId"];
[dic setValue:self.commentBO.commentId forKey:@"quoteId"];
}else if(self.commentBO.parent){
[dic setValue:self.commentBO.parent[@"commentId"] forKey:@"parentId"];
[dic setValue:self.commentBO.commentId forKey:@"quoteId"];
}else if(self.commentBO.parentInfo){
[dic setValue:self.commentBO.parentInfo[@"commentId"] forKey:@"parentId"];
[dic setValue:self.commentBO.commentId forKey:@"quoteId"];
}
}
}else if (self.type == aswerType) {
if(![dic[@"ot"] isEqualToString:@"3"]){
[MobClick event:@"21"];
}else {
[MobClick event:@"38"];
}
[dic setValue:@"3" forKey:@"commentType"];
if (self.commentBO) {
if (self.commentBO.parent[@"commentId"]) {
[dic setValue:self.commentBO.parent[@"commentId"] forKey:@"parentId"];
}else{
[dic setValue:self.commentBO.commentId forKey:@"parentId"];
}
// [dic setValue:self.commentBO.commentId forKey:@"quoteId"];
// [dic setValue:self.commentBO.commentId forKey:@"parentId"];
}
}else if (self.type == interactionAskType){
if(![dic[@"ot"] isEqualToString:@"3"]){
[MobClick event:@"20"];
}else {
[MobClick event:@"36"];
}
[dic setValue:@"2" forKey:@"commentType"];
}else if (self.type == interactionCommentType){
if(![dic[@"ot"] isEqualToString:@"3"]){
[MobClick event:@"21"];
}
[dic setValue:@"3" forKey:@"commentType"];
if (self.commentBO) {
[dic setValue:self.commentBO.commentId forKey:@"parentId"];
}
}
[Remote doJsonAction:0 requestUrl:commentURL parameter:dic delegate:self];
}
#pragma mark - text view delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([self isContainsEmoji:text]) {
return NO;
}else return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
if ([self isContainsEmoji:textView.text]) {
return;
}
self.commentContent = textView.text;
if (textView.text.length > 0) {
self.placeHolderLabel.hidden = YES;
}else self.placeHolderLabel.hidden = NO;
}
- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData {
[self closeKeyBoard];
NSString *message =responsData[@"resultMsg"];
// ShowTextMessage(message);
if ([message isMatchedByRegex:@"成功"]) {
if (self.isPush) {
if ([self.delegate respondsToSelector:@selector(commentSuccess:)]) {
ShowTextMessageWithDelay(message, 3);//话题详情页,提交一个问题,弹出的tips会闪一下(bug:4741)
[self.delegate commentSuccess:self.type];
}
}else {
[self dismissControllerAnimated:YES completion:^{
if ([self.delegate respondsToSelector:@selector(commentSuccess:)]) {
ShowTextMessageWithDelay(message, 3);//话题详情页,提交一个问题,弹出的tips会闪一下(bug:4741)
[self.delegate commentSuccess:self.type];
}
}];
}
}else {
ShowTextMessage(message);//话题详情页,提交一个问题,弹出的tips会闪一下(bug:4741)
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, 1ull*NSEC_PER_SEC);
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
if (self.isPush) {
if ([self.delegate respondsToSelector:@selector(commentSuccess:)]) {
[self.delegate commentSuccess:self.type];
}
}else {
[self dismissControllerAnimated:YES completion:^{
if ([self.delegate respondsToSelector:@selector(commentSuccess:)]) {
[self.delegate commentSuccess:self.type];
}
}];
}
});
}
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{
[self closeKeyBoard];
ShowTextMessage(message);
}
#pragma mark - is emoji
- (BOOL)isContainsEmoji:(NSString *)string {
__block BOOL isEomji = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
// surrogate pair
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
isEomji = YES;
}
}
} else {
// non surrogate
if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
isEomji = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
isEomji = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
isEomji = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
isEomji = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50|| hs == 0x231a ) {
isEomji = YES;
}
if (!isEomji && substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
isEomji = YES;
}
}
}
}];
return isEomji;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
|