Permalink
Browse files

Remove old-school accelerometer code to replace with iOS shake recogn…

…izer

Also make "random" story work with the new models -- no more dependence on position in the subreddit array.
(Note: Really we should load "random" stories from the reddit API if possible, not just a loaded subreddit)
  • Loading branch information...
1 parent 865abee commit fd4daddd1137b09ce28a5e5deac5b68af1533d93 @rbruels rbruels committed Dec 12, 2010
View
@@ -47,6 +47,9 @@ NSString *const RedditDidFinishLoggingInNotification;
NSString *const RedditWasAddedNotification;
NSString *const MessageCountDidChangeNotification;
+BOOL shouldDetectDeviceShake;
+NSString *const DeviceDidShakeNotification;
+
NSString *const redditSoundLightsaber;
NSString *const redditSoundAlienHunter;
NSString *const redditSoundAlleyBrawler;
View
@@ -50,6 +50,10 @@
NSString *const MessageCountDidChangeNotification = @"MessageCountDidChangeNotification";
+NSString *const DeviceDidShakeNotification = @"DeviceDidShakeNotification";
+
+BOOL shouldDetectDeviceShake = YES;
+
NSString *const redditSoundLightsaber = @"lightsaber";
NSString *const redditSoundAlienHunter = @"alien-hunter";
NSString *const redditSoundAlleyBrawler = @"alley-brawler";
@@ -201,7 +201,8 @@ - (void)requestDidFinishLoad:(TTURLRequest*)request
for (NSDictionary *result in results)
{
RedditMessage *newMessage = [RedditMessage messageWithDictionary:[result objectForKey:@"data"]];
- NSLog(@"fuck %@", result);
+ //rb
+ NSLog(@"message %@", result);
if (newMessage)
{
[self.items addObject:newMessage];
@@ -17,7 +17,7 @@
int newsModeIndex;
BOOL canLoadMore;
- NSInteger totalStories;
+ NSUInteger totalStories;
}
@property (nonatomic, readonly) NSString *subreddit;
@property (nonatomic, readonly) NSMutableArray *stories;
@@ -26,6 +26,6 @@
- (id)initWithSubreddit:(NSString *)subreddit;
- (NSString *)newsModeString;
-- (int)totalStories;
+- (NSUInteger)totalStories;
@end
@@ -50,7 +50,6 @@ - (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more
[[TTURLCache sharedCache] removeURL:loadURL fromDisk:YES];
}
- NSLog(@"loadURL: %@", loadURL);
BOOL savedReddit = [self.subreddit isEqual:@"/saved/"];
TTURLRequest *activeRequest = [TTURLRequest requestWithURL:loadURL delegate:self];
@@ -94,7 +93,7 @@ - (void)requestDidFinishLoad:(TTURLRequest*)request
NSDictionary *data = [result objectForKey:@"data"];
Story *theStory = [Story storyWithDictionary:data inReddit:self];
- theStory.index = totalStories++;
+ theStory.index = [_stories count];
[_stories addObject:theStory];
}
@@ -118,6 +117,11 @@ - (NSString *)fullURL
return [NSString stringWithFormat:@"%@%@%@%@", RedditBaseURLString, self.subreddit, [self newsModeString], RedditAPIExtensionString];
}
+- (NSUInteger)totalStories
+{
+ return [self.stories count];
+}
+
- (NSString *)newsModeString
{
switch (newsModeIndex)
@@ -26,6 +26,7 @@
@property (nonatomic, assign) TTTableViewController *viewController;
++ (SubredditDataSource *)lastLoadedSubreddit;
- (id)initWithSubreddit:(NSString *)subreddit;
- (Story *)storyWithIndex:(int)anIndex;
@@ -16,10 +16,17 @@
#import "NSDictionary+JSON.h"
#import "SubredditViewController.h"
+static id lastLoadedSubreddit = nil;
+
@implementation SubredditDataSource
@synthesize viewController;
++ (SubredditDataSource *)lastLoadedSubreddit
+{
+ return lastLoadedSubreddit;
+}
+
- (id)initWithSubreddit:(NSString *)subreddit
{
if (self = [self init])
@@ -52,6 +59,7 @@ - (void)tableViewDidLoadModel:(UITableView*)tableView
[lastLoadedTime release];
lastLoadedTime = [[NSDate date] retain];
self.items = _subredditModel.stories;
+ lastLoadedSubreddit = self;
}
- (Story *)storyWithIndex:(int)anIndex
@@ -117,7 +117,7 @@ - (void)didSelectObject:(TTTableItem*)object atIndexPath:(NSIndexPath*)indexPath
- (void)tableViewDidLoadModel:(UITableView*)tableView
{
- NSLog(@"wut");
+ NSLog(@"table loaded model");
}
- (void)requestDidFinishLoad:(TTURLRequest*)request
@@ -211,7 +211,7 @@ - (void)add:(id)sender
- (void)edit:(id)sender
{
- [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
+ shouldDetectDeviceShake = NO;
self.tableView.editing = YES;
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(stopEditing:)] autorelease];
@@ -235,7 +235,7 @@ - (void)stopEditing:(id)sender
[[NSUserDefaults standardUserDefaults] setObject:order forKey:redditSortOrderKey];
[[NSUserDefaults standardUserDefaults] synchronize];
- [[UIAccelerometer sharedAccelerometer] setDelegate:(id <UIAccelerometerDelegate>)[iRedditAppDelegate sharedAppDelegate]];
+ shouldDetectDeviceShake = YES;
}
- (void)messageCountChanged:(NSNotification *)note
@@ -13,7 +13,7 @@
#import "StoryViewController.h"
#import "MessageDataSource.h"
-@interface iRedditAppDelegate : NSObject <UIApplicationDelegate, UIAccelerometerDelegate>
+@interface iRedditAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UINavigationController *navController;
@@ -13,14 +13,6 @@
#import "Constants.h"
#import "LoginController.h"
-#define kAccelerometerFrequency 25 //Hz
-#define kFilteringFactor 0.15
-#define kMinEraseInterval 0.8
-#define kEraseAccelerationThreshold 3.0
-
-UIAccelerationValue myAccelerometer[3];
-CFAbsoluteTime lastTime = 0.0;
-
extern NSMutableArray *visitedArray;
iRedditAppDelegate *sharedAppDelegate;
@@ -87,19 +79,30 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application
SubredditViewController *controller = [[[SubredditViewController alloc] initWithField:[TTTableTextItem itemWithText:initialRedditTitle URL:initialRedditURL]] autorelease];
[navController pushViewController:controller animated:NO];
-
- [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
- [[UIAccelerometer sharedAccelerometer] setDelegate:(id <UIAccelerometerDelegate>)self];
+
//login
[[LoginController sharedLoginController] loginWithUsername:[defaults stringForKey:redditUsernameKey] password:[defaults stringForKey:redditPasswordKey]];
shakingSound = 0;
[self reloadSound];
+
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(deviceDidShake:)
+ name:DeviceDidShakeNotification
+ object:nil];
[self performSelector:@selector(loadDataWithDelay) withObject:nil afterDelay:1.0];
}
+- (void)deviceDidShake:(NSNotification *)notif
+{
+ if(shouldDetectDeviceShake)
+ {
+ [self showRandomStory];
+ }
+}
+
- (void)loadDataWithDelay
{
randomDataSource = [[SubredditDataSource alloc] initWithSubreddit:@"/randomrising/"];
@@ -151,16 +154,9 @@ - (void)showRandomStory
//[[Beacon shared] startSubBeaconWithName:@"serendipityTime" timeSession:YES];
}
- SubredditDataSource *activeDataSource = randomDataSource;
- NSArray *viewControllers = navController.viewControllers;
-
- if (rand()%4 != 0 && [viewControllers count] > 2 &&
- [[viewControllers objectAtIndex:[viewControllers count] - 2] isKindOfClass:[SubredditViewController class]] &&
- [(SubredditDataSource *)[[viewControllers objectAtIndex:[viewControllers count] - 2] dataSource] isLoaded])
- activeDataSource = (id <TTTableViewDataSource, TTURLRequestDelegate>)[[viewControllers objectAtIndex:[viewControllers count] - 2] dataSource];
-
- int count = [activeDataSource totalStories];
- int randomIndex = rand()%count;
+ SubredditDataSource *activeDataSource = [SubredditDataSource lastLoadedSubreddit];
+ NSInteger count = [((SubredditDataModel *)activeDataSource.model) totalStories];
+ NSInteger randomIndex = count > 0 ? arc4random() % count : 0;
Story *story = nil;
@@ -203,39 +199,6 @@ - (void)dismissRandomViewController
randomController = nil;
}
-- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
-{
- UIAccelerationValue length, x, y, z;
-
- //Use a basic high-pass filter to remove the influence of the gravity
- myAccelerometer[0] = acceleration.x * kFilteringFactor + myAccelerometer[0] * (1.0 - kFilteringFactor);
- myAccelerometer[1] = acceleration.y * kFilteringFactor + myAccelerometer[1] * (1.0 - kFilteringFactor);
- myAccelerometer[2] = acceleration.z * kFilteringFactor + myAccelerometer[2] * (1.0 - kFilteringFactor);
-
- // Compute values for the three axes of the acceleromater
- x = acceleration.x - myAccelerometer[0];
- y = acceleration.y - myAccelerometer[0];
- z = acceleration.z - myAccelerometer[0];
-
- //Compute the intensity of the current acceleration
- length = sqrt(x * x + y * y + z * z);
-
- // If above a given threshold, play the erase sounds and erase the drawing view
- if((length >= kEraseAccelerationThreshold) && (CFAbsoluteTimeGetCurrent() > lastTime + kMinEraseInterval))
- {
- if ([[NSUserDefaults standardUserDefaults] boolForKey:playSoundOnShakeKey])
- {
- AudioServicesPlaySystemSound(shakingSound);
- AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
- }
-
- if ([[NSUserDefaults standardUserDefaults] boolForKey:shakeForStoryKey])
- [self showRandomStory];
-
- lastTime = CFAbsoluteTimeGetCurrent();
- }
-}
-
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (![navigationController.viewControllers containsObject:randomController])
Oops, something went wrong.

0 comments on commit fd4dadd

Please sign in to comment.