forked from caged/gitnub
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement async load of gravatar images
- Loading branch information
Showing
3 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# | ||
# ImageLoadOperation.rb | ||
# gitnub | ||
# | ||
# Created by Kevin Ballard on 3/21/08. | ||
# Copyright (c) 2008 Kevin Ballard. All rights reserved. | ||
# | ||
|
||
require 'osx/cocoa' | ||
|
||
class ImageLoadOperation < OSX::NSOperation | ||
def initWithURL_delegate(url, delegate) | ||
init | ||
|
||
@url = url | ||
@delegate = delegate | ||
@executing = false | ||
@finished = false | ||
self | ||
end | ||
|
||
def isConcurrent | ||
true | ||
end | ||
|
||
def start | ||
request = NSURLRequest.requestWithURL(@url) | ||
@connection = NSURLConnection.connectionWithRequest_delegate(request, self) | ||
setExecuting true | ||
end | ||
|
||
def cancel | ||
super | ||
@connection.cancel | ||
setExecuting false | ||
end | ||
|
||
def isExecuting | ||
@executing | ||
end | ||
|
||
def isFinished | ||
@finished | ||
end | ||
|
||
# NSURLConnection Delegate methods | ||
def connection_didFailWithError(connection, error) | ||
@delegate.imageLoadForURL_didFailWithError(@url, error) | ||
setExecuting false | ||
setFinished true | ||
end | ||
|
||
def connection_didReceiveResponse(connection, response) | ||
length = response.expectedContentLength | ||
@data = NSMutableData.dataWithCapacity(length < 0 ? 0 : length) | ||
end | ||
|
||
def connection_didReceiveData(connection, data) | ||
@data.appendData(data) | ||
end | ||
|
||
def connectionDidFinishLoading(connection) | ||
image = NSImage.alloc.initWithData(@data) | ||
if image | ||
@delegate.imageLoadForURL_didFinishLoading(@url, image) | ||
else | ||
error = NSError.errorWithDomain_code_userInfo_( | ||
"GitNub", 0, {NSLocalizedDescriptionKey => "Could not recognize image data"} | ||
) | ||
@delegate.imageLoadForURL_didFailWithError(@url, error) | ||
end | ||
end | ||
|
||
private | ||
|
||
def setExecuting(bool) | ||
self.willChangeValueForKey("isExecuting") | ||
@executing = bool | ||
self.didChangeValueForKey("isExecuting") | ||
end | ||
|
||
def setFinished(bool) | ||
self.willChangeValueForKey("isFinished") | ||
@finished = bool | ||
self.didChangeValueForKey("isFinished") | ||
end | ||
end |