Skip to content

Commit

Permalink
Implement async load of gravatar images
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyball committed Mar 22, 2008
1 parent d054f6d commit 5ff35a9
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 5 deletions.
31 changes: 28 additions & 3 deletions CommitsController.rb
Expand Up @@ -10,6 +10,11 @@
require 'md5'
require 'cgi'

def gravatar_url(email, size=36)
hash = MD5.hexdigest(email.downcase)
NSURL.URLWithString("http://www.gravatar.com/avatar.php?gravatar_id=#{hash}&size=#{size}")
end

class CommitsController < OSX::NSObject
ib_outlet :commits_table
ib_outlet :branch_select
Expand All @@ -21,10 +26,14 @@ def awakeFromNib
@current_commit_offset = 0
@offset = 50
@active_commit = nil
@branch = :master
@branch = :master
@icon_queue = NSOperationQueue.alloc.init
@icon_url_map = {}
@icons = Hash.new do |hash, email|
gravatar = NSURL.URLWithString("http://www.gravatar.com/avatar.php?gravatar_id=#{MD5.hexdigest(email.downcase)}&size=36")
hash[email] = NSImage.alloc.initWithContentsOfURL(gravatar)
url = gravatar_url(email)
@icon_url_map[url] = email
@icon_queue.addOperation(ImageLoadOperation.alloc.initWithURL_delegate(url, self))
hash[email] = NSImage.imageNamed(NSImageNameUser)
end

if(fetch_git_repository)
Expand Down Expand Up @@ -106,6 +115,22 @@ def webView_didFinishLoadForFrame(view, frame)
def webView_contextMenuItemsForElement_defaultMenuItems(view, element, defaultMenuItems)
nil
end

def imageLoadForURL_didFinishLoading(url, image)
email = @icon_url_map[url]
@icons[email] = image
# indices = NSMutableIndexSet.indexSet
# @commits.each_with_index do |commit, idx|
# if commit.author.email == email
# indices.addIndex(idx)
# end
# end
@commits_table.setNeedsDisplay(true)
end

def imageLoadForURL_didFailWithError(url, error)
STDERR.puts "Async image load failed for URL: #{url}\n#{error}"
end

def select_latest_commit
@commits_table.selectRowIndexes_byExtendingSelection(NSIndexSet.indexSetWithIndex(0), false)
Expand Down
8 changes: 6 additions & 2 deletions GitNub.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0A4023590D949E0E00659E01 /* ImageLoadOperation.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0A4023580D949E0E00659E01 /* ImageLoadOperation.rb */; };
0ABA3B1B0D89E53E004B6667 /* nub in Resources */ = {isa = PBXBuildFile; fileRef = 0ABA3B1A0D89E53E004B6667 /* nub */; };
283254680D8B792500D99366 /* lib in Resources */ = {isa = PBXBuildFile; fileRef = 283254650D8B792500D99366 /* lib */; };
285BB47C0D8306C60027980C /* mime-types in Resources */ = {isa = PBXBuildFile; fileRef = 285BB4690D8306C60027980C /* mime-types */; };
Expand Down Expand Up @@ -52,6 +53,7 @@

/* Begin PBXFileReference section */
089C165DFE840E0CC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
0A4023580D949E0E00659E01 /* ImageLoadOperation.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = ImageLoadOperation.rb; sourceTree = "<group>"; };
0ABA3B1A0D89E53E004B6667 /* nub */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = nub; sourceTree = "<group>"; };
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
283254650D8B792500D99366 /* lib */ = {isa = PBXFileReference; lastKnownFileType = folder; path = lib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -104,6 +106,7 @@
children = (
285BB5450D849EA60027980C /* ApplicationController.rb */,
285BB5460D849EA60027980C /* CommitsController.rb */,
0A4023580D949E0E00659E01 /* ImageLoadOperation.rb */,
285BB5470D849EA60027980C /* InfoWindowController.rb */,
4D922CA60D8889DB002A5539 /* CommitSummaryCell.h */,
4D922CA70D8889DB002A5539 /* CommitSummaryCell.m */,
Expand Down Expand Up @@ -232,7 +235,7 @@
/* Begin PBXProject section */
29B97313FDCFA39411CA2CEA /* Project object */ = {
isa = PBXProject;
buildConfigurationList = 4D7A7B570ABF745500C2D4C0 /* Build configuration list for PBXProject "gitnub" */;
buildConfigurationList = 4D7A7B570ABF745500C2D4C0 /* Build configuration list for PBXProject "GitNub" */;
compatibilityVersion = "Xcode 2.4";
hasScannedForEncodings = 0;
mainGroup = 29B97314FDCFA39411CA2CEA /* GitNub */;
Expand Down Expand Up @@ -269,6 +272,7 @@
288CB41D0D878A1C0092B5CC /* Info.xib in Resources */,
0ABA3B1B0D89E53E004B6667 /* nub in Resources */,
283254680D8B792500D99366 /* lib in Resources */,
0A4023590D949E0E00659E01 /* ImageLoadOperation.rb in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -367,7 +371,7 @@
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
4D7A7B570ABF745500C2D4C0 /* Build configuration list for PBXProject "gitnub" */ = {
4D7A7B570ABF745500C2D4C0 /* Build configuration list for PBXProject "GitNub" */ = {
isa = XCConfigurationList;
buildConfigurations = (
4D7A7B580ABF745500C2D4C0 /* Debug */,
Expand Down
87 changes: 87 additions & 0 deletions ImageLoadOperation.rb
@@ -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

0 comments on commit 5ff35a9

Please sign in to comment.