Class: Tgios::UITableViewModelBinding

Inherits:
BindingBase show all
Defined in:
lib/tgios/ui_table_view_model_binding.rb

Instance Method Summary collapse

Methods inherited from BindingBase

#dealloc, #hook, #prepareForRelease, #unhook

Constructor Details

#initializeUITableViewModelBinding

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, 292, 25)
  }, :ios7) 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, 282, 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(114, 9, 194, 25)
  }, :ios7) 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(84, 9, 214, 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) }
  @events[:update_cell]=->(field_set, cell, index_path) { create_or_update_field(field_set, cell, index_path)}
  @events[:update_accessory]=->(field_set, cell, index_path, ui_field) { update_accessory_type_or_view(field_set, cell, index_path, ui_field)}
  @events[:update_cell_height]=->(field_set, index_path) { update_cell_height(field_set, index_path) }
  @contact_buttons = []
end

Instance Method Details

#bind(tableView, model, fields) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# 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={}
  self.model=model
  @tableView.dataSource=self
  @tableView.delegate=self
  self
end

#bindings_prepare_releaseObject



393
394
395
396
397
398
399
400
# File 'lib/tgios/ui_table_view_model_binding.rb', line 393

def bindings_prepare_release
  @bindings.values.each do |binding|
    binding.prepareForRelease
  end if @binding.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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tgios/ui_table_view_model_binding.rb', line 84

def build_cell(cell_identifier, type)
  if type == :checkbox
    cell = UITableViewCell.default(cell_identifier)
    cell.textLabel.numberOfLines = 0
  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
    elsif type == :label_only || type == :array
      cell.detailTextLabel.textColor = UIColor.colorWithRed(0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
    else
      cell.textLabel.numberOfLines = 2
    end
  end
  cell.clipsToBounds = true
  cell
end

#create_or_update_field(field_set, cell, index_path) ⇒ Object



105
106
107
108
109
110
111
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
# File 'lib/tgios/ui_table_view_model_binding.rb', line 105

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?
        performSelector('will_scroll_to_index_path:', withObject: index_path, afterDelay:0.01)
      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.detailTextLabel.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.detailTextLabel.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?
        performSelector('will_scroll_to_index_path:', withObject: index_path, afterDelay:0.01)
      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

#expand_table_view(note) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/tgios/ui_table_view_model_binding.rb', line 313

def expand_table_view(note)
  @expanding = true
  offset_y = @tableView.contentOffset.y
  frame_height = @tableView.frame.size.height
  content_height = @tableView.contentSize.height
  new_offset_height = nil
  if frame_height > content_height
    if offset_y != 0
      new_offset_height = 0
    end
  else
    bottom_offset = frame_height - content_height + offset_y
    if bottom_offset > 0
      new_offset_height = offset_y - bottom_offset
    end
  end

  if new_offset_height.nil?
    reset_content_inset_bottom
  else
    curve = note[UIKeyboardAnimationCurveUserInfoKey]
    duration = note[UIKeyboardAnimationDurationUserInfoKey]
    @animation_proc = -> {
      @tableView.setContentOffset([0, new_offset_height])
    }
    @completion_proc = lambda { |finished|
      reset_content_inset_bottom
    }
    UIView.animateWithDuration(duration-0.01, delay: 0, options: curve,
                               animations: @animation_proc,
                               completion: @completion_proc)
  end
end

#field_set_at_index_path(index_path) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/tgios/ui_table_view_model_binding.rb', line 278

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

#keyboardWillHide(note) ⇒ Object



309
310
311
# File 'lib/tgios/ui_table_view_model_binding.rb', line 309

def keyboardWillHide(note)
  expand_table_view(note)
end

#keyboardWillShow(note) ⇒ Object



305
306
307
# File 'lib/tgios/ui_table_view_model_binding.rb', line 305

def keyboardWillShow(note)
  shrink_table_view(note)
end

#listen_to_keyboardObject



294
295
296
297
298
# File 'lib/tgios/ui_table_view_model_binding.rb', line 294

def listen_to_keyboard
  @scroll_when_editing = true
  NSNotificationCenter.defaultCenter.addObserver(self, selector: 'keyboardWillShow:', name: UIKeyboardWillShowNotification, object: nil)
  NSNotificationCenter.defaultCenter.addObserver(self, selector: 'keyboardWillHide:', name: UIKeyboardWillHideNotification, object: nil)
end

#model=(value) ⇒ Object



54
55
56
57
# File 'lib/tgios/ui_table_view_model_binding.rb', line 54

def model=(value)
  @model=WeakRef.new(value)
  @tableView.reloadData()
end

#on(event_name, &block) ⇒ Object



63
64
65
66
# File 'lib/tgios/ui_table_view_model_binding.rb', line 63

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

#onPrepareForReleaseObject



402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/tgios/ui_table_view_model_binding.rb', line 402

def onPrepareForRelease
  bindings_prepare_release
  self.stop_listen_to_keyboard
  @animation_proc=nil
  @completion_proc=nil
  @bindings=nil
  @tv_bindings=nil
  @events=nil
  @tableView.dataSource=nil
  @tableView.delegate=nil
  @tableView=nil
  @model=nil
end

#reloadObject



59
60
61
# File 'lib/tgios/ui_table_view_model_binding.rb', line 59

def reload
  @tableView.reloadData
end

#reset_content_inset_bottomObject



358
359
360
361
362
363
364
365
366
# File 'lib/tgios/ui_table_view_model_binding.rb', line 358

def reset_content_inset_bottom
  @tableView.contentInset = UIEdgeInsetsZero
  @tableView.scrollIndicatorInsets = UIEdgeInsetsZero
  @expanding = false
  if @shrink_height
    set_content_inset_bottom(@shrink_height)
    @shrink_height = nil
  end
end

#scroll_to_index_path(index_path) ⇒ Object



388
389
390
391
# File 'lib/tgios/ui_table_view_model_binding.rb', line 388

def scroll_to_index_path(index_path)
  @tableView.scrollToRowAtIndexPath(index_path, atScrollPosition: UITableViewScrollPositionBottom, animated: true)
  @index_path_to_scroll = nil
end

#set_content_inset_bottom(height) ⇒ Object



368
369
370
371
372
373
374
375
376
# File 'lib/tgios/ui_table_view_model_binding.rb', line 368

def set_content_inset_bottom(height)
  @tableView.contentInset = UIEdgeInsetsMake(0,0,height,0)
  @tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0,0,height,0)
  @shrinking = false
  if @index_path_to_scroll
    scroll_to_index_path(@index_path_to_scroll)
    @index_path_to_scroll = nil
  end
