Merge pull request #55 from markrickert/version-0.6-table-refreshable

Version 0.6 table refreshable
This commit is contained in:
Jamon Holmgren
2013-05-06 21:39:59 -07:00
11 changed files with 177 additions and 0 deletions

View File

@@ -699,6 +699,21 @@ end
<td>searchable(placeholder: "placeholder text")</td>
<td>Class method to make the current table searchable.</td>
</tr>
<tr>
<td><pre><code>refreshable(
callback: :on_refresh,
pull_message: "Pull to refresh",
refreshing: "Refreshing data…",
updated_format: "Last updated at %s",
updated_time_format: "%l:%M %p"
)</code></pre></td>
<td>Class method to make the current table refreshable.
<p>All parameters are optional. If you do not specify a a callback, it will assume you've implemented an <code>on_refresh</code> method in your tableview.</p>
<pre><code>def on_refresh
# Code to start the refresh
end</code></pre>
<p>And after you're done with your asyncronous process, call <code>end_refreshing</code> to collapse the refresh view and update the last refreshed time and then <code>update_table_data</code>.</p></td>
</tr>
<tr>
<td colspan="2">
<h3>table_data</h3>

View File

@@ -0,0 +1,42 @@
module ProMotion::MotionTable
module RefreshableTable
def make_refreshable(params={})
pull_message = params[:pull_message] || "Pull to refresh"
@refreshing = params[:refreshing] || "Refreshing data..."
@updated_format = params[:updated_format] || "Last updated at %s"
@updated_time_format = params[:updated_time_format] || "%l:%M %p"
@refreshable_callback = params[:callback]|| :on_refresh
@refresh = UIRefreshControl.alloc.init
@refresh.attributedTitle = NSAttributedString.alloc.initWithString(pull_message)
@refresh.addTarget(self, action:'refreshView:', forControlEvents:UIControlEventValueChanged)
self.refreshControl = @refresh
end
alias :makeRefreshable :make_refreshable
######### iOS methods, headless camel case #######
# UIRefreshControl Delegates
def refreshView(refresh)
refresh.attributedTitle = NSAttributedString.alloc.initWithString(@refreshing)
if @refreshable_callback && self.respond_to?(@refreshable_callback)
self.send(@refreshable_callback)
else
ProMotion::Console.log("ProMotion Warning: you must implement the '#{@refreshable_callback}' method in your TableScreen.", with_color: ProMotion::Console::RED_COLOR)
end
end
def start_refreshing
return unless @refresh
@refresh.beginRefreshing
end
def end_refreshing
return unless @refresh
@refresh.attributedTitle = NSAttributedString.alloc.initWithString(sprintf(@updated_format, Time.now.strftime(@updated_time_format)))
@refresh.endRefreshing
end
end
end

View File

@@ -7,6 +7,11 @@ module ProMotion::MotionTable
if self.class.respond_to?(:get_searchable) && self.class.get_searchable
self.make_searchable(content_controller: self, search_bar: self.class.get_searchable_params)
end
if defined?(UIRefreshControl) && self.class.respond_to?(:get_refreshable) && self.class.get_refreshable
self.make_refreshable(self.class.get_refreshable_params)
else
ProMotion::Console.log("ProMotion Warning: to use the refresh control on < iOS 6, you need to include the Cocoapod 'CKRefreshControl'.", with_color: ProMotion::Console::RED_COLOR)
end
end
# @param [Array] Array of table data

View File

@@ -1,6 +1,7 @@
module ProMotion::MotionTable
module GroupedTable
include SectionedTable
include RefreshableTable
def table_view
@table_view ||= UITableView.alloc.initWithFrame(self.view.frame, style:UITableViewStyleGrouped)

View File

@@ -2,6 +2,7 @@ module ProMotion::MotionTable
module PlainTable
include SectionedTable
include SearchableTable
include RefreshableTable
def table_view
@table_view ||= UITableView.alloc.initWithFrame(self.view.frame, style:UITableViewStylePlain)

View File

@@ -2,6 +2,7 @@ module ProMotion
module TableScreenModule
include MotionTable::PlainTable
include MotionTable::SearchableTable
include MotionTable::RefreshableTable
include ProMotion::ScreenModule
def update_table_data
@@ -9,6 +10,7 @@ module ProMotion
end
module TableClassMethods
# Searchable
def searchable(params={})
@searchable_params = params
@searchable = true
@@ -21,6 +23,21 @@ module ProMotion
def get_searchable
@searchable ||= false
end
# Refreshable
def refreshable(params = {})
@refreshable_params = params
@refreshable = true
end
def get_refreshable
@refreshable ||= false
end
def get_refreshable_params
@refreshable_params ||= nil
end
end
def self.included(base)
base.extend(ClassMethods)

View File

View File

@@ -0,0 +1,44 @@
class TableScreen < ProMotion::SectionedTableScreen
def on_load
@tap_counter ||= 0
end
def table_data
[{
title: "Your Account",
cells: [
{ title: "Increment", action: :increment_counter_by, arguments: { number: 3 } },
{ title: "Add New Row", action: :add_tableview_row, accessibilityLabel: "Add New Row" },
{ title: "Just another blank row" }
]
}, {
title: "App Stuff",
cells: [
{ title: "Increment One", action: :increment_counter },
{ title: "Feedback", remote_image: { url: "http://placekitten.com/100/100", placeholder: "some-local-image", size: 50, radius: 15 } }
]
}]
end
def edit_profile(args={})
args[:id]
end
def add_tableview_row
@data[0][:cells] << {
title: "Dynamically Added"
}
update_table_data
end
def increment_counter
@tap_counter = @tap_counter + 1
end
def increment_counter_by(args)
@tap_counter = @tap_counter + args[:number]
end
end

View File

@@ -0,0 +1,9 @@
class TableScreenRefreshable < TableScreen
refreshable
def on_refresh
#do stuff
end
end

View File

@@ -0,0 +1,5 @@
class TableScreenSearchable < TableScreen
searchable
end

38
spec/table_screen_spec.rb Normal file
View File

@@ -0,0 +1,38 @@
describe "table screen basic functionality" do
before do
@screen = TableScreen.new
@screen.on_load
end
tests TableScreen
it "should display have 2 sections" do
@screen.tableView.numberOfSections.should == 2
end
it "should have prope r cell numbers" do
@screen.tableView.numberOfRowsInSection(0).should == 3
@screen.tableView.numberOfRowsInSection(1).should == 2
end
# it "should run methods without arguments when tapping cell" do
# #cell = @screen.cell_at_section_and_index(0, 0)
# [1..10].each do |index|
# @screen.instance_variable_get("@tap_counter").should == index
# tap view("Increment")
# end
# end
# it "should have a placeholder image in the last cell" do
# @screen.cell_at_section_and_index(1,1).imageView.should.be.a UIImage
# end
# it "should add a new cell to first section" do
# tap "Add New Row"
# wait 0.2 do
# @screen.tableView.numberOfRowsInSection(0).should == 4
# end
# end
end