Browse Source

New API Handling Class!

v1.2.2
Abhishek Banthia 9 years ago
parent
commit
de0b888fe6
  1. 6
      Clocker.xcodeproj/project.pbxproj
  2. BIN
      Clocker.xcodeproj/project.xcworkspace/xcuserdata/abhishekbanthia.xcuserdatad/UserInterfaceState.xcuserstate
  3. 19
      Clocker/API/CLAPI.h
  4. 82
      Clocker/API/CLAPI.m
  5. BIN
      Clocker/Images/ClockHands.gif
  6. BIN
      Clocker/Images/MenuBar.png
  7. BIN
      Clocker/Images/Untitled.png
  8. 4
      Clocker/Model/CLTimezoneData.h
  9. 50
      Clocker/Model/CLTimezoneData.m
  10. 105
      Clocker/Preferences/CLPreferencesViewController.m
  11. 21
      Media.xcassets/MenuBar.imageset/Contents.json
  12. BIN
      Media.xcassets/MenuBar.imageset/Untitled.png

6
Clocker.xcodeproj/project.pbxproj

@ -29,6 +29,7 @@
9A5951F51C1D3D81009C17AA /* CLTimezoneCellView.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A5951F41C1D3D81009C17AA /* CLTimezoneCellView.m */; }; 9A5951F51C1D3D81009C17AA /* CLTimezoneCellView.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A5951F41C1D3D81009C17AA /* CLTimezoneCellView.m */; };
9A5951FA1C1D5214009C17AA /* iVersion.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A5951F91C1D5214009C17AA /* iVersion.m */; }; 9A5951FA1C1D5214009C17AA /* iVersion.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A5951F91C1D5214009C17AA /* iVersion.m */; };
9A5951FD1C1D592D009C17AA /* iVersion.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 9A5951FC1C1D592D009C17AA /* iVersion.bundle */; }; 9A5951FD1C1D592D009C17AA /* iVersion.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 9A5951FC1C1D592D009C17AA /* iVersion.bundle */; };
9A63620A1C432F68004AD010 /* CLAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A6362091C432F68004AD010 /* CLAPI.m */; };
9A8605AE1BEC148400A810A4 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A8605AD1BEC148400A810A4 /* main.m */; }; 9A8605AE1BEC148400A810A4 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A8605AD1BEC148400A810A4 /* main.m */; };
9A8605B31BEC14A600A810A4 /* ColoredButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A8605B21BEC14A600A810A4 /* ColoredButton.m */; }; 9A8605B31BEC14A600A810A4 /* ColoredButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A8605B21BEC14A600A810A4 /* ColoredButton.m */; };
9A8605B61BEC14BE00A810A4 /* MenubarController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A8605B41BEC14BE00A810A4 /* MenubarController.m */; }; 9A8605B61BEC14BE00A810A4 /* MenubarController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A8605B41BEC14BE00A810A4 /* MenubarController.m */; };
@ -110,6 +111,8 @@
9A5951F91C1D5214009C17AA /* iVersion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = iVersion.m; path = "Clocker/Updated Version/iVersion.m"; sourceTree = "<group>"; }; 9A5951F91C1D5214009C17AA /* iVersion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = iVersion.m; path = "Clocker/Updated Version/iVersion.m"; sourceTree = "<group>"; };
9A5951FC1C1D592D009C17AA /* iVersion.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; name = iVersion.bundle; path = "Clocker/Updated Version/iVersion.bundle"; sourceTree = "<group>"; }; 9A5951FC1C1D592D009C17AA /* iVersion.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; name = iVersion.bundle; path = "Clocker/Updated Version/iVersion.bundle"; sourceTree = "<group>"; };
9A5B1A8D1BECDFB700A77C68 /* Clocker.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; name = Clocker.entitlements; path = Clocker/Clocker.entitlements; sourceTree = "<group>"; }; 9A5B1A8D1BECDFB700A77C68 /* Clocker.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; name = Clocker.entitlements; path = Clocker/Clocker.entitlements; sourceTree = "<group>"; };
9A6362081C432F68004AD010 /* CLAPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CLAPI.h; path = Clocker/API/CLAPI.h; sourceTree = "<group>"; };
9A6362091C432F68004AD010 /* CLAPI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CLAPI.m; path = Clocker/API/CLAPI.m; sourceTree = "<group>"; };
9A8605AD1BEC148400A810A4 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = main.m; path = Clocker/main.m; sourceTree = "<group>"; }; 9A8605AD1BEC148400A810A4 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = main.m; path = Clocker/main.m; sourceTree = "<group>"; };
9A8605B21BEC14A600A810A4 /* ColoredButton.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = ColoredButton.m; path = Clocker/ColoredButton.m; sourceTree = "<group>"; }; 9A8605B21BEC14A600A810A4 /* ColoredButton.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = ColoredButton.m; path = Clocker/ColoredButton.m; sourceTree = "<group>"; };
9A8605B41BEC14BE00A810A4 /* MenubarController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = MenubarController.m; path = Clocker/MenubarController.m; sourceTree = "<group>"; }; 9A8605B41BEC14BE00A810A4 /* MenubarController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = MenubarController.m; path = Clocker/MenubarController.m; sourceTree = "<group>"; };
@ -195,6 +198,8 @@
children = ( children = (
9A5951BB1C1D0A8D009C17AA /* CommonStrings.m */, 9A5951BB1C1D0A8D009C17AA /* CommonStrings.m */,
9A5951BC1C1D0A8D009C17AA /* CommonStrings.h */, 9A5951BC1C1D0A8D009C17AA /* CommonStrings.h */,
9A6362081C432F68004AD010 /* CLAPI.h */,
9A6362091C432F68004AD010 /* CLAPI.m */,
); );
name = Utilties; name = Utilties;
sourceTree = "<group>"; sourceTree = "<group>";
@ -514,6 +519,7 @@
9A8605BF1BEC14F600A810A4 /* BackgroundView.m in Sources */, 9A8605BF1BEC14F600A810A4 /* BackgroundView.m in Sources */,
9A87DAC51C358FA800A8CF3B /* DTConstants.m in Sources */, 9A87DAC51C358FA800A8CF3B /* DTConstants.m in Sources */,
9AB9357B1C1AD8F7001285A0 /* CLRatingCellView.m in Sources */, 9AB9357B1C1AD8F7001285A0 /* CLRatingCellView.m in Sources */,
9A63620A1C432F68004AD010 /* CLAPI.m in Sources */,
9A87DAC81C358FA800A8CF3B /* DTTimePeriodChain.m in Sources */, 9A87DAC81C358FA800A8CF3B /* DTTimePeriodChain.m in Sources */,
9A87DAC71C358FA800A8CF3B /* DTTimePeriod.m in Sources */, 9A87DAC71C358FA800A8CF3B /* DTTimePeriod.m in Sources */,
9A3247321C25FD3A00CF6B6E /* CLCustomSliderCell.m in Sources */, 9A3247321C25FD3A00CF6B6E /* CLCustomSliderCell.m in Sources */,

