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



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
# 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, 162.5 + 44], [104.5, 54]],
      highlighted_background_image: Tgios::CommonUIUtility.imageFromColor(:white),
      titleColor: :black.uicolor
  }, :ios7) unless Base.get_style_sheet(:decimal_button)

  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: :darkgray.uicolor,
      highlighted_title_color: :white.uicolor
  }) unless Base.get_style_sheet(:decimal_button)

  @field_name=field_name
  @options=options
  @events={}
  @ui_field=WeakRef.new(ui_field)
  @model=WeakRef.new(model)
  update_ui_field_style
  assign_value_to_field
end

Instance Method Details

#add_decimal_buttonObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tgios/ui_text_field_binding.rb', line 110

def add_decimal_button
  if is_decimal? && @ui_field.delegate == self
    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 = PlasticCup::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



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

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((@options[:precision] || default_precision)).to_s
                    else
                      val.to_s
                    end
    @model.send("#{@field_name}=", @ui_field.text)
  end
end

#deallocObject



261
262
263
264
265
# File 'lib/tgios/ui_text_field_binding.rb', line 261

def dealloc
  prepareForRelease
  ap 'dealloc ui_text_field_binding'
  super
end

#decimal_tappedObject



126
127
128
# File 'lib/tgios/ui_text_field_binding.rb', line 126

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



205
206
207
208
209
210
211
212
213
214
# File 'lib/tgios/ui_text_field_binding.rb', line 205

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



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/tgios/ui_text_field_binding.rb', line 216

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



229
230
231
232
233
# File 'lib/tgios/ui_text_field_binding.rb', line 229

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



106
107
108
# File 'lib/tgios/ui_text_field_binding.rb', line 106

def is_decimal?
  @options[:keyboard] == :decimal && is_number_pad?
end

#is_number_pad?Boolean



102
103
104
# File 'lib/tgios/ui_text_field_binding.rb', line 102

def is_number_pad?
  @ui_field.keyboardType == UIKeyboardTypeNumberPad
end

#keyboardDidShow(note) ⇒ Object



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

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

#listen_keyboardObject



236
237
238
239
240
241
# File 'lib/tgios/ui_text_field_binding.rb', line 236

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



62
63
64
65
# File 'lib/tgios/ui_text_field_binding.rb', line 62

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

#on(event, &block) ⇒ Object



73
74
75
# File 'lib/tgios/ui_text_field_binding.rb', line 73

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

#onPrepareForReleaseObject



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

def onPrepareForRelease
  stop_listen
  @model=nil
  @decimal_button=nil
  if !@ui_field.nil? && @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



243
244
245
# File 'lib/tgios/ui_text_field_binding.rb', line 243

def stop_listen
  NSNotificationCenter.defaultCenter.removeObserver(self)
end

#textFieldDidBeginEditing(textField) ⇒ Object



92
93
94
95
96
# File 'lib/tgios/ui_text_field_binding.rb', line 92

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



78
79
80
81
82
83
84
# File 'lib/tgios/ui_text_field_binding.rb', line 78

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

#textFieldShouldReturn(textField) ⇒ Object



86
87
88
89
90
# File 'lib/tgios/ui_text_field_binding.rb', line 86

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



55
56
57
58
59
60
# File 'lib/tgios/ui_text_field_binding.rb', line 55

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



67
68
69
70
71
# File 'lib/tgios/ui_text_field_binding.rb', line 67

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


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

def update_ui_field_style
  @ui_field.secureTextEntry=@options[:type]==:password
  @ui_field.autocorrectionType = get_auto_correct_type(@options[:auto_correct])
  @ui_field.autocapitalizationType = get_auto_capitalize_type(@options[:auto_capitalize])
  @ui_field.keyboardType = get_keyboard_type(@options[:keyboard])
  @ui_field.enabled = @options[:type] != :label
  @ui_field.adjustsFontSizeToFitWidth = @options[: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.uialignment,
                           text: '!',
                           tag: 888}
      error_label = PlasticCup::Base.style(UILabel.new, error_label_styles)
      @ui_field.leftView = error_label
    end
  else
    @ui_field.leftViewMode = UITextFieldViewModeNever
  end

  if is_number_pad?
    text_toolbar = PlasticCup::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