Class: Tgios::SearchController

Inherits:
ExtendedUIViewController show all
Includes:
ExtendedUITableView
Defined in:
lib/tgios/search_controller.rb

Constant Summary collapse

Events =
[:record_selected, :search, :after_layout, :load_more]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExtendedUITableView

#add_full_table_view_to, #add_title_to_table_header, #add_title_to_view

Methods inherited from ExtendedUIViewController

#dealloc, #dismissViewControllerAnimated, #hook, #prepareForRelease, #unhook, #viewDidDisappear

Instance Attribute Details

#field_nameObject

Returns the value of attribute field_name.



5
6
7
# File 'lib/tgios/search_controller.rb', line 5

def field_name
  @field_name
end

#hide_keyboardObject

Returns the value of attribute hide_keyboard.



5
6
7
# File 'lib/tgios/search_controller.rb', line 5

def hide_keyboard
  @hide_keyboard
end

#pop_after_selectedObject

Returns the value of attribute pop_after_selected.



5
6
7
# File 'lib/tgios/search_controller.rb', line 5

def pop_after_selected
  @pop_after_selected
end

#table_list_binding_optionsObject

Returns the value of attribute table_list_binding_options.



5
6
7
# File 'lib/tgios/search_controller.rb', line 5

def table_list_binding_options
  @table_list_binding_options
end

Instance Method Details

#initObject



60
61
62
63
64
65
66
# File 'lib/tgios/search_controller.rb', line 60

def init
  super
  @events={}
  @result=[]
  @pop_after_selected=true
  self
end

#on(event_name, &block) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
# File 'lib/tgios/search_controller.rb', line 7

def on(event_name, &block)
  raise ArgumentError.new("Event not found, valid events are: [#{Events.join(', ')}]") unless Events.include?(event_name)
  @events[event_name]=block
  self
end

#onPrepareForReleaseObject



68
69
70
71
72
73
# File 'lib/tgios/search_controller.rb', line 68

def onPrepareForRelease
  @events=nil
  @search_result_table_binding.prepareForRelease
  @search_result_table_binding=nil
  self.navigationItem.rightBarButtonItem = nil
end

#result=(value) ⇒ Object



13
14
15
16
# File 'lib/tgios/search_controller.rb', line 13

def result=(value)
  @result=value
  @search_result_table_binding.reload(@result) unless @search_result_table_binding.nil?
end

#searchBarSearchButtonClicked(searchBar) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tgios/search_controller.rb', line 43

def searchBarSearchButtonClicked(searchBar)
  searchBar.resignFirstResponder

  unless @events.nil? || @events[:search].nil?
    @events[:search].call(searchBar.text) do |success, result|
      if success
        @result=result
        if @result.count == 1
          select_record(@result.first)
        else
          @search_result_table_binding.reload(@result) unless @search_result_table_binding.nil?
        end
      end
    end
  end
end

#select_record(record) ⇒ Object



18
19
20
21
# File 'lib/tgios/search_controller.rb', line 18

def select_record(record)
  @events[:record_selected].call(record)
  self.navigationController.popViewControllerAnimated(true) if @pop_after_selected
end

#viewDidLoadObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tgios/search_controller.rb', line 23

def viewDidLoad
  super
  @search_result_table = add_full_table_view_to(self.view, :plain)
  @search_result_table_binding=UITableViewListBinding.new.bind(@search_result_table, @result, @field_name, @table_list_binding_options)
  @search_result_table_binding.on(:touch_row) do |record, event|
    select_record(record)
  end.on(:load_more) do |page, index_path, &block|
    @events[:load_more].call(@search_bar.text, page, index_path, &block) unless @events[:load_more].nil?
  end

  @search_bar=UISearchBar.alloc.init
  @search_bar.frame=[[0,0],['100',0]]
  @search_bar.sizeToFit
  @search_bar.delegate=self
  @search_result_table.tableHeaderView=@search_bar
  @search_bar.becomeFirstResponder unless @hide_keyboard
  @events[:after_layout].call(@search_bar, @search_result_table) unless @events[:after_layout].nil?

end