BIN
Clocker.xcodeproj/project.xcworkspace/xcuserdata/abhishekbanthia.xcuserdatad/UserInterfaceState.xcuserstate generated

Binary file not shown.

19
Clocker/API/CLAPI.h

@ -0,0 +1,19 @@
//
// CLAPI.h
// Clocker
//
// Created by Abhishek Banthia on 1/10/16.
//
//
#import <Foundation/Foundation.h>
@interface CLAPI : NSObject
+ (void)dataTaskWithServicePath:(NSString *)path
bySender:(id)sender
withCompletionBlock:(void (^)(NSError *error, NSDictionary *dictionary))completionBlock;
+ (BOOL)isUserConnectedToInternet;
@end

82
Clocker/API/CLAPI.m

@ -0,0 +1,82 @@
//
// CLAPI.m
// Clocker
//
// Created by Abhishek Banthia on 1/10/16.
//
//
#import "CLAPI.h"
#import "Reachability.h"
@implementation CLAPI
+ (void)dataTaskWithServicePath:(NSString *)path
bySender:(id)sender
withCompletionBlock:(void (^)(NSError *error, NSDictionary *dictionary))completionBlock
{
__block NSDictionary *responseDictionary = [NSDictionary dictionary];
NSView *currentView = [sender view];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.timeoutIntervalForRequest = 20;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
NSError *error;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", path]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSURLSessionDataTask *dataTask = [session
dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error)
{
/*Check if any error. If nil then proceed*/
if (error == nil)
{
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (httpResp.statusCode == 200)
{
responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
completionBlock(error, responseDictionary);
}
}
/*Error is not nil. Show error*/
else
{
completionBlock(error, nil);
}
}];
[dataTask resume];
}
+ (BOOL)isUserConnectedToInternet
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
if (networkStatus == NotReachable)
{
return NO;
}
return YES;
}
@end

