element-ios/matrixConsole/ViewController/MasterTabBarController.m

195 lines
6.5 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 "MatrixSDKHandler.h"
#import "RecentsViewController.h"
2015-04-11 23:44:25 +00:00
#import "RecentListDataSource.h"
#import "SettingsViewController.h"
@interface MasterTabBarController () {
UINavigationController *recentsNavigationController;
RecentsViewController *recentsViewController;
SettingsViewController *settingsViewController;
UIImagePickerController *mediaPicker;
2015-04-11 23:44:25 +00:00
id sessionStateObserver;
MXSession *mxSession;
}
@end
@implementation MasterTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 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;
if ([recents isKindOfClass:[UISplitViewController class]]) {
UISplitViewController *splitViewController = (UISplitViewController *)recents;
recentsNavigationController = [splitViewController.viewControllers objectAtIndex:0];
} else if ([recents isKindOfClass:[UINavigationController class]]) {
recentsNavigationController = (UINavigationController*)recents;
}
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];
if ([settings isKindOfClass:[UINavigationController class]]) {
UINavigationController *settingsNavigationController = (UINavigationController*)settings;
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-04-11 23:44:25 +00:00
// Register session state observer
sessionStateObserver = [[NSNotificationCenter defaultCenter] addObserverForName:MXSessionStateDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
// Check whether the concerned session is the associated one
if (notif.object != mxSession) {
mxSession = notif.object;
// List all the recents for the logged user
MXKRecentListDataSource *listDataSource = [[RecentListDataSource alloc] initWithMatrixSession:mxSession];
[recentsViewController displayList:listDataSource];
// Update settings tab
settingsViewController.mxSession = mxSession;
2015-04-11 23:44:25 +00:00
}
}];
}
2014-10-13 12:45:29 +00:00
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([MatrixSDKHandler sharedHandler].status == MatrixSDKHandlerStatusLoggedOut) {
[self showAuthenticationScreen];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
recentsNavigationController = nil;
recentsViewController = nil;
settingsViewController = nil;
[self dismissMediaPicker];
2015-04-11 23:44:25 +00:00
[[NSNotificationCenter defaultCenter] removeObserver:sessionStateObserver];
}
2014-10-13 12:45:29 +00:00
#pragma mark -
- (void)restoreInitialDisplay {
// Dismiss potential media picker
if (mediaPicker) {
if (mediaPicker.delegate && [mediaPicker.delegate respondsToSelector:@selector(imagePickerControllerDidCancel:)]) {
[mediaPicker.delegate imagePickerControllerDidCancel:mediaPicker];
} else {
[self dismissMediaPicker];
}
}
[self popRoomViewControllerAnimated:NO];
}
#pragma mark -
- (void)showAuthenticationScreen {
[self restoreInitialDisplay];
// Reset user's information in settings
settingsViewController.mxSession = nil;
[self performSegueWithIdentifier:@"showAuth" sender:self];
}
2014-10-13 12:45:29 +00:00
- (void)showRoomCreationForm {
// Switch in Home Tab
[self setSelectedIndex:TABBAR_HOME_INDEX];
}
- (void)showRoom:(NSString*)roomId {
[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.selectedRoomId = roomId;
2014-10-30 17:23:02 +00:00
});
2014-10-10 12:30:09 +00:00
}
- (void)popRoomViewControllerAnimated:(BOOL)animated {
// Force back to recents list if room details is displayed in Recents Tab
if (recentsViewController) {
[recentsNavigationController popToViewController:recentsViewController animated:animated];
// Release the current selected room
recentsViewController.selectedRoomId = nil;
}
}
- (BOOL)isPresentingMediaPicker {
return nil != mediaPicker;
}
- (void)presentMediaPicker:(UIImagePickerController*)aMediaPicker {
[self dismissMediaPicker];
[self presentViewController:aMediaPicker animated:YES completion:^{
mediaPicker = aMediaPicker;
}];
}
- (void)dismissMediaPicker {
if (mediaPicker) {
[self dismissViewControllerAnimated:NO completion:nil];
mediaPicker.delegate = nil;
mediaPicker = nil;
}
}
- (void)setVisibleRoomId:(NSString *)aVisibleRoomId {
[[MatrixSDKHandler sharedHandler] restoreInAppNotificationsForRoomId:aVisibleRoomId];
_visibleRoomId = aVisibleRoomId;
}
@end