YH_BarrageViewController.m 13 KB
//
//  YH_Barrage.m
//  YHBarrage
//
//  Created by Arthur on 16/6/6.
//  Copyright © 2016年 YOHO!. All rights reserved.
//

#import "YH_BarrageViewController.h"
#import "YH_BarrageTableView.h"
//#import "YH_YohoLoginViewModel.h"
#import "M16HUDUtil.h"

#define kBarrageTableViewHeight 253
#define kBarrageTableViewWidth  (245 * kScreenPointScale)
#define kInputTextFieldHeight 34
#define kInputTextFieldBottomEdge 49
#define kPaddingLeft 13
#define kCloseButtonWidth 19

#define kMessageTipLeftEdge 111
#define kMessageTipHeight 21.5
#define kMessageTipWidth 87.5

@interface YH_BarrageViewController ()<YH_BarrageTableViewScrollingDelegate, HPGrowingTextViewDelegate>

@property (strong, nonatomic) UIImageView *inputTextBgView;
@property (strong, nonatomic) UIButton *keyBoardCloseButton;
@property (strong, nonatomic) UIView *closeButtonContainer;
@property (strong, nonatomic) UIView *bottomContainerView;
@property (strong, nonatomic) UIImageView *messageTip;
@property (strong, nonatomic) UIImageView *messageArrow;
@property (strong, nonatomic)YH_BarrageTableView *barrageTableView;
@property (nonatomic) YHBarrageType barrageType;
@property (assign, nonatomic) CGSize keyboardSize;
@property (strong, nonatomic) UILabel *newsBubble;
@end

@implementation YH_BarrageViewController

#pragma mark - life cycle

- (instancetype)initWithType:(YHBarrageType)barrageType
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        self.barrageType = barrageType;
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.barrageTableView];
    [self setBottomView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (self.barrageType == YHBarrageTypeSend) {
        // 显示输入框
        self.inputTextField.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
        self.inputTextField.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
        self.inputTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    } else if(self.barrageType == YHBarrageTypeReadonly) {
        // 不显示输入框
        self.bottomContainerView.hidden = YES;
        self.messageTip.hidden = YES;
    }
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)setBottomView
{
    CGFloat scalePoint = SCREEN_HEIGHT / 667.0;
    
    [self.barrageTableView setFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds)-kBarrageTableViewHeight * scalePoint-kInputTextFieldBottomEdge, kBarrageTableViewWidth, kBarrageTableViewHeight * scalePoint)];
    
    self.bottomContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.barrageTableView.frame) + 5, SCREEN_WIDTH, kInputTextFieldHeight)];
    self.bottomContainerView.backgroundColor = [UIColor clearColor];
    [self.inputTextField setFrame:CGRectMake(kPaddingLeft, 0, CGRectGetWidth(self.view.bounds) - 2*kPaddingLeft, kInputTextFieldHeight)];
    
    self.inputTextBgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) - 2*kPaddingLeft, kInputTextFieldHeight)];
    self.inputTextBgView.image = [UIImage imageNamed:@"inputTextBg"];
    [self.inputTextField addSubview:self.inputTextBgView];
    [self.inputTextField sendSubviewToBack:self.inputTextBgView];
    [self.bottomContainerView addSubview:self.inputTextField];
    [self.bottomContainerView bringSubviewToFront:self.inputTextField];
    
    self.closeButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 2*kPaddingLeft- kCloseButtonWidth, 0, kCloseButtonWidth + 2*kPaddingLeft, kInputTextFieldHeight)];
    self.closeButtonContainer.centerY = self.inputTextField.centerY;
    UITapGestureRecognizer *closeGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoard:)];
    [self.closeButtonContainer addGestureRecognizer:closeGesture];
    self.closeButtonContainer.userInteractionEnabled = YES;
    self.closeButtonContainer.hidden = YES;
    self.keyBoardCloseButton = [[UIButton alloc] initWithFrame:CGRectMake(kPaddingLeft, 0, kCloseButtonWidth, kCloseButtonWidth)];
    [self.keyBoardCloseButton setImage:[UIImage imageNamed:@"keyBoardClose"] forState:UIControlStateNormal];
    self.keyBoardCloseButton.userInteractionEnabled = NO;
    self.keyBoardCloseButton.centerY = self.inputTextField.centerY;
    [self.closeButtonContainer addSubview:self.keyBoardCloseButton];
    [self.bottomContainerView addSubview:self.closeButtonContainer];
    
    [self.view addSubview:self.bottomContainerView];
    
    self.messageTip = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - kMessageTipWidth - kMessageTipLeftEdge, self.barrageTableView.bottom - kMessageTipHeight, kMessageTipWidth, kMessageTipHeight)];
    self.messageTip.image = [UIImage imageNamed:@"message_Tip"];
    self.messageTip.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(newsBubbleClick:)];
    tapGR.numberOfTapsRequired = 1;
    [self.messageTip addGestureRecognizer:tapGR];
    
    self.messageArrow = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 10, 6)];
    self.messageArrow.image = [UIImage imageNamed:@"messageArrow"];
    [self.messageTip addSubview:self.messageArrow];
    
    [self.newsBubble setFrame:CGRectMake(CGRectGetMaxX(self.messageArrow.frame) + 5, 0, kMessageTipWidth - CGRectGetMaxX(self.messageArrow.frame) - 10, kMessageTipHeight)];
    [self.messageTip addSubview:self.newsBubble];
    self.messageTip.hidden = YES;
    
    [self.view addSubview:self.messageTip];
}

