mirror of
https://github.com/zhigang1992/ProMotion.git
synced 2026-03-26 06:55:04 +08:00
Merge branch 'version-2.3' of github.com:clearsightstudio/ProMotion into version-2.3
This commit is contained in:
@@ -94,3 +94,46 @@ class TableScreenSearchable < TestTableScreen
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class TableScreenStabbySearchable < TableScreenSearchable
|
||||
searchable with: -> (cell, search_string) {
|
||||
result = true
|
||||
search_string.split(/\s+/).each {|term|
|
||||
result &&= cell[:properties][:searched_title].downcase.strip.include?(term.downcase.strip)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
def build_cell(title)
|
||||
{
|
||||
title: title,
|
||||
subtitle: @subtitle.to_s,
|
||||
action: :update_subtitle,
|
||||
properties: {
|
||||
searched_title: "#{title} - stabby"
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
class TableScreenSymbolSearchable < TableScreenSearchable
|
||||
searchable with: :custom_search
|
||||
|
||||
def custom_search(cell, search_string)
|
||||
result = true
|
||||
search_string.split(/\s+/).all? {|term|
|
||||
cell[:properties][:searched_title].downcase.strip.include?(term.downcase.strip)
|
||||
}
|
||||
end
|
||||
|
||||
def build_cell(title)
|
||||
{
|
||||
title: title,
|
||||
subtitle: @subtitle.to_s,
|
||||
action: :update_subtitle,
|
||||
properties: {
|
||||
searched_title: "#{title} - symbol"
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -278,7 +278,7 @@ This is useful for information that needs to only be at the very bottom of a tab
|
||||
|
||||
### Class Methods
|
||||
|
||||
#### searchable(placeholder: "placeholder text")
|
||||
#### searchable(placeholder: "placeholder text", with: -> (cell, search_string){})
|
||||
|
||||
Class method to make the current table searchable.
|
||||
|
||||
@@ -288,6 +288,31 @@ class MyTableScreen < PM::TableScreen
|
||||
end
|
||||
```
|
||||
|
||||
Without a `with:` specifier, search is performed on the `title` attribute, and
|
||||
the `search_text` attribute, if present. If you want to create a custom search
|
||||
method, specify it as the value of the `with` key (`find_by`, `search_by` and `filter_by`
|
||||
are aliases). E.g.:
|
||||
|
||||
```ruby
|
||||
class MyTableScreen < PM::TableScreen
|
||||
searchable placeholder: "Search This Table", with: -> (cell, search_string){
|
||||
cell[:properties][:some_obscure_attribute].strip.downcase.include? search_string.strip.downcase
|
||||
}
|
||||
end
|
||||
```
|
||||
|
||||
or if you want to create a version that is less resistant to refactoring:
|
||||
|
||||
```ruby
|
||||
class MyTableScreen < PM::TableScreen
|
||||
searchable placeholder: "Search This Table", with: :custom_search_method
|
||||
|
||||
def custom_search_method(cell, search_string)
|
||||
cell[:properties][:some_obscure_attribute].strip.downcase.include? search_string.strip.downcase
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||

|
||||
|
||||
To initially hide the search bar behind the nav bar until the user scrolls it into view, use `hide_initially`.
|
||||
|
||||
@@ -63,7 +63,7 @@ module ProMotion
|
||||
private
|
||||
|
||||
def on_tab_selected_try(vc)
|
||||
if pm_tab_delegate && pm_tab_delegate.respond_to?("on_tab_selected:")
|
||||
if pm_tab_delegate && pm_tab_delegate.respond_to?(:weakref_alive?) && pm_tab_delegate.weakref_alive? && pm_tab_delegate.respond_to?("on_tab_selected:")
|
||||
pm_tab_delegate.send(:on_tab_selected, vc)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,9 +2,10 @@ module ProMotion
|
||||
class TableData
|
||||
include ProMotion::Table::Utils
|
||||
|
||||
attr_accessor :data, :filtered_data, :search_string, :original_search_string, :filtered, :table_view
|
||||
attr_accessor :data, :filtered_data, :search_string, :original_search_string, :filtered, :table_view, :search_params
|
||||
|
||||
def initialize(data, table_view)
|
||||
def initialize(data, table_view, search_action = nil)
|
||||
@search_action ||= search_action
|
||||
self.data = data
|
||||
self.table_view = WeakRef.new(table_view)
|
||||
end
|
||||
@@ -38,6 +39,10 @@ module ProMotion
|
||||
section(to.section)[:cells].insert(to.row, section(from.section)[:cells].delete_at(from.row))
|
||||
end
|
||||
|
||||
def default_search(cell, search_string)
|
||||
cell[:searchable] != false && "#{cell[:title]}\n#{cell[:search_text]}".downcase.strip.include?(search_string.downcase.strip)
|
||||
end
|
||||
|
||||
def search(search_string)
|
||||
start_searching(search_string)
|
||||
|
||||
@@ -45,7 +50,11 @@ module ProMotion
|
||||
new_section = {}
|
||||
|
||||
new_section[:cells] = section[:cells].map do |cell|
|
||||
cell[:searchable] != false && "#{cell[:title]}\n#{cell[:search_text]}".downcase.strip.include?(self.search_string) ? cell : nil
|
||||
if @search_action
|
||||
@search_action.call(cell, search_string)
|
||||
else
|
||||
self.default_search(cell, search_string)
|
||||
end ? cell : nil
|
||||
end.compact
|
||||
|
||||
if new_section[:cells] && new_section[:cells].length > 0
|
||||
|
||||
@@ -27,7 +27,7 @@ module ProMotion
|
||||
end
|
||||
|
||||
def promotion_table_data
|
||||
@promotion_table_data ||= TableData.new(table_data, table_view)
|
||||
@promotion_table_data ||= TableData.new(table_data, table_view, setup_search_method)
|
||||
end
|
||||
|
||||
def set_up_header_footer_views
|
||||
@@ -52,6 +52,20 @@ module ProMotion
|
||||
end
|
||||
end
|
||||
|
||||
def setup_search_method
|
||||
params = self.class.get_searchable_params
|
||||
if params.nil?
|
||||
return nil
|
||||
else
|
||||
@search_method || begin
|
||||
params = self.class.get_searchable_params
|
||||
@search_action = params[:with] || params[:find_by] || params[:search_by] || params[:filter_by]
|
||||
@search_action = method(@search_action) if @search_action.is_a?(Symbol) || @search_action.is_a?(String)
|
||||
@search_action
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def set_up_refreshable
|
||||
if self.class.respond_to?(:get_refreshable) && self.class.get_refreshable
|
||||
if defined?(UIRefreshControl)
|
||||
@@ -141,9 +155,9 @@ module ProMotion
|
||||
new_cell.extend(PM::TableViewCellModule) unless new_cell.is_a?(PM::TableViewCellModule)
|
||||
new_cell.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin
|
||||
new_cell.clipsToBounds = true # fix for changed default in 7.1
|
||||
new_cell.send(:on_load) if new_cell.respond_to?(:on_load)
|
||||
new_cell
|
||||
end
|
||||
|
||||
table_cell.setup(data_cell, self) if table_cell.respond_to?(:setup)
|
||||
table_cell.send(:on_reuse) if !new_cell && table_cell.respond_to?(:on_reuse)
|
||||
table_cell
|
||||
|
||||
@@ -58,4 +58,49 @@ describe "Searchable table spec" do
|
||||
controller.searchDisplayController(controller, didLoadSearchResultsTableView: tableView)
|
||||
end
|
||||
|
||||
describe "custom search" do
|
||||
before do
|
||||
@stabby_controller = TableScreenStabbySearchable.new
|
||||
@proc_controller = TableScreenSymbolSearchable.new
|
||||
end
|
||||
|
||||
after do
|
||||
@stabby_controller = nil
|
||||
@proc_controller = nil
|
||||
end
|
||||
|
||||
it "should allow searching for all the 'New' states using a custom search proc" do
|
||||
@stabby_controller.searchDisplayController(@stabby_controller, shouldReloadTableForSearchString:"New Stabby")
|
||||
@stabby_controller.tableView(@stabby_controller.tableView, numberOfRowsInSection:0).should == 4
|
||||
rows = @stabby_controller.promotion_table_data.search("New stabby")
|
||||
rows.first[:cells].length.should == 4
|
||||
rows.first[:cells].each do |row|
|
||||
# Starts with "New" and ends with "stabby"
|
||||
row[:properties][:searched_title].should.match(/^New(.+)?stabby$/)
|
||||
end
|
||||
end
|
||||
|
||||
it "should allow searching for all the 'New' states using a symbol as a search proc" do
|
||||
@proc_controller.searchDisplayController(@proc_controller, shouldReloadTableForSearchString:"New Symbol")
|
||||
cell_count = @proc_controller.tableView(@proc_controller.tableView, numberOfRowsInSection:0)
|
||||
cell_count.should == 4
|
||||
rows = @proc_controller.promotion_table_data.search("New Symbol")
|
||||
rows.first[:cells].length.should == 4
|
||||
rows.first[:cells].each do |row|
|
||||
# Starts with "New" and ends with "symbol"
|
||||
row[:properties][:searched_title].should.match(/^New(.+)?symbol$/)
|
||||
end
|
||||
end
|
||||
|
||||
it "custom searches empty with stabby proc if there is no match" do
|
||||
@stabby_controller.searchDisplayController(@stabby_controller, shouldReloadTableForSearchString:"Totally Bogus")
|
||||
@stabby_controller.tableView(@stabby_controller.tableView, numberOfRowsInSection:0).should == 0
|
||||
end
|
||||
|
||||
it "custom searches empty with symbol for proc if there is no match" do
|
||||
@proc_controller.searchDisplayController(@proc_controller, shouldReloadTableForSearchString:"Totally Bogus")
|
||||
@proc_controller.tableView(@proc_controller.tableView, numberOfRowsInSection:0).should == 0
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user