Major refactoring to Mac example code, removing a lot of code in favor of bindings

This commit is contained in:
Mattt Thompson 2012-01-19 11:33:30 -08:00
parent 71bb48fd16
commit cc49cd8d1c
5 changed files with 109 additions and 118 deletions

View file

@ -23,8 +23,8 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import <CoreLocation/CoreLocation.h> #import <CoreLocation/CoreLocation.h>
@interface NearbySpotsController : NSObject <CLLocationManagerDelegate, NSTableViewDataSource, NSTableViewDelegate> @interface NearbySpotsController : NSObject <CLLocationManagerDelegate>
@property (strong) IBOutlet NSTableView *tableView; @property (strong) NSArray *nearbySpots;
@end @end

View file

@ -24,12 +24,8 @@
#import "Spot.h" #import "Spot.h"
#import "AFImageRequestOperation.h"
@interface NearbySpotsController () @interface NearbySpotsController ()
@property (strong) NSArray *nearbySpots;
@property (strong) CLLocationManager *locationManager; @property (strong) CLLocationManager *locationManager;
@property (strong) NSOperationQueue *imageOperationQueue;
- (void)loadSpotsForLocation:(CLLocation *)location; - (void)loadSpotsForLocation:(CLLocation *)location;
@end @end
@ -37,8 +33,6 @@
@implementation NearbySpotsController @implementation NearbySpotsController
@synthesize nearbySpots = _nearbySpots; @synthesize nearbySpots = _nearbySpots;
@synthesize locationManager = _locationManager; @synthesize locationManager = _locationManager;
@synthesize imageOperationQueue = _imageOperationQueue;
@synthesize tableView = _tableView;
- (id)init { - (id)init {
self = [super init]; self = [super init];
@ -52,9 +46,6 @@
self.locationManager.delegate = self; self.locationManager.delegate = self;
self.locationManager.distanceFilter = 80.0; self.locationManager.distanceFilter = 80.0;
self.imageOperationQueue = [[NSOperationQueue alloc] init];
self.imageOperationQueue.maxConcurrentOperationCount = 8;
return self; return self;
} }
@ -80,8 +71,6 @@
return NSOrderedSame; return NSOrderedSame;
} }
}]; }];
[self.tableView reloadData];
}]; }];
} }
@ -91,32 +80,4 @@
[self loadSpotsForLocation:newLocation]; [self loadSpotsForLocation:newLocation];
} }
#pragma mark - NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [self.nearbySpots count];
}
// 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.
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
Spot *spot = [self.nearbySpots objectAtIndex:row];
if ([[tableColumn dataCell] isMemberOfClass:[NSImageCell class]]) {
if (spot.image) {
return spot.image;
} else {
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:spot.imageURLString]];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:imageRequest success:^(NSImage *image) {
spot.image = image;
[tableView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:row] columnIndexes:[NSIndexSet indexSetWithIndex:0]];
}];
[self.imageOperationQueue addOperation:operation];
return [NSImage imageNamed:@"placeholder-stamp.png"];
}
} else {
return spot.name;
}
}
@end @end

View file

@ -26,15 +26,13 @@
@interface Spot : NSObject { @interface Spot : NSObject {
@private @private
NSString *_name; NSString *_name;
NSString *_imageURLString; NSURL *_imageURL;
NSImage *_image;
NSNumber *_latitude; NSNumber *_latitude;
NSNumber *_longitude; NSNumber *_longitude;
} }
@property (strong) NSString *name; @property (strong) NSString *name;
@property (strong) NSString *imageURLString; @property (strong) NSURL *imageURL;
@property (strong) NSImage *image;
@property (strong) NSNumber *latitude; @property (strong) NSNumber *latitude;
@property (strong) NSNumber *longitude; @property (strong) NSNumber *longitude;
@property (readonly) CLLocation *location; @property (readonly) CLLocation *location;

View file

