Class: Tgios::UITextFieldBinding

Inherits:
BindingBase show all
Includes:
PlasticCup
Defined in:
lib/tgios/ui_text_field_binding.rb

Instance Method Summary collapse

Methods inherited from BindingBase

#hook, #prepareForRelease, #unhook

Constructor Details

#initialize(model, ui_field, field_name, options = {}) ⇒ UITextFieldBinding

Returns a new instance of UITextFieldBinding.



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

def initialize(model, ui_field, field_name, options={})
  super

  Base.add_style_sheet(:decimal_button_common, {
      title: '.',
      titleFont: lambda {UIFont.boldSystemFontOfSize(30)},
  }) unless Base.get_style_sheet(:decimal_button_common)
  Base.add_style_sheet(:decimal_button, {
      extends: :decimal_button_common,
      frame: [[0, 163 + 44], [105, 54]],
      highlighted_background_image: Tgios::CommonUIUtility.imageFromColor(UIColor.colorWithRed(0.324, green: 0.352, blue: 0.402, alpha: 1)),
      titleColor: :dark_gray.uicolor,
      highlighted_title_color: :white.uicolor
  }, :ios6) unless Base.get_style_sheet(:decimal_button)

  Base.add_style_sheet(:decimal_button, {
      extends: :decimal_button_common,
      frame: [[0, 162.5 + 44], [104.5, 54]],
      highlighted_background_image: Tgios::CommonUIUtility.imageFromColor(:white),
      titleColor: :black.uicolor
  }) unless Base.get_style_sheet(:decimal_button)

  @field_name=field_name
  @options=options.dup
  %w(precision keyboard ignore_number_addon type auto_correct auto_capitalize reduce_font_size max_length field_style).each do |k|
    instance_variable_set("@#{k}", @options.delete(k.to_sym))
  end

  @events={}
  @ui_field=WeakRef.new(ui_field)
  @model=WeakRef.new(model)
  update(ui_field, model)
end

Instance Method Details

#add_decimal_buttonObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/tgios/ui_text_field_binding.rb', line 143

def add_decimal_button
  if is_decimal? && @ui_field.delegate == self && !@ignore_number_addon
    temp_window = (UIApplication.sharedApplication.windows[1] || UIApplication.sharedApplication.windows[0])
    temp_window.subviews.each do |keyboard|
      if keyboard.description.hasPrefix('<UIPeripheralHost')
        if @decimal_button.nil?
          @decimal_button = Base.style(UIButton.custom, :decimal_button)
          @decimal_button.addTarget(self, action: 'decimal_tapped', forControlEvents: UIControlEventTouchUpInside)
        end
        keyboard.addSubview(@decimal_button)
        break
      end
    end
  end
end

#assign_value_to_fieldObject



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

def assign_value_to_field
  val = @model.send(@field_name)
  if val.is_a?(String) || val.nil?
    @ui_field.text=val
  else
    @original_value = val
    @ui_field.text= if val.respond_to?(:round)
                      default_precision = 0
                      default_precision = 6 unless val.is_a?(Integer)
                      val.round((@precision || default_precision)).to_s
                    else
                      val.to_s
                    end
    @model.send("#{@field_name}=", @ui_field.text)
  end
end

#deallocObject



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

def dealloc
  prepareForRelease
  super
end

#decimal_tappedObject



159
160
161
# File 'lib/tgios/ui_text_field_binding.rb', line 159

def decimal_tapped
  @ui_field.text = "#{@ui_field.text}." unless !@ui_field.text.nil? && @ui_field.text.include?('.')
end

#get_auto_capitalize_type(type = nil) ⇒ Object



239
240
241
242
243
244
245
246
247
248
# File 'lib/tgios/ui_text_field_binding.rb', line 239

def get_auto_capitalize_type(type=nil)
  case type
    when true, nil
      UITextAutocapitalizationTypeSentences
    when false
      UITextAutocapitalizationTypeNone
    else
      type
  end
