Skip to content

Commit

Permalink
Simplify searchable code and fix bug which resulted in only being abl…
Browse files Browse the repository at this point in the history
…e to search the first time the screen was visible.
  • Loading branch information
andrewhavens committed Apr 17, 2018
1 parent d424fc1 commit a0e62bb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 63 deletions.
27 changes: 10 additions & 17 deletions lib/ProMotion/table/data/table_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class TableData
include ProMotion::Table::Utils
include ProMotion::TableDataBuilder

attr_accessor :data, :filtered_data, :search_string, :original_search_string, :filtered, :table_view, :search_params
attr_accessor :data, :filtered_data, :table_view

def initialize(data, table_view, search_action = nil)
@search_action ||= search_action
Expand All @@ -27,7 +27,7 @@ def section(index)
end

def sections
self.filtered ? self.filtered_data : self.data
filtered? ? self.filtered_data : self.data
end

def section_length(index)
Expand Down Expand Up @@ -55,8 +55,13 @@ def default_search(cell, search_string)
cell[:searchable] != false && "#{cell[:title]}\n#{cell[:search_text]}".downcase.strip.include?(search_string.downcase.strip)
end

def filtered?
@filtered == true
end

def search(search_string)
start_searching(search_string) # update the search string
@filtered = true
self.filtered_data = []

self.data.compact.each do |section|
new_section = {}
Expand All @@ -74,22 +79,10 @@ def search(search_string)
self.filtered_data << new_section
end
end

self.filtered_data
end

def start_searching(search_string = '')
self.filtered_data = []
self.filtered = true
self.search_string = search_string.downcase.strip
self.original_search_string = search_string
end

def stop_searching
self.filtered_data = []
self.filtered = false
self.search_string = false
self.original_search_string = false
def clear_filter
@filtered = false
end
end
end
2 changes: 1 addition & 1 deletion lib/ProMotion/table/extensions/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ProMotion
module Table
module Indexable
def table_data_index
return nil if self.promotion_table_data.filtered || !self.class.get_indexable
return nil if self.promotion_table_data.filtered? || !self.class.get_indexable

index = self.promotion_table_data.sections.collect{ |section| (section[:title] || " ")[0] } || []
index.unshift("{search}") if self.class.get_searchable
Expand Down
2 changes: 1 addition & 1 deletion lib/ProMotion/table/extensions/longpressable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def on_long_press(gesture)
private

def pressed_table_view
searching? ? @table_search_display_controller.searchResultsTableView : table_view
table_view
end

end
Expand Down
67 changes: 37 additions & 30 deletions lib/ProMotion/table/extensions/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,80 @@ module ProMotion
module Table
module Searchable

def search_controller
@search_controller ||= UISearchController.alloc.initWithSearchResultsController(nil)
end

def make_searchable(params = {})
params = set_searchable_param_defaults(params)
def make_searchable(params = nil) # params argument is deprecated. No longer need to use it.
params = get_searchable_params

self.definesPresentationContext = true
search_controller = UISearchController.alloc.initWithSearchResultsController(nil)
search_controller.delegate = params[:delegate]
search_controller.searchResultsUpdater = params[:search_results_updater]
search_controller.hidesNavigationBarDuringPresentation = params[:hidesNavigationBarDuringPresentation]
search_controller.dimsBackgroundDuringPresentation = params[:obscuresBackgroundDuringPresentation] # iOS 8+ (not deprecated yet)
# search_controller.obscuresBackgroundDuringPresentation = params[:obscuresBackgroundDuringPresentation] # iOS 9.1+ recommends using this instead of dimsBackgroundDuringPresentation
search_controller.hidesNavigationBarDuringPresentation = params[:hides_nav_bar]
search_controller.dimsBackgroundDuringPresentation = params[:obscures_background] # iOS 8+ (not deprecated yet)
# search_controller.obscuresBackgroundDuringPresentation = params[:obscures_background] # iOS 9.1+ recommends using this instead of dimsBackgroundDuringPresentation

search_bar = search_controller.searchBar
search_bar.delegate = params[:search_bar_delegate]
search_bar.autoresizingMask = UIViewAutoresizingFlexibleWidth
search_bar.placeholder = NSLocalizedString(params[:placeholder], nil) if params[:placeholder]
if params[:scopes]
@scopes = params[:scopes]
search_bar.scopeButtonTitles = @scopes
end
tableView.tableHeaderView = search_bar
search_bar.sizeToFit

