Class: Tgios::UITableViewModelBinding
- Inherits:
-
BindingBase
- Object
- BindingBase
- Tgios::UITableViewModelBinding
- Defined in:
- lib/tgios/ui_table_view_model_binding.rb
Instance Method Summary collapse
- #bind(tableView, model, fields) ⇒ Object
- #bindings_prepare_release ⇒ Object
- #build_cell(cell_identifier, type) ⇒ Object
- #create_or_update_field(field_set, cell, index_path) ⇒ Object
- #field_set_at_index_path(index_path) ⇒ Object
-
#initialize ⇒ UITableViewModelBinding
constructor
A new instance of UITableViewModelBinding.
- #listen_to_keyboard ⇒ Object
- #model=(value) ⇒ Object
- #on(event_name, &block) ⇒ Object
- #onPrepareForRelease ⇒ Object
- #reload ⇒ Object
- #stop_listen_to_keyboard ⇒ Object
- #tableView(tableView, heightForRowAtIndexPath: index_path) ⇒ Object
- #update_accessory_type_or_view(field_set, cell, index_path, ui_field) ⇒ Object
- #update_cell_height(field_set, index_path) ⇒ Object
Methods inherited from BindingBase
#dealloc, #hook, #prepareForRelease, #unhook
Constructor Details
#initialize ⇒ UITableViewModelBinding
Returns a new instance of UITableViewModelBinding.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 4 def initialize super PlasticCup::Base.add_style_sheet(:ui_field_default_styles, { clearButtonMode: UITextFieldViewModeWhileEditing, tag: 99 }) unless Base.get_style_sheet(:ui_field_default_styles) PlasticCup::Base.add_style_sheet(:ui_field_without_label, { extends: :ui_field_default_styles, frame: CGRectMake(16, 9, 282, 25) }, :ios6) unless Base.get_style_sheet(:ui_field_without_label) PlasticCup::Base.add_style_sheet(:ui_field_without_label, { extends: :ui_field_default_styles, frame: CGRectMake(16, 9, 292, 25) }) unless Base.get_style_sheet(:ui_field_without_label) PlasticCup::Base.add_style_sheet(:ui_field_with_label, { extends: :ui_field_default_styles, frame: CGRectMake(84, 9, 214, 25) }, :ios6) unless Base.get_style_sheet(:ui_field_with_label) PlasticCup::Base.add_style_sheet(:ui_field_with_label, { extends: :ui_field_default_styles, frame: CGRectMake(114, 9, 194, 25) }) unless Base.get_style_sheet(:ui_field_with_label) PlasticCup::Base.add_style_sheet(:ui_view_default_styles, { font: lambda {UIFont.systemFontOfSize(15)}, backgroundColor: :clear.uicolor, tag: 88 }) unless Base.get_style_sheet(:ui_view_default_styles) @events={} @events[:build_cell]=->(cell_identifier, type) { build_cell(cell_identifier, type) }.weak! @events[:update_cell]=->(field_set, cell, index_path) { create_or_update_field(field_set, cell, index_path)}.weak! @events[:update_accessory]=->(field_set, cell, index_path, ui_field) { update_accessory_type_or_view(field_set, cell, index_path, ui_field)}.weak! @events[:update_cell_height]=->(field_set, index_path) { update_cell_height(field_set, index_path) }.weak! @contact_buttons = [] end |
Instance Method Details
#bind(tableView, model, fields) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 42 def bind(tableView, model, fields) @tableView=WeakRef.new(tableView) @fields=fields bindings_prepare_release @bindings={} @tv_bindings={} @tableView.dataSource=self @tableView.delegate=self self.model=model if @table_utility_binding.nil? @table_utility_binding = UITableViewUtilityBinding.new.bind(@tableView) else @table_utility_binding.bind(@tableView) end self end |
#bindings_prepare_release ⇒ Object
310 311 312 313 314 315 316 317 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 310 def bindings_prepare_release @bindings.values.each do |binding| binding.prepareForRelease end if @bindings.is_a?(Hash) @tv_bindings.values.each do |binding| binding.prepareForRelease end if @tv_bindings.is_a?(Hash) end |
#build_cell(cell_identifier, type) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 89 def build_cell(cell_identifier, type) if type == :checkbox || type == :label_only || type == :array cell = UITableViewCell.default(cell_identifier) cell.textLabel.numberOfLines = 0 if type == :label_only || type == :array cell.textLabel.font = UIFont.systemFontOfSize(14) cell.textLabel.textColor = UIColor.colorWithRed(0, green: 122.0/255.0, blue: 1.0, alpha: 1.0) end else cell = UITableViewCell.value2(cell_identifier) if type == :dynamic_label || type == :big_label cell.detailTextLabel.numberOfLines = 0 cell.detailTextLabel.backgroundColor = :clear.uicolor cell.textLabel.numberOfLines = 0 else cell.textLabel.numberOfLines = 2 end end cell.clipsToBounds = true cell end |
#create_or_update_field(field_set, cell, index_path) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 112 def create_or_update_field(field_set, cell, index_path) ui_field=nil #ap field_set cell.textLabel.text= field_set[:show_label] ? field_set[:label] : '' cell.selectionStyle=UITableViewCellSelectionStyleNone case field_set[:type] when :text, :password, :label ui_field=cell.contentView.viewWithTag(99) if ui_field.nil? ui_field = UITextField.new cell.contentView.addSubview(ui_field) end if field_set[:show_label] PlasticCup::Base.style(ui_field, :ui_field_with_label) else PlasticCup::Base.style(ui_field, :ui_field_without_label) end ui_field.placeholder= field_set[:show_label] ? field_set[:placeholder] : field_set[:label] text_field_binding=@bindings[field_set[:name]] if text_field_binding.nil? text_field_binding=UITextFieldBinding.new(@model, ui_field, field_set[:name], field_set) text_field_binding.on(:return_tapped) do |model, field_name, event| @events[:text_return_tapped].call(model, field_name, event) unless @events[:text_return_tapped].nil? end @bindings[field_set[:name]]=text_field_binding end text_field_binding.on(:begin_edit) do |model, field_name, event| @events[:text_begin_edit].call(model, field_name, event) unless @events[:text_begin_edit].nil? @table_utility_binding.will_scroll_to_index_path(index_path) end text_field_binding.update(ui_field, @model) ui_field.becomeFirstResponder if field_set[:first_responder] # TODO: not work when cell is not visible, buggy when :big_label, :dynamic_label cell.detailTextLabel.text = @model.send(field_set[:name]) when :array if field_set[:child_index].nil? cell.textLabel.text = field_set[:label] else child_field = field_set[:child_field] child = @model.send(field_set[:name])[field_set[:child_index]] if child_field[:type] == :checkbox cell.textLabel.text = child[child_field[:display_field]] cell.imageView.image = child[:selected] ? 'tick_select.png'.uiimage : 'tick_deselect.png'.uiimage else cell.textLabel.text = nil cell.detailTextLabel.numberOfLines = 0 cell.detailTextLabel.text = child[child_field[:display_field]] end end when :checkbox cell.textLabel.text = field_set[:label] cell.imageView.image = @model.send(field_set[:name])== true ? 'tick_select.png'.uiimage : 'tick_deselect.png'.uiimage when :label_only cell.textLabel.text = field_set[:label] when :text_view ui_field=cell.contentView.viewWithTag(88) ui_field_default_frame = Rect([[16, 9], [282, 90]]) if ui_field.nil? ui_field = PlasticCup::Base.style(UITextView.new, :ui_view_default_styles) cell.contentView.addSubview(ui_field) end ui_field.frame = ui_field_default_frame text_field_binding=@tv_bindings[field_set[:name]] if text_field_binding.nil? text_field_binding=UITextViewBinding.new(@model, ui_field, field_set[:name], field_set) text_field_binding.on(:return_tapped) do |model, field_name, event| @events[:text_return_tapped].call(model, field_name, event) unless @events[:text_return_tapped].nil? end @tv_bindings[field_set[:name]]=text_field_binding end text_field_binding.on(:begin_edit) do |model, field_name, event| @events[:text_begin_edit].call(model, field_name, event) unless @events[:text_begin_edit].nil? @table_utility_binding.will_scroll_to_index_path(index_path) end text_field_binding.update(ui_field, @model) ui_field.becomeFirstResponder if field_set[:first_responder] # TODO: not work when cell is not visible, buggy end ui_field end |
#field_set_at_index_path(index_path) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 286 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 = @model.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 |
#listen_to_keyboard ⇒ Object
302 303 304 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 302 def listen_to_keyboard @table_utility_binding.listen_to_keyboard end |
#model=(value) ⇒ Object
59 60 61 62 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 59 def model=(value) @model=WeakRef.new(value) @tableView.reloadData() end |
#on(event_name, &block) ⇒ Object
68 69 70 71 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 68 def on(event_name, &block) @events[event_name]=block.weak! self end |
#onPrepareForRelease ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 319 def onPrepareForRelease bindings_prepare_release @table_utility_binding.prepareForRelease @bindings=nil @tv_bindings=nil @events=nil @tableView.dataSource=nil @tableView.delegate=nil @tableView=nil @model=nil end |
#reload ⇒ Object
64 65 66 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 64 def reload @tableView.reloadData end |
#stop_listen_to_keyboard ⇒ Object
306 307 308 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 306 def stop_listen_to_keyboard @table_utility_binding.stop_listen_to_keyboard end |
#tableView(tableView, heightForRowAtIndexPath: index_path) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 73 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.to_s}" cell=tableView.dequeueReusableCellWithIdentifier(cell_identifier) isReusedCell=!cell.nil? cell=@events[:build_cell].call(cell_identifier, type) unless isReusedCell weak_cell=WeakRef.new(cell) ui_field=@events[:update_cell].call(field_set, weak_cell, index_path) unless @events[:update_cell].nil? @events[:update_accessory].call(field_set, weak_cell, index_path, ui_field) unless @events[:update_accessory].nil? cell end |
#update_accessory_type_or_view(field_set, cell, index_path, ui_field) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 208 def update_accessory_type_or_view(field_set, cell, index_path, ui_field) accessory = field_set[:child_index].nil? ? field_set[:accessory] : field_set[:child_field][:accessory] accessory_view = cell.accessoryView if accessory == :contact if accessory_view && accessory_view.is_a?(UIButton) && accessory_view. == :contact. && accessory_view.tag == 52 = accessory_view else = (@contact_buttons.pop || Base.style(UIButton.contact, tag: 52)) cell.accessoryView = end unhook(, :tapped) hook(, :tapped) do tableView(@tableView, didSelectRowAtIndexPath:index_path) end else if accessory_view && accessory_view.is_a?(UIButton) && accessory_view. == :contact. && accessory_view.tag == 52 @contact_buttons << accessory_view cell.accessoryView = nil end cell.accessoryType = (accessory || :none).uitablecellaccessory cell.accessoryView = nil end end |
#update_cell_height(field_set, index_path) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/tgios/ui_table_view_model_binding.rb', line 265 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 |