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

Multi-region architecture implementation #3

Open
wants to merge 1 commit into
base: Feature
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
13 changes: 13 additions & 0 deletions Classes/CycleAtlantaAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "ProgressView.h"
#import "OBARegionHelper.h"

@class TripManager;
@class NoteManager;
Expand Down Expand Up @@ -68,10 +69,22 @@
@property (nonatomic, assign) BOOL isRecording;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) ProgressView *storeLoadingView;
@property (nonatomic, strong) OBARegionHelper *regionHelper;
@property (nonatomic, strong) id regionObserver;

- (void) regionSelected;
- (void) showRegionListViewController;

/**
* Abstracts OBAModelDAO setters / getters and calls the appropriate analytics methods.
*/
- (void)writeSetRegionAutomatically:(BOOL) setRegionAutomatically;
- (BOOL)readSetRegionAutomatically;


- (NSString *)applicationDocumentsDirectory;
- (void)initUniqueIDHash;
- (void) showRegionSelectMessage;

@end

64 changes: 62 additions & 2 deletions Classes/CycleAtlantaAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#import "NoteManager.h"
#import <CoreData/NSMappingModel.h>
#import "ProgressView.h"
#import "OBAApplication.h"


#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
Expand All @@ -63,6 +65,8 @@

@implementation CycleAtlantaAppDelegate



@synthesize window;
@synthesize tabBarController;
@synthesize uniqueIDHash;
Expand All @@ -86,7 +90,36 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application
[window makeKeyAndVisible];
[self performSelectorInBackground:@selector(loadPersistentStore) withObject:nil];
*/
}

- (id)init {
self = [super init];
if (self) {
[[OBAApplication sharedApplication] start];
self.regionHelper = [[OBARegionHelper alloc] init];
[self.regionHelper updateNearestRegion];
}
return self;
}

- (void) showRegionSelectMessage {
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:@"Alert"
message:@"Region couldn't be found. Please select a region from settings."
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction
actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainWindow" bundle:nil];
PersonalInfoViewController *myVC = (PersonalInfoViewController *)[storyboard instantiateViewControllerWithIdentifier:@"personalInfo"];
[myVC setHideViews];
[self.window.rootViewController presentViewController:myVC animated:YES completion:nil];

}];

[alert addAction:defaultAction];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
}

-(void)loadPersistentStore
Expand Down Expand Up @@ -395,15 +428,42 @@ - (NSString *) applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

- (void)writeSetRegionAutomatically:(BOOL)setRegionAutomatically {
[[OBAApplication sharedApplication].modelDao writeSetRegionAutomatically:setRegionAutomatically];

}

- (BOOL)readSetRegionAutomatically {
BOOL readSetRegionAuto = [OBAApplication sharedApplication].modelDao.readSetRegionAutomatically;

return readSetRegionAuto;
}

- (void)regionSelected {
// [_regionNavigationController removeFromParentViewController];
// _regionNavigationController = nil;
// _regionListViewController = nil;
//
// [[OBAApplication sharedApplication] refreshSettings];
//
// self.window.rootViewController = self.tabBarController;
// [_window makeKeyAndVisible];
}

- (void)showRegionListViewController {
// _regionListViewController = [[OBARegionListViewController alloc] init];
// _regionNavigationController = [[UINavigationController alloc] initWithRootViewController:_regionListViewController];
//
// self.window.rootViewController = _regionNavigationController;
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
self.isRecording = nil;



}


Expand Down
18 changes: 18 additions & 0 deletions Classes/JsonUrlFetcherImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// JsonUrlFetcherImpl.h
// OneBusAwaySDK
//
// Created by Aaron Brethorst on 12/16/15.
// Copyright © 2015 One Bus Away. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "OBADataSource.h"

@interface JsonUrlFetcherImpl : NSObject<NSURLConnectionDelegate, NSURLConnectionDataDelegate, OBADataSourceConnection>
@property (nonatomic, copy) OBADataSourceCompletion completionBlock;
@property (nonatomic, copy) OBADataSourceProgress progressBlock;

- (instancetype)initWithCompletionBlock:(OBADataSourceCompletion)completion progressBlock:(OBADataSourceProgress)progress;
- (void)loadRequest:(NSURLRequest *)request;
@end
61 changes: 61 additions & 0 deletions Classes/JsonUrlFetcherImpl.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// JsonUrlFetcherImpl.m
// OneBusAwaySDK
//
// Created by Aaron Brethorst on 12/16/15.
// Copyright © 2015 One Bus Away. All rights reserved.
//

#import "JsonUrlFetcherImpl.h"

@interface JsonUrlFetcherImpl ()
@property(nonatomic,strong) NSURLSessionDataTask *task;
@end

@implementation JsonUrlFetcherImpl

- (instancetype)initWithCompletionBlock:(OBADataSourceCompletion)completion progressBlock:(OBADataSourceProgress)progress {
self = [super init];

if (self) {
_completionBlock = completion;
_progressBlock = progress;
}

return self;
}

- (void)loadRequest:(NSURLRequest *)request {

self.task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
id responseObject = nil;

NSURL *url = [NSURL URLWithString:@"https://script.google.com/macros/s/AKfycbxpN47XZQGAoh-N5wQtBETp51tznG3JnOrWsAVNy0xGJOkD8ibS/exec"];
NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:url];
NSURLResponse * response2;
data = [NSURLConnection sendSynchronousRequest:request2 returningResponse:&response error:&error];

