Files
yinmeng-ios/xplan-ios/Main/Tabbar/TabbarViewController.m
2022-03-28 15:56:38 +08:00

336 lines
14 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// ViewController.m
// xplan-ios
//
// Created by zu on 2021/8/31.
//
#import <UIKit/UIKit.h>
///Third
#import <flutter_boost/FlutterBoost.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import <NIMSDK/NIMSDK.h>
///Tool
#import "XPMacro.h"
#import "ThemeColor.h"
#import "UIImage+Utils.h"
#import "XCHUDTool.h"
#import "AccountInfoStorage.h"
#import "TTPopup.h"
#import "XPConstant.h"
#import "XPRoomMiniManager.h"
#import "ClientConfig.h"
#import "RtcManager.h"
#import "XCCurrentVCStackManager.h"
///Model
#import "AccountModel.h"
#import "RoomInfoModel.h"
#import "AttachmentModel.h"
#import "NobleLevelUpModel.h"
#import "XPMineVisitorUnReadModel.h"
///VC
#import "TabbarViewController.h"
#import "BaseViewController.h"
#import "LoginViewController.h"
#import "BaseNavigationController.h"
#import "LoginBindPhoneViewController.h"
#import "LoginFullInfoViewController.h"
#import "XplanFBFlutterViewContainer.h"
#import "XPMineViewController.h"
#import "SessionListViewController.h"
#import "XPHomeViewController.h"
#import "XPMiniRoomView.h"
#import "XPTeenagerAlertView.h"
#import "XPRoomViewController.h"
#import "XPNobleUpgradeLevelView.h"
///Present
#import "MainPresenter.h"
#import "MainProtocol.h"
#import "RoomHostDelegate.h"
@interface TabbarViewController () <BaseMvpProtocol, MainProtocol, NIMLoginManagerDelegate, NIMSystemNotificationManagerDelegate>
@property (nonatomic, strong) MainPresenter *presenter;
///房间最小化
@property (nonatomic,strong) XPMiniRoomView *roomMineView;
@end
@implementation TabbarViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self configTheme];
[self initTabs:NO];
[[ClientConfig shareConfig] clientInit];
[[NIMSDK sharedSDK].loginManager addDelegate:self];
[self.presenter juliandAdCallBackApi];
[self.view addSubview:self.roomMineView];
[[NSNotificationCenter defaultCenter] addObserverForName:kRoomMiniNotificationKey object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[self configRoomMiniNView:note.userInfo];
}];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.presenter autoLogin];
[self.presenter checkTranscation];
if ([XPRoomMiniManager shareManager].getRoomInfo == nil) {
[self.roomMineView hiddenRoomMiniView];
}
}
- (MainPresenter *)presenter {
if (_presenter == nil) {
_presenter = [[MainPresenter alloc] init];
[_presenter attatchView:self];
}
return _presenter;
}
/** 登录成功通过token且换取ticket后
1. 登录云信。
2. 获取用户信息。
3. 其实目前多余回调V层可以在P层里自行做这个逻辑。
*/
- (void)autoLoginSuccess {
[self.presenter loginNIM];
[self.presenter getUserInfo];
}
/// 获取用户信息后检查1、是否绑定手机号2、是否需要完善用户信息。
/// 该逻辑仅在刷新ticket后执行一次。
/// 当前服务端接口是有未完善信息的全局拦截的在BaseMvpPresenter里会触发1415去完善用户信息。
- (void)getUserInfoSuccess:(UserInfoModel *)userInfo {
if (!userInfo.isBindPhone && [ClientConfig shareConfig].iosPhoneBind) {
LoginBindPhoneViewController * bindPhoneVC = [[LoginBindPhoneViewController alloc] init];
BaseNavigationController * nav = [[BaseNavigationController alloc] initWithRootViewController:bindPhoneVC];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[self.navigationController presentViewController:nav animated:YES completion:nil];
return;
}
if (userInfo.nick == nil || userInfo.avatar == nil) {
[self completeUserInfo];
return;
}
[self initTabs:YES];
[self showTeenagerAlertView:userInfo];
[[NIMSDK sharedSDK].systemNotificationManager addDelegate:self];
}
#pragma mark - BaseMvpProtocol
- (void)tokenInvalid {
LoginViewController *lvc = [[LoginViewController alloc] init];
BaseNavigationController * nav = [[BaseNavigationController alloc] initWithRootViewController:lvc];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[self.navigationController presentViewController:nav animated:YES completion:nil];
}
- (void)completeUserInfo {
LoginFullInfoViewController * bindPhoneVC = [[LoginFullInfoViewController alloc] init];
BaseNavigationController * nav = [[BaseNavigationController alloc] initWithRootViewController:bindPhoneVC];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[self.navigationController presentViewController:nav animated:YES completion:nil];
}
#pragma mark - NIMLoginManagerDelegate
- (void)onAutoLoginFailed:(NSError *)error {
// 如果非上次登录设备 autoLogin 会返回 417
if (error.code == 417) {
@weakify(self);
AccountModel* accountModel = [AccountInfoStorage instance].getCurrentAccountInfo;
[[NIMSDK sharedSDK].loginManager login:accountModel.uid token:accountModel.netEaseToken completion:^(NSError * _Nullable error) {
if (error) {
@strongify(self);
[self.presenter logout];
}
}];
return;
}
[self.presenter logout];
}
- (void)onKickout:(NIMLoginKickoutResult *)result {
[XCHUDTool showErrorWithMessage:@"您已被踢下线,若非正常行为,请及时修改密码"];
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
if ([rootViewController presentedViewController]) { //有一个被present出来的控制器 要先dismiss
[[rootViewController presentedViewController] dismissViewControllerAnimated:YES completion:^{
if ([[XCCurrentVCStackManager shareManager] getCurrentVC].navigationController.viewControllers.count > 1) {
[[[XCCurrentVCStackManager shareManager] getCurrentVC].navigationController popToRootViewControllerAnimated:YES];
}
if ([[UIApplication sharedApplication].delegate.window.rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
if (tabVC.selectedViewController.navigationController) {
[tabVC.selectedViewController.navigationController popToRootViewControllerAnimated:YES];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[tabVC setSelectedIndex:0];
});
}
}];
}else { //不是被present出来的 就有可能是rootvc或者是被push出来的 所以要判断nav是不是只有一个vc 如果有多个就pop
if ([[XCCurrentVCStackManager shareManager] getCurrentVC].navigationController.viewControllers.count > 1) {
[[[XCCurrentVCStackManager shareManager] getCurrentVC].navigationController popToRootViewControllerAnimated:YES];
}
if ([[UIApplication sharedApplication].delegate.window.rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
if (tabVC.selectedViewController) {
if ([tabVC.selectedViewController isKindOfClass:[UINavigationController class]]) {
[tabVC.selectedViewController popToRootViewControllerAnimated:YES];
} else {
[tabVC.selectedViewController.navigationController popToRootViewControllerAnimated:YES];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[tabVC setSelectedIndex:0];
});
}
}
}
if ([XPRoomMiniManager shareManager].getRoomInfo) {
[[RtcManager instance] exitRoom];
[[NIMSDK sharedSDK].chatroomManager exitChatroom:[NSString stringWithFormat:@"%ld", [XPRoomMiniManager shareManager].getRoomInfo.roomId] completion:nil];
[self.roomMineView hiddenRoomMiniView];
}
[self.presenter logout];
}
- (void)configTheme {
self.view.backgroundColor = [ThemeColor tabbarViewColor];
[[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[ThemeColor tabbarViewColor] size:CGSizeMake(KScreenWidth, kTabBarHeight)]];
[[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
if (@available(iOS 10.0, *)) {
[[UITabBar appearance] setUnselectedItemTintColor:[ThemeColor tabbarNormalColor]];
}
}
- (void)initTabs:(BOOL)logined {
NSArray *normalImageNames = @[@"tab_gameHome_normal",@"tab_game_normal",@"tab_message_normal", @"tab_mine_normal"];
NSArray *selectImageNames = @[@"tab_gameHome_selected",@"tab_game_selected",@"tab_message_selected", @"tab_mine_selected"];
NSArray *tabLabel = @[@"派对",@"赛事",@"消息", @"我的"];
UIViewController *game;
UIViewController *msg;
UIViewController *me;
UIViewController * home;
if (logined) {
XplanFBFlutterViewContainer *fvcGame = XplanFBFlutterViewContainer.new;
#ifdef DEBUG
[fvcGame setName:@"debug" uniqueId:nil params:nil];
#else
[fvcGame setName:@"/" uniqueId:nil params:nil];
#endif
game = fvcGame;
msg = [[SessionListViewController alloc] init];
me = [[XPMineViewController alloc] init];
home = [[XPHomeViewController alloc] init];
} else {
game = [[BaseViewController alloc]init];
msg = [[SessionListViewController alloc]init];
me = [[BaseViewController alloc]init];
home = [[BaseViewController alloc] init];
}
[self createTabBarItem:home title:tabLabel[0] image:normalImageNames[0] selectedImage:selectImageNames[0]];
[self createTabBarItem:game title:tabLabel[1] image:normalImageNames[1] selectedImage:selectImageNames[1]];
[self createTabBarItem:msg title:tabLabel[2] image:normalImageNames[2] selectedImage:selectImageNames[2]];
[self createTabBarItem:me title:tabLabel[3] image:normalImageNames[3] selectedImage:selectImageNames[3]];
self.viewControllers = @[home,game,msg,me];
self.selectedIndex = 0;
}
- (BaseNavigationController *)createTabBarItem:(UIViewController *)itemVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{
itemVc.title = title;
itemVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
itemVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[itemVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[ThemeColor mainTextColor]} forState:UIControlStateSelected];
[itemVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[ThemeColor tabbarNormalColor]} forState:UIControlStateNormal];
BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:itemVc];
return nav;
}
- (void)showSuccessToast:(NSString *)msg {
[XCHUDTool showSuccessWithMessage:msg];
}
- (void)showErrorToast:(NSString *)msg {
[XCHUDTool showErrorWithMessage:msg];
}
- (void)showLoading {
[XCHUDTool showLoading];
}
- (void)hideHUD {
[XCHUDTool hideHUD];
}
#pragma mark - 房间最小化
- (void)configRoomMiniNView:(NSDictionary *)dic {
UserInfoModel * userInfo = [UserInfoModel modelWithDictionary:dic[@"userInfo"]];
RoomInfoModel * roomInfo = [RoomInfoModel modelWithDictionary:dic[@"roomInfo"]];
NSMutableDictionary * microQueue = dic[@"microQueue"];
[self.roomMineView configRoomMiniView:roomInfo userInfo:userInfo micQueue:microQueue];
}
#pragma mark - 青少年模式
- (void)showTeenagerAlertView:(UserInfoModel *)userInfo {
if (!userInfo || userInfo.uid <=0) return; ///用户信息为空 不处理
if (userInfo.parentMode) return;// 开启了青少年模式的话 就不用处理
NSString * teenagerKey = [NSString stringWithFormat:@"%@_%ld", kTeenagerAlertDatekey, userInfo.uid];
NSDate * oldDate = [[NSUserDefaults standardUserDefaults] objectForKey:teenagerKey];
BOOL isSameDay = [[NSCalendar currentCalendar] isDateInToday:oldDate];
if (isSameDay) return;
XPTeenagerAlertView * teenagerView = [[XPTeenagerAlertView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth - 45 * 2, 260)];
teenagerView.controller = self;
[TTPopup popupView:teenagerView style:TTPopupStyleAlert];
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:teenagerKey];
}
#pragma mark -NIMSystemNotificationManagerDelegate
- (void)onReceiveCustomSystemNotification:(NIMCustomSystemNotification *)notification {
if (notification.receiverType == NIMSessionTypeP2P) {
if (notification.content != nil) {
NSData *jsonData = [notification.content dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
if(err) {
NSLog(@"json解析失败%@",err);
return;
}
if ([dic[@"first"] intValue] == CustomMessageType_Noble_VIP && [dic[@"second"] intValue] == Custom_Message_Sub_Noble_LevelUp_User) { // 用户贵族升级
if ([dic isKindOfClass:[NSDictionary class]]) {
[self receiveNobleLevelUp:dic];
}
} else if ([dic[@"first"] integerValue] == CustomMessageType_First_VisitorRecord){//访客记录数量更新提示
AttachmentModel *attachment = [AttachmentModel modelWithJSON:notification.content];
XPMineVisitorUnReadModel *model = [XPMineVisitorUnReadModel modelWithJSON:attachment.data];
if ([model isKindOfClass:XPMineVisitorUnReadModel.class]) {
[[NSNotificationCenter defaultCenter] postNotificationName:kVisitorUnReadCountNotificationKey object:model];
}
}
}
}
}
- (void)receiveNobleLevelUp:(NSDictionary *)data {
XPNobleUpgradeLevelView *view = [[XPNobleUpgradeLevelView alloc] initWithFrame:CGRectMake(0, 0, 300, 285 + 22 + 44)];
NobleLevelUpModel *receiveInfo = [NobleLevelUpModel modelWithDictionary:data[@"data"]];
view.levelUpInfo = receiveInfo;
[TTPopup popupView:view style:TTPopupStyleAlert];
}
#pragma mark - Getters And Setters
- (XPMiniRoomView *)roomMineView {
if (!_roomMineView) {
_roomMineView = [[XPMiniRoomView alloc] init];
_roomMineView.controller = self;
_roomMineView.hidden = YES;
}
return _roomMineView;
}
@end