Skip to content

Commit cc49cd8

Browse files
committed
Major refactoring to Mac example code, removing a lot of code in favor of bindings
1 parent 71bb48f commit cc49cd8

File tree

5 files changed

+109
-118
lines changed

5 files changed

+109
-118
lines changed

Mac Example/Classes/Controllers/NearbySpotsController.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#import <Cocoa/Cocoa.h>
2424
#import <CoreLocation/CoreLocation.h>
2525

26-
@interface NearbySpotsController : NSObject <CLLocationManagerDelegate, NSTableViewDataSource, NSTableViewDelegate>
26+
@interface NearbySpotsController : NSObject <CLLocationManagerDelegate>
2727

28-
@property (strong) IBOutlet NSTableView *tableView;
28+
@property (strong) NSArray *nearbySpots;
2929

3030
@end

Mac Example/Classes/Controllers/NearbySpotsController.m

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,15 @@
2424

2525
#import "Spot.h"
2626

27-
#import "AFImageRequestOperation.h"
28-
2927
@interface NearbySpotsController ()
30-
@property (strong) NSArray *nearbySpots;
3128
@property (strong) CLLocationManager *locationManager;
32-
@property (strong) NSOperationQueue *imageOperationQueue;
3329

3430
- (void)loadSpotsForLocation:(CLLocation *)location;
3531
@end
3632

3733
@implementation NearbySpotsController
3834
@synthesize nearbySpots = _nearbySpots;
3935
@synthesize locationManager = _locationManager;
40-
@synthesize imageOperationQueue = _imageOperationQueue;
41-
@synthesize tableView = _tableView;
4236

4337
- (id)init {
4438
self = [super init];
@@ -52,9 +46,6 @@ - (id)init {
5246
self.locationManager.delegate = self;
5347
self.locationManager.distanceFilter = 80.0;
5448

55-
self.imageOperationQueue = [[NSOperationQueue alloc] init];
56-
self.imageOperationQueue.maxConcurrentOperationCount = 8;
57-
5849
return self;
5950
}
6051

@@ -79,9 +70,7 @@ - (void)loadSpotsForLocation:(CLLocation *)location {
7970
} else {
8071
return NSOrderedSame;
8172
}
82-
}];
83-
84-
[self.tableView reloadData];
73+
}];
8574
}];
8675
}
8776

@@ -91,32 +80,4 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLoca
9180
[self loadSpotsForLocation:newLocation];
9281
}
9382

94-
#pragma mark - NSTableViewDataSource
95-
96-
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
97-
return [self.nearbySpots count];
98-
}
99-
100-
// The following is what happens when a longtime iOS dev attempts to work with AppKit. I'm sure there's a _much_ better way to do this.
101-
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
102-
Spot *spot = [self.nearbySpots objectAtIndex:row];
103-
104-
if ([[tableColumn dataCell] isMemberOfClass:[NSImageCell class]]) {
105-
if (spot.image) {
106-
return spot.image;
107-
} else {
108-
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:spot.imageURLString]];
109-
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:imageRequest success:^(NSImage *image) {
110-
spot.image = image;
111-
[tableView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:row] columnIndexes:[NSIndexSet indexSetWithIndex:0]];
112-
}];
113-
[self.imageOperationQueue addOperation:operation];
114-
115-
return [NSImage imageNamed:@"placeholder-stamp.png"];
116-
}
117-
} else {
118-
return spot.name;
119-
}
120-
}
121-
12283
@end

Mac Example/Classes/Models/Spot.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@
2626
@interface Spot : NSObject {
2727
@private
2828
NSString *_name;
29-
NSString *_imageURLString;
30-
NSImage *_image;
29+
NSURL *_imageURL;
3130
NSNumber *_latitude;
3231
NSNumber *_longitude;
3332
}
3433

3534
@property (strong) NSString *name;
36-
@property (strong) NSString *imageURLString;
37-
@property (strong) NSImage *image;
35+
@property (strong) NSURL *imageURL;
3836
@property (strong) NSNumber *latitude;
3937
@property (strong) NSNumber *longitude;
4038
@property (readonly) CLLocation *location;

Mac Example/Classes/Models/Spot.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626

2727
@implementation Spot
2828
@synthesize name = _name;
29-
@synthesize imageURLString = _imageURLString;
30-
@synthesize image = _image;
29+
@synthesize imageURL = _imageURL;
3130
@synthesize latitude = _latitude;
3231
@synthesize longitude = _longitude;
3332
@dynamic location;
@@ -39,7 +38,7 @@ - (id)initWithAttributes:(NSDictionary *)attributes {
3938
}
4039

4140
self.name = [attributes valueForKeyPath:@"name"];
42-
self.imageURLString = [attributes valueForKeyPath:@"image_url"];
41+
self.imageURL = [NSURL URLWithString:[attributes valueForKeyPath:@"image_url"]];
4342
self.latitude = [attributes valueForKeyPath:@"lat"];
4443
self.longitude = [attributes valueForKeyPath:@"lng"];
4544

0 commit comments

Comments
 (0)