Sha256: 86981b203cd4fedcff1591e0cc8e12ba19def5e1aa73ed51434021eea185e4bb

Contents?: true

Size: 1.62 KB

Versions: 23

Compression:

Stored size: 1.62 KB

Contents

# This module adds search functionality, to Screen or TableSection
module MotionPrime
  module HasSearchBar
    def add_search_bar(options = {}, &block)
      @_search_timeout = options.delete(:timeout)
      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
      BW::Reactor.cancel_timer(@_search_timer) if @_search_timer
      @_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)
      BW::Reactor.cancel_timer(@_search_timer) if @_search_timer
      if @_search_timeout
        @_search_timer = BW::Reactor.add_timer(@_search_timeout.to_f/1000, proc{ @search_callback.call(text) }.weak!)
      else
        @search_callback.call(text)
      end
    end

    def searchBarSearchButtonClicked(search_bar)
      BW::Reactor.cancel_timer(@_search_timer) if @_search_timer
      @search_callback.call(search_bar.text)
      search_bar.resignFirstResponder
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
motion-prime-0.9.9 motion-prime/helpers/has_search_bar.rb
motion-prime-0.9.8 motion-prime/helpers/has_search_bar.rb
motion-prime-0.9.7 motion-prime/helpers/has_search_bar.rb
motion-prime-0.9.6 motion-prime/helpers/has_search_bar.rb
motion-prime-0.9.5 motion-prime/helpers/has_search_bar.rb
motion-prime-0.9.4 motion-prime/helpers/has_search_bar.rb
motion-prime-0.9.3 motion-prime/helpers/has_search_bar.rb
motion-prime-0.9.2 motion-prime/helpers/has_search_bar.rb
motion-prime-0.9.1 motion-prime/helpers/has_search_bar.rb
motion-prime-0.9.0 motion-prime/helpers/has_search_bar.rb
motion-prime-0.8.12 motion-prime/helpers/has_search_bar.rb
motion-prime-0.8.11 motion-prime/helpers/has_search_bar.rb
motion-prime-0.8.10 motion-prime/helpers/has_search_bar.rb
motion-prime-0.8.9 motion-prime/helpers/has_search_bar.rb
motion-prime-0.8.8 motion-prime/helpers/has_search_bar.rb
motion-prime-0.8.7 motion-prime/helpers/has_search_bar.rb
motion-prime-0.8.6 motion-prime/helpers/has_search_bar.rb
motion-prime-0.8.5 motion-prime/helpers/has_search_bar.rb
motion-prime-0.8.4 motion-prime/helpers/has_search_bar.rb
motion-prime-0.8.3 motion-prime/helpers/has_search_bar.rb