Skip to content
This repository has been archived by the owner on Mar 12, 2023. It is now read-only.

Commit

Permalink
set connection timeout to 15 seconds for faster feedback about bad co…
Browse files Browse the repository at this point in the history
…nnections. made note page size constant.
  • Loading branch information
mpowell committed Jan 10, 2014
1 parent 03d2caf commit c2aea09
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Evernote/Evernote.m
Expand Up @@ -87,7 +87,7 @@ - (void) connect {

NSURL* noteStoreUri = [[NSURL alloc] initWithString: user.noteStoreUrl];
NSString* agentString = [NSString stringWithFormat:@"Mars Images/2.0;iOS/%@", [UIDevice currentDevice].systemVersion];
THTTPClient* noteStoreHttpClient = [[THTTPClient alloc] initWithURL:noteStoreUri userAgent:agentString timeout:15000];
THTTPClient* noteStoreHttpClient = [[THTTPClient alloc] initWithURL:noteStoreUri userAgent:agentString timeout:15];

TBinaryProtocol* noteStoreProtocol = [[TBinaryProtocol alloc] initWithTransport:noteStoreHttpClient];
self.noteStore = [[EDAMNoteStoreClient alloc] initWithProtocol:noteStoreProtocol];
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions Mars-Images/Mars-Images-Info.plist
Expand Up @@ -5,27 +5,27 @@
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>Mars Images</string>
<string>Mars 2.0</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIcons</key>
<dict/>
<key>CFBundleIcons~ipad</key>
<dict/>
<key>CFBundleIdentifier</key>
<string>com.powellware.${PRODUCT_NAME:rfc1034identifier}</string>
<string>com.powellware.${PRODUCT_NAME:rfc1034identifier}2</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2.0</string>
<string>1.9.12</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>2.0</string>
<string>1.9.12</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIMainStoryboardFile</key>
Expand Down
5 changes: 4 additions & 1 deletion Mars-Images/MarsImageNotebook.h
Expand Up @@ -25,16 +25,19 @@
#define MSL_NOTEBOOK_ID @"0296f732-694d-4ccd-9f5b-5983dc98b9e0"
#define SPIRIT_NOTEBOOK_ID @"f1a72415-56e7-4244-8e12-def9be9c512b"

#define NOTE_PAGE_SIZE 15

@interface MarsImageNotebook : NSObject

@property(nonatomic, strong) NSDictionary* evernoteUsers;
@property(nonatomic, strong) Reachability* internetReachable;
@property(nonatomic, readonly) int lastRequestedStartIndexToLoad;
@property(nonatomic, strong) NSDate* lastSleepTime;
@property(nonatomic, strong) NSDictionary* missions;
@property(nonatomic, strong) NSString* missionName;
@property(nonatomic, strong) NSDictionary* notebookIDs;
@property(nonatomic, strong) NSDictionary* notes;
@property(nonatomic, strong) UIAlertView* networkAlert;
@property(nonatomic, strong) UIAlertView* serviceAlert;
@property(nonatomic, strong) NSArray* sols;
@property(nonatomic, strong) NSDictionary* sections;
@property(nonatomic, strong) NSArray* notesArray;
Expand Down
84 changes: 41 additions & 43 deletions Mars-Images/MarsImageNotebook.m
Expand Up @@ -35,7 +35,7 @@ - (MarsImageNotebook*) init {
_sections = [[NSMutableDictionary alloc] init];
_sols = [[NSMutableArray alloc] init];
_lastSleepTime = nil;
_lastRequestedStartIndexToLoad = -1;

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs stringForKey:MISSION] == nil) {
[prefs setObject:OPPORTUNITY forKey: MISSION];
Expand All @@ -55,6 +55,18 @@ - (MarsImageNotebook*) init {

[Evernote instance].publicUser = [_evernoteUsers valueForKey:self.missionName];

_networkAlert = [[UIAlertView alloc] initWithTitle:@"Network Error"
message:@"Unable to connect to the network."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];

_serviceAlert = [[UIAlertView alloc] initWithTitle:@"Service Unavailable"
message:@"The Mars image service is currently unavailable. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];

// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
[self setInternetReachable:[Reachability reachabilityForInternetConnection]];
Expand Down Expand Up @@ -82,55 +94,31 @@ + (void) notifyNotesReturned: (int) total {

- (void) loadMoreNotes: (int) startIndex
withTotal: (int) total {
if (_internetReachable.currentReachabilityStatus == NotReachable) {
if (!_networkAlert.visible) {
[_networkAlert show];
}
[MarsImageNotebook notifyNotesReturned:0];
return;
}

@synchronized (self) {
[MarsImageNotebook notify: BEGIN_NOTE_LOADING];

//if we already have these notes, don't load them again.
if (startIndex == _lastRequestedStartIndexToLoad) {
dispatch_async(noteDownloadQueue, ^{
if (_notesArray.count > startIndex) {
return;
}
_lastRequestedStartIndexToLoad = startIndex;

if (_internetReachable.currentReachabilityStatus == NotReachable) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Network Error"
message:@"Unable to connect to the network."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
[MarsImageNotebook notifyNotesReturned:0];
return;
}
[MarsImageNotebook notify: BEGIN_NOTE_LOADING];

dispatch_async(noteDownloadQueue, ^{
EDAMNoteList* notelist = [[EDAMNoteList alloc] init];
@try {
EDAMNoteFilter* filter = [[EDAMNoteFilter alloc] init];

filter.notebookGuid = [_notebookIDs valueForKey:_missionName];
filter.order = NoteSortOrder_TITLE;
filter.ascending = NO;
if (_searchWords != nil && [_searchWords length]>0) {
filter.words = [self formatSearch:_searchWords];
}
EDAMNoteList* notelist = [[EDAMNoteList alloc] init];
@try {
notelist = [[Evernote instance] findNotes: filter withStartIndex:startIndex withTotal:total];
}
@catch (NSException *e) {
NSLog(@"Exception listing notes: %@ %@", e.name, e.description);
[[Evernote instance] setNoteStore: nil];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Service Unavailable"
message:@"The Mars image service is currently unavailable. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
});
[MarsImageNotebook notifyNotesReturned:0];
return;
}

notelist = [[Evernote instance] findNotes: filter withStartIndex:startIndex withTotal:total];
[(NSMutableArray*)_notesArray addObjectsFromArray: notelist.notes];
for (int j = 0; j < notelist.notes.count; j++) {
EDAMNote* note = [notelist.notes objectAtIndex:j];
Expand All @@ -152,10 +140,21 @@ - (void) loadMoreNotes: (int) startIndex
NSLog(@"Brown alert: sections and sols counts don't match each other.");
}
}

[MarsImageNotebook notifyNotesReturned:notelist.notes.count];
});
}

} @catch (NSException *e) {
NSLog(@"Exception listing notes: %@ %@", e.name, e.description);
[[Evernote instance] setNoteStore: nil];
dispatch_async(dispatch_get_main_queue(), ^{
if (!_serviceAlert.visible) {
[_serviceAlert show];
}
});
[MarsImageNotebook notifyNotesReturned:0];
return;
}
});
}

- (NSString*) formatSearch: (NSString*) text {
Expand Down Expand Up @@ -209,8 +208,7 @@ - (void) reloadNotes {
[(NSMutableArray*)_notePhotosArray removeAllObjects];
[(NSMutableDictionary*) _sections removeAllObjects];
[(NSMutableArray*) _sols removeAllObjects];
_lastRequestedStartIndexToLoad = -1;
[self loadMoreNotes:0 withTotal:15];
[self loadMoreNotes:0 withTotal:NOTE_PAGE_SIZE];
}

- (void) checkNetworkStatus:(NSNotification *)notice {
Expand Down
14 changes: 9 additions & 5 deletions Mars-Images/MarsImageTableViewController.m
Expand Up @@ -226,10 +226,6 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell = [[FixedWidthImageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}

// Configure the cell...
// cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17.0];
// cell.detailTextLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0];

if ([MarsImageNotebook instance].sols.count <= indexPath.section) return nil;

NSNumber* sol = [[MarsImageNotebook instance].sols objectAtIndex:indexPath.section];
Expand All @@ -251,7 +247,15 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
int imageCount = imagesForSol.count;
if ([MarsImageNotebook instance].sections.count > 0 &&
sectionCount - 1 == [indexPath section] && imageCount - 1 == [indexPath row]) {
[[MarsImageNotebook instance] loadMoreNotes:[MarsImageNotebook instance].notesArray.count withTotal:15];

Reachability* internet = [MarsImageNotebook instance].internetReachable;
if (internet.currentReachabilityStatus == NotReachable) {
UIAlertView* networkAlert = [MarsImageNotebook instance].networkAlert;
if (!networkAlert.visible) [networkAlert show];
}
else {
[[MarsImageNotebook instance] loadMoreNotes:[MarsImageNotebook instance].notesArray.count withTotal:NOTE_PAGE_SIZE];
}
}

return cell;
Expand Down
2 changes: 1 addition & 1 deletion Mars-Images/MarsImageViewController.m
Expand Up @@ -276,7 +276,7 @@ - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUI

int count = [MarsImageNotebook instance].notePhotosArray.count;
if (index == count-1) {
[[MarsImageNotebook instance] loadMoreNotes:count withTotal:15];
[[MarsImageNotebook instance] loadMoreNotes:count withTotal:NOTE_PAGE_SIZE];
}
[(MarsSidePanelController*)self.viewDeckController imageSelected:index from:self];
}
Expand Down
2 changes: 1 addition & 1 deletion Mars-Images/MarsSidePanelController.m
Expand Up @@ -24,7 +24,7 @@ - (void) viewDidLoad {
[self setDelegate: self];
[self configureLeftPanel: [UIApplication sharedApplication].statusBarOrientation];
//load first notes in background
[[MarsImageNotebook instance] loadMoreNotes:0 withTotal:15];
[[MarsImageNotebook instance] loadMoreNotes:0 withTotal:NOTE_PAGE_SIZE];
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
Expand Down

0 comments on commit c2aea09

Please sign in to comment.