mirror of
https://github.com/moonlight-stream/moonlight-ios.git
synced 2025-07-27 06:32:59 +00:00
162 lines
7.0 KiB
Objective-C
162 lines
7.0 KiB
Objective-C
//
|
|
// AppDelegate.m
|
|
// Moonlight
|
|
//
|
|
// Created by Diego Waxemberg on 1/17/14.
|
|
// Copyright (c) 2014 Moonlight Stream. All rights reserved.
|
|
//
|
|
|
|
#import "AppDelegate.h"
|
|
|
|
@implementation AppDelegate
|
|
|
|
@synthesize managedObjectContext = _managedObjectContext;
|
|
@synthesize managedObjectModel = _managedObjectModel;
|
|
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
|
|
|
|
static NSOperationQueue* mainQueue;
|
|
|
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
|
{
|
|
[[UILabel appearance] setFont:[UIFont fontWithName:@"Roboto-Regular" size:[UIFont systemFontSize]]];
|
|
[[UIButton appearance].titleLabel setFont:[UIFont fontWithName:@"Roboto-Regular" size:[UIFont systemFontSize]]];
|
|
|
|
|
|
// Generate selected segment background image
|
|
CGSize borderImageSize = CGSizeMake(1.f, 100.f);
|
|
|
|
UIGraphicsBeginImageContext(borderImageSize);
|
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
|
|
|
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
|
|
CGContextFillRect(context, CGRectMake(0.f, borderImageSize.height * 0.8, borderImageSize.width, borderImageSize.height));
|
|
|
|
UIImage *selectedSegmentBG = UIGraphicsGetImageFromCurrentImageContext();
|
|
UIGraphicsEndImageContext();
|
|
|
|
// Clear default border and background color
|
|
[[UISegmentedControl appearance] setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
|
|
[[UISegmentedControl appearance] setDividerImage:[[UIImage alloc] init] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
|
|
|
|
// Set selected segment background image
|
|
[[UISegmentedControl appearance] setBackgroundImage:[selectedSegmentBG imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
|
|
|
|
// Change font on UISegmentedControl
|
|
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
|
|
[UIColor whiteColor], NSForegroundColorAttributeName,
|
|
[UIFont fontWithName:@"Roboto-Regular" size:[UIFont systemFontSize]], NSFontAttributeName, nil] forState:UIControlStateNormal];
|
|
|
|
return YES;
|
|
}
|
|
|
|
- (void)applicationWillResignActive:(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.
|
|
}
|
|
|
|
- (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.
|
|
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
|
|
}
|
|
|
|
- (void)applicationWillEnterForeground:(UIApplication *)application
|
|
{
|
|
// 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.
|
|
}
|
|
|
|
- (void)applicationDidBecomeActive:(UIApplication *)application
|
|
{
|
|
// 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.
|
|
}
|
|
|
|
- (void)applicationWillTerminate:(UIApplication *)application
|
|
{
|
|
// Saves changes in the application's managed object context before the application terminates.
|
|
[self saveContext];
|
|
}
|
|
|
|
- (void)saveContext
|
|
{
|
|
NSError *error = nil;
|
|
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
|
|
if (managedObjectContext != nil) {
|
|
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
|
|
Log(LOG_E, @"Critical database error: %@, %@", error, [error userInfo]);
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma mark - Core Data stack
|
|
|
|
// Returns the managed object context for the application.
|
|
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
|
|
- (NSManagedObjectContext *)managedObjectContext
|
|
{
|
|
if (_managedObjectContext != nil) {
|
|
return _managedObjectContext;
|
|
}
|
|
|
|
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
|
|
if (coordinator != nil) {
|
|
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
|
|
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
|
|
}
|
|
return _managedObjectContext;
|
|
}
|
|
|
|
// Returns the managed object model for the application.
|
|
// If the model doesn't already exist, it is created from the application's model.
|
|
- (NSManagedObjectModel *)managedObjectModel
|
|
{
|
|
if (_managedObjectModel != nil) {
|
|
return _managedObjectModel;
|
|
}
|
|
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
|
|
return _managedObjectModel;
|
|
}
|
|
|
|
// Returns the persistent store coordinator for the application.
|
|
// If the coordinator doesn't already exist, it is created and the application's store added to it.
|
|
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
|
|
{
|
|
if (_persistentStoreCoordinator != nil) {
|
|
return _persistentStoreCoordinator;
|
|
}
|
|
|
|
NSURL *storeURL = [self getStoreURL];
|
|
|
|
NSError *error = nil;
|
|
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
|
|
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
|
|
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
|
|
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
|
|
// Log the error
|
|
Log(LOG_E, @"Critical database error: %@, %@", error, [error userInfo]);
|
|
|
|
// Drop the database
|
|
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
|
|
|
|
// Try again
|
|
return [self persistentStoreCoordinator];
|
|
}
|
|
|
|
return _persistentStoreCoordinator;
|
|
}
|
|
|
|
#pragma mark - Application's Documents directory
|
|
|
|
// Returns the URL to the application's Documents directory.
|
|
- (NSURL *)applicationDocumentsDirectory
|
|
{
|
|
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
|
|
}
|
|
|
|
- (NSURL*) getStoreURL {
|
|
return [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Limelight_iOS.sqlite"];
|
|
}
|
|
|
|
@end
|