-(void)closeKeyBoard:(UIButton *)sender
{
    [self.inputTextField resignFirstResponder];
}


#pragma mark -

- (void)show
{
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self.view];
    [[UIApplication sharedApplication].keyWindow.rootViewController addChildViewController:self];
}


- (void)insertItem:(NSDictionary *)itemdic
{
    [self.barrageTableView yhBarrageTableView:self.barrageTableView insertCellWithItem:itemdic];
    
}

- (void)reset
{
    [self.barrageTableView reset];
}

- (void)newsBubbleClick:(id)sender
{
    [self.barrageTableView startAutoScolling];
    [self.messageTip setHidden:YES];
}

#pragma mark - YH_BarrageTableViewScrollingDelegate

- (void)manualScrollingWithNewMsgCount:(NSString *)newMsgCount
{
    NSString *msgCount ;
    if ([newMsgCount integerValue] > 99) {
        msgCount = @"99+条信息";
    }else {
        msgCount = [NSString stringWithFormat:@"%ld条信息",(long)[newMsgCount integerValue]];
    }
    [self.newsBubble setText:msgCount];
    CGSize size = CGSizeMake(60,kMessageTipHeight);
    CGSize labelsize = [self.newsBubble sizeThatFits:size];
    [self.newsBubble setFrame:CGRectMake(25, (kMessageTipHeight - labelsize.height) / 2, labelsize.width, labelsize.height)];
    [self.messageTip setWidth:CGRectGetMaxX(self.newsBubble.frame) + 10];
    [self.messageTip setHidden:NO];
}

- (void)autoScollingCallback
{
    [self.messageTip setHidden:YES];
}

#pragma mark - getters and setters

- (YH_BarrageTableView *)barrageTableView
{
    if (_barrageTableView == nil) {
        _barrageTableView = [[YH_BarrageTableView alloc]init];
        _barrageTableView.barrageScrollingDelegate = self;
    }
    
    return _barrageTableView;
}

- (HPGrowingTextView *)inputTextField
{
    if (_inputTextField == nil) {
        _inputTextField = [[HPGrowingTextView alloc]init];
        _inputTextField.isScrollable = NO;
        _inputTextField.returnKeyType = UIReturnKeySend;
        _inputTextField.backgroundColor = [UIColor clearColor];
        _inputTextField.placeholder = @"说点什么吧...";
        _inputTextField.font = [UIFont systemFontOfSize:15.0f];
        _inputTextField.textColor = [UIColor whiteColor];
        _inputTextField.minNumberOfLines = 1;
        _inputTextField.maxNumberOfLines = 1;
        _inputTextField.internalTextView.textContainer.maximumNumberOfLines = 1;
        _inputTextField.internalTextView.textContainer.lineBreakMode = NSLineBreakByTruncatingHead;
        
        _inputTextField.delegate = self;
    }
    
    return _inputTextField;
}

