Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State restoration #69

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions KitchenSink/ExampleFiles/MMAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

#import <QuartzCore/QuartzCore.h>

static NSString *MMExampleCenterNavigationControllerRestorationKey = @"MMExampleCenterNavigationControllerRestorationKey";

@interface MMAppDelegate ()

@property (nonatomic,strong) MMDrawerController * drawerController;
Expand All @@ -38,23 +40,23 @@ @interface MMAppDelegate ()
@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"];
[navigationController setRestorationIdentifier:MMExampleCenterNavigationControllerRestorationKey];

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

[self.drawerController
setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
MMDrawerControllerDrawerVisualStateBlock block;
Expand Down Expand Up @@ -87,7 +89,7 @@ - (void)applicationWillResignActive:(UIApplication *)application

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

Expand Down Expand Up @@ -121,7 +123,7 @@ - (UIViewController *)application:(UIApplication *)application viewControllerWit
if([key isEqualToString:@"MMDrawer"]){
return self.window.rootViewController;
}
else if ([key isEqualToString:@"MMExampleCenterNavigationControllerRestorationKey"]) {
else if ([key isEqualToString:MMExampleCenterNavigationControllerRestorationKey]) {
return ((MMDrawerController *)self.window.rootViewController).centerViewController;
}
else if ([key isEqualToString:@"MMExampleLeftSideDrawerController"]){
Expand Down
91 changes: 67 additions & 24 deletions KitchenSink/ExampleFiles/MMExampleCenterTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ typedef NS_ENUM(NSInteger, MMCenterViewControllerSection){
MMCenterViewControllerSectionRightDrawerAnimation,
};

@interface MMExampleCenterTableViewController ()
static NSString *MMDrawerLeftDrawerEnabledKey = @"MMDrawerLeftDrawerEnabled";
static NSString *MMDrawerAnimationTypeLeftKey = @"MMDrawerAnimationTypeLeft";
static NSString *MMDrawerRightDrawerEnabledKey = @"MMDrawerRightDrawerEnabled";
static NSString *MMDrawerAnimationTypeRightKey = @"MMDrawerAnimationTypeRight";

@interface MMExampleCenterTableViewController () <UIViewControllerRestoration>

@end

Expand All @@ -48,39 +53,77 @@ - (id)initWithStyle:(UITableViewStyle)style
self = [super initWithStyle:style];
if (self) {
[self setRestorationIdentifier:@"MMExampleCenterControllerRestorationKey"];
[self setRestorationClass:[self class]];
}
return self;
}

#pragma mark - State Restoration
+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder{
return [[self alloc] initWithStyle:UITableViewStyleGrouped];
}

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder{
MMDrawerAnimationType leftDrawerAnimationType = [[MMExampleDrawerVisualStateManager sharedManager] leftDrawerAnimationType];
[coder encodeInteger:leftDrawerAnimationType forKey:MMDrawerAnimationTypeLeftKey];

MMDrawerAnimationType rightDrawerAnimationType = [[MMExampleDrawerVisualStateManager sharedManager] rightDrawerAnimationType];
[coder encodeInteger:rightDrawerAnimationType forKey:MMDrawerAnimationTypeRightKey];

[coder encodeBool:(self.mm_drawerController.leftDrawerViewController != nil) forKey:MMDrawerLeftDrawerEnabledKey];
[coder encodeBool:(self.mm_drawerController.rightDrawerViewController != nil) forKey:MMDrawerRightDrawerEnabledKey];
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder{
MMDrawerAnimationType leftDrawerAnimationType = [coder decodeIntegerForKey:MMDrawerAnimationTypeLeftKey];
[[MMExampleDrawerVisualStateManager sharedManager] setLeftDrawerAnimationType:leftDrawerAnimationType];

MMDrawerAnimationType rightDrawerAnimationType = [coder decodeIntegerForKey:MMDrawerAnimationTypeRightKey];
[[MMExampleDrawerVisualStateManager sharedManager] setRightDrawerAnimationType:rightDrawerAnimationType];

BOOL leftDrawerEnabled = [coder decodeBoolForKey:MMDrawerLeftDrawerEnabledKey];
BOOL rightDrawerEnabled = [coder decodeBoolForKey:MMDrawerRightDrawerEnabledKey];

if (!leftDrawerEnabled) {
[self.mm_drawerController setLeftDrawerViewController:nil];
[self.navigationItem setLeftBarButtonItems:nil animated:NO];
}

if (!rightDrawerEnabled) {
[self.mm_drawerController setRightDrawerViewController:nil];
[self.navigationItem setRightBarButtonItems:nil animated:NO];
}
}

- (void)viewDidLoad
{
[super viewDidLoad];

UITapGestureRecognizer * doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:doubleTap];

UITapGestureRecognizer * twoFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerDoubleTap:)];
[twoFingerDoubleTap setNumberOfTapsRequired:2];
[twoFingerDoubleTap setNumberOfTouchesRequired:2];
[self.view addGestureRecognizer:twoFingerDoubleTap];


[self setupLeftMenuButton];
[self setupRightMenuButton];

[self.navigationController.navigationBar setTintColor:[UIColor
colorWithRed:78.0/255.0
green:156.0/255.0
blue:206.0/255.0
alpha:1.0]];


MMLogoView * logo = [[MMLogoView alloc] initWithFrame:CGRectMake(0, 0, 29, 31)];
[self.navigationItem setTitleView:logo];
[self.navigationController.view.layer setCornerRadius:10.0f];


UIView *backView = [[UIView alloc] init];
[backView setBackgroundColor:[UIColor colorWithRed:208.0/255.0
green:208.0/255.0
Expand Down Expand Up @@ -144,14 +187,14 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

cell = [[MMCenterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}

UIColor * selectedColor = [UIColor
colorWithRed:1.0/255.0
green:15.0/255.0
Expand All @@ -162,18 +205,18 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
green:93.0/255.0
blue:102.0/255.0
alpha:1.0];

switch (indexPath.section) {
case MMCenterViewControllerSectionLeftDrawerAnimation:
case MMCenterViewControllerSectionRightDrawerAnimation:{
MMDrawerAnimationType animationTypeForSection;
MMDrawerAnimationType animationTypeForSection;
if(indexPath.section == MMCenterViewControllerSectionLeftDrawerAnimation){
animationTypeForSection = [[MMExampleDrawerVisualStateManager sharedManager] leftDrawerAnimationType];
}
else {
animationTypeForSection = [[MMExampleDrawerVisualStateManager sharedManager] rightDrawerAnimationType];
}

if(animationTypeForSection == indexPath.row){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
[cell.textLabel setTextColor:selectedColor];
Expand Down Expand Up @@ -201,7 +244,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
default:
break;
}
break;
break;
}
case MMCenterViewControllerSectionLeftViewState:{
[cell.textLabel setText:@"Enabled"];
Expand Down Expand Up @@ -230,7 +273,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
default:
break;
}

return cell;
}

Expand All @@ -252,9 +295,9 @@ -(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSIntege

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), 30)];

UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectInset(containerView.bounds, 14, 0)];

[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setText:[tableView.dataSource tableView:tableView titleForHeaderInSection:section]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:18.0]];
Expand All @@ -264,9 +307,9 @@ -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)s
alpha:1.0]];
[titleLabel setShadowColor:[[UIColor whiteColor] colorWithAlphaComponent:.5]];
[titleLabel setShadowOffset:CGSizeMake(0, 1)];

