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
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 3

def initialize
  super

  @events={}
  @events[:build_cell]=->(cell_identifier, type) { build_cell(cell_identifier, type)}.weak!
  @events[:update_cell]=->(field_set, cell, index_path) { update_field(field_set, cell, index_path)}.weak!
  @events[:update_cell_height]=->(field_set, index_path) { update_cell_height(field_set, index_path) }.weak!
  self

end

Instance Method Details

#bind(tableView, models, fields) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 14

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

#build_cell(cell_identifier, type) ⇒ Object



52
53
54
55
56
57
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 52

def build_cell(cell_identifier, type)
  cell = UITableViewCell.value1(cell_identifier)
  cell.detailTextLabel.adjustsFontSizeToFitWidth = true
  cell.clipsToBounds = true
  cell
end

#field_set_at_index_path(index_path) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 186

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



24
25
26
27
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 24

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

#numberOfSectionsInTableView(tableView) ⇒ Object



116
117
118
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 116

def numberOfSectionsInTableView(tableView)
  @models.length
end

#on(event_name, &block) ⇒ Object



29
30
31
32
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 29

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

#onPrepareForReleaseObject



202
203
204
205
206
207
208
209
210
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 202

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

end

#tableView(tableView, willDisplayCell: cell, forRowAtIndexPath: indexPath) ⇒ Object



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

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, type) unless isReusedCell

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

  cell

end

#update_cell_height(field_set, index_path) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 159

def update_cell_height(field_set, index_path)
  field_set = field_set[:child_field] unless field_set[:child_index].nil?
  return field_set[:height] unless field_set[:height].nil?
  if field_set[:type] == :big_label || field_set[:type] == :checkbox
    26 + 19 * (field_set[:lines] || 2)
  elsif field_set[:type] == :text_view
    110
  elsif field_set[:type] == :dynamic_label
    return 45 if @model.send(field_set[:name]).nil?
    width = 284
    width -= 20 unless field_set[:accessory].nil? || field_set[:accessory] == :none
    width -= 94 if field_set[:show_label]
    height = @model.send(field_set[:name]).sizeWithFont(UIFont.systemFontOfSize(14),
                                                        constrainedToSize: [width, 9999],
                                                        lineBreakMode: UILineBreakModeCharacterWrap).height + 20
    [height, 45].max
  else
    45
  end
end

#update_field(o_field_set, cell, index_path) ⇒ Object



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
97
98
99
100
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 59

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