From a0e62bb7c060b8a0317c14cfc976281613032a31 Mon Sep 17 00:00:00 2001 From: Andrew Havens Date: Mon, 16 Apr 2018 17:15:48 -0700 Subject: [PATCH] Simplify searchable code and fix bug which resulted in only being able to search the first time the screen was visible. --- lib/ProMotion/table/data/table_data.rb | 27 +++----- lib/ProMotion/table/extensions/indexable.rb | 2 +- .../table/extensions/longpressable.rb | 2 +- lib/ProMotion/table/extensions/searchable.rb | 67 ++++++++++--------- lib/ProMotion/table/table.rb | 16 +---- 5 files changed, 51 insertions(+), 63 deletions(-) diff --git a/lib/ProMotion/table/data/table_data.rb b/lib/ProMotion/table/data/table_data.rb index aebd1359..c80e8791 100644 --- a/lib/ProMotion/table/data/table_data.rb +++ b/lib/ProMotion/table/data/table_data.rb @@ -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 @@ -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) @@ -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 = {} @@ -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 diff --git a/lib/ProMotion/table/extensions/indexable.rb b/lib/ProMotion/table/extensions/indexable.rb index fa4acaad..8da7c2d9 100644 --- a/lib/ProMotion/table/extensions/indexable.rb +++ b/lib/ProMotion/table/extensions/indexable.rb @@ -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 diff --git a/lib/ProMotion/table/extensions/longpressable.rb b/lib/ProMotion/table/extensions/longpressable.rb index f17001df..bd2f0335 100644 --- a/lib/ProMotion/table/extensions/longpressable.rb +++ b/lib/ProMotion/table/extensions/longpressable.rb @@ -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 diff --git a/lib/ProMotion/table/extensions/searchable.rb b/lib/ProMotion/table/extensions/searchable.rb index e3b3e0ba..ea3f24ba 100644 --- a/lib/ProMotion/table/extensions/searchable.rb +++ b/lib/ProMotion/table/extensions/searchable.rb @@ -2,51 +2,56 @@ 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 @@ -54,21 +59,23 @@ def set_searchable_param_defaults(params) ######### 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) diff --git a/lib/ProMotion/table/table.rb b/lib/ProMotion/table/table.rb index 9382c0f5..defa1c63 100644 --- a/lib/ProMotion/table/table.rb +++ b/lib/ProMotion/table/table.rb @@ -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 = {}) @@ -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) @@ -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