mirror of
https://github.com/zhigang1992/ProMotion.git
synced 2026-05-25 01:31:37 +08:00
Merge pull request #186 from clearsightstudio/refactor_table_modules
Refactored table folder and modules
This commit is contained in:
45
lib/ProMotion/table/extensions/refreshable.rb
Normal file
45
lib/ProMotion/table/extensions/refreshable.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
module ProMotion
|
||||
module Table
|
||||
module Refreshable
|
||||
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_control = UIRefreshControl.alloc.init
|
||||
@refresh_control.attributedTitle = NSAttributedString.alloc.initWithString(pull_message)
|
||||
@refresh_control.addTarget(self, action:'refreshView:', forControlEvents:UIControlEventValueChanged)
|
||||
self.refreshControl = @refresh_control
|
||||
end
|
||||
|
||||
def start_refreshing
|
||||
return unless @refresh_control
|
||||
|
||||
@refresh_control.beginRefreshing
|
||||
end
|
||||
alias :begin_refreshing :start_refreshing
|
||||
|
||||
def end_refreshing
|
||||
return unless @refresh_control
|
||||
|
||||
@refresh_control.attributedTitle = NSAttributedString.alloc.initWithString(sprintf(@updated_format, Time.now.strftime(@updated_time_format)))
|
||||
@refresh_control.endRefreshing
|
||||
end
|
||||
alias :stop_refreshing :end_refreshing
|
||||
|
||||
######### 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
|
||||
PM.logger.warn "You must implement the '#{@refreshable_callback}' method in your TableScreen."
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
61
lib/ProMotion/table/extensions/searchable.rb
Normal file
61
lib/ProMotion/table/extensions/searchable.rb
Normal file
@@ -0,0 +1,61 @@
|
||||
module ProMotion
|
||||
module Table
|
||||
module Searchable
|
||||
|
||||
def make_searchable(params={})
|
||||
params = set_searchable_param_defaults(params)
|
||||
|
||||
search_bar = create_search_bar(params)
|
||||
|
||||
if params[:search_bar] && params[:search_bar][:placeholder]
|
||||
search_bar.placeholder = params[:search_bar][:placeholder]
|
||||
end
|
||||
|
||||
@table_search_display_controller = UISearchDisplayController.alloc.initWithSearchBar(search_bar, contentsController: params[:content_controller])
|
||||
@table_search_display_controller.delegate = params[:delegate]
|
||||
@table_search_display_controller.searchResultsDataSource = params[:data_source]
|
||||
@table_search_display_controller.searchResultsDelegate = params[:search_results_delegate]
|
||||
|
||||
self.table_view.tableHeaderView = search_bar
|
||||
end
|
||||
alias :makeSearchable :make_searchable
|
||||
|
||||
def set_searchable_param_defaults(params)
|
||||
params[:content_controller] ||= params[:contentController]
|
||||
params[:data_source] ||= params[:searchResultsDataSource]
|
||||
params[:search_results_delegate] ||= params[:searchResultsDelegate]
|
||||
|
||||
params[:frame] ||= CGRectMake(0, 0, 320, 44) # TODO: Don't hardcode this...
|
||||
params[:content_controller] ||= self
|
||||
params[:delegate] ||= self
|
||||
params[:data_source] ||= self
|
||||
params[:search_results_delegate] ||= self
|
||||
params
|
||||
end
|
||||
|
||||
def create_search_bar(params)
|
||||
search_bar = UISearchBar.alloc.initWithFrame(params[:frame])
|
||||
search_bar.autoresizingMask = UIViewAutoresizingFlexibleWidth
|
||||
search_bar
|
||||
end
|
||||
|
||||
######### iOS methods, headless camel case #######
|
||||
|
||||
def searchDisplayController(controller, shouldReloadTableForSearchString:search_string)
|
||||
@promotion_table_data.search(search_string)
|
||||
true
|
||||
end
|
||||
|
||||
def searchDisplayControllerWillEndSearch(controller)
|
||||
@promotion_table_data.stop_searching
|
||||
@promotion_table_data_data = nil
|
||||
self.table_view.setScrollEnabled true
|
||||
self.table_view.reloadData
|
||||
end
|
||||
|
||||
def searchDisplayControllerWillBeginSearch(controller)
|
||||
self.table_view.setScrollEnabled false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,16 +1,6 @@
|
||||
module ProMotion
|
||||
module GroupedTable
|
||||
include Table
|
||||
include RefreshableTable
|
||||
|
||||
def table_view
|
||||
@table_view ||= begin
|
||||
t = UITableView.alloc.initWithFrame(self.view.frame, style:UITableViewStyleGrouped)
|
||||
t.dataSource = self
|
||||
t.delegate = self
|
||||
t
|
||||
end
|
||||
end
|
||||
alias :tableView :table_view
|
||||
include ProMotion::Table
|
||||
TABLE_STYLE = UITableViewStyleGrouped
|
||||
end
|
||||
end
|
||||
|
||||
6
lib/ProMotion/table/grouped_table_screen.rb
Normal file
6
lib/ProMotion/table/grouped_table_screen.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
module ProMotion
|
||||
class GroupedTableScreen < TableViewController
|
||||
include ProMotion::ScreenModule
|
||||
include ProMotion::GroupedTable
|
||||
end
|
||||
end
|
||||
@@ -1,17 +0,0 @@
|
||||
module ProMotion
|
||||
module PlainTable
|
||||
include Table
|
||||
include SearchableTable
|
||||
include RefreshableTable
|
||||
|
||||
def table_view
|
||||
@table_view ||= begin
|
||||
t = UITableView.alloc.initWithFrame(self.view.frame, style:UITableViewStylePlain)
|
||||
t.dataSource = self
|
||||
t.delegate = self
|
||||
t
|
||||
end
|
||||
end
|
||||
alias :tableView :table_view
|
||||
end
|
||||
end
|
||||
@@ -1,44 +0,0 @@
|
||||
module ProMotion
|
||||
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_control = UIRefreshControl.alloc.init
|
||||
@refresh_control.attributedTitle = NSAttributedString.alloc.initWithString(pull_message)
|
||||
@refresh_control.addTarget(self, action:'refreshView:', forControlEvents:UIControlEventValueChanged)
|
||||
self.refreshControl = @refresh_control
|
||||
end
|
||||
|
||||
def start_refreshing
|
||||
return unless @refresh_control
|
||||
|
||||
@refresh_control.beginRefreshing
|
||||
end
|
||||
alias :begin_refreshing :start_refreshing
|
||||
|
||||
def end_refreshing
|
||||
return unless @refresh_control
|
||||
|
||||
@refresh_control.attributedTitle = NSAttributedString.alloc.initWithString(sprintf(@updated_format, Time.now.strftime(@updated_time_format)))
|
||||
@refresh_control.endRefreshing
|
||||
end
|
||||
alias :stop_refreshing :end_refreshing
|
||||
|
||||
######### 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
|
||||
PM.logger.warn "You must implement the '#{@refreshable_callback}' method in your TableScreen."
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,60 +0,0 @@
|
||||
module ProMotion
|
||||
module SearchableTable
|
||||
|
||||
def make_searchable(params={})
|
||||
params = set_searchable_param_defaults(params)
|
||||
|
||||
search_bar = create_search_bar(params)
|
||||
|
||||
if params[:search_bar] && params[:search_bar][:placeholder]
|
||||
search_bar.placeholder = params[:search_bar][:placeholder]
|
||||
end
|
||||
|
||||
@table_search_display_controller = UISearchDisplayController.alloc.initWithSearchBar(search_bar, contentsController: params[:content_controller])
|
||||
@table_search_display_controller.delegate = params[:delegate]
|
||||
@table_search_display_controller.searchResultsDataSource = params[:data_source]
|
||||
@table_search_display_controller.searchResultsDelegate = params[:search_results_delegate]
|
||||
|
||||
self.table_view.tableHeaderView = search_bar
|
||||
end
|
||||
alias :makeSearchable :make_searchable
|
||||
|
||||
def set_searchable_param_defaults(params)
|
||||
params[:content_controller] ||= params[:contentController]
|
||||
params[:data_source] ||= params[:searchResultsDataSource]
|
||||
params[:search_results_delegate] ||= params[:searchResultsDelegate]
|
||||
|
||||
params[:frame] ||= CGRectMake(0, 0, 320, 44) # TODO: Don't hardcode this...
|
||||
params[:content_controller] ||= self
|
||||
params[:delegate] ||= self
|
||||
params[:data_source] ||= self
|
||||
params[:search_results_delegate] ||= self
|
||||
params
|
||||
end
|
||||
|
||||
def create_search_bar(params)
|
||||
search_bar = UISearchBar.alloc.initWithFrame(params[:frame])
|
||||
search_bar.autoresizingMask = UIViewAutoresizingFlexibleWidth
|
||||
search_bar
|
||||
end
|
||||
|
||||
######### iOS methods, headless camel case #######
|
||||
|
||||
def searchDisplayController(controller, shouldReloadTableForSearchString:search_string)
|
||||
@promotion_table_data.search(search_string)
|
||||
true
|
||||
end
|
||||
|
||||
def searchDisplayControllerWillEndSearch(controller)
|
||||
@promotion_table_data.stop_searching
|
||||
@promotion_table_data_data = nil
|
||||
self.table_view.setScrollEnabled true
|
||||
self.table_view.reloadData
|
||||
end
|
||||
|
||||
def searchDisplayControllerWillBeginSearch(controller)
|
||||
self.table_view.setScrollEnabled false
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,5 +0,0 @@
|
||||
module ProMotion
|
||||
module SectionedTable
|
||||
include Table
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,19 @@
|
||||
module ProMotion
|
||||
module Table
|
||||
|
||||
include ProMotion::ViewHelper
|
||||
include ProMotion::Table::Searchable
|
||||
include ProMotion::Table::Refreshable
|
||||
TABLE_STYLE = UITableViewStylePlain
|
||||
|
||||
def table_view
|
||||
@table_view ||= begin
|
||||
t = UITableView.alloc.initWithFrame(self.view.frame, style:TABLE_STYLE)
|
||||
t.dataSource = self
|
||||
t.delegate = self
|
||||
t
|
||||
end
|
||||
end
|
||||
|
||||
def screen_setup
|
||||
check_table_data
|
||||
@@ -124,6 +137,10 @@ module ProMotion
|
||||
table_cell
|
||||
end
|
||||
|
||||
def update_table_data
|
||||
self.update_table_view_data(self.table_data)
|
||||
end
|
||||
|
||||
########## Cocoa touch methods #################
|
||||
def numberOfSectionsInTableView(table_view)
|
||||
return @promotion_table_data.data.size
|
||||
@@ -211,5 +228,40 @@ module ProMotion
|
||||
delete_cell(index_paths, animation)
|
||||
end
|
||||
|
||||
module TableClassMethods
|
||||
# Searchable
|
||||
def searchable(params={})
|
||||
@searchable_params = params
|
||||
@searchable = true
|
||||
end
|
||||
|
||||
def get_searchable_params
|
||||
@searchable_params ||= nil
|
||||
end
|
||||
|
||||
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(TableClassMethods)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,16 +1,6 @@
|
||||
module ProMotion
|
||||
# You can inherit a table screen from any UITableViewController if you include TableScreenModule
|
||||
# Just make sure to implement the Obj-C methods in cocoatouch/TableViewController.rb.
|
||||
class TableScreen < TableViewController
|
||||
include ProMotion::TableScreenModule
|
||||
# Includes PM::PlainTable already
|
||||
include ProMotion::ScreenModule
|
||||
include ProMotion::Table
|
||||
end
|
||||
|
||||
class GroupedTableScreen < TableScreen
|
||||
include ProMotion::GroupedTable
|
||||
end
|
||||
|
||||
class SectionedTableScreen < TableScreen
|
||||
include ProMotion::SectionedTable
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
module ProMotion
|
||||
module TableScreenModule
|
||||
include PlainTable
|
||||
include SearchableTable
|
||||
include RefreshableTable
|
||||
include ScreenModule
|
||||
|
||||
def update_table_data
|
||||
self.update_table_view_data(table_data)
|
||||
end
|
||||
|
||||
module TableClassMethods
|
||||
# Searchable
|
||||
def searchable(params={})
|
||||
@searchable_params = params
|
||||
@searchable = true
|
||||
end
|
||||
|
||||
def get_searchable_params
|
||||
@searchable_params ||= nil
|
||||
end
|
||||
|
||||
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)
|
||||
base.extend(TableClassMethods)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -9,10 +9,6 @@ describe "ProMotion::TestTableScreen functionality" do
|
||||
@controller.navigation_controller
|
||||
end
|
||||
|
||||
after do
|
||||
@controller = nil
|
||||
end
|
||||
|
||||
it "should have a navigation bar" do
|
||||
@controller.navigationController.should.be.kind_of(UINavigationController)
|
||||
end
|
||||
@@ -40,34 +36,46 @@ describe "ProMotion::TestTableScreen functionality" do
|
||||
it "should delete the specified row from the table view on tap" do
|
||||
@controller.tableView(@controller.tableView, numberOfRowsInSection:0).should == 6
|
||||
tap("Delete the row below")
|
||||
@controller.tableView(@controller.tableView, numberOfRowsInSection:0).should == 5
|
||||
wait 0.3 do
|
||||
@controller.tableView(@controller.tableView, numberOfRowsInSection:0).should == 5
|
||||
end
|
||||
end
|
||||
|
||||
it "should delete the specified row from the table view on tap with an animation" do
|
||||
@controller.tableView(@controller.tableView, numberOfRowsInSection:0).should == 6
|
||||
tap("Delete the row below with an animation")
|
||||
@controller.tableView(@controller.tableView, numberOfRowsInSection:0).should == 5
|
||||
wait 0.3 do
|
||||
@controller.tableView(@controller.tableView, numberOfRowsInSection:0).should == 5
|
||||
end
|
||||
end
|
||||
|
||||
it "should call a method when the switch is flipped" do
|
||||
@controller.scroll_to_bottom
|
||||
tap "switch_1"
|
||||
@controller.tap_counter.should == 1
|
||||
wait 0.3 do
|
||||
@controller.tap_counter.should == 1
|
||||
end
|
||||
end
|
||||
|
||||
it "should call the method with arguments when the switch is flipped and when the cell is tapped" do
|
||||
@controller.scroll_to_bottom
|
||||
tap "switch_3"
|
||||
@controller.tap_counter.should == 3
|
||||
wait 0.3 do
|
||||
@controller.tap_counter.should == 3
|
||||
|
||||
tap "Switch With Cell Tap, Switch Action And Parameters"
|
||||
@controller.tap_counter.should == 13
|
||||
tap "Switch With Cell Tap, Switch Action And Parameters"
|
||||
wait 0.3 do
|
||||
@controller.tap_counter.should == 13
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "should call the method with arguments when the switch is flipped" do
|
||||
@controller.scroll_to_bottom
|
||||
tap "switch_2"
|
||||
@controller.tap_counter.should == 3
|
||||
wait 0.3 do
|
||||
@controller.tap_counter.should == 3
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class TestTableScreen < ProMotion::SectionedTableScreen
|
||||
class TestTableScreen < ProMotion::TableScreen
|
||||
|
||||
attr_accessor :tap_counter
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ describe "map properties" do
|
||||
placemarks = @map.infinite_loop_points
|
||||
placemarks.count.should == 1
|
||||
placemarks.first.postalCode.should == "95014"
|
||||
puts placemarks.first.description.include?("Cupertino").should == true
|
||||
placemarks.first.description.include?("Cupertino").should == true
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user