Sha256: 958c1c477a92a6f9bcdbc72540e16f927821cdc1faf815558dc4a070d9a9ef2c

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

# This module adds search functionality, to Screen or TableSection
module MotionPrime
  module HasSearchBar
    def add_search_bar(options = {}, &block)
      target = options.delete(:target)

      @_search_bar = create_search_bar(options)
      @_search_bar.setDelegate self

      if target
        target.addSubview @_search_bar
      elsif is_a?(TableSection)
        self.table_view.tableHeaderView = @_search_bar
      end

      @search_callback = block
      @_search_bar
    rescue
      NSLog("can't add search bar to #{self.class_name_without_kvo}")
    end

    def dealloc
      @_search_bar.try(:setDelegate, nil)
      @_search_bar = nil
      super
    end

    def create_search_bar(options = {})
      name = is_a?(TableSection) ? name : self.class_name_without_kvo.underscore
      screen = is_a?(TableSection) ? self.screen : self
      options[:styles] ||= []
      options[:styles] += [:"base_search_bar", :"base_#{name}_search_bar"]

      screen.search_bar(options).view
    end

    def searchBar(search_bar, textDidChange: text)
      @search_callback.call(text)
    end

    def searchBarSearchButtonClicked(search_bar)
      search_bar.resignFirstResponder
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
motion-prime-0.7.2 motion-prime/helpers/has_search_bar.rb
motion-prime-0.7.1 motion-prime/helpers/has_search_bar.rb
motion-prime-0.7.0 motion-prime/helpers/has_search_bar.rb