end

#shrink_table_view(note) ⇒ Object



347
348
349
350
351
352
353
354
355
356
# File 'lib/tgios/ui_table_view_model_binding.rb', line 347

def shrink_table_view(note)
  # TODO: don't shrink when table frame bottom is above the keyboard
  @shrinking = true
  rect = note[UIKeyboardFrameEndUserInfoKey].CGRectValue
  if @expanding
    @shrink_height = rect.size.height
  else
    set_content_inset_bottom(rect.size.height)
  end
end

#stop_listen_to_keyboardObject



300
301
302
303
# File 'lib/tgios/ui_table_view_model_binding.rb', line 300

def stop_listen_to_keyboard
  @scroll_when_editing = false
  NSNotificationCenter.defaultCenter.removeObserver(self)
end

#tableView(tableView, heightForRowAtIndexPath: index_path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tgios/ui_table_view_model_binding.rb', line 68

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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/tgios/ui_table_view_model_binding.rb', line 201

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.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 = (accessory || :none).uitablecellaccessory
    cell.accessoryView = nil
  end
end

#update_cell_height(field_set, index_path) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/tgios/ui_table_view_model_binding.rb', line 258

def update_cell_height(field_set, index_path)
  field_set = field_set[:child_field] unless field_set[:child_index].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

#will_scroll_to_index_path(index_path) ⇒ Object



378
379
380
381
382
383
384
385
386
# File 'lib/tgios/ui_table_view_model_binding.rb', line 378

def will_scroll_to_index_path(index_path)
  if @scroll_when_editing
    if @shrinking
      @index_path_to_scroll = index_path
    else
      scroll_to_index_path(index_path)
    end
  end
end