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
16
# 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.clipsToBounds = 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



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

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 154

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



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

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

#numberOfSectionsInTableView(tableView) ⇒ Object



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

def numberOfSectionsInTableView(tableView)
  @models.length
end

#on(event_name, &block) ⇒ Object



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

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

#onPrepareForReleaseObject



170
171
172
173
174
175
176
177
178
# File 'lib/tgios/ui_table_view_model_list_binding.rb', line 170

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



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

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



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

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