diff --git a/lib/ProMotion/table/extensions/refreshable.rb b/lib/ProMotion/table/extensions/refreshable.rb new file mode 100644 index 0000000..b661be7 --- /dev/null +++ b/lib/ProMotion/table/extensions/refreshable.rb @@ -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 diff --git a/lib/ProMotion/table/extensions/searchable.rb b/lib/ProMotion/table/extensions/searchable.rb new file mode 100644 index 0000000..76b40cb --- /dev/null +++ b/lib/ProMotion/table/extensions/searchable.rb @@ -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 diff --git a/lib/ProMotion/table/grouped_table.rb b/lib/ProMotion/table/grouped_table.rb index f0fcadc..f44e53b 100644 --- a/lib/ProMotion/table/grouped_table.rb +++ b/lib/ProMotion/table/grouped_table.rb @@ -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 diff --git a/lib/ProMotion/table/grouped_table_screen.rb b/lib/ProMotion/table/grouped_table_screen.rb new file mode 100644 index 0000000..cbc4e4f --- /dev/null +++ b/lib/ProMotion/table/grouped_table_screen.rb @@ -0,0 +1,6 @@ +module ProMotion + class GroupedTableScreen < TableViewController + include ProMotion::ScreenModule + include ProMotion::GroupedTable + end +end diff --git a/lib/ProMotion/table/plain_table.rb b/lib/ProMotion/table/plain_table.rb deleted file mode 100644 index 9b11a31..0000000 --- a/lib/ProMotion/table/plain_table.rb +++ /dev/null @@ -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 diff --git a/lib/ProMotion/table/refreshable_table.rb b/lib/ProMotion/table/refreshable_table.rb deleted file mode 100644 index 7261368..0000000 --- a/lib/ProMotion/table/refreshable_table.rb +++ /dev/null @@ -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 diff --git a/lib/ProMotion/table/searchable_table.rb b/lib/ProMotion/table/searchable_table.rb deleted file mode 100644 index 6a50e56..0000000 --- a/lib/ProMotion/table/searchable_table.rb +++ /dev/null @@ -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 diff --git a/lib/ProMotion/table/sectioned_table.rb b/lib/ProMotion/table/sectioned_table.rb deleted file mode 100644 index 10a9585..0000000 --- a/lib/ProMotion/table/sectioned_table.rb +++ /dev/null @@ -1,5 +0,0 @@ -module ProMotion - module SectionedTable - include Table - end -end diff --git a/lib/ProMotion/table/table.rb b/lib/ProMotion/table/table.rb index 95b8718..7c20d9d 100644 --- a/lib/ProMotion/table/table.rb +++ b/lib/ProMotion/table/table.rb @@ -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 diff --git a/lib/ProMotion/table/table_screen.rb b/lib/ProMotion/table/table_screen.rb index a03f86e..441a768 100644 --- a/lib/ProMotion/table/table_screen.rb +++ b/lib/ProMotion/table/table_screen.rb @@ -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 \ No newline at end of file +end diff --git a/lib/ProMotion/table/table_screen_module.rb b/lib/ProMotion/table/table_screen_module.rb deleted file mode 100644 index 994fe2d..0000000 --- a/lib/ProMotion/table/table_screen_module.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/lib/ProMotion/screen/compatibility/formotion_screen.rb b/lib/ProMotion/thirdparty/formotion_screen.rb similarity index 100% rename from lib/ProMotion/screen/compatibility/formotion_screen.rb rename to lib/ProMotion/thirdparty/formotion_screen.rb diff --git a/spec/functional/func_table_screen_spec.rb b/spec/functional/func_table_screen_spec.rb index 32e98e2..bb06eb1 100644 --- a/spec/functional/func_table_screen_spec.rb +++ b/spec/functional/func_table_screen_spec.rb @@ -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 diff --git a/spec/helpers/table_screen.rb b/spec/helpers/table_screen.rb index 18a26bc..54fe96f 100644 --- a/spec/helpers/table_screen.rb +++ b/spec/helpers/table_screen.rb @@ -1,4 +1,4 @@ -class TestTableScreen < ProMotion::SectionedTableScreen +class TestTableScreen < ProMotion::TableScreen attr_accessor :tap_counter diff --git a/spec/unit/map_spec.rb b/spec/unit/map_spec.rb index 1afa557..0916e8d 100644 --- a/spec/unit/map_spec.rb +++ b/spec/unit/map_spec.rb @@ -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