GiGaMaskTime/GIGA/Modules/Mask/Controller/GiGaFlingCommitVC.m

300 lines
9.8 KiB
Objective-C

//
// GiGaFlingCommitVC.m
// GIGA
//
// Created by lianxiang on 2018/8/28.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaFlingCommitVC.h"
#import "GiGaFlyingCommitInputView.h"
#import "LXDanMuManager.h"
#import "GiGaCommentView.h"
#import "GiGaCommentModel.h"
#import "LXDanMuManagerConfig.h"
#import "IQKeyboardManager.h"
#import <SocketIO/SocketIO-Swift.h>
#import "NSString+Unicode.h"
#import "GIGAMBHUDHelper.h"
#import "LXCustomSwith.h"
#import "GiGaUserDefault.h"
#import "GiGaServerConfig.h"
#import "GiGaQuestionVC.h"
//弹幕控制器
@interface GiGaFlingCommitVC ()
@property (nonatomic,strong) GiGaFlyingCommitInputView *commitInputView;
@property (nonatomic,strong) SocketIOClient *client;
@property (nonatomic,strong) SocketManager *manager;
//@property (nonatomic,strong) LXCustomSwith *danmuSwitch;
//开启弹幕按钮
@property(nonatomic,strong) UIButton *openDanmuButton;
@property(nonatomic,strong) UIButton *testBtn;
@end
@implementation GiGaFlingCommitVC
//- (LXCustomSwith *)danmuSwitch{
// if (!_danmuSwitch) {
// _danmuSwitch = [[LXCustomSwith alloc] initWithFrame:CGRectMake(KMainW - 50 - 25 , 64 + PhoneX_TopMargin +10, 50, 18)];
//
// [_danmuSwitch setOffthumTitle:@"弹幕"];
// [_danmuSwitch setOnthumTitle:@"弹幕"];
// [_danmuSwitch setOnTintColor:GIGARGB(91, 0, 2, 1)];
// [_danmuSwitch setTintColor:GIGARGB(91, 0, 2, 1)];
// [_danmuSwitch setThumbTintColor:GIGARGB(203, 42, 50, 1)];
// _danmuSwitch.on = YES;
// _danmuSwitch.thumbLabel.font = GIGA_TEXTFONTMEDIUM(10);
// _danmuSwitch.thumbLabel.textColor = [UIColor whiteColor];
// [_danmuSwitch addTarget:self action:@selector(mycuntomSwitchAction:) forControlEvents:UIControlEventValueChanged];
// }
//
// return _danmuSwitch;
//
//}
- (UIButton *)testBtn
{
if (!_testBtn) {
_testBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_testBtn setTitle:@"" forState:UIControlStateNormal];
[_testBtn addTarget:self action:@selector(testBtnAction) forControlEvents:UIControlEventTouchUpInside];
//_testBtn.backgroundColor = [UIColor redColor];
_testBtn.frame = CGRectMake(30, 476 + PhoneX_TopMargin,KMainW - 30 *2, 50);
}
return _testBtn;
}
-(void)testBtnAction{
//GIGA_ShowToast(@"请先关闭弹幕");
[self dismissViewControllerAnimated:YES completion:^{
[[LXDanMuManager shareInstance] destory];
[self destorySocket];
NC_POST_NAME_OBJECT(@"UserCloseChatRoomAction", nil);
NC_POST_NAME_OBJECT(@"flingcommitViewTestAreaAction", nil);
}];
}
- (UIButton *)openDanmuButton{
if (!_openDanmuButton) {
_openDanmuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_openDanmuButton addTarget:self action:@selector(openDanmuButtonAction) forControlEvents:UIControlEventTouchUpInside];
_openDanmuButton.backgroundColor = GIGARGB(112, 0, 0, 1);
_openDanmuButton.frame = CGRectMake(KMainW - 25 -50,SAFE_NAV_HEIGHT + 15, 50, 20);
_openDanmuButton.layer.masksToBounds = YES;
_openDanmuButton.layer.cornerRadius = 10;
NSAttributedString *att = [GiGaHelper stringWithText:@"弹幕" textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTMEDIUM(10) leterSpace:0];
[_openDanmuButton setAttributedTitle:att forState:UIControlStateNormal];
[_openDanmuButton setImage:[UIImage imageNamed:@"btn_talk"] forState:UIControlStateNormal];
_openDanmuButton.imageEdgeInsets = UIEdgeInsetsMake(0,18, 0, -18);
_openDanmuButton.titleEdgeInsets = UIEdgeInsetsMake(0, -4, 0, 4);
}
return _openDanmuButton;
}
- (GiGaFlyingCommitInputView *)commitInputView{
if (!_commitInputView) {
_commitInputView = [[GiGaFlyingCommitInputView alloc] initWithFrame:self.view.bounds];
[_commitInputView setPalceholderText:@"请输入弹幕"];
}
return _commitInputView;
}
//弹幕按钮
-(void)mycuntomSwitchAction:(id)sender{
LXCustomSwith *nkswitch = (LXCustomSwith *)sender;
if (nkswitch.isOn)
NSLog(@"switchPressed ON");
else
[[LXDanMuManager shareInstance] destory];
[self destorySocket];
NC_POST_NAME_OBJECT(@"UserCloseChatRoomAction", nil);
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"switchPressed OFF");
}
-(void)openDanmuButtonAction{
[[LXDanMuManager shareInstance] destory];
[self destorySocket];
NC_POST_NAME_OBJECT(@"UserCloseChatRoomAction", nil);
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NC_ADD_TARGET_NAME_OBJECT(self, @selector(maskTimeEnd), kUserNoti_MASKEND, nil);
[IQKeyboardManager sharedManager].enable = NO;
[[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NC_REMOVE_NAME(self, kUserNoti_MASKEND, nil);
[IQKeyboardManager sharedManager].enable = YES;
[[IQKeyboardManager sharedManager] setEnableAutoToolbar:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.testBtn];
// Do any additional setup after loading the view.
[self.view addSubview: self.commitInputView];
// [self.view addSubview:self.danmuSwitch];
// [self.view bringSubviewToFront:self.danmuSwitch];
[self.view addSubview:self.openDanmuButton];
[self.view addSubview:self.testBtn];
weakify(self);
self.commitInputView.SendCommentHandler = ^(NSString *content) {
GILog(@"发送->:%@",content);
NSArray *arr = @[@{
@"content":content,
@"room":@"",
@"sendUserName":@"lianxiang"
}];
[weakSelf.client emit:@"talk" with:arr];
};
//开启弹幕
[[LXDanMuManager shareInstance] createDanmuWithTrackSpeedArray:@[@1.00,@1.00,@3.00,@3.00] centerY:KMainH / 4 trackWidth:KMainW showInView:self.view];
[[LXDanMuManager shareInstance] start];
//socket
[self connetction];
//显示隐藏输入框
BOOL isUserLogin = [GiGaUserDefault isUserLogin];
if (!isUserLogin) {
self.commitInputView.hidden = YES;
}
}
-(void)connetction{
//@"connectParams":@{@"token":@""}
NSString *token = [GiGaUserDefault getCurentToken];
if (!token) {
token = @"1";
}
NSString *urlstr = [GiGaServerConfig getsocketUrl];
NSURL* url = [[NSURL alloc] initWithString:urlstr];
SocketManager *manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @NO, @"forcePolling": @YES,@"extraHeaders":@{@"device":@"ios"},@"connectParams":@{@"token":token}}];
_manager = manager;
_client = manager.defaultSocket;
[self.manager connect];
weakify(self);
[self.client on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
//GIGA_WIndowTost(@"客户端上线");
NSLog(@"*************\n\niOS客户端上线\n\n*************");
NSArray *arr = @[@{
@"token":token,
}];
[weakSelf.client emit:@"login" with:arr];
}];
[self.client on:@"onLogin" callback:^(NSArray * _Nonnull event, SocketAckEmitter * _Nonnull ack) {
//
[GIGAMBHUDHelper messageAddto:self.view title:@"弹幕链接成功"];
NSLog(@"login 监听:%@",event);
}];
[self.client on:@"connectedSocket" callback:^(NSArray * _Nonnull event, SocketAckEmitter * _Nonnull ack) {
// [GIGAMBHUDHelper messageAddto:self.view title:@"弹幕链接成功"];
NSLog(@"connectedSocket:%@",event);
}];
[self.client on:@"onTalk" callback:^(NSArray * _Nonnull data, SocketAckEmitter * _Nonnull ack) {
//NSString *json =data[0];
//NSDictionary *dic = [GiGaHelper dictionaryWithJsonString:json];
NSDictionary *dic = data[0];
NSLog(@"callback json***:%@",dic);
if (dic == nil) {
NSLog(@"解析失败");
}else{
GiGaCommentModel *commentModel = [[GiGaCommentModel alloc] initWithDictionary:dic error:nil];
GILog(@"接收->:%@",[dic[@"content"] replaceUnicode:dic[@"content"]]);
[weakSelf showDanMuWith:commentModel];
}
}];
[self.client on:@"disconnect" callback:^(NSArray * _Nonnull event, SocketAckEmitter * _Nonnull ack) {
GIGA_WIndowTost(@"弹幕已断开");
NSLog(@"*************\n\niOS客户端下线\n\n*************%@",event?event[0]:@"");
}];
[self.client on:@"error" callback:^(NSArray * _Nonnull event, SocketAckEmitter * _Nonnull ack) {
[GIGAMBHUDHelper messageAddto:self.view title:@"连接弹幕中..."];
GILog(@"请求超时");
}];
}
//弹幕插入
-(void)showDanMuWith:(GiGaCommentModel *)model{
NSArray *classArray = @[@"GiGaCommentView",@"GiGaCommentView"];
int classRandomIndex = arc4random() % 2;
GiGaCommentView *customView = [[NSClassFromString(classArray[classRandomIndex]) alloc] initWithMsgModel:model height:TRACK_HEIGHT];
[[LXDanMuManager shareInstance] appendDanMuWithCustomView:customView toTrackIndex:-1];
}
-(void)maskTimeEnd{
// 关闭弹幕
[[LXDanMuManager shareInstance] destory];
//关闭socket
[self destorySocket];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)dealloc{
NSLog(@"弹幕销毁 聊天关闭");
}
-(void)destorySocket{
[self.manager disconnectSocket:self.client];
[self.manager disconnect];
[self.client disconnect];
self.client = nil;
self.manager = nil;
}
@end