Skip to content

Commit

Permalink
Merge pull request #688 from clearsightstudio/sdwebimage
Browse files Browse the repository at this point in the history
Adds support for SDWebImage and takes preference over JMImageCache
  • Loading branch information
jamonholmgren committed May 12, 2015
2 parents b6103ac + 94aef5d commit 0047517
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
8 changes: 7 additions & 1 deletion app/test_screens/test_mini_table_screen.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
class TestCell < PM::TableViewCell
attr_accessor :on_reuse_fired
attr_accessor :on_reuse_fired, :prepare_for_reuse_fired, :on_reuse_time, :prepare_for_reuse_time

def on_reuse
self.on_reuse_fired = true
self.on_reuse_time = Time.now
end

def prepare_for_reuse
self.prepare_for_reuse_fired = true
self.prepare_for_reuse_time = Time.now
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ def table_data
image: {
image: "something", # PM will do `UIImage.imageNamed("something")` for you
radius: 15 # radius is optional
},
},
# You can also specify an image with just a UIImage or a String
# image: UIImage.imageNamed("something"),
# image: "something",
remote_image: { # remote image, requires JMImageCache CocoaPod
remote_image: { # remote image, requires SDWebImage CocoaPod
url: "http://placekitten.com/200/300",
placeholder: "some-local-image", # NOTE: this is required!
size: 50,
Expand All @@ -78,4 +78,4 @@ def table_data
}]
}]
end
```
```
40 changes: 33 additions & 7 deletions lib/ProMotion/table/cell/table_view_cell_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,26 @@ def set_subtitle
end

def set_remote_image
return unless data_cell[:remote_image] && jm_image_cache?
return unless data_cell[:remote_image] && (sd_web_image? || jm_image_cache?)

self.imageView.image = remote_placeholder
JMImageCache.sharedCache.imageForURL(data_cell[:remote_image][:url].to_url, completionBlock:proc { |downloaded_image|
self.imageView.image = downloaded_image
self.setNeedsLayout
})

if sd_web_image?
@remote_image_operation = SDWebImageManager.sharedManager.downloadWithURL(data_cell[:remote_image][:url].to_url,
options:SDWebImageRefreshCached,
progress:nil,
completed: -> image, error, cacheType, finished {
self.imageView.image = image unless image.nil?
})
elsif jm_image_cache?
PM.logger.warning "'JMImageCache' is known to have issues with ProMotion. Please consider switching to 'SDWebImage'. 'JMImageCache' support will be deprecated in the next major version."
JMImageCache.sharedCache.imageForURL(data_cell[:remote_image][:url].to_url, completionBlock:proc { |downloaded_image|
self.imageView.image = downloaded_image
self.setNeedsLayout
})
else
PM.logger.error "To use remote_image with TableScreen you need to include the CocoaPod 'SDWebImage'."
end

self.imageView.layer.masksToBounds = true
self.imageView.layer.cornerRadius = data_cell[:remote_image][:radius] if data_cell[:remote_image][:radius]
Expand Down Expand Up @@ -89,12 +102,25 @@ def set_accessory_type
self.accessoryType = map_accessory_type_symbol(data_cell[:accessory_type]) if data_cell[:accessory_type]
end

def prepareForReuse
super
if @remote_image_operation && @remote_image_operation.respond_to?(:cancel)
@remote_image_operation.cancel
@remote_image_operation = nil
end
self.send(:prepare_for_reuse) if self.respond_to?(:prepare_for_reuse)
end

private

def sd_web_image?
return false if RUBYMOTION_ENV == 'test'
!!defined?(SDWebImageManager)
end

def jm_image_cache?
return false if RUBYMOTION_ENV == 'test'
return true if self.imageView.respond_to?("setImageWithURL:placeholder:")
PM.logger.error "ProMotion Warning: to use remote_image with TableScreen you need to include the CocoaPod 'JMImageCache'."
!!defined?(JMImageCache)
false
end

Expand Down
28 changes: 27 additions & 1 deletion spec/functional/func_table_screen_cell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,33 @@ def controller
end
end

end
it "no cells have fired prepare_for_reuse before scrolling" do
ip = NSIndexPath.indexPathForRow(0, inSection: 0)
cell = table_screen.tableView(table_screen.table_view, cellForRowAtIndexPath: ip)
cell.prepare_for_reuse_fired.should.not == true
end

it "cell has fired prepare_for_reuse after scrolling" do
ip = NSIndexPath.indexPathForRow(10, inSection: 0)
table_screen.tableView.scrollToRowAtIndexPath(ip, atScrollPosition: UITableViewScrollPositionTop, animated: false)
wait 0.001 do
ip = NSIndexPath.indexPathForRow(0, inSection: 0)
table_screen.tableView.scrollToRowAtIndexPath(ip, atScrollPosition: UITableViewScrollPositionTop, animated: false)

cell = views(TestCell).first
cell.prepare_for_reuse_fired.should == true
end
end

it "should fire prepare_for_reuse before on_reuse" do
ip = NSIndexPath.indexPathForRow(10, inSection: 0)
table_screen.tableView.scrollToRowAtIndexPath(ip, atScrollPosition: UITableViewScrollPositionTop, animated: false)
wait 0.001 do
ip = NSIndexPath.indexPathForRow(0, inSection: 0)
table_screen.tableView.scrollToRowAtIndexPath(ip, atScrollPosition: UITableViewScrollPositionTop, animated: false)

cell = views(TestCell).first
cell.prepare_for_reuse_time.should < cell.on_reuse_time
end
end
end
2 changes: 1 addition & 1 deletion spec/unit/tables/table_module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def custom_cell
value: true # whether it's "checked" or not
},
image: { image: @image, radius: 15 },
remote_image: { # remote image, requires JMImageCache CocoaPod
remote_image: { # remote image, requires SDWebImage CocoaPod
url: "http://placekitten.com/200/300",
placeholder: "some-local-image",
size: 50,
Expand Down

0 comments on commit 0047517

Please sign in to comment.