cdts/xdts-ios 3/TreeHole/Code/Features/Chat/ViewController/MsgListViewController.m

511 lines
23 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
//
// MsgListViewController.m
// Youth
//
// Created by mambaxie on 2022/1/1.
//
#import "MsgListViewController.h"
#import "TUIConversationListController.h"
#import "AvatarView.h"
#import "ChatViewController.h"
#import "BottleReadView.h"
#import "TUILogin.h"
#import "LoginService.h"
#import "WZLBadgeImport.h"
#import "TZImagePickerController+MTImagePicker.h"
#import "MessageNotificationListViewController.h"
#import "THGiveVipMessageCellData.h"
@interface MsgListViewController () <TUIConversationListControllerListener, V2TIMConversationListener>
@property (nonatomic, strong) AvatarView *avatarView;
@property (nonatomic, strong) UILabel* unreadCountLab;
@property (nonatomic, strong) UILabel *nickNameLabel;
@property (nonatomic, strong) TUIConversationListController *convVC;
@property (nonatomic, strong) UIButton *changeAccountButton;
@property (nonatomic, strong) UIButton *changeAccountTipsButton;
@property (nonatomic, strong) NSTimer *msgUnreadTimer;
@property (nonatomic, assign) int currentAcountUnreadMsgCount;
@property (nonatomic, assign) int anotherAcountUnreadMsgCount;
@property (nonatomic, assign) int needVipToClone;
@property (nonatomic, strong) UIButton *receiveNewMsgButton;
@property (nonatomic, strong) UIImage *avatarImage;
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) MTAlertView *alertView;
@end
@implementation MsgListViewController
- (BOOL)mt_nagationBarTransparent {
return YES;
}
- (BOOL)mt_prefersNavigationBarShadowHidden {
return NO;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (void)dealloc {
// [[V2TIMManager sharedInstance] removeConversationListener:self];
[self.msgUnreadTimer invalidate];
self.msgUnreadTimer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)updateChangeAccount {
[self.changeAccountButton setTitle:[LoginService isClone] ? @"切主号" : @"切分身" forState:UIControlStateNormal];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"聊天";
WeakSelf(self);
self.msgUnreadTimer = [NSTimer timerWithTimeInterval:60 repeats:YES block:^(NSTimer * _Nonnull timer) {
[weakself getC2CUnreadMsgCountWithCompletion:nil];
}];
[[NSRunLoop mainRunLoop] addTimer:self.msgUnreadTimer forMode:NSRunLoopCommonModes];
[self.msgUnreadTimer fire];
UIButton *msgButton = [[UIButton alloc] init];
msgButton.size = CGSizeMake(FIX_SIZE(32), FIX_SIZE(32));
// [msgButton setImage:ImageNamed(@"TH_praty_notice_icon") forState:UIControlStateNormal];
[msgButton setImage:ImageNamed(@"TH_praty_notice_icon") forState:UIControlStateNormal];
// UIView *msgBgView = [[UIView alloc] initWithFrame:msgButton.frame];
// msgButton.backgroundColor = [UIColor whiteColor];
msgButton.layer.cornerRadius = FIX_SIZE(12);
_unreadCountLab = [[UILabel alloc]initWithFrame:CGRectMake(msgButton.right - 13, -5, 18, 18)];
_unreadCountLab.text = @"1";
_unreadCountLab.font = MT_FONT_MEDIUM_NO_SCALE_SIZE(11);
_unreadCountLab.textAlignment = NSTextAlignmentCenter;
_unreadCountLab.textColor = COLOR_WITH_RGB(0xFFFFFF);
_unreadCountLab.backgroundColor = [UIColor redColor];
_unreadCountLab.layer.cornerRadius = 9;
_unreadCountLab.layer.masksToBounds = YES;
_unreadCountLab.hidden = YES;
[msgButton addSubview:_unreadCountLab];
// msgBgView.layer.borderWidth = FIX_SIZE(1.5);
// msgBgView.layer.borderColor = COLOR_WITH_RGB(0x090600).CGColor;
// msgBgView.clipsToBounds = YES;
// [self.view addSubview:msgBgView];
// msgButton.backgroundColor = [UIColor clearColor];
[msgButton addTouchUpInsideWithAction:^(UIButton * _Nullable button) {
[weakself.navigationController pushViewController:[[MessageNotificationListViewController alloc] init] animated:YES];
}];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:msgButton];
UIButton *changeAccountButton = [UIButton buttonWithType:UIButtonTypeSystem];
changeAccountButton.height = 24;
changeAccountButton.width = 61;
changeAccountButton.titleLabel.font = MT_FONT_REGULAR_NO_SCALE_SIZE(13);
[changeAccountButton setTitleColor:COLOR_WITH_RGB(0xE2B594) forState:UIControlStateNormal];
// changeAccountButton.backgroundColor = COLOR_WITH_RGB(0x2C2942);
changeAccountButton.layer.cornerRadius = 12;
changeAccountButton.layer.borderWidth = 1;
changeAccountButton.layer.borderColor = COLOR_WITH_RGB(0xE2B594).CGColor;
[changeAccountButton addTouchUpInsideWithAction:^(UIButton * _Nullable button) {
[weakself changeAccount];
}];
self.changeAccountButton = changeAccountButton;
[self updateChangeAccount];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:changeAccountButton];
UIButton *changeAccountTipsButton = [UIButton buttonWithType:UIButtonTypeSystem];
changeAccountTipsButton.height = 38;
changeAccountTipsButton.titleLabel.font = MT_FONT_REGULAR_NO_SCALE_SIZE(15);
[changeAccountTipsButton setTitleColor:COLOR_WITH_RGB(0x040000) forState:UIControlStateNormal];
[changeAccountTipsButton addTouchUpInsideWithAction:^(UIButton * _Nullable button) {
[weakself changeAccount];
}];
self.changeAccountTipsButton = changeAccountTipsButton;
UIImageView *bgImageView = [[UIImageView alloc] init];
// bgImageView.image = ImageNamed(@"TH_chat_bg");
bgImageView.image = ImageNamed(@"TH_BaseLocalBg");
bgImageView.frame = CGRectMake(0, 0,self.view.width , self.view.height);
bgImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:bgImageView];
// bgImageView.x = FIX_SIZE(37.5);
// bgImageView.width = self.view.width - bgImageView.x * 2;
// CGSize imageSize = bgImageView.image.size;
// bgImageView.height = bgImageView.width * imageSize.height / imageSize.width;
// TUIConversationListController
TUIConversationListController *conv = [[TUIConversationListController alloc] init];
conv.delegate = self;
conv.view.y = NAVIGATION_BAR_HEIGHT;
conv.view.backgroundColor = [UIColor clearColor];
conv.tableView.backgroundColor = [UIColor clearColor];
conv.view.height = Screen_Height - TAB_BAR_HEIGHT - conv.view.y;
conv.tableView.height = conv.view.height;
// [conv.view addSubview:bgImageView];
self.convVC = conv;
bgImageView.centerY = conv.view.height * 0.5;
// TUIConversationListController ViewController
[self addChildViewController:conv];
[self.view addSubview:conv.view];
[[V2TIMManager sharedInstance] addConversationListener:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vipBuySucceed) name:BuyVIPSuccessNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadedUserOtherInfos) name:LoadedUserOtherInfosNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLoginSucceed) name:LoginSuccessNotification object:nil];
UIButton *receiveNewMsgButton = [UIButton buttonWithType:UIButtonTypeSystem];
[receiveNewMsgButton setTintColor:UIColor.whiteColor];
// [receiveNewMsgButton setTitle:@"缘分匹配❤️" forState:UIControlStateNormal];
receiveNewMsgButton.size = CGSizeMake(FIX_SIZE(100), FIX_SIZE(100)*0.37);
// receiveNewMsgButton.layer.cornerRadius = FIX_SIZE(16);
// [receiveNewMsgButton setBackgroundColor:COLOR_WITH_RGB_A(0xC1971D, 0.6)];
// [receiveNewMsgButton setBackgroundImage:ImageNamed(@"TH_home_yuanFen") forState:UIControlStateNormal];
receiveNewMsgButton.titleLabel.font = MT_FONT_REGULAR_SIZE(14);
receiveNewMsgButton.right = self.view.width - FIX_SIZE(15);
receiveNewMsgButton.bottom = self.view.height - TabBar_Height - FIX_SIZE(60);
[receiveNewMsgButton addTouchUpInsideWithAction:^(UIButton * _Nullable button) {
[SVProgressHUD showWithStatus:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[PYHTTPManager postWithPath:@"getnewchatmessage" params:nil callback:^(id _Nullable rsp, NSError * _Nullable error) {
if (!error) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
return;
}
}];
});
}];
self.receiveNewMsgButton = receiveNewMsgButton;
receiveNewMsgButton.hidden = YES;
[self.view addSubview:receiveNewMsgButton];
#warning 1
[[V2TIMManager sharedInstance] getTotalUnreadMessageCount:^(UInt64 totalCount) {
// totalCount
// UI
// [weakself.customTabbar setNumAtIndex:1 num:totalCount];
weakself.currentAcountUnreadMsgCount = (int)totalCount;
[UIApplication sharedApplication].applicationIconBadgeNumber = totalCount;
} fail:^(int code, NSString *desc) {
//
// [weakself.tabBarController.tabBar setNumAtIndex:1 num:0];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}];
}
- (void)reloadMsgBadge {
WeakSelf(self);
[LoginService launchAppToHomeWithCompletion:^(id _Nullable rsp, NSError * _Nullable error) {
if (!error) {
int unreadCount = [rsp[@"unread_notice"] intValue];
// unreadCount = 2;
weakself.unreadCountLab.text = [NSString stringWithFormat:@"%d",unreadCount];
// self.navRightBtn.badgeCenterOffset = CGPointMake(-FIX_SIZE(5), FIX_SIZE(5));
if (unreadCount > 0) {
weakself.unreadCountLab.hidden = NO;
// [self.navRightBtn showBadgeWithStyle:WBadgeStyleNumber value:unreadCount animationType:WBadgeAnimTypeNone];
} else {
weakself.unreadCountLab.hidden = YES;
// [self.navRightBtn clearBadge];
}
// [PYAppService showBadgeOnTabBarItemAtIndex:1 number:unreadCount];
[PYAppService appConfig].is_wallet_notice = [rsp[@"is_wallet_notice"] intValue];
[PYAppService appConfig].wallet_des = rsp[@"wallet_des"];
// if ([ProfileViewController shouldShowBadge]) {
// [PYAppService showBadgeOnTabBarItemAtIndex:2 number:-1];
// }
}
}];
}
- (void)vipBuySucceed{
self.needVipToClone = 0;
// TODO
// [[MTAlertView currentShowingAlertView] dismiss];
}
- (void)getC2CUnreadMsgCountWithCompletion:(PYHTTPManagerCallback)completion {
[UserService getC2CUnreadMsgCountWithCompletion:^(id _Nullable rsp, NSError * _Nullable error) {
if (!error) {
int unreadCount = [rsp[@"unread_ctoc_num"] intValue];
self.needVipToClone = [rsp[@"need_vip"] intValue];
self.anotherAcountUnreadMsgCount = unreadCount;
NSString *tips = rsp[@"vip_alert_title"];
// tips = @"有6人和你交换了照片";
if (tips.length > 0) {
[self.changeAccountTipsButton setTitle:tips forState:UIControlStateNormal];
[self.changeAccountTipsButton sizeToFit];
self.changeAccountTipsButton.height = 38;
self.changeAccountTipsButton.width += 14;
UIImage *image = ImageNamed(@"chat_tips_bg");
//
image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(20, 150, 20, 30) resizingMode:UIImageResizingModeStretch];
[self.changeAccountTipsButton setBackgroundImage:image forState:UIControlStateNormal];
CGRect rect = [self.changeAccountButton.titleLabel convertRect:self.changeAccountButton.titleLabel.bounds toView:self.changeAccountButton.superview];
self.changeAccountTipsButton.top = CGRectGetMaxY(rect);
self.changeAccountTipsButton.left = [self.changeAccountButton convertRect:self.changeAccountButton.bounds toView:self.changeAccountButton.superview].origin.x;
[self.changeAccountButton.superview addSubview:self.changeAccountTipsButton];
} else {
[self.changeAccountTipsButton removeFromSuperview];
}
[self updateUnreadCountUI];
}
if (completion) {
completion(rsp, error);
}
}];
}
- (void)setupCloneInfo {
UIView *setupCloneInfoView = [[UIView alloc] init];
setupCloneInfoView.width = kMTAlertContentSizeDefaultWidth;
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.font = MT_FONT_MEDIUM_SIZE(16);
titleLabel.text = @"请设置你的分身昵称";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.y = FIX_SIZE(30);
[titleLabel sizeToFit];
titleLabel.centerX = setupCloneInfoView.width * 0.5;
[setupCloneInfoView addSubview:titleLabel];
PYThemeTextField *nicknameTextFiled = [PYThemeTextField fieldWithPlaceholder:@"分身是一个新的账户哦" maxLength:15];
nicknameTextFiled.y = titleLabel.bottom + 15;
nicknameTextFiled.x = setupCloneInfoView.width - FIX_SIZE(20);
nicknameTextFiled.width = setupCloneInfoView.width - nicknameTextFiled.x * 2;
[setupCloneInfoView addSubview:nicknameTextFiled];
UILabel *avatarTitleLabel = [[UILabel alloc] init];
avatarTitleLabel.font = MT_FONT_MEDIUM_SIZE(16);
avatarTitleLabel.text = @"请设置你的分身头像";
avatarTitleLabel.textColor = [UIColor whiteColor];
avatarTitleLabel.y = nicknameTextFiled.bottom + FIX_SIZE(30);
[avatarTitleLabel sizeToFit];
avatarTitleLabel.centerX = setupCloneInfoView.width * 0.5;
[setupCloneInfoView addSubview:avatarTitleLabel];
UIView *addAvatarItem = [[UIView alloc] init];
addAvatarItem.size = CGSizeMake(FIX_SIZE(120), FIX_SIZE(120));
addAvatarItem.backgroundColor = SUB_BG_COLOR;
addAvatarItem.layer.cornerRadius = FIX_SIZE(16);
addAvatarItem.clipsToBounds = YES;
addAvatarItem.y = avatarTitleLabel.bottom + FIX_SIZE(15);
addAvatarItem.centerX = setupCloneInfoView.width * 0.5;
[addAvatarItem addTapWithAction:^{
TZImagePickerController *picker = [TZImagePickerController mt_imagePickerWithMaxImagesCount:1 didFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
if (photos.count) {
self.avatarImageView.image = photos.firstObject;
self.avatarImageView.hidden = NO;
self.avatarImage = self.avatarImageView.image;
}
}];
[[PYAppService currentNavigationController] presentViewController:picker animated:YES completion:nil];
}];
[setupCloneInfoView addSubview:addAvatarItem];
UIImageView *addIconView = [[UIImageView alloc] init];
addIconView.size = CGSizeMake(FIX_SIZE(26), FIX_SIZE(26));
addIconView.image = [ImageNamed(@"TH_login_add_icon") mt_imageWithTintColor:UIColor.whiteColor];
addIconView.center = CGPointMake(addAvatarItem.width * 0.5, addAvatarItem.height * 0.5);
[addAvatarItem addSubview:addIconView];
UILabel *addLabel = [[UILabel alloc] init];
addLabel.text = @"点击选取头像";
addLabel.textColor = CONTENT_COLOR;
addLabel.font = SMALL_FONT;
[addLabel sizeToFit];
addLabel.y = addIconView.bottom + FIX_SIZE(13.5);
addLabel.centerX = addIconView.centerX;
[addAvatarItem addSubview:addLabel];
PYImageView *avatarImageView = [[PYImageView alloc] init];
avatarImageView.frame = addAvatarItem.bounds;;
avatarImageView.backgroundColor = addAvatarItem.backgroundColor;
avatarImageView.hidden = YES;
[addAvatarItem addSubview:avatarImageView];
self.avatarImageView = avatarImageView;
self.avatarImage = nil;
setupCloneInfoView.height = addAvatarItem.bottom;
// setupCloneInfoView.height = 100;
MTAlertView *alertView = [MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) {
config.customView = setupCloneInfoView;
config.otherTitle = @"OK";
config.otherHandler = ^(MTAlertButton *button) {
if (nicknameTextFiled.text.length < 2) {
[SVProgressHUD showInfoWithStatus:@"昵称至少2个字符"];
button.tag = MTNO_DISMISS_TAG;
return;
}
if (!self.avatarImage) {
[SVProgressHUD showInfoWithStatus:@"请先选择头像"];
button.tag = MTNO_DISMISS_TAG;
return;
}
button.tag = MTNO_DISMISS_TAG;
[SVProgressHUD showWithStatus:nil];
[PYHTTPManager uploadFileWithScene:AliOSSUploadSceneAvatar data:compressImageToDataIfNeed(self.avatarImage) completion:^(id _Nullable rsp, NSError * _Nullable error) {
if (!error) {
[UserService updateUserToRemoteWithParams:@{
@"avatar": rsp ?: @"",
@"nick_name": nicknameTextFiled.text
} completion:^(id _Nullable rsp, NSError * _Nullable error) {
if (!error) { //
[UserService currentUser].is_default_avatar = false;
[self.alertView dismiss];
}else{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[SVProgressHUD showErrorWithStatus:[error.userInfo objectForKey:@"message"]];
});
}
}];
}
}];
};
}];
self.alertView = alertView;
}
- (void)onLoginSucceed {
if ([UserService currentUser].is_new_clone) { // 使 +
[self setupCloneInfo];
}
}
- (void)changeAccount {
[self getC2CUnreadMsgCountWithCompletion:^(id _Nullable rsp, NSError * _Nullable error) {
if (![LoginService isClone]){
if (self.needVipToClone > 0) {
[PYAppService showVipAlertWithPath:@"open_clone" title:@"多一个身份,便多几段缘分" msg:@"和更多想认识的人放肆聊~\n这是送给会员的福利赶紧开启会员吧" source:@"open_clone"];
return;
}
}
[SVProgressHUD showWithStatus:nil];
[TUILogin logout:^{ // 退IM
if ([LoginService isClone]) {
[UserService backToMainWithCompletion:^(id _Nullable rsp, NSError * _Nullable error) {
[SVProgressHUD dismiss];
if (!error) {
}else {
[ToastUtil showToast:[error.userInfo objectForKey:@"message"]];
}
}];
} else {
[UserService changeToCloneWithCompletion:^(id _Nullable rsp, NSError * _Nullable error) {
[SVProgressHUD dismiss];
if (!error) {
}else {
[ToastUtil showToast:[error.userInfo objectForKey:@"message"]];
}
}];
}
} fail:^(int code, NSString *msg) {
[SVProgressHUD showErrorWithStatus:msg ? msg : @"切换账户失败(im error)"];
}];
}];
}
- (void)loadedUserOtherInfos {
[self.convVC.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//.
[self reloadMsgBadge];
[PYHTTPManager getWithPath:@"appconfig" params:nil callback:^(id _Nullable rsp, NSError * _Nullable error) {
// [self.receiveNewMsgButton setTitle:rsp[@"next_message_title"] forState:UIControlStateNormal];
// [self.receiveNewMsgButton sizeToFit];
[self.receiveNewMsgButton sd_setBackgroundImageWithURL:rsp[@"next_message_image"] forState:UIControlStateNormal];
// self.receiveNewMsgButton.size = CGSizeMake(FIX_SIZE(self.receiveNewMsgButton.width + 16), FIX_SIZE(30));
// self.receiveNewMsgButton.layer.cornerRadius = FIX_SIZE(16);
self.receiveNewMsgButton.hidden = [rsp[@"chat_has_next_message"] intValue] <= 0;
}];
[self.convVC.tableView reloadData];
}
- (void)updateUnreadCountUI {
self.changeAccountButton.badgeCenterOffset = CGPointMake(-5, 5);
[self.changeAccountButton showBadgeWithStyle:WBadgeStyleNumber value:self.anotherAcountUnreadMsgCount animationType:WBadgeAnimTypeNone];
[PYAppService showBadgeOnTabBarItemAtIndex:0 number:self.currentAcountUnreadMsgCount + self.anotherAcountUnreadMsgCount];
}
#pragma mark - TUIConversationListControllerListener
/**
*
*
* @param conversation
*/
- (NSString *)getConversationDisplayString:(V2TIMConversation *)conversation {
V2TIMMessage *lastMsg = conversation.lastMessage;
if (lastMsg.elemType == V2TIM_ELEM_TYPE_CUSTOM) {
//
if (lastMsg.customElem.data) {
NSDictionary *param = [NSJSONSerialization JSONObjectWithData:lastMsg.customElem.data options:NSJSONReadingAllowFragments error:nil];
NSString *typeIdStr = param[@"businessID"];
if ([typeIdStr isEqualToString:@"custom_message_vip"]) {
//VIP
return [THGiveVipMessageCellData getDisplayString:lastMsg];
}
}
}
return nil;
}
- (void)conversationListController:(TUIConversationListController *)conversationController didSelectConversation:(TUIConversationCell *)conversation
{
ChatViewController *chatVC = [[ChatViewController alloc] init];
chatVC.convData = conversation.convData;
[self.navigationController pushViewController:chatVC animated:YES];
}
#pragma mark - V2TIMConversationListener
- (void)onTotalUnreadMessageCountChanged:(UInt64)totalUnreadCount {
self.currentAcountUnreadMsgCount = (int)totalUnreadCount;
[self updateUnreadCountUI];
}
- (void)onNewConversation:(NSArray<V2TIMConversation*> *) conversationList {
NSMutableArray *userIDsM = [NSMutableArray array];
for (V2TIMConversation *conv in conversationList) {
if (conv.userID) {
[userIDsM addObject:conv.userID];
}
}
[UserService loadUserOtherInfosWithUserIDs:userIDsM userCache:YES comoletion:nil];
}
- (void)onSyncServerFinish {
NSMutableArray *userIDsM = [NSMutableArray array];
for (TUIConversationCellData *data in self.convVC.dataProvider.dataList) {
if (data.userID) {
[userIDsM addObject:data.userID];
}
}
[UserService loadUserOtherInfosWithUserIDs:userIDsM userCache:YES comoletion:nil];
}
@end