Watch 9 Star 68 Fork 13

江南孤鹜 / ChatDemo-UI3.00-SimpleObjective-C

Join us
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
环信IM优化版Demo -- 基于环信IM官方Demo(版本3.x) spread retract

Clone or download
MainViewController.m 15.83 KB
Copy Edit Web IDE Raw Blame History
/************************************************************
* * Hyphenate CONFIDENTIAL
* __________________
* Copyright (C) 2016 Hyphenate Inc. All rights reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Hyphenate Inc.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Hyphenate Inc.
*/
#import "MainViewController.h"
#import "SettingsViewController.h"
#import "ApplyViewController.h"
#import "ChatViewController.h"
#import "ConversationListController.h"
#import "ContactListViewController.h"
#import "ChatUIHelper.h"
#import "RedPacketChatViewController.h"
#import <UserNotifications/UserNotifications.h>
static NSString *kConversationChatter = @"ConversationChatter";
static NSString *kMessageType = @"MessageType";
#if DEMO_CALL == 1
#import <Hyphenate/Hyphenate.h>
@interface MainViewController () <UIAlertViewDelegate, EMCallManagerDelegate>
#else
@interface MainViewController () <UIAlertViewDelegate>
#endif
{
ConversationListController *_chatListVC;
ContactListViewController *_contactsVC;
SettingsViewController *_settingsVC;
UIBarButtonItem *_addFriendItem;
}
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//if 使tabBarController中管理的viewControllers都符合 UIRectEdgeNone
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
[self setEdgesForExtendedLayout: UIRectEdgeNone];
}
self.title = NSLocalizedString(@"title.conversation", @"Conversations");
//获取未读消息数,此时并没有把self注册为SDK的delegate,读取出的未读数是上次退出程序时的
// [self didUnreadMessagesCountChanged];
NOTIFY_ADD(setupUntreatedApplyCount, kSetupUntreatedApplyCount);
NOTIFY_ADD(setupUnreadMessageCount, kSetupUnreadMessageCount);
NOTIFY_ADD(networkChanged, kConnectionStateChanged);
[self setupSubviews];
self.selectedIndex = 0;
UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[addButton setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
[addButton addTarget:_contactsVC action:@selector(addFriendAction) forControlEvents:UIControlEventTouchUpInside];
_addFriendItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
[self setupUnreadMessageCount];
[self setupUntreatedApplyCount];
TTAlertNoTitle(@"求大侠在github上给简版demo点赞(Star)😘 \n https://github.com/mengmakies/ChatDemo-UI3.00-Simple");
[ChatUIHelper shareHelper].contactViewVC = _contactsVC;
[ChatUIHelper shareHelper].conversationListVC = _chatListVC;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)dealloc
{
}
#pragma mark - UITabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item.tag == 0) {
self.title = NSLocalizedString(@"title.conversation", @"Conversations");
self.navigationItem.rightBarButtonItem = nil;
}else if (item.tag == 1){
self.title = NSLocalizedString(@"title.addressbook", @"AddressBook");
self.navigationItem.rightBarButtonItem = _addFriendItem;
}else if (item.tag == 2){
self.title = NSLocalizedString(@"title.setting", @"Setting");
self.navigationItem.rightBarButtonItem = nil;
[_settingsVC refreshConfig];
}
}
#pragma mark - private
- (void)setupSubviews
{
self.tabBar.accessibilityIdentifier = @"tabbar";
self.tabBar.backgroundImage = [[UIImage imageNamed:@"tabbarBackground"] stretchableImageWithLeftCapWidth:25 topCapHeight:25];
self.tabBar.selectionIndicatorImage = [[UIImage imageNamed:@"tabbarSelectBg"] stretchableImageWithLeftCapWidth:25 topCapHeight:25];
_chatListVC = [[ConversationListController alloc] initWithNibName:nil bundle:nil];
[_chatListVC networkChanged:_connectionState];
_chatListVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"title.conversation", @"Conversations")
image:[UIImage imageNamed:@"tabbar_chats"]
selectedImage:[UIImage imageNamed:@"tabbar_chatsHL"]];
_chatListVC.tabBarItem.tag = 0;
_chatListVC.tabBarItem.accessibilityIdentifier = @"conversation";
[self unSelectedTapTabBarItems:_chatListVC.tabBarItem];
[self selectedTapTabBarItems:_chatListVC.tabBarItem];
_contactsVC = [[ContactListViewController alloc] initWithNibName:nil bundle:nil];
_contactsVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"title.addressbook", @"AddressBook")
image:[UIImage imageNamed:@"tabbar_contacts"]
selectedImage:[UIImage imageNamed:@"tabbar_contactsHL"]];
_contactsVC.tabBarItem.tag = 1;
_contactsVC.tabBarItem.accessibilityIdentifier = @"contact";
[self unSelectedTapTabBarItems:_contactsVC.tabBarItem];
[self selectedTapTabBarItems:_contactsVC.tabBarItem];
_settingsVC = [[SettingsViewController alloc] init];
_settingsVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"title.setting", @"Setting")
image:[UIImage imageNamed:@"tabbar_setting"]
selectedImage:[UIImage imageNamed:@"tabbar_settingHL"]];
_settingsVC.tabBarItem.tag = 2;
_settingsVC.tabBarItem.accessibilityIdentifier = @"setting";
_settingsVC.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self unSelectedTapTabBarItems:_settingsVC.tabBarItem];
[self selectedTapTabBarItems:_settingsVC.tabBarItem];
self.viewControllers = @[_chatListVC, _contactsVC, _settingsVC];
[self selectedTapTabBarItems:_chatListVC.tabBarItem];
}
-(void)unSelectedTapTabBarItems:(UITabBarItem *)tabBarItem
{
[tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:14], NSFontAttributeName,
[UIColor whiteColor],NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
}
-(void)selectedTapTabBarItems:(UITabBarItem *)tabBarItem
{
[tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:14],NSFontAttributeName,
RGBACOLOR(0x00, 0xac, 0xff, 1),NSForegroundColorAttributeName,
nil] forState:UIControlStateSelected];
}
// 统计未读消息数
-(void)setupUnreadMessageCount
{
NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
NSInteger unreadCount = 0;
for (EMConversation *conversation in conversations) {
unreadCount += conversation.unreadMessagesCount;
}
if (_chatListVC) {
if (unreadCount > 0) {
_chatListVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%i",(int)unreadCount];
}else{
_chatListVC.tabBarItem.badgeValue = nil;
}
}
UIApplication *application = [UIApplication sharedApplication];
[application setApplicationIconBadgeNumber:unreadCount];
}
- (void)setupUntreatedApplyCount
{
NSInteger unreadCount = [[[ApplyViewController shareController] dataSource] count];
if (_contactsVC) {
if (unreadCount > 0) {
_contactsVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%i",(int)unreadCount];
}else{
_contactsVC.tabBarItem.badgeValue = nil;
}
}
}
- (void)networkChanged
{
_connectionState = [ChatUIHelper shareHelper].connectionState;
[_chatListVC networkChanged:_connectionState];
}
#pragma mark - 自动登录回调
- (void)willAutoReconnect{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSNumber *showreconnect = [ud objectForKey:@"identifier_showreconnect_enable"];
if (showreconnect && [showreconnect boolValue]) {
[self hideHud];
[self showHint:NSLocalizedString(@"reconnection.ongoing", @"reconnecting...")];
}
}
- (void)didAutoReconnectFinishedWithError:(NSError *)error{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSNumber *showreconnect = [ud objectForKey:@"identifier_showreconnect_enable"];
if (showreconnect && [showreconnect boolValue]) {
[self hideHud];
if (error) {
[self showHint:NSLocalizedString(@"reconnection.fail", @"reconnection failure, later will continue to reconnection")];
}else{
[self showHint:NSLocalizedString(@"reconnection.success", @"reconnection successful!")];
}
}
}
#pragma mark - public
- (void)jumpToChatList
{
if ([self.navigationController.topViewController isKindOfClass:[ChatViewController class]]) {
// ChatViewController *chatController = (ChatViewController *)self.navigationController.topViewController;
// [chatController hideImagePicker];
}
else if(_chatListVC)
{
[self.navigationController popToViewController:self animated:NO];
[self setSelectedViewController:_chatListVC];
}
}
- (EMConversationType)conversationTypeFromMessageType:(EMChatType)type
{
EMConversationType conversatinType = EMConversationTypeChat;
switch (type) {
case EMChatTypeChat:
conversatinType = EMConversationTypeChat;
break;
case EMChatTypeGroupChat:
conversatinType = EMConversationTypeGroupChat;
break;
case EMChatTypeChatRoom:
conversatinType = EMConversationTypeChatRoom;
break;
default:
break;
}
return conversatinType;
}
- (void)didReceiveLocalNotification:(UILocalNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
if (userInfo)
{
if ([self.navigationController.topViewController isKindOfClass:[ChatViewController class]]) {
// ChatViewController *chatController = (ChatViewController *)self.navigationController.topViewController;
// [chatController hideImagePicker];
}
NSArray *viewControllers = self.navigationController.viewControllers;
[viewControllers enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop){
if (obj != self)
{
if (![obj isKindOfClass:[ChatViewController class]])
{
[self.navigationController popViewControllerAnimated:NO];
}
else
{
NSString *conversationChatter = userInfo[kConversationChatter];
ChatViewController *chatViewController = (ChatViewController *)obj;
if (![chatViewController.conversation.conversationId isEqualToString:conversationChatter])
{
[self.navigationController popViewControllerAnimated:NO];
EMChatType messageType = [userInfo[kMessageType] intValue];
#ifdef REDPACKET_AVALABLE
chatViewController = [[RedPacketChatViewController alloc] initWithConversationChatter:conversationChatter conversationType:[self conversationTypeFromMessageType:messageType]];
#else
chatViewController = [[ChatViewController alloc] initWithConversationChatter:conversationChatter conversationType:[self conversationTypeFromMessageType:messageType]];
#endif
[self.navigationController pushViewController:chatViewController animated:NO];
}
*stop= YES;
}
}
else
{
ChatViewController *chatViewController = nil;
NSString *conversationChatter = userInfo[kConversationChatter];
EMChatType messageType = [userInfo[kMessageType] intValue];
#ifdef REDPACKET_AVALABLE
chatViewController = [[RedPacketChatViewController alloc] initWithConversationChatter:conversationChatter conversationType:[self conversationTypeFromMessageType:messageType]];
#else
chatViewController = [[ChatViewController alloc] initWithConversationChatter:conversationChatter conversationType:[self conversationTypeFromMessageType:messageType]];
#endif
[self.navigationController pushViewController:chatViewController animated:NO];
}
}];
}
else if (_chatListVC)
{
[self.navigationController popToViewController:self animated:NO];
[self setSelectedViewController:_chatListVC];
}
}
- (void)didReceiveUserNotification:(UNNotification *)notification
{
NSDictionary *userInfo = notification.request.content.userInfo;
if (userInfo)
{
if ([self.navigationController.topViewController isKindOfClass:[ChatViewController class]]) {
// ChatViewController *chatController = (ChatViewController *)self.navigationController.topViewController;
// [chatController hideImagePicker];
}
NSArray *viewControllers = self.navigationController.viewControllers;
[viewControllers enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop){
if (obj != self)
{
if (![obj isKindOfClass:[ChatViewController class]])
{
[self.navigationController popViewControllerAnimated:NO];
}
else
{
NSString *conversationChatter = userInfo[kConversationChatter];
ChatViewController *chatViewController = (ChatViewController *)obj;
if (![chatViewController.conversation.conversationId isEqualToString:conversationChatter])
{
[self.navigationController popViewControllerAnimated:NO];
EMChatType messageType = [userInfo[kMessageType] intValue];
#ifdef REDPACKET_AVALABLE
chatViewController = [[RedPacketChatViewController alloc] initWithConversationChatter:conversationChatter conversationType:[self conversationTypeFromMessageType:messageType]];
#else
chatViewController = [[ChatViewController alloc] initWithConversationChatter:conversationChatter conversationType:[self conversationTypeFromMessageType:messageType]];
#endif
[self.navigationController pushViewController:chatViewController animated:NO];
}
*stop= YES;
}
}
else
{
ChatViewController *chatViewController = nil;
NSString *conversationChatter = userInfo[kConversationChatter];
EMChatType messageType = [userInfo[kMessageType] intValue];
#ifdef REDPACKET_AVALABLE
chatViewController = [[RedPacketChatViewController alloc] initWithConversationChatter:conversationChatter conversationType:[self conversationTypeFromMessageType:messageType]];
#else
chatViewController = [[ChatViewController alloc] initWithConversationChatter:conversationChatter conversationType:[self conversationTypeFromMessageType:messageType]];
#endif
[self.navigationController pushViewController:chatViewController animated:NO];
}
}];
}
else if (_chatListVC)
{
[self.navigationController popToViewController:self animated:NO];
[self setSelectedViewController:_chatListVC];
}
}
@end

Comment ( 0 )

Sign in for post a comment

Objective-C
1
https://gitee.com/markies/ChatDemo-UI3.00-Simple.git
git@gitee.com:markies/ChatDemo-UI3.00-Simple.git
markies
ChatDemo-UI3.00-Simple
ChatDemo-UI3.00-Simple
master

Help Search