@ -26,8 +26,7 @@
@implementation Spot @implementation Spot
@synthesize name = _name; @synthesize name = _name;
@synthesize imageURLString = _imageURLString; @synthesize imageURL = _imageURL;
@synthesize image = _image;
@synthesize latitude = _latitude; @synthesize latitude = _latitude;
@synthesize longitude = _longitude; @synthesize longitude = _longitude;
@dynamic location; @dynamic location;
@ -39,7 +38,7 @@
} }
self.name = [attributes valueForKeyPath:@"name"]; self.name = [attributes valueForKeyPath:@"name"];
self.imageURLString = [attributes valueForKeyPath:@"image_url"]; self.imageURL = [NSURL URLWithString:[attributes valueForKeyPath:@"image_url"]];
self.latitude = [attributes valueForKeyPath:@"lat"]; self.latitude = [attributes valueForKeyPath:@"lat"];
self.longitude = [attributes valueForKeyPath:@"lng"]; self.longitude = [attributes valueForKeyPath:@"lng"];

View file

@ -1,28 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="7.10"> <archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="7.10">
<data> <data>
<int key="IBDocument.SystemTarget">0</int> <int key="IBDocument.SystemTarget">1070</int>
<string key="IBDocument.SystemVersion">11A511</string> <string key="IBDocument.SystemVersion">11C74</string>
<string key="IBDocument.InterfaceBuilderVersion">1900</string> <string key="IBDocument.InterfaceBuilderVersion">1938</string>
<string key="IBDocument.AppKitVersion">1138</string> <string key="IBDocument.AppKitVersion">1138.23</string>
<string key="IBDocument.HIToolboxVersion">566.00</string> <string key="IBDocument.HIToolboxVersion">567.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions"> <object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin</string> <string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="NS.object.0">1900</string> <string key="NS.object.0">1938</string>
</object> </object>
<object class="NSArray" key="IBDocument.IntegratedClassDependencies"> <object class="NSArray" key="IBDocument.IntegratedClassDependencies">
<bool key="EncodedWithXMLCoder">YES</bool> <bool key="EncodedWithXMLCoder">YES</bool>
<string>NSView</string> <string>NSScroller</string>
<string>NSArrayController</string>
<string>NSMenuItem</string>
<string>NSMenu</string> <string>NSMenu</string>
<string>NSScrollView</string> <string>NSScrollView</string>
<string>NSWindowTemplate</string>
<string>NSMenuItem</string>
<string>NSTableView</string>
<string>NSTextFieldCell</string> <string>NSTextFieldCell</string>
<string>NSImageCell</string> <string>NSImageCell</string>
<string>NSTableColumn</string> <string>NSTableView</string>
<string>NSScroller</string>
<string>NSCustomObject</string> <string>NSCustomObject</string>
<string>NSView</string>
<string>NSWindowTemplate</string>
<string>NSTableColumn</string>
</object> </object>
<object class="NSArray" key="IBDocument.PluginDependencies"> <object class="NSArray" key="IBDocument.PluginDependencies">
<bool key="EncodedWithXMLCoder">YES</bool> <bool key="EncodedWithXMLCoder">YES</bool>
@ -278,6 +279,7 @@
<int key="NSvFlags">256</int> <int key="NSvFlags">256</int>
<string key="NSFrameSize">{373, 498}</string> <string key="NSFrameSize">{373, 498}</string>
<reference key="NSSuperview" ref="411548182"/> <reference key="NSSuperview" ref="411548182"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="830874312"/> <reference key="NSNextKeyView" ref="830874312"/>
<string key="NSReuseIdentifierKey">_NS:1197</string> <string key="NSReuseIdentifierKey">_NS:1197</string>
<bool key="NSEnabled">YES</bool> <bool key="NSEnabled">YES</bool>
@ -412,6 +414,7 @@
</object> </object>
<string key="NSFrame">{{1, 1}, {373, 498}}</string> <string key="NSFrame">{{1, 1}, {373, 498}}</string>
<reference key="NSSuperview" ref="905625827"/> <reference key="NSSuperview" ref="905625827"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="256434433"/> <reference key="NSNextKeyView" ref="256434433"/>
<string key="NSReuseIdentifierKey">_NS:1195</string> <string key="NSReuseIdentifierKey">_NS:1195</string>
<reference key="NSDocView" ref="256434433"/> <reference key="NSDocView" ref="256434433"/>
@ -423,6 +426,7 @@
<int key="NSvFlags">-2147483392</int> <int key="NSvFlags">-2147483392</int>
<string key="NSFrame">{{224, 17}, {15, 102}}</string> <string key="NSFrame">{{224, 17}, {15, 102}}</string>
<reference key="NSSuperview" ref="905625827"/> <reference key="NSSuperview" ref="905625827"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="870849666"/> <reference key="NSNextKeyView" ref="870849666"/>
<string key="NSReuseIdentifierKey">_NS:1214</string> <string key="NSReuseIdentifierKey">_NS:1214</string>
<reference key="NSTarget" ref="905625827"/> <reference key="NSTarget" ref="905625827"/>
@ -434,6 +438,8 @@
<int key="NSvFlags">-2147483392</int> <int key="NSvFlags">-2147483392</int>
<string key="NSFrame">{{1, 526}, {374, 15}}</string> <string key="NSFrame">{{1, 526}, {374, 15}}</string>
<reference key="NSSuperview" ref="905625827"/> <reference key="NSSuperview" ref="905625827"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView"/>
<string key="NSReuseIdentifierKey">_NS:1216</string> <string key="NSReuseIdentifierKey">_NS:1216</string>
<int key="NSsFlags">1</int> <int key="NSsFlags">1</int>
<reference key="NSTarget" ref="905625827"/> <reference key="NSTarget" ref="905625827"/>
@ -443,6 +449,7 @@
</object> </object>
<string key="NSFrameSize">{375, 500}</string> <string key="NSFrameSize">{375, 500}</string>
<reference key="NSSuperview" ref="439893737"/> <reference key="NSSuperview" ref="439893737"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="411548182"/> <reference key="NSNextKeyView" ref="411548182"/>
<string key="NSReuseIdentifierKey">_NS:1193</string> <string key="NSReuseIdentifierKey">_NS:1193</string>
<int key="NSsFlags">133682</int> <int key="NSsFlags">133682</int>
@ -454,9 +461,10 @@
</object> </object>
<string key="NSFrameSize">{375, 500}</string> <string key="NSFrameSize">{375, 500}</string>
<reference key="NSSuperview"/> <reference key="NSSuperview"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="905625827"/> <reference key="NSNextKeyView" ref="905625827"/>
</object> </object>
<string key="NSScreenRect">{{0, 0}, {1920, 1178}}</string> <string key="NSScreenRect">{{0, 0}, {1440, 878}}</string>
<string key="NSMinSize">{375, 222}</string> <string key="NSMinSize">{375, 222}</string>
<string key="NSMaxSize">{375, 1302}</string> <string key="NSMaxSize">{375, 1302}</string>
<bool key="NSWindowIsRestorable">YES</bool> <bool key="NSWindowIsRestorable">YES</bool>
@ -467,6 +475,15 @@
<object class="NSCustomObject" id="755631768"> <object class="NSCustomObject" id="755631768">
<string key="NSClassName">NSFontManager</string> <string key="NSClassName">NSFontManager</string>
</object> </object>
<object class="NSArrayController" id="680254480">
<string key="NSObjectClassName">Spot</string>
<object class="_NSManagedProxy" key="_NSManagedProxy"/>
<bool key="NSAvoidsEmptySelection">YES</bool>
<bool key="NSPreservesSelection">YES</bool>
<bool key="NSSelectsInsertedObjects">YES</bool>
<bool key="NSFilterRestrictsInsertion">YES</bool>
<bool key="NSClearsFilterPredicateOnInsertion">YES</bool>
</object>
<object class="NSCustomObject" id="91820041"> <object class="NSCustomObject" id="91820041">
<string key="NSClassName">NearbySpotsController</string> <string key="NSClassName">NearbySpotsController</string>
</object> </object>
@ -474,6 +491,30 @@
<object class="IBObjectContainer" key="IBDocument.Objects"> <object class="IBObjectContainer" key="IBDocument.Objects">
<object class="NSMutableArray" key="connectionRecords"> <object class="NSMutableArray" key="connectionRecords">
<bool key="EncodedWithXMLCoder">YES</bool> <bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">terminate:</string>
<reference key="source" ref="1050"/>
<reference key="destination" ref="632727374"/>
</object>
<int key="connectionID">449</int>
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">orderFrontStandardAboutPanel:</string>
<reference key="source" ref="1021"/>
<reference key="destination" ref="238522557"/>
</object>
<int key="connectionID">142</int>
</object>
<object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection">
<string key="label">delegate</string>
<reference key="source" ref="1021"/>
<reference key="destination" ref="976324537"/>
</object>
<int key="connectionID">495</int>
</object>
<object class="IBConnectionRecord"> <object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection"> <object class="IBActionConnection" key="connection">
<string key="label">performMiniaturize:</string> <string key="label">performMiniaturize:</string>
@ -490,14 +531,6 @@
</object> </object>
<int key="connectionID">39</int> <int key="connectionID">39</int>
</object> </object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">orderFrontStandardAboutPanel:</string>
<reference key="source" ref="1021"/>
<reference key="destination" ref="238522557"/>
</object>
<int key="connectionID">142</int>
</object>
<object class="IBConnectionRecord"> <object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection"> <object class="IBActionConnection" key="connection">
<string key="label">performZoom:</string> <string key="label">performZoom:</string>
@ -530,14 +563,6 @@
</object> </object>
<int key="connectionID">370</int> <int key="connectionID">370</int>
</object> </object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">terminate:</string>
<reference key="source" ref="1050"/>
<reference key="destination" ref="632727374"/>
</object>
<int key="connectionID">449</int>
</object>
<object class="IBConnectionRecord"> <object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection"> <object class="IBActionConnection" key="connection">
<string key="label">showHelp:</string> <string key="label">showHelp:</string>
@ -546,14 +571,6 @@
</object> </object>
<int key="connectionID">493</int> <int key="connectionID">493</int>
</object> </object>
<object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection">
<string key="label">delegate</string>
<reference key="source" ref="1021"/>
<reference key="destination" ref="976324537"/>
</object>
<int key="connectionID">495</int>
</object>
<object class="IBConnectionRecord"> <object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection"> <object class="IBOutletConnection" key="connection">
<string key="label">window</string> <string key="label">window</string>
@ -563,28 +580,52 @@
<int key="connectionID">532</int> <int key="connectionID">532</int>
</object> </object>
<object class="IBConnectionRecord"> <object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection"> <object class="IBBindingConnection" key="connection">
<string key="label">tableView</string> <string key="label">valueURL: arrangedObjects.imageURL</string>
<reference key="source" ref="91820041"/> <reference key="source" ref="553559566"/>
<reference key="destination" ref="256434433"/> <reference key="destination" ref="680254480"/>
<object class="NSNibBindingConnector" key="connector">
<reference key="NSSource" ref="553559566"/>
<reference key="NSDestination" ref="680254480"/>
<string key="NSLabel">valueURL: arrangedObjects.imageURL</string>
<string key="NSBinding">valueURL</string>
<string key="NSKeyPath">arrangedObjects.imageURL</string>
<int key="NSNibBindingConnectorVersion">2</int>
</object>
</object> </object>
<int key="connectionID">550</int> <int key="connectionID">562</int>
</object> </object>
<object class="IBConnectionRecord"> <object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection"> <object class="IBBindingConnection" key="connection">
<string key="label">dataSource</string> <string key="label">value: arrangedObjects.name</string>
<reference key="source" ref="256434433"/> <reference key="source" ref="879600957"/>
<reference key="destination" ref="91820041"/> <reference key="destination" ref="680254480"/>
<object class="NSNibBindingConnector" key="connector">
<reference key="NSSource" ref="879600957"/>
<reference key="NSDestination" ref="680254480"/>
<string key="NSLabel">value: arrangedObjects.name</string>
<string key="NSBinding">value</string>
<string key="NSKeyPath">arrangedObjects.name</string>
<int key="NSNibBindingConnectorVersion">2</int>
</object>
</object> </object>
<int key="connectionID">551</int> <int key="connectionID">566</int>
</object> </object>
<object class="IBConnectionRecord"> <object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection"> <object class="IBBindingConnection" key="connection">
<string key="label">delegate</string> <string key="label">contentArray: nearbySpots</string>
<reference key="source" ref="256434433"/> <reference key="source" ref="680254480"/>
<reference key="destination" ref="91820041"/> <reference key="destination" ref="91820041"/>
<object class="NSNibBindingConnector" key="connector">
<reference key="NSSource" ref="680254480"/>
<reference key="NSDestination" ref="91820041"/>
<string key="NSLabel">contentArray: nearbySpots</string>
<string key="NSBinding">contentArray</string>
<string key="NSKeyPath">nearbySpots</string>
<int key="NSNibBindingConnectorVersion">2</int>
</object>
</object> </object>
<int key="connectionID">552</int> <int key="connectionID">560</int>
</object> </object>
</object> </object>
<object class="IBMutableOrderedSet" key="objectRecords"> <object class="IBMutableOrderedSet" key="objectRecords">
@ -859,7 +900,12 @@
<reference key="parent" ref="553559566"/> <reference key="parent" ref="553559566"/>
</object> </object>
<object class="IBObjectRecord"> <object class="IBObjectRecord">
<int key="objectID">549</int> <int key="objectID">555</int>
<reference key="object" ref="680254480"/>
<reference key="parent" ref="0"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">556</int>
<reference key="object" ref="91820041"/> <reference key="object" ref="91820041"/>
<reference key="parent" ref="0"/> <reference key="parent" ref="0"/>
</object> </object>
@ -904,7 +950,8 @@
<string>543.IBPluginDependency</string> <string>543.IBPluginDependency</string>
<string>544.IBPluginDependency</string> <string>544.IBPluginDependency</string>
<string>546.IBPluginDependency</string> <string>546.IBPluginDependency</string>
<string>549.IBPluginDependency</string> <string>555.IBPluginDependency</string>
<string>556.IBPluginDependency</string>
<string>56.IBPluginDependency</string> <string>56.IBPluginDependency</string>
<string>57.IBPluginDependency</string> <string>57.IBPluginDependency</string>
<string>58.IBPluginDependency</string> <string>58.IBPluginDependency</string>
@ -952,6 +999,7 @@
<string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
</object> </object>
</object> </object>
<object class="NSMutableDictionary" key="unlocalizedProperties"> <object class="NSMutableDictionary" key="unlocalizedProperties">
@ -966,7 +1014,7 @@
<reference key="dict.values" ref="0"/> <reference key="dict.values" ref="0"/>
</object> </object>
<nil key="sourceID"/> <nil key="sourceID"/>
<int key="maxID">554</int> <int key="maxID">567</int>
</object> </object>
<object class="IBClassDescriber" key="IBDocument.Classes"> <object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions"> <object class="NSMutableArray" key="referencedPartialClassDescriptions">
@ -993,17 +1041,6 @@
<object class="IBPartialClassDescription"> <object class="IBPartialClassDescription">
<string key="className">NearbySpotsController</string> <string key="className">NearbySpotsController</string>
<string key="superclassName">NSObject</string> <string key="superclassName">NSObject</string>
<object class="NSMutableDictionary" key="outlets">
<string key="NS.key.0">tableView</string>
<string key="NS.object.0">NSTableView</string>
</object>
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
<string key="NS.key.0">tableView</string>
<object class="IBToOneOutletInfo" key="NS.object.0">
<string key="name">tableView</string>
<string key="candidateClassName">NSTableView</string>
</object>
</object>
<object class="IBClassDescriptionSource" key="sourceIdentifier"> <object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string> <string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/NearbySpotsController.h</string> <string key="minorKey">./Classes/NearbySpotsController.h</string>
@ -1013,10 +1050,6 @@
</object> </object>
<int key="IBDocument.localizationMode">0</int> <int key="IBDocument.localizationMode">0</int>
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaFramework</string> <string key="IBDocument.TargetRuntimeIdentifier">IBCocoaFramework</string>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin.macosx</string>
<real value="0.0" key="NS.object.0"/>
</object>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDevelopmentDependencies"> <object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDevelopmentDependencies">
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin.InterfaceBuilder3</string> <string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin.InterfaceBuilder3</string>
<integer value="3000" key="NS.object.0"/> <integer value="3000" key="NS.object.0"/>