Class: Tgios::UITextViewBinding

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

Instance Method Summary collapse

Methods inherited from BindingBase

#hook, #prepareForRelease, #unhook

Constructor Details

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

Returns a new instance of UITextViewBinding.



4
5
6
7
8
9
10
11
12
13
# File 'lib/tgios/ui_text_view_binding.rb', line 4

def initialize(model, ui_field, field_name, options={})
  super
  @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

#assign_value_to_fieldObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tgios/ui_text_view_binding.rb', line 15

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



171
172
173
174
175
# File 'lib/tgios/ui_text_view_binding.rb', line 171

def dealloc
  prepareForRelease
  ap 'dealloc ui_text_view input_binding'
  super
end

#did_return(field) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/tgios/ui_text_view_binding.rb', line 144

def did_return(field)
  ap 'did return'
  @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

#get_auto_capitalize_type(type = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/tgios/ui_text_view_binding.rb', line 106

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



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/tgios/ui_text_view_binding.rb', line 117

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



130
131
132
133
134
# File 'lib/tgios/ui_text_view_binding.rb', line 130

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

#model=(val) ⇒ Object



39
40
41
42
# File 'lib/tgios/ui_text_view_binding.rb', line 39

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

#on(event, &block) ⇒ Object



50
51
52
# File 'lib/tgios/ui_text_view_binding.rb', line 50

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

#onPrepareForReleaseObject



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/tgios/ui_text_view_binding.rb', line 159

def onPrepareForRelease
  @model=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

#textViewDidBeginEditing(field) ⇒ Object



152
153
154
155
156
157
# File 'lib/tgios/ui_text_view_binding.rb', line 152

def textViewDidBeginEditing(field)
  ap 'did_begin_editing'
  weak_text_field=WeakRef.new(field)
  @events[:begin_edit].call(@model, @field_name, {text_field: weak_text_field}) unless @events[:begin_edit].nil?

end

#textViewDidEndEditing(field) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/tgios/ui_text_view_binding.rb', line 136

def textViewDidEndEditing(field)
  ap 'did_end_editing'
  @model.send("#{@field_name}=", field.text)
  weak_text_field=WeakRef.new(field)
  @events[:end_edit].call(@model, @field_name, {text_field: weak_text_field}) unless @events[:end_edit].nil?

end

#ui_field=(val) ⇒ Object



32
33
34
35
36
37
# File 'lib/tgios/ui_text_view_binding.rb', line 32

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



44
45
46
47
48
# File 'lib/tgios/ui_text_view_binding.rb', line 44

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


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tgios/ui_text_view_binding.rb', line 88

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


  text_view_toolbar = PlasticCup::Base.style(UIToolbar.new, frame: CGRectMake(0,0,320,44))
  done_button = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemDone, target: self, action: 'did_return:')

  text_view_toolbar.items=[
      UIBarButtonItem.flexible_space, done_button
  ]
  @ui_field.inputAccessoryView = text_view_toolbar

end