BIN
Clocker/Images/ClockHands.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

BIN
Clocker/Images/MenuBar.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

BIN
Clocker/Images/Untitled.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 B

4
Clocker/Model/CLTimezoneData.h

@ -8,7 +8,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@interface CLTimezoneData : NSObject @interface CLTimezoneData : NSObject<NSCoding>
@property (strong, nonatomic) NSString *customLabel; @property (strong, nonatomic) NSString *customLabel;
@property (strong, nonatomic) NSString *formattedAddress; @property (strong, nonatomic) NSString *formattedAddress;
@ -17,4 +17,6 @@
@property (strong, nonatomic) NSString *sunsetTime; @property (strong, nonatomic) NSString *sunsetTime;
@property (strong, nonatomic) NSString *timezoneID; @property (strong, nonatomic) NSString *timezoneID;
- (BOOL)saveObjectToPreferences:(CLTimezoneData *)object;
@end @end

50
Clocker/Model/CLTimezoneData.m

@ -15,4 +15,54 @@
} }
- (BOOL)saveObjectToPreferences:(CLTimezoneData *)object
{
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
NSMutableArray *array = [NSMutableArray new];
[array addObject:encodedObject];
[[NSUserDefaults standardUserDefaults] setObject:array forKey:@"checking"];
[self getCustomObject];
return YES;
}
- (void)getCustomObject
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *encodedObject = [defaults objectForKey:@"checking"];
CLTimezoneData *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject[0]];
NSLog(@"Object:%@", object.place_id);
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.place_id forKey:@"place_id"];
[coder encodeObject:self.formattedAddress forKey:@"formattedAddress"];
[coder encodeObject:self.customLabel forKey:@"customLabel"];
[coder encodeObject:self.sunriseTime forKey:@"sunriseTime"];
[coder encodeObject:self.sunsetTime forKey:@"sunsetTime"];
[coder encodeObject:self.timezoneID forKey:@"timezoneID"];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
if (self == [super init])
{
self.place_id = [coder decodeObjectForKey:@"place_id"];
self.formattedAddress = [coder decodeObjectForKey:@"formattedAddress"];
self.customLabel = [coder decodeObjectForKey:@"customLabel"];
self.sunsetTime = [coder decodeObjectForKey:@"sunsetTime"];
self.sunriseTime = [coder decodeObjectForKey:@"sunriseTime"];
self.timezoneID = [coder decodeObjectForKey:@"timezoneID"];
}
return self;
}
@end @end

105
Clocker/Preferences/CLPreferencesViewController.m