end

#get_auto_correct_type(type = nil) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/tgios/ui_text_field_binding.rb', line 250

def get_auto_correct_type(type=nil)
  case type
    when true
      UITextAutocorrectionTypeYes
    when false
      UITextAutocorrectionTypeNo
    when nil
      UITextAutocorrectionTypeDefault
    else
      type
  end
end

#get_keyboard_type(type = nil) ⇒ Object



263
264
265
266
267
# File 'lib/tgios/ui_text_field_binding.rb', line 263

def get_keyboard_type(type=nil)
  return type if type.is_a?(Integer)
  ktype = type == :decimal ? :number : type
  (ktype || :default).uikeyboardtype
end

#is_decimal?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/tgios/ui_text_field_binding.rb', line 139

def is_decimal?
  @keyboard == :decimal && is_number_pad?
end

#is_number_pad?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/tgios/ui_text_field_binding.rb', line 135

def is_number_pad?
  @ui_field.keyboardType == UIKeyboardTypeNumberPad
end

#keyboardDidShow(note) ⇒ Object



131
132
133
# File 'lib/tgios/ui_text_field_binding.rb', line 131

def keyboardDidShow(note)
  add_decimal_button if @ui_field.isFirstResponder
end

#listen_keyboardObject



270
271
272
273
274
275
# File 'lib/tgios/ui_text_field_binding.rb', line 270

def listen_keyboard
  if is_decimal? && @ui_field.delegate == self
    NSNotificationCenter.defaultCenter.addObserver(self, selector: 'keyboardDidShow:', name: UIKeyboardDidShowNotification, object: nil)
  end

end

#model=(val) ⇒ Object



65
66
67
68
# File 'lib/tgios/ui_text_field_binding.rb', line 65

def model=(val)
  @model=WeakRef.new(val)
  assign_value_to_field
end

#on(event, &block) ⇒ Object



76
77
78
79
# File 'lib/tgios/ui_text_field_binding.rb', line 76

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

#onPrepareForReleaseObject



281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/tgios/ui_text_field_binding.rb', line 281

def onPrepareForRelease
  stop_listen
  @model=nil
  @decimal_button=nil
  if !@ui_field.nil? && @ui_field.weakref_alive? && @ui_field.delegate == self
    @ui_field.delegate = nil
    toolbar = @ui_field.inputAccessoryView
    toolbar.items = nil unless toolbar.nil?
    @ui_field.inputAccessoryView = nil
  end
  @ui_field=nil
  @events=nil
end

#stop_listenObject



277
278
279
# File 'lib/tgios/ui_text_field_binding.rb', line 277

def stop_listen
  NSNotificationCenter.defaultCenter.removeObserver(self)
end

#textField(textField, shouldChangeCharactersInRange: range, replacementString: string) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/tgios/ui_text_field_binding.rb', line 121

def textField(textField, shouldChangeCharactersInRange:range, replacementString:string)
  unless @max_length.is_a?(Numeric)
    return true
  end

  new_length = textField.text.length - range.length + string.length

  return new_length <= @max_length || string.include?("\n")
end

#textFieldDidBeginEditing(textField) ⇒ Object



96
97
98
99
100
# File 'lib/tgios/ui_text_field_binding.rb', line 96

def textFieldDidBeginEditing(textField)
  add_decimal_button
  weak_text_field=WeakRef.new(textField)
  @events[:begin_edit].call(@model, @field_name, {text_field: weak_text_field}) unless @events[:begin_edit].nil?
end

#textFieldDidEndEditing(textField) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/tgios/ui_text_field_binding.rb', line 82

def textFieldDidEndEditing(textField)
  puts "textFieldDidEndEditing"
  @model.send("#{@field_name}=", textField.text)
  weak_text_field=WeakRef.new(textField)
  @events[:end_edit].call(@model, @field_name, {text_field: weak_text_field}) unless @events[:end_edit].nil?
  @decimal_button.removeFromSuperview unless @decimal_button.nil?
