element-ios/Vector/ViewController/MasterTabBarController.m

231 lines
6.8 KiB
Mathematica
Raw Normal View History

/*
Copyright 2014 OpenMarket Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "MasterTabBarController.h"
#import "AppDelegate.h"
#import "RecentsViewController.h"
2015-04-11 23:44:25 +00:00
#import "RecentListDataSource.h"
#import "SettingsViewController.h"
2015-06-11 15:03:31 +00:00
@interface MasterTabBarController ()
{
//Array of `MXSession` instances.
NSMutableArray *mxSessionArray;
// Tab bar view controllers
2015-04-17 12:24:08 +00:00
UINavigationController *recentsNavigationController;
RecentsViewController *recentsViewController;
SettingsViewController *settingsViewController;
}
@end
@implementation MasterTabBarController
2015-06-11 15:03:31 +00:00
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mxSessionArray = [NSMutableArray array];
// To simplify navigation into the app, we retrieve here the navigation controller and the view controller related
// to the recents list in Recents Tab.
// Note: UISplitViewController is not supported on iPhone for iOS < 8.0
UIViewController* recents = [self.viewControllers objectAtIndex:TABBAR_RECENTS_INDEX];
recentsNavigationController = nil;
2015-06-11 15:03:31 +00:00
if ([recents isKindOfClass:[UISplitViewController class]])
{
UISplitViewController *splitViewController = (UISplitViewController *)recents;
recentsNavigationController = [splitViewController.viewControllers objectAtIndex:0];
2015-06-11 15:03:31 +00:00
}
else if ([recents isKindOfClass:[UINavigationController class]])
{
recentsNavigationController = (UINavigationController*)recents;
}
2015-06-11 15:03:31 +00:00
if (recentsNavigationController)
{
for (UIViewController *viewController in recentsNavigationController.viewControllers)
{
if ([viewController isKindOfClass:[RecentsViewController class]])
{
recentsViewController = (RecentsViewController*)viewController;
}
}
}
2015-04-11 23:44:25 +00:00
// Retrieve the settings view controller
UIViewController* settings = [self.viewControllers objectAtIndex:TABBAR_SETTINGS_INDEX];
2015-06-11 15:03:31 +00:00
if ([settings isKindOfClass:[UINavigationController class]])
{
UINavigationController *settingsNavigationController = (UINavigationController*)settings;
2015-06-11 15:03:31 +00:00
for (UIViewController *viewController in settingsNavigationController.viewControllers)
{
if ([viewController isKindOfClass:[SettingsViewController class]])
{
settingsViewController = (SettingsViewController*)viewController;
}
}
}
// Sanity check
NSAssert(recentsViewController && settingsViewController, @"Something wrong in Main.storyboard");
}
2015-06-11 15:03:31 +00:00
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
2015-04-17 12:24:08 +00:00
// Check whether we're not logged in
2015-06-11 15:03:31 +00:00
if (![MXKAccountManager sharedManager].accounts.count)
{
[self showAuthenticationScreen];
}
}
2015-06-11 15:03:31 +00:00
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[[AppDelegate theDelegate] reloadMatrixSessions:NO];
}
2015-06-11 15:03:31 +00:00
- (void)dealloc
{
mxSessionArray = nil;
recentsNavigationController = nil;
recentsViewController = nil;
settingsViewController = nil;
}
2014-10-13 12:45:29 +00:00
#pragma mark -
2015-06-11 15:03:31 +00:00
- (void)restoreInitialDisplay
{
// Dismiss potential media picker
2015-08-17 17:15:58 +00:00
if (self.presentedViewController)
2015-06-11 15:03:31 +00:00
{
2015-08-17 17:15:58 +00:00
[self dismissViewControllerAnimated:NO completion:nil];
}
[self popRoomViewControllerAnimated:NO];
}
#pragma mark -
2015-06-11 15:03:31 +00:00
- (NSArray*)mxSessions
{
return [NSArray arrayWithArray:mxSessionArray];
}
2015-06-11 15:03:31 +00:00
- (void)addMatrixSession:(MXSession *)mxSession
{
if (mxSession)
{
// Update recents data source (The recents view controller will be updated by its data source)
2015-06-11 15:03:31 +00:00
if (!mxSessionArray.count)
{
// This is the first added session, list all the recents for the logged user
RecentListDataSource *recentlistDataSource = [[RecentListDataSource alloc] initWithMatrixSession:mxSession];
[recentsViewController displayList:recentlistDataSource];
2015-06-11 15:03:31 +00:00
}
else
{
[recentsViewController.dataSource addMatrixSession:mxSession];
}
// Update settings tab
[settingsViewController addMatrixSession:mxSession];
[mxSessionArray addObject:mxSession];
}
}
2015-06-11 15:03:31 +00:00
- (void)removeMatrixSession:(MXSession*)mxSession
{
// Update recents data source
[recentsViewController.dataSource removeMatrixSession:mxSession];
// Update settings tab
[settingsViewController removeMatrixSession:mxSession];
2015-04-17 12:24:08 +00:00
[mxSessionArray removeObject:mxSession];
// Check whether there are others sessions
2015-06-11 15:03:31 +00:00
if (!mxSessionArray.count)
{
// Keep reference on existing dataSource to release it properly
MXKRecentsDataSource *previousRecentlistDataSource = recentsViewController.dataSource;
[recentsViewController displayList:nil];
[previousRecentlistDataSource destroy];
}
}
2015-06-11 15:03:31 +00:00
- (void)showAuthenticationScreen
{
[self restoreInitialDisplay];
[self performSegueWithIdentifier:@"showAuth" sender:self];
}
2015-06-11 15:03:31 +00:00
- (void)showRoom:(NSString*)roomId withMatrixSession:(MXSession*)mxSession
{
[self restoreInitialDisplay];
// Switch on Recents Tab
2014-10-10 12:30:09 +00:00
[self setSelectedIndex:TABBAR_RECENTS_INDEX];
2014-10-30 17:23:02 +00:00
// Select room to display its details (dispatch this action in order to let TabBarController end its refresh)
dispatch_async(dispatch_get_main_queue(), ^{
[recentsViewController selectRoomWithId:roomId inMatrixSession:mxSession];
2014-10-30 17:23:02 +00:00
});
2014-10-10 12:30:09 +00:00
}
2015-06-11 15:03:31 +00:00
- (void)popRoomViewControllerAnimated:(BOOL)animated
{
// Force back to recents list if room details is displayed in Recents Tab
2015-06-11 15:03:31 +00:00
if (recentsViewController)
{
[recentsNavigationController popToViewController:recentsViewController animated:animated];
// Release the current selected room
[recentsViewController closeSelectedRoom];
}
}
2015-06-11 15:03:31 +00:00
- (void)setVisibleRoomId:(NSString *)roomId
{
if (roomId)
{
// Enable inApp notification for this room in all existing accounts.
NSArray *mxAccounts = [MXKAccountManager sharedManager].accounts;
2015-06-11 15:03:31 +00:00
for (MXKAccount *account in mxAccounts)
{
[account updateNotificationListenerForRoomId:roomId ignore:NO];
}
}
_visibleRoomId = roomId;
}
@end