Class: SearchViewController

Inherits:
UIViewController
  • Object
show all
Includes:
SearchDisplay
Defined in:
app/search_view_controller.rb

Instance Method Summary collapse

Methods included from SearchDisplay

#add_search_display

Instance Method Details

#didSearch(index, result: result, query: query) ⇒ Object



29
30
31
32
33
# File 'app/search_view_controller.rb', line 29

def didSearch(index, result:result, query:query)
  @suggestions = result
  # Reload tableView based on results
  @search_display.searchResultsTableView.reloadData
end

#searchBar(search_bar, textDidChange: search_text) ⇒ Object



24
25
26
27
# File 'app/search_view_controller.rb', line 24

def searchBar(search_bar, textDidChange:search_text)
  # Search query in the index
  @as_index.asyncSearch(ASSearchQuery.queryWithString(search_text))
end

#tableView(tableView, numberOfRowsInSection: section) ⇒ Object

update number of rows when reloading tableView



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/search_view_controller.rb', line 72

def tableView(tableView, cellForRowAtIndexPath:indexPath)
  @reuseIdentifier ||= 'ALGOLIASEARCHCELL'

  cell = tableView.dequeueReusableCellWithIdentifier(@reuseIdentifier)
  if cell.nil?
    cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:@reuseIdentifier)

    name_label = ASUILabel.alloc.initWithFrame([[10, 5], [cell.frame.size.width - 20, cell.frame.size.height - 20]])
    name_label.tag = 42
    name_label.font = UIFont.fontWithName("Helvetica-Bold", size:20)
    name_label.backgroundColor = UIColor.clearColor
    name_label.textColor = UIColor.blackColor
    cell.contentView.addSubview(name_label)

    company_label = ASUILabel.alloc.initWithFrame([[10, cell.frame.size.height - 20],[cell.frame.size.width - 20, 20]])
    company_label.tag = 43
    company_label.font = UIFont.fontWithName("Helvetica", size:14)
    company_label.backgroundColor = UIColor.clearColor
    company_label.textColor = UIColor.blackColor
    cell.contentView.addSubview(company_label)

    cell.accessoryType = UITableViewCellAccessoryNone
  end

  hit = @suggestions.hits[indexPath.row]
  contact = hit.userData
  name_label = cell.viewWithTag(42)
  company_label = cell.viewWithTag(43)

  #highlight letters of the results
  name_label.text = @as_index.highlight(contact.name, withHit:hit).highlightedText
  company_label.text = contact.company

  cell
end

#viewDidLoadObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/search_view_controller.rb', line 5

def viewDidLoad
  super
  self.view.backgroundColor = UIColor.whiteColor
  self.add_search_display # boilerplate code, non related to algolia

  # Index creation
  @suggestions = nil
  @as_index = ASIndex.alloc.initWithName("QuickStart", delegate:self, userDataClass:Contact)

  # Indexation
  # Each entry is a Cotact object defined in contact.rb
  unless ASIndex.indexExists("QuickStart")
    @as_index.setEntry(Contact.alloc.initWithName("Kate Bell", andCompany:"Creative Consulting"))
    @as_index.setEntry(Contact.alloc.initWithName("Anna Haro", andCompany:"Apple Inc"))
    @as_index.setEntry(Contact.alloc.initWithName("Anna Bell", andCompany:"Turba Corp"))
    @as_index.publishChanges
  end
end