Skip to content

Commit

Permalink
Merge branch 'state_restoration'
Browse files Browse the repository at this point in the history
Conflicts:
	MMDrawerController/MMDrawerController.m
  • Loading branch information
kcharwood committed Aug 16, 2013
2 parents 1fbb1b9 + 9fc11ba commit 22adcbd
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 16 deletions.
69 changes: 54 additions & 15 deletions KitchenSink/ExampleFiles/MMAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,51 @@

#import <QuartzCore/QuartzCore.h>

@implementation MMAppDelegate
@interface MMAppDelegate ()

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
@property (nonatomic,strong) MMDrawerController * drawerController;

@end

@implementation MMAppDelegate
-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UIViewController * leftSideDrawerViewController = [[MMExampleLeftSideDrawerViewController alloc] init];

UIViewController * centerViewController = [[MMExampleCenterTableViewController alloc] initWithStyle:UITableViewStyleGrouped];

UIViewController * rightSideDrawerViewController = [[MMExampleRightSideDrawerViewController alloc] init];

UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:centerViewController];
[navigationController setRestorationIdentifier:@"MMExampleCenterNavigationControllerRestorationKey"];

MMDrawerController * drawerController = [[MMDrawerController alloc]
initWithCenterViewController:navigationController
leftDrawerViewController:leftSideDrawerViewController
rightDrawerViewController:rightSideDrawerViewController];
[drawerController setMaximumRightDrawerWidth:200.0];
[drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
[drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
self.drawerController = [[MMDrawerController alloc]
initWithCenterViewController:navigationController
leftDrawerViewController:leftSideDrawerViewController
rightDrawerViewController:rightSideDrawerViewController];
[self.drawerController setRestorationIdentifier:@"MMDrawer"];
[self.drawerController setMaximumRightDrawerWidth:200.0];
[self.drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
[self.drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];

[drawerController
[self.drawerController
setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
MMDrawerControllerDrawerVisualStateBlock block;
block = [[MMExampleDrawerVisualStateManager sharedManager]
drawerVisualStateBlockForDrawerSide:drawerSide];
drawerVisualStateBlockForDrawerSide:drawerSide];
if(block){
block(drawerController, drawerSide, percentVisible);
}
}];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:drawerController];
// Override point for customization after application launch.
[self.window setRootViewController:self.drawerController];

return YES;
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
Expand Down Expand Up @@ -94,4 +106,31 @@ - (void)applicationWillTerminate:(UIApplication *)application
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder{
return YES;
}

- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder{
return YES;
}


- (UIViewController *)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
NSString * key = [identifierComponents lastObject];
if([key isEqualToString:@"MMDrawer"]){
return self.window.rootViewController;
}
else if ([key isEqualToString:@"MMExampleCenterNavigationControllerRestorationKey"]) {
return ((MMDrawerController *)self.window.rootViewController).centerViewController;
}
else if ([key isEqualToString:@"MMExampleLeftSideDrawerController"]){
return ((MMDrawerController *)self.window.rootViewController).leftDrawerViewController;
}
else if ([key isEqualToString:@"MMExampleRightSideDrawerController"]){
return ((MMDrawerController *)self.window.rootViewController).rightDrawerViewController;
}
return nil;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ - (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
[self setRestorationIdentifier:@"MMExampleCenterControllerRestorationKey"];
}
return self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ @interface MMExampleLeftSideDrawerViewController ()

@implementation MMExampleLeftSideDrawerViewController

-(id)init{
self = [super init];
if(self){
[self setRestorationIdentifier:@"MMExampleLeftSideDrawerController"];
}
return self;
}

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"Left will appear");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ @interface MMExampleRightSideDrawerViewController ()
@end

@implementation MMExampleRightSideDrawerViewController
-(id)init{
self = [super init];
if(self){
[self setRestorationIdentifier:@"MMExampleRightSideDrawerController"];
}
return self;
}

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
Expand Down
3 changes: 3 additions & 0 deletions MMDrawerController/MMDrawerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
## How Open/Close Gestures are handled
Two gestures are added to every instance of a drawer controller, one for pan and one for touch. `MMDrawerController` is the delegate for each of the gesture recoginzers, and determines if a touch should be sent to the appropriate gesture when a touch is detected compared with the masks set for open and close gestures and the state of the drawer controller.
## Integrating with State Restoration
In order to opt in to state restoration for `MMDrawerController`, you must set the `restorationIdentifier` of your drawer controller. Instances of your centerViewController, leftDrawerViewController and rightDrawerViewController must also be configured with their own `restorationIdentifier` (and optionally a restorationClass) if you intend for those to be restored as well. If your MMDrawerController had an open drawer when your app was sent to the background, that state will also be restored.
## What this library doesn't do.
This library is not meant for:
- Top or bottom drawer views
Expand Down
45 changes: 45 additions & 0 deletions MMDrawerController/MMDrawerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
return animation;
}

static NSString *MMDrawerLeftDrawerKey = @"MMDrawerLeftDrawer";
static NSString *MMDrawerRightDrawerKey = @"MMDrawerRightDrawer";
static NSString *MMDrawerCenterKey = @"MMDrawerCenter";
static NSString *MMDrawerOpenSideKey = @"MMDrawerOpenSide";

@interface MMDrawerCenterContainerView : UIView
@property (nonatomic,assign) MMDrawerOpenCenterInteractionMode centerInteractionMode;
@property (nonatomic,assign) MMDrawerSide openSide;
Expand Down Expand Up @@ -182,6 +187,46 @@ -(void)commonSetup{
[self setCenterHiddenInteractionMode:MMDrawerOpenCenterInteractionModeNavigationBarOnly];
}

#pragma mark - State Restoration
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder{
[super encodeRestorableStateWithCoder:coder];
if (self.leftDrawerViewController){
[coder encodeObject:self.leftDrawerViewController forKey:MMDrawerLeftDrawerKey];
}

if (self.rightDrawerViewController){
[coder encodeObject:self.rightDrawerViewController forKey:MMDrawerRightDrawerKey];
}

if (self.centerViewController){
[coder encodeObject:self.centerViewController forKey:MMDrawerCenterKey];
}

[coder encodeInteger:self.openSide forKey:MMDrawerOpenSideKey];
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder{
UIViewController *controller;
MMDrawerSide openside;

[super decodeRestorableStateWithCoder:coder];

if ((controller = [coder decodeObjectForKey:MMDrawerLeftDrawerKey])){
self.leftDrawerViewController = [coder decodeObjectForKey:MMDrawerLeftDrawerKey];
}

if ((controller = [coder decodeObjectForKey:MMDrawerRightDrawerKey])){
self.rightDrawerViewController = controller;
}

if ((controller = [coder decodeObjectForKey:MMDrawerCenterKey])){
self.centerViewController = controller;
}

if ((openside = [coder decodeIntegerForKey:MMDrawerOpenSideKey])){
[self openDrawerSide:openside animated:false completion:nil];
}
}
#pragma mark - Open/Close methods
-(void)toggleDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated completion:(void (^)(BOOL finished))completion{
NSParameterAssert(drawerSide!=MMDrawerSideNone);
Expand Down

0 comments on commit 22adcbd

Please sign in to comment.