if params[:hide_initially]
tableView.contentOffset = CGPointMake(0, search_bar.frame.size.height)
if navigationItem && navigationItem.respond_to?(:setSearchController)
# For iOS 11 and later, we place the search bar in the navigation bar.
navigationItem.searchController = search_controller
navigationItem.hidesSearchBarWhenScrolling = params[:hides_search_bar_when_scrolling]
else
# For iOS 10 and earlier, we place the search bar in the table view's header.
search_bar.autoresizingMask = UIViewAutoresizingFlexibleWidth
tableView.tableHeaderView = search_bar
if params[:hide_initially]
tableView.contentOffset = CGPointMake(0, search_bar.frame.size.height)
end
end

@search_controller = search_controller
end

def set_searchable_param_defaults(params)
def get_searchable_params
params = self.class.get_searchable_params.dup

# support camelCase params
params[:search_results_updater] ||= params[:searchResultsUpdater]
params[:hides_nav_bar] = params[:hidesNavigationBarDuringPresentation] if params[:hides_nav_bar].nil?
params[:obscures_background] = params[:obscuresBackgroundDuringPresentation] if params[:obscures_background].nil?
params[:hides_search_bar_when_scrolling] = params[:hidesSearchBarWhenScrolling] if params[:hides_search_bar_when_scrolling].nil?

params[:delegate] ||= self
params[:search_results_updater] ||= self
params[:search_bar_delegate] ||= self

if params[:hidesNavigationBarDuringPresentation].nil?
params[:hidesNavigationBarDuringPresentation] = true
end

if params[:obscuresBackgroundDuringPresentation].nil?
params[:obscuresBackgroundDuringPresentation] = false
end
params[:hides_nav_bar] = true if params[:hides_nav_bar].nil?
params[:obscures_background] = false if params[:obscures_background].nil?
params[:hides_search_bar_when_scrolling] = false if params[:hides_search_bar_when_scrolling].nil?

params
end

######### UISearchControllerDelegate methods #######

def willPresentSearchController(search_controller)
promotion_table_data.start_searching
search_controller.delegate.will_begin_search if search_controller.delegate.respond_to? "will_begin_search"
end

def willDismissSearchController(search_controller)
promotion_table_data.stop_searching
table_view.reloadData
search_controller.delegate.will_end_search if search_controller.delegate.respond_to? "will_end_search"
end

# UISearchResultsUpdating protocol method
######### UISearchResultsUpdating protocol methods #########

def updateSearchResultsForSearchController(search_controller)
search_string = search_controller.searchBar.text
promotion_table_data.search(search_string) if searching?
update_table_data
search_text = search_controller.searchBar.text
if search_text.empty?
promotion_table_data.clear_filter
else
promotion_table_data.search(search_text)
end
table_view.reloadData
end

def searchBar(search_bar, selectedScopeButtonIndexDidChange: selected_scope_index)
Expand Down
16 changes: 2 additions & 14 deletions lib/ProMotion/table/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,7 @@ def set_up_row_height
end

def searching?
self.promotion_table_data.filtered
end

def original_search_string
self.promotion_table_data.original_search_string
end

def search_string
self.promotion_table_data.search_string
self.promotion_table_data.filtered?
end

def update_table_view_data(data, args = {})
Expand All @@ -114,10 +106,6 @@ def update_table_view_data(data, args = {})
else
table_view.reloadData
end

if searching? && @table_search_display_controller.respond_to?(:searchResultsTableView)
@table_search_display_controller.searchResultsTableView.reloadData
end
end

def accessory_toggled_switch(switch)
Expand Down Expand Up @@ -193,7 +181,7 @@ def tableView(_, titleForFooterInSection: section)

# Set table_data_index if you want the right hand index column (jumplist)
def sectionIndexTitlesForTableView(_)
return if self.promotion_table_data.filtered
return if searching?
return self.table_data_index if self.respond_to?(:table_data_index)
nil
end
Expand Down

0 comments on commit a0e62bb

Please sign in to comment.