From 9feb13682fc2b7dc95bf4654c6fcd781f27eb942 Mon Sep 17 00:00:00 2001 From: Yann Diorcet Date: Fri, 10 Aug 2012 01:21:30 +0200 Subject: [PATCH] Better UIViewController handling in UICompositeViewController --- .../LinphoneUI/UICompositeViewController.h | 6 +- .../LinphoneUI/UICompositeViewController.m | 160 +++++++++++------- Classes/LinphoneUI/UIMainBar.m | 10 +- Classes/SettingsViewController.m | 4 + Settings/InAppSettings.bundle/Root.plist | 8 + 5 files changed, 120 insertions(+), 68 deletions(-) diff --git a/Classes/LinphoneUI/UICompositeViewController.h b/Classes/LinphoneUI/UICompositeViewController.h index 98e59a478..8a64479d3 100644 --- a/Classes/LinphoneUI/UICompositeViewController.h +++ b/Classes/LinphoneUI/UICompositeViewController.h @@ -66,11 +66,8 @@ @interface UICompositeViewController : TPMultiLayoutViewController { @private UIView *stateBarView; - UIViewController *stateBarViewController; UIView *contentView; - UIViewController *contentViewController; UIView *tabBarView; - UIViewController *tabBarViewController; NSMutableDictionary *viewControllerCache; @@ -91,5 +88,8 @@ - (void)setToolBarHidden:(BOOL) hidden; - (UIViewController *)getCurrentViewController; - (UIInterfaceOrientation)currentOrientation; +#ifdef DEBUG +- (void)clearCache; +#endif @end diff --git a/Classes/LinphoneUI/UICompositeViewController.m b/Classes/LinphoneUI/UICompositeViewController.m index 9ab23d6b3..e81aca9be 100644 --- a/Classes/LinphoneUI/UICompositeViewController.m +++ b/Classes/LinphoneUI/UICompositeViewController.m @@ -78,6 +78,13 @@ [super dealloc]; } +@end +@interface UICompositeViewController () + +@property (nonatomic, retain) UIViewController *stateBarViewController; +@property (nonatomic, retain) UIViewController *tabBarViewController; +@property (nonatomic, retain) UIViewController *contentViewController; + @end @implementation UICompositeViewController @@ -85,6 +92,9 @@ @synthesize stateBarView; @synthesize contentView; @synthesize tabBarView; +@synthesize tabBarViewController = _tabBarViewController; +@synthesize stateBarViewController = _stateBarViewController; +@synthesize contentViewController = _contentViewController; @synthesize viewTransition; @@ -123,6 +133,10 @@ - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; + [self.stateBarViewController release]; + [self.tabBarViewController release]; + [self.contentViewController release]; + [contentView release]; [stateBarView release]; [tabBarView release]; @@ -134,6 +148,21 @@ } +#pragma mark - Property Functions + +- (UIViewController*)stateBarViewController { + return _stateBarViewController; +} + +- (UIViewController*)contentViewController { + return _contentViewController; +} + +- (UIViewController*)tabBarViewController { + return _tabBarViewController; +} + + #pragma mark - ViewController Functions - (void)viewDidLoad { @@ -148,56 +177,56 @@ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - [contentViewController viewWillAppear:animated]; - [tabBarViewController viewWillAppear:animated]; - [stateBarViewController viewWillAppear:animated]; + [self.contentViewController viewWillAppear:animated]; + [self.tabBarViewController viewWillAppear:animated]; + [self.stateBarViewController viewWillAppear:animated]; [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; - [contentViewController viewDidAppear:animated]; - [tabBarViewController viewDidAppear:animated]; - [stateBarViewController viewDidAppear:animated]; + [self.contentViewController viewDidAppear:animated]; + [self.tabBarViewController viewDidAppear:animated]; + [self.stateBarViewController viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; - [contentViewController viewWillDisappear:animated]; - [tabBarViewController viewWillDisappear:animated]; - [stateBarViewController viewWillDisappear:animated]; + [self.contentViewController viewWillDisappear:animated]; + [self.tabBarViewController viewWillDisappear:animated]; + [self.stateBarViewController viewWillDisappear:animated]; [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; - [contentViewController viewDidDisappear:animated]; - [tabBarViewController viewDidDisappear:animated]; - [stateBarViewController viewDidDisappear:animated]; + [self.contentViewController viewDidDisappear:animated]; + [self.tabBarViewController viewDidDisappear:animated]; + [self.stateBarViewController viewDidDisappear:animated]; } - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { currentOrientation = [self getCorrectInterfaceOrientation:toInterfaceOrientation]; [super willRotateToInterfaceOrientation:currentOrientation duration:duration]; - [contentViewController willRotateToInterfaceOrientation:currentOrientation duration:duration]; - [tabBarViewController willRotateToInterfaceOrientation:currentOrientation duration:duration]; - [stateBarViewController willRotateToInterfaceOrientation:currentOrientation duration:duration]; + [self.contentViewController willRotateToInterfaceOrientation:currentOrientation duration:duration]; + [self.tabBarViewController willRotateToInterfaceOrientation:currentOrientation duration:duration]; + [self.stateBarViewController willRotateToInterfaceOrientation:currentOrientation duration:duration]; } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [super willAnimateRotationToInterfaceOrientation:currentOrientation duration:duration]; - [contentViewController willAnimateRotationToInterfaceOrientation:currentOrientation duration:duration]; - [tabBarViewController willAnimateRotationToInterfaceOrientation:currentOrientation duration:duration]; - [stateBarViewController willAnimateRotationToInterfaceOrientation:currentOrientation duration:duration]; + [self.contentViewController willAnimateRotationToInterfaceOrientation:currentOrientation duration:duration]; + [self.tabBarViewController willAnimateRotationToInterfaceOrientation:currentOrientation duration:duration]; + [self.stateBarViewController willAnimateRotationToInterfaceOrientation:currentOrientation duration:duration]; [self update:nil tabBar:nil stateBar:nil fullscreen:nil]; } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; - [contentViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; - [tabBarViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; - [stateBarViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; + [self.contentViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; + [self.tabBarViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; + [self.stateBarViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { @@ -219,6 +248,10 @@ #pragma mark - +- (void)clearCache { + [viewControllerCache removeAllObjects]; +} + - (UIInterfaceOrientation)currentOrientation { return currentOrientation; } @@ -328,26 +361,29 @@ [super willAnimateRotationToInterfaceOrientation:correctOrientation duration:0]; [super didRotateFromInterfaceOrientation:orientation]; - orientation = contentViewController.interfaceOrientation; - [contentViewController willRotateToInterfaceOrientation:correctOrientation duration:0]; - [contentViewController willAnimateRotationToInterfaceOrientation:correctOrientation duration:0]; - [contentViewController didRotateFromInterfaceOrientation:orientation]; + orientation = self.contentViewController.interfaceOrientation; + [self.contentViewController willRotateToInterfaceOrientation:correctOrientation duration:0]; + [self.contentViewController willAnimateRotationToInterfaceOrientation:correctOrientation duration:0]; + [self.contentViewController didRotateFromInterfaceOrientation:orientation]; - orientation = tabBarViewController.interfaceOrientation; - [tabBarViewController willRotateToInterfaceOrientation:correctOrientation duration:0]; - [tabBarViewController willAnimateRotationToInterfaceOrientation:correctOrientation duration:0]; - [tabBarViewController didRotateFromInterfaceOrientation:orientation]; + orientation = self.tabBarViewController.interfaceOrientation; + [self.tabBarViewController willRotateToInterfaceOrientation:correctOrientation duration:0]; + [self.tabBarViewController willAnimateRotationToInterfaceOrientation:correctOrientation duration:0]; + [self.tabBarViewController didRotateFromInterfaceOrientation:orientation]; - orientation = stateBarViewController.interfaceOrientation; - [stateBarViewController willRotateToInterfaceOrientation:correctOrientation duration:0]; - [stateBarViewController willAnimateRotationToInterfaceOrientation:correctOrientation duration:0]; - [stateBarViewController didRotateFromInterfaceOrientation:orientation]; + orientation = self.stateBarViewController.interfaceOrientation; + [self.stateBarViewController willRotateToInterfaceOrientation:correctOrientation duration:0]; + [self.stateBarViewController willAnimateRotationToInterfaceOrientation:correctOrientation duration:0]; + [self.stateBarViewController didRotateFromInterfaceOrientation:orientation]; } #define IPHONE_STATUSBAR_HEIGHT 20 - (void)update: (UICompositeViewDescription*) description tabBar:(NSNumber*)tabBar stateBar:(NSNumber*)stateBar fullscreen:(NSNumber*)fullscreen { + UIViewController *oldContentViewController = self.contentViewController; + UIViewController *oldStateBarViewController = self.stateBarViewController; + UIViewController *oldTabBarViewController = self.tabBarViewController; // Copy view description UICompositeViewDescription *oldViewDescription = nil; @@ -370,18 +406,22 @@ [tabBarView.layer addAnimation:viewTransition forKey:@"transition"]; } } - - [UICompositeViewController removeSubView: contentViewController]; - if(oldViewDescription != nil && tabBarViewController != nil && oldViewDescription.tabBar != currentViewDescription.tabBar) { - [UICompositeViewController removeSubView: tabBarViewController]; - } - if(oldViewDescription != nil && stateBarViewController != nil && oldViewDescription.stateBar != currentViewDescription.stateBar) { - [UICompositeViewController removeSubView: stateBarViewController]; - } - stateBarViewController = [self getCachedController:description.stateBar]; - contentViewController = [self getCachedController:description.content]; - tabBarViewController = [self getCachedController:description.tabBar]; + UIViewController *newContentViewController = [self getCachedController:description.content]; + UIViewController *newStateBarViewController = [self getCachedController:description.stateBar]; + UIViewController *newTabBarViewController = [self getCachedController:description.tabBar]; + + [UICompositeViewController removeSubView: oldContentViewController]; + if(oldTabBarViewController != nil && oldTabBarViewController != newTabBarViewController) { + [UICompositeViewController removeSubView:oldTabBarViewController]; + } + if(oldStateBarViewController != nil && oldStateBarViewController != newStateBarViewController) { + [UICompositeViewController removeSubView:oldStateBarViewController]; + } + + self.stateBarViewController = newStateBarViewController; + self.contentViewController = newContentViewController; + self.tabBarViewController = newTabBarViewController; // Update rotation UIInterfaceOrientation correctOrientation = [self getCorrectInterfaceOrientation:[[UIDevice currentDevice] orientation]]; @@ -442,7 +482,7 @@ if(currentViewDescription.fullscreen) origin = 0; - if(stateBarViewController != nil && currentViewDescription.stateBarEnabled) { + if(self.stateBarViewController != nil && currentViewDescription.stateBarEnabled) { contentFrame.origin.y = origin + stateBarFrame.size.height; stateBarFrame.origin.y = origin; } else { @@ -452,15 +492,15 @@ // Resize TabBar CGRect tabFrame = tabBarView.frame; - if(tabBarViewController != nil && currentViewDescription.tabBarEnabled) { + if(self.tabBarViewController != nil && currentViewDescription.tabBarEnabled) { tabFrame.origin.y = viewFrame.size.height; tabFrame.origin.x = viewFrame.size.width; - tabFrame.size.height = tabBarViewController.view.frame.size.height; - tabFrame.size.width = tabBarViewController.view.frame.size.width; + tabFrame.size.height = self.tabBarViewController.view.frame.size.height; + tabFrame.size.width = self.tabBarViewController.view.frame.size.width; tabFrame.origin.y -= tabFrame.size.height; tabFrame.origin.x -= tabFrame.size.width; contentFrame.size.height = tabFrame.origin.y - contentFrame.origin.y; - for (UIView *view in tabBarViewController.view.subviews) { + for (UIView *view in self.tabBarViewController.view.subviews) { if(view.tag == -1) { contentFrame.size.height += view.frame.origin.y; break; @@ -478,15 +518,15 @@ // Set frames [contentView setFrame: contentFrame]; - [contentViewController.view setFrame: [contentView bounds]]; + [self.contentViewController.view setFrame: [contentView bounds]]; [tabBarView setFrame: tabFrame]; - CGRect frame = [tabBarViewController.view frame]; + CGRect frame = [self.tabBarViewController.view frame]; frame.size.width = [tabBarView bounds].size.width; - [tabBarViewController.view setFrame:frame]; + [self.tabBarViewController.view setFrame:frame]; [stateBarView setFrame: stateBarFrame]; - frame = [stateBarViewController.view frame]; + frame = [self.stateBarViewController.view frame]; frame.size.width = [stateBarView bounds].size.width; - [stateBarViewController.view setFrame:frame]; + [self.stateBarViewController.view setFrame:frame]; // Commit animation if(tabBar != nil || stateBar != nil || fullscreen != nil) { @@ -495,12 +535,12 @@ // Change view if(description != nil) { - [UICompositeViewController addSubView: contentViewController view:contentView]; - if(oldViewDescription == nil || oldViewDescription.tabBar != currentViewDescription.tabBar) { - [UICompositeViewController addSubView: tabBarViewController view:tabBarView]; + [UICompositeViewController addSubView: self.contentViewController view:contentView]; + if(oldTabBarViewController == nil || oldTabBarViewController != self.tabBarViewController) { + [UICompositeViewController addSubView: self.tabBarViewController view:tabBarView]; } - if(oldViewDescription == nil || oldViewDescription.stateBar != currentViewDescription.stateBar) { - [UICompositeViewController addSubView: stateBarViewController view:stateBarView]; + if(oldStateBarViewController == nil || oldStateBarViewController != self.stateBarViewController) { + [UICompositeViewController addSubView: self.stateBarViewController view:stateBarView]; } } @@ -528,7 +568,7 @@ } - (UIViewController *) getCurrentViewController { - return contentViewController; + return [[self.contentViewController retain] autorelease]; } @end diff --git a/Classes/LinphoneUI/UIMainBar.m b/Classes/LinphoneUI/UIMainBar.m index 2f12c029e..e513babd5 100644 --- a/Classes/LinphoneUI/UIMainBar.m +++ b/Classes/LinphoneUI/UIMainBar.m @@ -310,26 +310,26 @@ #pragma mark - Action Functions -- (IBAction)onHistoryClick: (id) sender { +- (IBAction)onHistoryClick:(id)event { [[PhoneMainView instance] changeCurrentView:[HistoryViewController compositeViewDescription]]; } -- (IBAction)onContactsClick: (id) event { +- (IBAction)onContactsClick:(id)event { [ContactSelection setSelectionMode:ContactSelectionModeNone]; [ContactSelection setAddAddress:nil]; [ContactSelection setSipFilter:FALSE]; [[PhoneMainView instance] changeCurrentView:[ContactsViewController compositeViewDescription]]; } -- (IBAction)onDialerClick: (id) event { +- (IBAction)onDialerClick:(id)event { [[PhoneMainView instance] changeCurrentView:[DialerViewController compositeViewDescription]]; } -- (IBAction)onSettingsClick: (id) event { +- (IBAction)onSettingsClick:(id)event { [[PhoneMainView instance] changeCurrentView:[SettingsViewController compositeViewDescription]]; } -- (IBAction)onChatClick: (id) event { +- (IBAction)onChatClick:(id)event { [[PhoneMainView instance] changeCurrentView:[ChatViewController compositeViewDescription]]; } diff --git a/Classes/SettingsViewController.m b/Classes/SettingsViewController.m index e5db3b200..b0c555e68 100644 --- a/Classes/SettingsViewController.m +++ b/Classes/SettingsViewController.m @@ -19,6 +19,7 @@ #import "SettingsViewController.h" #import "LinphoneManager.h" +#import "PhoneMainView.h" #import "UILinphone.h" #import "UACellBackgroundView.h" @@ -455,6 +456,7 @@ static UICompositeViewDescription *compositeDescription = nil; #ifndef DEBUG [hiddenKeys addObject:@"release_button"]; + [hiddenKeys addObject:@"clear_cache_button"]; #endif [hiddenKeys addObject:@"quit_button"]; // Hide for the moment @@ -505,6 +507,8 @@ static UICompositeViewDescription *compositeDescription = nil; [[UIApplication sharedApplication].keyWindow setRootViewController:nil]; [[LinphoneManager instance] destroyLibLinphone]; [LinphoneManager instanceRelease]; + } else if([key isEqual:@"clear_cache_button"]) { + [[PhoneMainView instance].mainViewController clearCache]; } #endif } diff --git a/Settings/InAppSettings.bundle/Root.plist b/Settings/InAppSettings.bundle/Root.plist index ebddc44c5..0d496d742 100644 --- a/Settings/InAppSettings.bundle/Root.plist +++ b/Settings/InAppSettings.bundle/Root.plist @@ -184,6 +184,14 @@ Type IASKButtonSpecifier + + Key + clear_cache_button + Title + Clear cache + Type + IASKButtonSpecifier + StringsTable Root