Skip to content

Commit

Permalink
[UI] Added Persistence mode to TTLauncher
Browse files Browse the repository at this point in the history
  • Loading branch information
aporat committed Jul 11, 2011
1 parent 06e0189 commit 78128d6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Three20UI/Headers/TTLauncherPersistenceMode.h
@@ -0,0 +1,21 @@
//
// Copyright 2009-2011 Facebook
//
// 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.
//

typedef enum {
TTLauncherPersistenceModeNone, // no persistence
TTLauncherPersistenceModeAll, // persists all pages & buttons
} TTLauncherPersistenceMode;

35 changes: 35 additions & 0 deletions src/Three20UI/Headers/TTLauncherView.h
Expand Up @@ -17,6 +17,9 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

// UI
#import "Three20UI/TTLauncherPersistenceMode.h"

@protocol TTLauncherViewDelegate;
@class TTPageControl;
@class TTLauncherButton;
Expand Down Expand Up @@ -51,6 +54,9 @@
BOOL _springing;
BOOL _editable;

NSString* _persistenceKey;
TTLauncherPersistenceMode _persistenceMode;

id<TTLauncherViewDelegate> _delegate;
}

Expand All @@ -71,6 +77,20 @@
@property (nonatomic, readonly) BOOL editing;
@property (nonatomic, assign) BOOL editable;

/**
* The key to use for storing persistence information.
*
* @default launcherViewPages
*/
@property (nonatomic, copy) NSString* persistenceKey;

/**
* How buttons are automatically persisted on termination and restored on launch.
*
* @default TTLauncherPersistenceModeNone
*/
@property (nonatomic) TTLauncherPersistenceMode persistenceMode;

- (void)addItem:(TTLauncherItem*)item animated:(BOOL)animated;

- (void)removeItem:(TTLauncherItem*)item animated:(BOOL)animated;
Expand All @@ -85,6 +105,21 @@

- (void)endEditing;

/**
* Persists all pages & buttons to user defaults.
*/
- (void)persistLauncherItems;

/**
* Restores all pages & button from user defaults and returns if sucess
*/
- (BOOL)restoreLauncherItems;

/**
* Erases all data stored in user defaults.
*/
- (void)resetDefaults;

