123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- //
- // XYVoiceMatchViewController.m
- // Starbuds
- //
- // Created by 翟玉磊 on 2020/12/4.
- // Copyright © 2020 翟玉磊. All rights reserved.
- //
- #import "XYVoiceMatchViewController.h"
- #import "XYVoiceMatchFilterView.h"
- #import "XYVoiceMatchButton.h"
- #import "XYSphereView.h"
- #import <SDWebImage/UIButton+WebCache.h>
- #import "XYVoiceMatchSuccessView.h"
- #import "XYVoiceMatchMessageModel.h"
- #import "XYVoiceChatManager.h"
- #import "XYQuickMatchTopMatchSuccessView.h"
- #import "XYChatAPIManager.h"
- #import "XYVoiceChatViewController.h"
- #import "XYPeiPlayUserInfoEditAlertView.h"
- @interface XYVoiceMatchViewController ()<XYVoiceMatchingFilterViewDelegate>
- @property (nonatomic, strong) XYVoiceMatchViewModel *viewModel;
- @property (nonatomic, strong) UIButton *filterButton;
- @property (nonatomic, strong) XYVoiceMatchFilterView *filterView;
- @property (nonatomic, strong) XYSphereView *sphereView;
- @property (nonatomic, strong) XYVoiceMatchButton *matchButton;
- @property (nonatomic, strong) XYVoiceMatchSuccessView *matchSuccessView;
- @property (nonatomic, strong) XYQuickMatchTopMatchSuccessView *quickMatchTopMatchSuccessView;
- @property (nonatomic, assign) BOOL isAnimation;
- @property (nonatomic, strong) UIButton *backButton;
- @end
- @implementation XYVoiceMatchViewController
- - (void)dealloc {
- // if (_sphereView) {
- // [_sphereView clear];
- // [_sphereView removeFromSuperview];
- // _sphereView = nil;
- // }
- // if(_quickMatchTopMatchSuccessView){
- // [_quickMatchTopMatchSuccessView clear];
- // [_quickMatchTopMatchSuccessView removeFromSuperview];
- // _quickMatchTopMatchSuccessView = nil;
- // }
- }
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- self.prefersNavigationBarHidden = YES;
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self setupUI];
-
- /// 速配通知
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(voiceMatchMessageNotification:) name:VOICE_MATCH_NOTIFICATION object:nil];
-
- /// 语聊退出操作 彻底退出/重新匹配
- [[XYVoiceChatManager sharedInstance] setCloseVoiceChatBlock:^(BOOL isMatch) {
- self.viewModel.matchStatus = XYVoiceMatchingStatusNormal;
- [self updateViewMatchStatus];
- if (isMatch) {
- [self startVoiceMatch];
- }
- [self requestRemoteData];
- }];
- }
- - (void)viewWillDisappear:(BOOL)animated{
- [super viewWillDisappear:animated];
- self.quickMatchTopMatchSuccessView.isViewAppear = NO;
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
- self.quickMatchTopMatchSuccessView.isViewAppear = YES;
-
- [self getMatchUserList];
- }
- - (void)voiceMatchMessageNotification:(NSNotification *)notification {
- NSDictionary *msg = notification.object;
- XYVoiceMatchMessageModel *msgModel = XYVoiceMatchMessageModel.new;
- [msgModel yy_modelSetWithDictionary:msg];
-
- if ([msgModel.type isEqualToString:VOICE_CHAT_MATCH_FAIL]) {
- // 语音速配失败
- [SVProgressHUD showInfoWithStatus:kLocalizedString(@"匹配失败,请稍后再试")];
- if ([msgModel.hisId isEqualToString:self.viewModel.hisId]) {
- self.viewModel.matchStatus = XYVoiceMatchingStatusMatchFailure;
- [self updateViewMatchStatus];
- }
- }else if ([msgModel.type isEqualToString:VOICE_CHAT_MATCH_SUCCESS]) {
- // 语音速配成功
- if ([msgModel.hisId isEqualToString:self.viewModel.hisId]) {
- [self.matchSuccessView setupVoiceMatchWaitMessage:msgModel];
- self.viewModel.matchStatus = XYVoiceMatchingStatusMatchSuccess;
- [self updateViewMatchStatus];
-
- // 延迟跳转语聊界面
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- // 进入聊天界面
- [[XYVoiceChatManager sharedInstance] pushVoiceChatWithHisId:msgModel.hisId token:msgModel.userToken channelName:msgModel.channelName block:^{
- }];
- });
- }
- }else if ([msgModel.type isEqualToString:VOICE_CHAT_REJECT]) {
- // 语音速配拒绝
- if ([msgModel.hisId isEqualToString:self.viewModel.hisId]) {
- self.viewModel.matchStatus = XYVoiceMatchingStatusMatchFailure;
- [self startVoiceMatch];
- }
- }else if ([msgModel.type isEqualToString:VOICE_CHAT_CANCEL]) {
- // 语音速配取消
- if ([msgModel.hisId isEqualToString:self.viewModel.hisId]) {
- self.viewModel.matchStatus = XYVoiceMatchingStatusMatchFailure;
- [self updateViewMatchStatus];
- }
- }else {
- }
- }
- #pragma mark — Ovrride
- - (void)bindViewModel {
- self.viewModel = XYVoiceMatchViewModel.new;
- }
- - (void)requestRemoteData {
- //获取配置
- [self.viewModel loadData:^(id _Nullable responseModel) {
- [self setupViewData];
- [self reloadQuickMatchTopMatchSuccessView];
- } failure:^(ZYLNetworkError * _Nullable error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
- }];
- }
- - (void)setupViewData {
- // 更新状态
- [self updateViewMatchStatus];
- }
- - (void)updateViewMatchStatus {
- switch (self.viewModel.matchStatus) {
- case XYVoiceMatchingStatusNormal:
- case XYVoiceMatchingStatusMatchFailure:
-
- self.matchSuccessView.hidden = YES;
- self.matchButton.title = kLocalizedString(@"心动速配");
- if (self.viewModel.freeTime > 0) {
- self.matchButton.subtitle = [NSString stringWithFormat:kLocalizedString(@"免费试聊%ld秒"), self.viewModel.freeTime];
- }else {
- self.matchButton.subtitle = @"";
- }
- self.sphereView.isTimerStart = NO;
- break;
- case XYVoiceMatchingStatusOpenMatching:
-
- self.sphereView.isTimerStart = YES;
- self.matchSuccessView.hidden = YES;
- [self.filterView dismiss];
- self.matchButton.title = kLocalizedString(@"取消匹配");
- self.matchButton.subtitle = @"";
- break;
- case XYVoiceMatchingStatusMatchSuccess:
-
- self.sphereView.isTimerStart = NO;
- self.matchSuccessView.hidden = NO;
- [self.view bringSubviewToFront:self.matchSuccessView];
- break;
- default:
- break;
- }
- }
- - (void)reloadsphereView{
- [_sphereView removeFromSuperview];
- _sphereView = nil;
- [self.view addSubview:self.sphereView];
- }
- - (void)getMatchUserList{
- [self.viewModel getMatchUserList:^(ZYLResponseModel *_Nullable responseModel) {
-
- [self reloadsphereView];
-
- NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
-
- NSMutableArray *userarray = [[NSMutableArray alloc] initWithCapacity:0];
- NSArray *list = (NSArray *)responseModel.data[@"list"];
- for (NSInteger i = 0; i < list.count; i ++) {
- NSDictionary *dict = list[i];
- XYUserModel *model = [[XYUserModel alloc] init];
- model.userAvatar = dict[@"userAvatar"];
- [userarray addObject:model];
-
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- [btn sd_setImageWithURL:UrlForString(model.userAvatar) forState:UIControlStateNormal placeholderImage:placeholderUserMainBgImage(model.userSex)];
- btn.contentMode = UIViewContentModeScaleAspectFill;
- btn.frame = CGRectMake(0, 0, 44.0f, 44.0f);
- btn.tag = i;
- [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
- [array addObject:btn];
- [self.sphereView addSubview:btn];
-
- }
- [self.sphereView setItems:array];
-
- if (self.XYVoiceMatchViewControllerUserArrsBlock) {
- self.XYVoiceMatchViewControllerUserArrsBlock(userarray);
- }
-
- } failure:^(ZYLNetworkError * _Nullable error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
-
- }];
-
- }
- - (void)reloadQuickMatchTopMatchSuccessView{
- [self.viewModel getMatchSuccessList:^(ZYLResponseModel *_Nullable responseModel) {
- NSMutableArray *userarray = [[NSMutableArray alloc] initWithCapacity:0];
- NSArray *list = (NSArray *)responseModel.data[@"list"];
- for (NSInteger i = 0; i < list.count; i ++) {
- NSDictionary *dict = list[i];
- XYUserModel *model = [[XYUserModel alloc] init];
- model.userAvatar = dict[@"userAvatar"];
- model.userName = dict[@"targetUserName"];
- model.userId = dict[@"targetUserAvatar"];
- model.userNo = dict[@"targetUserName"];
- [userarray addObject:model];
- }
-
- [self.quickMatchTopMatchSuccessView reload:userarray];
-
- } failure:^(ZYLNetworkError * _Nullable error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
-
- }];
-
- }
- #pragma mark — Action
- - (void)backButtonAction{
- [_sphereView clear];
- [_sphereView removeFromSuperview];
- _sphereView = nil;
-
- [_quickMatchTopMatchSuccessView clear];
- [_quickMatchTopMatchSuccessView removeFromSuperview];
- _quickMatchTopMatchSuccessView = nil;
-
- if(self.viewModel.matchStatus == XYVoiceMatchingStatusOpenMatching){
- [self.viewModel cancelVoiceMatch:^(id _Nullable responseModel) {
- } failure:^(ZYLNetworkError * _Nullable error) {
- }];
- }
-
- [self.navigationController popViewControllerAnimated:YES];
- }
- /// 显示筛选view
- - (void)filterButtonAction:(id)sender {
- // 交友Tab,点[筛选]的次数
- [StatisticsManager event:@"home_match_screen_click"];
-
- if (self.filterView.hidden) {
- [self.filterView show];
- }else {
- [self.filterView dismiss];
- }
- }
- - (void)buttonPressed:(UIButton *)btn {
-
- // 交友Tab,点匹配模块-[用户头像]的次数
- [StatisticsManager event:@"home_match_headportrait_click"];
-
- if (self.isAnimation) {
- return;
- }
- self.isAnimation = YES;
- [self.sphereView timerStop];
-
- [UIView animateWithDuration:0.3 animations:^{
- btn.transform = CGAffineTransformMakeScale(2., 2.);
- } completion:^(BOOL finished) {
- [UIView animateWithDuration:0.3 animations:^{
- btn.transform = CGAffineTransformMakeScale(1., 1.);
- } completion:^(BOOL finished) {
- [self.sphereView timerStart];
- self.isAnimation = NO;
-
- XYUserModel *model = self.viewModel.matchUserArray[btn.tag];
- if (StringIsNotEmpty(model.userNo)) {
- [[XYChatRoomManager sharedInstance] showChatRoomWithRoomId:model.userNo roomCoverUrl:@"" completion:^{
-
- }];
- }else if (StringIsNotEmpty(model.userId)) {
- XYUserMainViewController *controller = XYUserMainViewController.new;
- controller.targetId = model.userId;
- [self.navigationController pushViewController:controller animated:YES];
- }
- }];
- }];
- }
- /// 开始语音匹配
- - (void)startVoiceMatch {
-
- if ([self.matchButton.title isEqualToString:kLocalizedString(@"心动速配")]) {
- // 交友Tab,点[匹配动态]的次数
- [StatisticsManager event:@"home_match_heartbeat_click"];
- }
-
- if ([XYUserInfoManager nowUser].userScore < 50 && [self.matchButton.title isEqualToString:kLocalizedString(@"心动速配")]) {
- [[XYPeiPlayUserInfoEditAlertView shareInstance] show:JoinTypeVoiceChatStart];
- //return;
- }
-
- if (![XYUserInfoManager nowUser]) {
- [ApplicationDelegate createQulickLoginPageVC];
- return;
- }
- [self voiceMatchAction];
- }
- - (void)voiceMatchAction {
- switch (self.viewModel.matchStatus) {
- case XYVoiceMatchingStatusNormal:
- case XYVoiceMatchingStatusMatchFailure:
- // 默认/暂停/失败->开始匹配
- {
- // 隐藏筛选viwe
- [self.filterView dismiss];
- [SVProgressHUD show];
- [self.viewModel sendVoiceMatch:^(id _Nullable responseModel) {
- [SVProgressHUD dismiss];
- [self updateViewMatchStatus];
- } failure:^(ZYLNetworkError * _Nullable error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
- }];
- }
- break;
- case XYVoiceMatchingStatusOpenMatching:
- // 匹配中->取消匹配
- {
- [SVProgressHUD show];
- [self.viewModel cancelVoiceMatch:^(id _Nullable responseModel) {
- [SVProgressHUD showSuccessWithStatus:kLocalizedString(@"已取消匹配")];
- [self updateViewMatchStatus];
- } failure:^(ZYLNetworkError * _Nullable error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
- }];
- }
- break;
- default:
- break;
- }
- }
- - (XYVoiceMatchingStatus)matchStatus {
- return self.viewModel.matchStatus;
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- [super touchesBegan:touches withEvent:event];
-
- [self.filterView dismiss];
- }
- #pragma mark — XYVoiceMatchingFilterViewDelegate
- - (void)voiceMatchingFilterType:(XYVoiceMatchingFilterType)type {
- self.viewModel.filterType = type;
- [self getMatchUserList];
- }
- #pragma mark — 设置子view
- - (void)setupUI {
-
- self.view.backgroundColor = Color_Clear;
-
- [self.view addSubview:self.filterButton];
- [self.view addSubview:self.sphereView];
- [self.view addSubview:self.matchButton];
-
- [self.filterButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.view).offset(-10.0f);
- make.top.equalTo(self.view).offset(STATUS_HEIGHT);
- make.height.equalTo(@48.0f);
- make.width.equalTo(@50.0f);
- }];
- [self.matchButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.view);
- make.width.equalTo(@160.0f);
- make.height.equalTo(@40.0f);
- make.bottom.equalTo(self.view).offset(-(68.0f+30.0f+40.0f));
- }];
-
- [self.matchButton addViewBorder:Color_Clear redian:20];
-
- [self quickMatchTopMatchSuccessView];
-
- [self backButton];
-
- }
- #pragma mark — Getter
- - (UIButton *)filterButton {
- if (!_filterButton) {
- _filterButton = [UIButton createButtonTextColor:Color_White textFont:Font(12)];
- [_filterButton setImage:ImageNamed(@"xy_Friend_voicematch_filter") forState:UIControlStateNormal];
- [_filterButton setTitle:kLocalizedString(@" 筛选") forState:UIControlStateNormal];
- [_filterButton addTarget:self action:@selector(filterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _filterButton;
- }
- - (XYVoiceMatchFilterView *)filterView {
- if (!_filterView) {
- _filterView = [[XYVoiceMatchFilterView alloc] init];
- [_filterView setupSelectedType:self.viewModel.filterType];
- _filterView.delegate = self;
- _filterView.hidden = YES;
- _filterView.alpha = 0;
- [self.view addSubview:_filterView];
- [_filterView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.view).offset(-16.0f);
- make.top.equalTo(self.filterButton.mas_bottom).offset(-10.0f);
- make.width.equalTo(@152.0f);
- make.height.equalTo(@74.0f);
- }];
- }
- return _filterView;
- }
- - (XYSphereView *)sphereView {
- if (!_sphereView) {
- CGFloat sphereViewW = SCREEN_WIDTH - 40 * 2;
- CGFloat sphereViewH = sphereViewW;
- _sphereView = [[XYSphereView alloc] initWithFrame:CGRectMake(40.0f, NAVGATION_HEIGHT+60.0f+40, sphereViewW, sphereViewH)];
- }
- return _sphereView;
- }
- - (XYVoiceMatchButton *)matchButton {
- if (!_matchButton) {
- _matchButton = [[XYVoiceMatchButton alloc] init];
- _matchButton.title = kLocalizedString(@"心动速配");
- _matchButton.subtitle = kLocalizedString(@"免费试聊0秒");
- _matchButton.backgroundColor = [UIColor colorGradientChangeWithSize:CGSizeMake(160.0f, 40.0f) direction:GradientChangeDirectionLevel startColor:ColorFromHexString(@"#5F0AFF") endColor:ColorFromHexString(@"#9858FF")];
- WeakSelf
- [_matchButton setActionButtonBlock:^{
- [weakSelf startVoiceMatch];
- }];
- }
- return _matchButton;
- }
- - (XYVoiceMatchSuccessView *)matchSuccessView {
- if (!_matchSuccessView) {
- _matchSuccessView = [[XYVoiceMatchSuccessView alloc] initWithFrame:self.view.bounds];
- _matchSuccessView.backgroundColor = ColorFromHexStringWithAlpha(@"#000000", 0.4f);
- _matchSuccessView.hidden = YES;
- [self.view addSubview:_matchSuccessView];
- }
- return _matchSuccessView;
- }
- - (XYQuickMatchTopMatchSuccessView *)quickMatchTopMatchSuccessView{
- if (_quickMatchTopMatchSuccessView == nil) {
- _quickMatchTopMatchSuccessView = [[XYQuickMatchTopMatchSuccessView alloc] init];
- _quickMatchTopMatchSuccessView.frame = CGRectMake(56, STATUS_HEIGHT+12, 165, 24);
- [self.view addSubview:_quickMatchTopMatchSuccessView];
- [_quickMatchTopMatchSuccessView setup];
- }
- return _quickMatchTopMatchSuccessView;
- }
- - (UIButton *)backButton {
- if (!_backButton) {
- _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
- _backButton.frame = CGRectMake(16.0f, STATUS_HEIGHT+12, 24, 24);
- [_backButton setImage:ImageNamed(@"icon_back_bai") forState:UIControlStateNormal];
- [_backButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:_backButton];
- }
- return _backButton;
- }
- @end
|