[containerView addSubview:titleLabel];

return containerView;
}

Expand Down Expand Up @@ -304,7 +347,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
sideDrawerViewController = self.mm_drawerController.rightDrawerViewController;
drawerSide = MMDrawerSideRight;
}

if(sideDrawerViewController){
[self.mm_drawerController
closeDrawerAnimated:YES
Expand All @@ -328,7 +371,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
UIViewController * vc = [[MMExampleLeftSideDrawerViewController alloc] init];
[self.mm_drawerController setLeftDrawerViewController:vc];
[self setupLeftMenuButton];

}
else if(drawerSide == MMDrawerSideRight){
UIViewController * vc = [[MMExampleRightSideDrawerViewController alloc] init];
Expand All @@ -339,7 +382,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

break;
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ -(void)viewDidDisappear:(BOOL)animated{


-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

if(section == MMDrawerSectionDrawerWidth)
return @"Left Drawer Width";
else
Expand Down Expand Up @@ -99,4 +99,4 @@ -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *
}
}

@end
@end
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ -(void)viewDidDisappear:(BOOL)animated{
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

if(section == MMDrawerSectionDrawerWidth)
return @"Right Drawer Width";
else
Expand All @@ -73,7 +73,7 @@ -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSI
[cell setAccessoryType:UITableViewCellAccessoryNone];
[cell.textLabel setText:[NSString stringWithFormat:@"Width %d",[self.drawerWidths[indexPath.row] intValue]]];
}

return cell;
}

Expand All @@ -93,4 +93,4 @@ -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *
}
}

@end
@end
Loading