@ -12,8 +12,9 @@
#import "ApplicationDelegate.h" #import "ApplicationDelegate.h"
#import <QuartzCore/QuartzCore.h> #import <QuartzCore/QuartzCore.h>
#import "CommonStrings.h" #import "CommonStrings.h"
#import "Reachability.h"
#import <Parse/Parse.h> #import <Parse/Parse.h>
#import "CLAPI.h"
NSString *const CLSearchPredicateKey = @"SELF CONTAINS[cd]%@"; NSString *const CLSearchPredicateKey = @"SELF CONTAINS[cd]%@";
NSString *const CLPreferencesViewNibIdentifier = @"PreferencesWindow"; NSString *const CLPreferencesViewNibIdentifier = @"PreferencesWindow";
@ -353,7 +354,10 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
return YES; return YES;
} }
-(NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation -(NSDragOperation)tableView:(NSTableView *)tableView
validateDrop:(id<NSDraggingInfo>)info
proposedRow:(NSInteger)row
proposedDropOperation:(NSTableViewDropOperation)dropOperation
{ {
return NSDragOperationEvery; return NSDragOperationEvery;
} }
@ -371,11 +375,7 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
self.placeholderLabel.hidden = NO; self.placeholderLabel.hidden = NO;
Reachability *reachability = [Reachability reachabilityForInternetConnection]; if (![CLAPI isUserConnectedToInternet]) {
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
if (networkStatus == NotReachable)
{
self.placeholderLabel.placeholderString = CLNoInternetConnectivityError; self.placeholderLabel.placeholderString = CLNoInternetConnectivityError;
return; return;
} }
@ -389,35 +389,23 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
NSString *urlString = [NSString stringWithFormat:CLLocationSearchURL, searchString]; NSString *urlString = [NSString stringWithFormat:CLLocationSearchURL, searchString];
NSURL *url = [NSURL URLWithString:urlString]; [CLAPI dataTaskWithServicePath:urlString
bySender:self
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; withCompletionBlock:^(NSError *error, NSDictionary *json) {
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"GET";
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSError *error = nil;
if (!error) {
self.dataTask= [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (httpResp.statusCode == 200) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
self.placeholderLabel.placeholderString = [error.localizedDescription isEqualToString:@"The Internet connection appears to be offline."] ?
CLNoInternetConnectivityError : CLTryAgainMessage;
self.activityInProgress = NO;
return;
}
self.placeholderLabel.placeholderString = CLEmptyString; self.placeholderLabel.placeholderString = CLEmptyString;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:nil];
if ([json[@"status"] isEqualToString:@"ZERO_RESULTS"]) { if ([json[@"status"] isEqualToString:@"ZERO_RESULTS"]) {
self.placeholderLabel.placeholderString = @"No results! 😔 Try entering the exact name."; self.placeholderLabel.placeholderString = @"No results! 😔 Try entering the exact name.";
self.activityInProgress = NO; self.activityInProgress = NO;
@ -446,31 +434,12 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
}); });
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
self.placeholderLabel.placeholderString = [error.localizedDescription isEqualToString:@"The Internet connection appears to be offline."] ?
CLNoInternetConnectivityError : CLTryAgainMessage;
self.activityInProgress = NO;
});
}
}
}]; }];
[self.dataTask resume];
}
} }
- (void)getTimeZoneForLatitude:(NSString *)latitude andLongitude:(NSString *)longitude - (void)getTimeZoneForLatitude:(NSString *)latitude andLongitude:(NSString *)longitude
{ {
Reachability *reachability = [Reachability reachabilityForInternetConnection]; if (![CLAPI isUserConnectedToInternet]) {
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
if (networkStatus == NotReachable)
{
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
self.placeholderLabel.placeholderString = CLNoInternetConnectivityError; self.placeholderLabel.placeholderString = CLNoInternetConnectivityError;
self.activityInProgress = NO; self.activityInProgress = NO;
@ -481,6 +450,7 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
return; return;
} }
self.searchField.placeholderString = @"Fetching data might take some time!"; self.searchField.placeholderString = @"Fetching data might take some time!";
self.placeholderLabel.placeholderString = @"Retrieving timezone data"; self.placeholderLabel.placeholderString = @"Retrieving timezone data";
@ -489,34 +459,14 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
NSString *urlString = [NSString stringWithFormat:@"http://api.geonames.org/timezoneJSON?lat=%@&lng=%@&username=abhishaker17", latitude, longitude]; NSString *urlString = [NSString stringWithFormat:@"http://api.geonames.org/timezoneJSON?lat=%@&lng=%@&username=abhishaker17", latitude, longitude];
NSURL *url = [NSURL URLWithString:urlString]; [CLAPI dataTaskWithServicePath:urlString
bySender:self
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; withCompletionBlock:^(NSError *error, NSDictionary *json) {
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"GET";
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSError *error = nil;
if (!error) {
self.dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (httpResp.statusCode == 200) {
if (!error)
{
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:nil];
if (json.count == 0) { if (json.count == 0) {
self.activityInProgress = NO; self.activityInProgress = NO;
self.placeholderLabel.placeholderString = @"No results found! ! 😔 Try Again?"; self.placeholderLabel.placeholderString = @"No results found! ! 😔 Try Again?";
@ -589,20 +539,17 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
}); });
} }
}
else else
{ {
self.placeholderLabel.placeholderString = [error.localizedDescription isEqualToString:@"The Internet connection appears to be offline."] ? self.placeholderLabel.placeholderString = [error.localizedDescription isEqualToString:@"The Internet connection appears to be offline."] ?
CLNoInternetConnectivityError : CLTryAgainMessage; CLNoInternetConnectivityError : CLTryAgainMessage;
self.activityInProgress = NO; self.activityInProgress = NO;
} }
}]; }];
[self.dataTask resume];
}
} }
@end @end

21
Media.xcassets/MenuBar.imageset/Contents.json vendored

@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "Untitled.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

BIN
Media.xcassets/MenuBar.imageset/Untitled.png vendored

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 B

Loading…
Cancel
Save