Class: Tgios::UITableViewModelListBinding

Inherits:
BindingBase
  • Object
show all
Defined in:
lib/tgios/ui_table_view_model_list_binding.rb

Instance Method Summary collapse

Methods inherited from BindingBase

#dealloc, #hook, #prepareForRelease, #unhook

Constructor Details

#initializeUITableViewModelListBinding

Returns a new instance of UITableViewModelListBinding.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 3

def initialize
  super

  @events={}
  @events[:build_cell]=->(cell_identifier) {
    cell = UITableViewCell.value1(cell_identifier)
    cell.detailTextLabel.adjustsFontSizeToFitWidth = true
    cell
  }
  @events[:update_cell]=->(field_set, cell, index_path) { update_field(field_set, cell, index_path)}
  self

end

Instance Method Details

#bind(tableView, models, fields) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 17

def bind(tableView, models, fields)
  @tableView=WeakRef.new(tableView)
  @fields=fields
  @contact_buttons = []
  @models=WeakRef.new(models)
  @tableView.dataSource=self
  @tableView.delegate=self
  self
end

#field_set_at_index_path(index_path) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 147

def field_set_at_index_path(index_path)
  row = index_path.row
  array_indices = @fields.each_index.select{|i| @fields[i][:type] == :array}
  return @fields[row] if array_indices.empty? || array_indices.first >= row
  array_count_sum = 0
  array_indices.each do |a_idx|
    array_count = @models[index_path.section].send(@fields[a_idx][:name]).length
    if row <= a_idx + array_count_sum + array_count
      sub_idx = row - a_idx - array_count_sum - 1
      return sub_idx < 0 ? @fields[a_idx + sub_idx + 1] : @fields[a_idx].merge(child_index: sub_idx)
    end
    array_count_sum += array_count
  end
  @fields[row - array_count_sum]
end

#models=(value) ⇒ Object



27
28
29
30
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 27

def models=(value)
  @models=value
  @tableView.reloadData
end

#numberOfSectionsInTableView(tableView) ⇒ Object



112
113
114
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 112

def numberOfSectionsInTableView(tableView)
  @models.length
end

#on(event_name, &block) ⇒ Object



32
33
34
35
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 32

def on(event_name, &block)
  @events[event_name]=block
  self
end

#onPrepareForReleaseObject



163
164
165
166
167
168
169
170
171
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 163

def onPrepareForRelease
  @contact_buttons = nil
  @events=nil
  @models=nil
  @tableView.delegate=nil
  @tableView.dataSource=nil
  @tableView=nil

end

#tableView(tableView, heightForRowAtIndexPath: index_path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 38

def tableView(tableView, cellForRowAtIndexPath: index_path)

  field_set = field_set_at_index_path(index_path)
  type = field_set[:child_index].nil? ? field_set[:type] : field_set[:child_field][:type]

  cell_identifier = "CELL_IDENTIFIER_#{type}"
  cell=tableView.dequeueReusableCellWithIdentifier(cell_identifier)
  isReusedCell=!cell.nil?

  cell=@events[:build_cell].call(cell_identifier) unless isReusedCell

  @events[:update_cell].call(field_set, cell, index_path)

  cell

end

#update_field(o_field_set, cell, index_path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 55

def update_field(o_field_set, cell, index_path)
  model = @models[index_path.section]
  field_set = o_field_set
  if !field_set[:child_index].nil?
    model = model.send(field_set[:name])[field_set[:child_index]]
    field_set = field_set[:child_field]
  end

  accessory_view = cell.accessoryView
  if field_set[:accessory] == :contact
    if accessory_view && accessory_view.buttonType == :contact.uibuttontype
      contact_button = accessory_view
    else
      contact_button = (@contact_buttons.pop || UIButton.contact)
      cell.accessoryView = contact_button
    end
    unhook(contact_button, :tapped)
    hook(contact_button, :tapped) do
      tableView(@tableView, didSelectRowAtIndexPath:index_path)
    end
  else
    if accessory_view && accessory_view.buttonType == :contact.uibuttontype
      @contact_buttons << accessory_view
      cell.accessoryView = nil
    end
    cell.accessoryType = (field_set[:accessory] || :none).uitablecellaccessory
  end

  case field_set[:type]
    when :array
      cell.textLabel.text=field_set[:label]
      cell.detailTextLabel.text = ''
    when :label, :text
      cell.textLabel.text= field_set[:show_label] ? field_set[:label] : field_set[:label_name].nil? ? '' : model.send(field_set[:label_name])
      cell.detailTextLabel.text = model.send(field_set[:name])
    when :big_label
      cell.detailTextLabel.numberOfLines = 0
      cell.detailTextLabel.backgroundColor = :clear.uicolor
      cell.detailTextLabel.text = model.send(field_set[:name])
  end

end