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

Added force touch support in order to access 4 recently played roms #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
120 changes: 81 additions & 39 deletions Classes/LMAppDelegate.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "LMAppDelegate.h"

#import "LMROMBrowserController.h"
#import "LMEmulatorController.h"

// TODO: LM: Better save UI to allow for multiple slots
// TODO: LM: save/show screenshots for save states in the save state manager
Expand All @@ -27,61 +28,102 @@ - (void)dealloc

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#if TARGET_IPHONE_SIMULATOR
// where are we?
NSLog(@"\nDocuments Directory:\n%@\n\n", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]);
#endif

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

LMROMBrowserController* romBrowser = [[LMROMBrowserController alloc] init];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:romBrowser];
self.viewController = nav;
[nav release];
[romBrowser release];
[self launch];
BOOL callPerformActionForShortcutItem = YES;
// Checks if force touch is available, but I'm not sure what will happen if this method is called on devices < 9.0
if([self forceTouchAvailable])
{
[self threeDTouchSetup];

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];
if(shortcutItem != nil)
{
[self openShortcut: shortcutItem];
callPerformActionForShortcutItem = NO;
}
}
return callPerformActionForShortcutItem;
}

- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
if([self forceTouchAvailable]) [self threeDTouchSetup];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
/*
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.
*/
BOOL shortcutItemWasOpened = [self openShortcut:shortcutItem];
completionHandler(shortcutItemWasOpened);
}

- (void)applicationWillEnterForeground:(UIApplication *)application
- (BOOL)openShortcut:(UIApplicationShortcutItem *)shortcutItem
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
BOOL opened = NO;

NSString *gameName = shortcutItem.localizedTitle;
if(gameName != nil)
{
LMEmulatorController* emulator = [[LMEmulatorController alloc] init];
emulator.romFileName = gameName;
[self.viewController presentViewController:emulator animated:YES completion:nil];
opened = YES;
}

return opened;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
- (BOOL)forceTouchAvailable
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
if([[[UIScreen mainScreen] traitCollection] forceTouchCapability] == UIForceTouchCapabilityAvailable)
{
NSLog(@"Force Touch Available");
return YES;
}
NSLog(@"Force Touch Unavailable");
return NO;
}

- (void)applicationWillTerminate:(UIApplication *)application
- (void)threeDTouchSetup
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
NSString *key = @"RECENTLY_TOUCHED";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *recentGames = [defaults stringArrayForKey:key];
if(recentGames != nil)
{
NSMutableArray *shortcutItems = [[NSMutableArray alloc] init];
NSString *shortcutType = @"com.MeSNEmu.recent";
UIApplicationShortcutIcon *shortcutFavoriteIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeFavorite];
for(int i = 0; i < [recentGames count]; i++)
{
UIApplicationShortcutItem *shortcutItem = [[UIApplicationShortcutItem alloc]
initWithType:shortcutType
localizedTitle:recentGames[i]
localizedSubtitle:nil
icon:shortcutFavoriteIcon
userInfo:nil];
[shortcutItems addObject:shortcutItem];
}
[UIApplication sharedApplication].shortcutItems = shortcutItems;
}
}

- (void)launch
{
#if TARGET_IPHONE_SIMULATOR
// where are we?
NSLog(@"\nDocuments Directory:\n%@\n\n", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]);
#endif

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

LMROMBrowserController* romBrowser = [[LMROMBrowserController alloc] init];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:romBrowser];
self.viewController = nav;
[nav release];
[romBrowser release];

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}

@end
48 changes: 47 additions & 1 deletion Classes/LMROMBrowserController.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)
{
LMEmulatorController* emulator = [[LMEmulatorController alloc] init];
LMFileListItem* item = [self LM_romItemForTableView:tableView indexPath:indexPath];
[self addMostRecentlyTouched: item];
if(_detailsItem == nil)
emulator.romFileName = item.fileName;
else
Expand All @@ -518,6 +519,36 @@ - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)
[emulator release];
}

- (void)addMostRecentlyTouched:(LMFileListItem *)item
{
NSString *key = @"RECENTLY_TOUCHED";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *gameName = item.fileName;
NSMutableArray *recentGames = [NSMutableArray arrayWithArray: [defaults stringArrayForKey:key]];


if(recentGames == nil)
{
NSArray *recentGames = [NSArray arrayWithObject:gameName];
[defaults setValue:recentGames forKey:key];
}
else if([recentGames containsObject:gameName])
{
[recentGames removeObject:gameName];
[recentGames addObject:gameName];
NSArray *newRecentGames = [NSArray arrayWithArray:recentGames];
[defaults setObject:newRecentGames forKey:key];
}
else
{
if([recentGames count] >= 4) [recentGames removeObjectAtIndex:0];
[recentGames addObject:gameName];
NSArray *newRecentGames = [NSArray arrayWithArray:recentGames];
[defaults setObject:newRecentGames forKey:key];
}

}

- (void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath
{
LMROMBrowserController* detailsBrowser = [[LMROMBrowserController alloc] initWithStyle:UITableViewStyleGrouped];
Expand All @@ -536,7 +567,7 @@ - (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEdi
LMFileListItem* item = [self LM_romItemForTableView:tableView indexPath:indexPath];
[[NSFileManager defaultManager] removeItemAtPath:[_romPath stringByAppendingPathComponent:item.fileName] error:nil];
[self LM_reloadROMList:NO];

[self deleteMostRecentlyDeleted: item];
BOOL isROMDetail = (_detailsItem != nil);
if(isROMDetail == YES)
{
Expand All @@ -554,6 +585,21 @@ - (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEdi
}
}

- (void)deleteMostRecentlyDeleted:(LMFileListItem *)item
{
NSString *key = @"RECENTLY_TOUCHED";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *gameName = item.fileName;
NSMutableArray *recentGames = [NSMutableArray arrayWithArray: [defaults stringArrayForKey:key]];

if(recentGames != nil)
{
[recentGames removeObject:gameName];
NSArray *newRecentGames = [NSArray arrayWithArray:recentGames];
[defaults setObject:newRecentGames forKey:key];
}
}

@end

#pragma mark -
Expand Down