/**
* Dims the launcher view except for a transparent circle around the given item. The given text
* will also be shown center-aligned below or above the circle, as appropriate. The item can be
Expand Down
2 changes: 2 additions & 0 deletions src/Three20UI/Headers/Three20UI.h
Expand Up @@ -46,9 +46,11 @@
#import "Three20UI/TTScrollViewDelegate.h"
#import "Three20UI/TTScrollViewDataSource.h"

// Launcher
#import "Three20UI/TTLauncherView.h"
#import "Three20UI/TTLauncherViewDelegate.h"
#import "Three20UI/TTLauncherItem.h"
#import "Three20UI/TTLauncherPersistenceMode.h"

#import "Three20UI/TTLabel.h"
#import "Three20UI/TTStyledTextLabel.h"
Expand Down
45 changes: 45 additions & 0 deletions src/Three20UI/Sources/TTLauncherView.m
Expand Up @@ -23,6 +23,7 @@
#import "Three20UI/TTPageControl.h"
#import "Three20UI/UIViewAdditions.h"


// UI (private)
#import "Three20UI/private/TTLauncherScrollView.h"
#import "Three20UI/private/TTLauncherHighlightView.h"
Expand Down Expand Up @@ -69,6 +70,8 @@ @implementation TTLauncherView
@synthesize editing = _editing;
@synthesize delegate = _delegate;
@synthesize editable = _editable;
@synthesize persistenceMode = _persistenceMode;
@synthesize persistenceKey = _persistenceKey;

///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(CGRect)frame {
Expand Down Expand Up @@ -99,6 +102,9 @@ - (id)initWithFrame:(CGRect)frame {
self.autoresizesSubviews = YES;
self.columnCount = kDefaultColumnCount;
self.editable = YES;
self.persistenceKey = @"launcherViewPages";
self.persistenceMode = TTLauncherPersistenceModeNone;

}

return self;
Expand Down Expand Up @@ -985,11 +991,50 @@ - (void)endEditing {

[self layoutButtons];

if (self.persistenceMode == TTLauncherPersistenceModeAll) {
[self persistLauncherItems];
}

if ([_delegate respondsToSelector:@selector(launcherViewDidEndEditing:)]) {
[_delegate launcherViewDidEndEditing:self];
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)persistLauncherItems {
NSData* pagesData = [NSKeyedArchiver archivedDataWithRootObject:self.pages];
[[NSUserDefaults standardUserDefaults] setValue:pagesData forKey:self.persistenceKey];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)restoreLauncherItems {
if (self.persistenceMode == TTLauncherPersistenceModeAll) {
NSData* pagesData = [[NSUserDefaults standardUserDefaults] objectForKey:self.persistenceKey];

NSObject* pages;
if (pagesData!=nil) {
pages = [NSKeyedUnarchiver unarchiveObjectWithData:pagesData];
}

if (pagesData!=nil && pages!=nil && [pages isKindOfClass:[NSArray class]]) {
self.pages = (NSArray*)pages;
return YES;
}
}

return NO;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)resetDefaults {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

[defaults removeObjectForKey:_persistenceKey];
[defaults synchronize];
}



///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)beginHighlightItem:(TTLauncherItem*)item withText:(NSString*)text {
Expand Down
6 changes: 6 additions & 0 deletions src/Three20UI/Three20UI.xcodeproj/project.pbxproj
Expand Up @@ -17,6 +17,7 @@
66F2E85712D426AF006FB485 /* TTTableFooterInfiniteScrollView.m in Sources */ = {isa = PBXBuildFile; fileRef = 66F2E85512D426AF006FB485 /* TTTableFooterInfiniteScrollView.m */; };
66F2E85F12D426DA006FB485 /* TTTableViewNetworkEnabledDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 66F2E85D12D426DA006FB485 /* TTTableViewNetworkEnabledDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; };
66F2E86512D426EF006FB485 /* TTTableViewNetworkEnabledDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 66F2E86312D426EF006FB485 /* TTTableViewNetworkEnabledDelegate.m */; };
6DB1E37D13CA885B00A72466 /* TTLauncherPersistenceMode.h in Headers */ = {isa = PBXBuildFile; fileRef = 6DB1E37C13CA885B00A72466 /* TTLauncherPersistenceMode.h */; settings = {ATTRIBUTES = (Public, ); }; };
6E60820111B0C31400C93CD4 /* TTNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E6081FF11B0C31400C93CD4 /* TTNavigationController.m */; };
6E60820311B0C32600C93CD4 /* TTNavigationController.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E60820211B0C32600C93CD4 /* TTNavigationController.h */; settings = {ATTRIBUTES = (Public, ); }; };
6E6454741184D2CD00F08CB1 /* Three20UI.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E6454731184D2CD00F08CB1 /* Three20UI.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -443,6 +444,7 @@
66F2E85512D426AF006FB485 /* TTTableFooterInfiniteScrollView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TTTableFooterInfiniteScrollView.m; path = Sources/TTTableFooterInfiniteScrollView.m; sourceTree = "<group>"; };
66F2E85D12D426DA006FB485 /* TTTableViewNetworkEnabledDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TTTableViewNetworkEnabledDelegate.h; path = Headers/TTTableViewNetworkEnabledDelegate.h; sourceTree = "<group>"; };
66F2E86312D426EF006FB485 /* TTTableViewNetworkEnabledDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TTTableViewNetworkEnabledDelegate.m; path = Sources/TTTableViewNetworkEnabledDelegate.m; sourceTree = "<group>"; };
6DB1E37C13CA885B00A72466 /* TTLauncherPersistenceMode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TTLauncherPersistenceMode.h; path = Headers/TTLauncherPersistenceMode.h; sourceTree = "<group>"; };
6E6081FF11B0C31400C93CD4 /* TTNavigationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TTNavigationController.m; path = Sources/TTNavigationController.m; sourceTree = "<group>"; };
6E60820211B0C32600C93CD4 /* TTNavigationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TTNavigationController.h; path = Headers/TTNavigationController.h; sourceTree = "<group>"; };
6E64543D1184BE1B00F08CB1 /* Project.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Project.xcconfig; path = Configurations/Project.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1561,6 +1563,7 @@
6E64583E1184E26500F08CB1 /* Launcher */ = {
isa = PBXGroup;
children = (
6DB1E37C13CA885B00A72466 /* TTLauncherPersistenceMode.h */,
6E6454D61184D4C500F08CB1 /* TTLauncherView.h */,
6E6456121184D4DA00F08CB1 /* TTLauncherView.m */,
6E6454D71184D4C500F08CB1 /* TTLauncherViewDelegate.h */,
Expand Down Expand Up @@ -1704,7 +1707,9 @@
isa = PBXGroup;
children = (
6EE738A31184ADB400A35176 /* libThree20Network.a */,
6DB1E2AF13CA813200A72466 /* libThree20Network-Xcode3.2.5.a */,
6EE738A51184ADB400A35176 /* NetworkUnitTests.octest */,
6DB1E2B113CA813200A72466 /* NetworkUnitTests-Xcode3.2.5.octest */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -1859,6 +1864,7 @@
666E18F51294543F001C1D97 /* TTSplitViewController.h in Headers */,
66F2E85412D426A5006FB485 /* TTTableFooterInfiniteScrollView.h in Headers */,
66F2E85F12D426DA006FB485 /* TTTableViewNetworkEnabledDelegate.h in Headers */,
6DB1E37D13CA885B00A72466 /* TTLauncherPersistenceMode.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 78128d6

Please sign in to comment.