end

#textFieldShouldBeginEditing(textField) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/tgios/ui_text_field_binding.rb', line 102

def textFieldShouldBeginEditing(textField)
  if @events[:should_edit].nil?
    true
  else
    weak_text_field=WeakRef.new(textField)
    @events[:should_edit].call(@model, @field_name, {text_field: weak_text_field})
  end
end

#textFieldShouldClear(textField) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/tgios/ui_text_field_binding.rb', line 111

def textFieldShouldClear(textField)
  if @events[:should_clear].nil?
    true
  else
    weak_text_field=WeakRef.new(textField)
    @events[:should_clear].call(@model, @field_name, {text_field: weak_text_field})
  end
end

#textFieldShouldReturn(textField) ⇒ Object



90
91
92
93
94
# File 'lib/tgios/ui_text_field_binding.rb', line 90

def textFieldShouldReturn(textField)
  @ui_field.resignFirstResponder
  weak_text_field=WeakRef.new(@ui_field)
  @events[:return_tapped].call(@model, @field_name, {text_field: weak_text_field}) unless @events[:return_tapped].nil?
end

#ui_field=(val) ⇒ Object



58
59
60
61
62
63
# File 'lib/tgios/ui_text_field_binding.rb', line 58

def ui_field=(val)
  @ui_field=WeakRef.new(val)
  @ui_field.delegate=self
  update_ui_field_style
  assign_value_to_field
end

#update(ui_field, model) ⇒ Object



70
71
72
73
74
# File 'lib/tgios/ui_text_field_binding.rb', line 70

def update(ui_field, model)
  self.ui_field=ui_field
  self.model=model

end

#update_ui_field_styleObject

options type:

:password
:label

auto_correct:

true
false
UITextAutocorrectionType

auto_capitalize:

true
false
UITextAutocapitalizationType

keyboard:

:decimal
uikeyboardtype (sugarcube)
UIKeyboardType

precision: Numeric (only useful when value is Numeric)

default:
  Integer: 0
  Float:   6

error:

true
false

reduce_font_size:

true
false


197
198
199
200
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
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/tgios/ui_text_field_binding.rb', line 197

def update_ui_field_style
  Base.style(@ui_field, @field_style) if @field_style.present?
  @ui_field.secureTextEntry=@type==:password
  @ui_field.autocorrectionType = get_auto_correct_type(@auto_correct)
  @ui_field.autocapitalizationType = get_auto_capitalize_type(@auto_capitalize)
  @ui_field.keyboardType = get_keyboard_type(@keyboard)
  @ui_field.enabled = @type != :label
  @ui_field.adjustsFontSizeToFitWidth = @reduce_font_size

  if @model.respond_to?(:has_error?) && @model.has_error?(@field_name)
    @ui_field.leftViewMode = UITextFieldViewModeAlways
    if @ui_field.leftView.nil? || @ui_field.leftView.tag != 888
      error_label_styles ={frame: [[0,0], [25,25]],
                           textColor: :red.uicolor,
                           backgroundColor: :clear.uicolor,
                           font: 'GillSans-Bold'.uifont(25),
                           textAlignment: :center.nstextalignment,
                           text: '!',
                           tag: 888}
      error_label = Base.style(UILabel.new, error_label_styles)
      @ui_field.leftView = error_label
    end
  else
    @ui_field.leftViewMode = UITextFieldViewModeNever
  end

  if is_number_pad? && !@ignore_number_addon
    text_toolbar = Base.style(UIToolbar.new, frame: CGRectMake(0,0,320,44))
    done_button = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemDone, target: self, action: 'textFieldShouldReturn:')
    text_toolbar.items=[
        UIBarButtonItem.flexible_space, done_button
    ]
    @ui_field.inputAccessoryView = text_toolbar
  else
    @ui_field.inputAccessoryView = nil

  end

  stop_listen
  listen_keyboard
end