if (data.length) {
NSError *jsonError = nil;
responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];

if (!responseObject && jsonError) {
error = jsonError;
}
}

dispatch_async(dispatch_get_main_queue(), ^{
self.completionBlock(responseObject, ((NSHTTPURLResponse*)response).statusCode, error);
});
}];



[self.task resume];
}

- (void)cancel {
[self.task cancel];
}

@end
15 changes: 15 additions & 0 deletions Classes/NSArray+OBAAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// NSArray+OBAAdditions.h
// org.onebusaway.iphone
//
// Created by Aaron Brethorst on 2/16/16.
// Copyright © 2016 OneBusAway. All rights reserved.
//

#import <Foundation/NSArray.h>

@interface NSArray (OBAAdditions)

- (NSArray*)oba_pickFirst:(NSUInteger)count;
- (NSArray*)oba_subarrayFromIndex:(NSUInteger)index;
@end
36 changes: 36 additions & 0 deletions Classes/NSArray+OBAAdditions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// NSArray+OBAAdditions.m
// org.onebusaway.iphone
//
// Created by Aaron Brethorst on 2/16/16.
// Copyright © 2016 OneBusAway. All rights reserved.
//

#import "NSArray+OBAAdditions.h"

@implementation NSArray (OBAAdditions)

- (NSArray*)oba_pickFirst:(NSUInteger)count
{
if (self.count == 0)
{
return self;
}

if (self.count < count)
{
count = self.count;
}

return [self subarrayWithRange:NSMakeRange(0, count)];
}

- (NSArray*)oba_subarrayFromIndex:(NSUInteger)index {
if (index >= self.count) {
return @[];
}

return [self subarrayWithRange:NSMakeRange(index, self.count - index)];
}

@end
13 changes: 13 additions & 0 deletions Classes/OBAAgencyV2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import "OBAHasReferencesV2.h"

NS_ASSUME_NONNULL_BEGIN

@interface OBAAgencyV2 : OBAHasReferencesV2<NSCopying,NSCoding>

@property (nonatomic, strong) NSString *agencyId;
@property (nonatomic, strong) NSString *url;
@property (nonatomic, strong) NSString *name;

@end

NS_ASSUME_NONNULL_END
36 changes: 36 additions & 0 deletions Classes/OBAAgencyV2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#import "OBAAgencyV2.h"


@implementation OBAAgencyV2

#pragma mark - NSCoding

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];

if (self) {
_agencyId = [aDecoder decodeObjectForKey:@"agencyId"];
_url = [aDecoder decodeObjectForKey:@"url"];
_name = [aDecoder decodeObjectForKey:@"name"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_agencyId forKey:@"agencyId"];
[aCoder encodeObject:_url forKey:@"url"];
[aCoder encodeObject:_name forKey:@"name"];
}

#pragma mark - NSCopying

- (id)copyWithZone:(NSZone *)zone {
OBAAgencyV2 *agency = [[self.class alloc] init];
agency->_agencyId = [_agencyId copyWithZone:zone];
agency->_url = [_url copyWithZone:zone];
agency->_name = [_name copyWithZone:zone];

return agency;
}

@end
78 changes: 78 additions & 0 deletions Classes/OBAApplication.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// OBAApplication.h
// OneBusAwaySDK
//
// Created by Dima Belov on 4/25/15.
//
// Copyright (c) 2015 Dima Belov
// Copyright Sebastian Kießling
// Copyright Ben Bodenmiller
// Copyright Aaron Brethorst
// Copyright Caitlin Bonnar
// Copyright Jon Bell
// Copyright Andrew Sullivan
// Copyright Aengus McMillin
//

#import <Foundation/Foundation.h>
#import "OBAModelDAO.h"
#import "OBAModelService.h"
#import "OBALocationManager.h"

NS_ASSUME_NONNULL_BEGIN

/**
* This notification is posted in refernce to a specific refreshSettings event, specifically when modelDao does not have an assigned region.
*/
extern NSString *const kOBAApplicationSettingsRegionRefreshNotification;

@interface OBAApplication : NSObject

@property (nonatomic, strong, readonly) OBAReferencesV2 *references;
@property (nonatomic, strong, readonly) OBAModelDAO *modelDao;
@property (nonatomic, strong, readonly) OBAModelService *modelService;
@property (nonatomic, strong, readonly) OBALocationManager *locationManager;

@property(nonatomic,strong,nullable) OBARegionV2 *region;
@property(nonatomic, retain) NSArray *allRegions;

/**
* This method should always be used to get an instance of this class. This class should not be initialized directly.
*
* @return singleton. Thread safe.
*/
+ (instancetype)sharedApplication;

/**
* Call this when the object has been fully configured.
*/
- (void)start;

/**
* Refreshes the internal in-memory state by reading the latest persisted data.
*/
- (void)refreshSettings;

/**
* Returns YES if the user has enabled darker system colors or reduced transparency.
*/
- (BOOL)useHighContrastUI;

/**
* e.g. "2.4.2"
*/
- (NSString*)formattedAppVersion;

/**
* e.g. "20151218.18"
*/
- (NSString*)formattedAppBuild;

/**
* e.g. "2.4.2 (20151218.18)"
*/
- (NSString*)fullAppVersionString;

@end

NS_ASSUME_NONNULL_END
Loading