- (UILabel *)newsBubble
{
    if (_newsBubble == nil) {
        _newsBubble = [[UILabel alloc]init];
        _newsBubble.userInteractionEnabled = YES;
        _newsBubble.textAlignment = NSTextAlignmentCenter;
        _newsBubble.font = [UIFont fontOfSCWithSize:11];
        _newsBubble.textColor = [UIColor blackColor];
    }
    
    return _newsBubble;
}

#pragma mark - HPGrowingTextViewDelegate
- (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//    if ([text isEqualToString:@"\n"]) {
//        if (![self.inputTextField.text isEqualToString:@""]) {
//            if ([GVUserDefaults standardUserDefaults].userLogged) {
//                [self.inputTextField resignFirstResponder];
//                if (self.sendMsgBlock) {
//                    self.sendMsgBlock(self.inputTextField.text);
//                }
//                self.inputTextField.text = @"";
//                return YES;
//            } else {
//                __weak typeof(self) weakSelf = self;
//                [self.inputTextField resignFirstResponder];
//                [[YH_YohoLoginViewModel loginMgr] excuteBlockRequireHasLogin:^{
//                    __strong typeof(weakSelf) strongSelf = weakSelf;
//                    if (strongSelf.sendMsgBlock) {
//                        strongSelf.sendMsgBlock(self.inputTextField.text);
//                    }
//                    
//                    if (strongSelf.loginToTCPServerBlock) {
//                        strongSelf.loginToTCPServerBlock();
//                    }
//                    strongSelf.inputTextField.text = @"";
//                }];
//                return YES;
//            }
//        }else {
//            return NO;
//        }
//    }else {
//        NSString * aString = [self.inputTextField.text stringByReplacingCharactersInRange:range withString:text];
//        if (aString.length > 30) {
//            [self showText:@"内容不可以超过30个字"];
//            self.inputTextField.text = [aString substringToIndex:30];
//            return NO;
//        }
//    }
    return YES;
}

//-(void)textViewDidChange:(HPGrowingTextView *)growingTextView
//{
//    NSInteger length = self.inputTextField.text.length;
//    if (length > 30) {
//        [self showText:@"内容不可以超过30个字"];
//        self.inputTextField.text = [self.inputTextField.text substringToIndex:30];
//    }
//}

#pragma mark - UIKeyboard changes notification

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    self.keyboardSize = [value CGRectValue].size;
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect iRect = self.bottomContainerView.frame;
        iRect.origin.y = SCREEN_HEIGHT - self.keyboardSize.height - CGRectGetHeight(self.bottomContainerView.frame) - 5;
        self.bottomContainerView.frame = iRect;
        self.inputTextField.width = SCREEN_WIDTH - 3*kPaddingLeft - kCloseButtonWidth;
        self.inputTextBgView.width = SCREEN_WIDTH - 3*kPaddingLeft - kCloseButtonWidth;
        self.closeButtonContainer.hidden = NO;
        
        CGRect tRect = self.barrageTableView.frame;
        tRect.origin.y = self.bottomContainerView.top - CGRectGetHeight(tRect) - 5;
        self.barrageTableView.frame = tRect;
        [self.messageTip setY:self.barrageTableView.bottom - kMessageTipHeight];
        
    }];
    if (self.keyBoardShowBlock) {
        self.keyBoardShowBlock(YES, self.keyboardSize.height);
    }
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    self.keyboardSize = CGSizeZero;
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect iRect = self.bottomContainerView.frame;
        iRect.origin.y = SCREEN_HEIGHT - iRect.size.height - 5;
        self.bottomContainerView.frame = iRect;
        self.inputTextField.width = SCREEN_WIDTH - 2*kPaddingLeft;
        self.inputTextBgView.width = SCREEN_WIDTH - 2*kPaddingLeft;
        
        self.closeButtonContainer.hidden = YES;
        
        CGRect tRect = self.barrageTableView.frame;
        tRect.origin.y = SCREEN_HEIGHT - CGRectGetHeight(tRect) - kInputTextFieldBottomEdge;
        self.barrageTableView.frame = tRect;
        [self.messageTip setY:self.barrageTableView.bottom - kMessageTipHeight];
        
    }];
    if (self.keyBoardShowBlock) {
        self.keyBoardShowBlock(NO, self.keyboardSize.height);
    }
    
}

//- (void)showText:(NSString *)text
//{
//    if ([text notNilOrEmpty]) {
//        [M16HUDUtil textOnly:text inView:self.view];
//    }
//}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end