Class: ProMotion::XLFormScreen

Inherits:
XlFormViewController show all
Includes:
ScreenModule, XLFormModule
Defined in:
lib/ProMotion/XLForm/xl_form_screen.rb

Direct Known Subclasses

XLSubFormScreen

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from XLFormModule

included

Methods inherited from XlFormViewController

#didRotateFromInterfaceOrientation, #loadView, new, #shouldAutorotate, #shouldAutorotateToInterfaceOrientation, #viewDidAppear, #viewDidDisappear, #viewWillAppear, #viewWillDisappear, #willRotateToInterfaceOrientation

Instance Attribute Details

#form_object ⇒ Object (readonly)

Returns the value of attribute form_object.



6
7
8
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 6

def form_object
  @form_object
end

Instance Method Details

#add_cell(cell, opts = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 119

def add_cell(cell, opts={})
  if opts[:before]
    row = opts[:before]
    if row.is_a?(Symbol)
      row = cell_with_tag(row)
    end
    self.form.addFormRow(cell, beforeRow: row)
  elsif opts[:after]
    row = opts[:after]
    if row.is_a?(Symbol)
      row = cell_with_tag(row)
    end
    self.form.addFormRow(cell, afterRow: row)
  else
    mp "Don't know where to add cell, please provides either :before or :after", force_color: :red
  end
end

#add_section(section, opts = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 137

def add_section(section, opts={})
  if opts[:index]
    self.form.addFormSection(section, atIndex: opts[:index])
  elsif opts[:after]
    self.form.addFormSection(section, afterSection: opts[:after])
  else
    self.form.addFormSection(section)
  end
end

#cell_with_tag(tag) ⇒ Object



108
109
110
111
112
113
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 108

def cell_with_tag(tag)
  if tag.respond_to? :to_s
    tag = tag.to_s
  end
  self.form.formRowWithTag(tag)
end

#display_errors ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 84

def display_errors
  return if valid?

  errors = validation_errors.map do |error|
    error.localizedDescription
  end

  if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1
    alert  = UIAlertController.alertControllerWithTitle(NSLocalizedString('Error', nil),
                                                        message:        errors.join(', '),
                                                        preferredStyle: UIAlertControllerStyleAlert)
    action = UIAlertAction.actionWithTitle(NSLocalizedString('OK', nil),
                                           style:   UIAlertActionStyleDefault,
                                           handler: nil)
    alert.addAction(action)
    presentViewController(alert, animated: true, completion: nil)
  else
    alert         = UIAlertView.new
    alert.title   = NSLocalizedString('Error', nil)
    alert.message = errors.join(', ')
    alert.show
  end
end

#enabled=(value) ⇒ Object



165
166
167
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 165

def enabled=(value)
  self.form.disabled = !value
end

#enabled? ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 169

def enabled?
  !self.form.isDisabled
end

#form_data ⇒ Object



47
48
49
50
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 47

def form_data
  PM.logger.info "You need to implement a `form_data` method in #{self.class.to_s}."
  []
end

#reload(cell) ⇒ Object



115
116
117
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 115

def reload(cell)
  reloadFormRow(cell)
end

#remove_cell!(cell) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 155

def remove_cell!(cell)
  if cell.is_a?(Symbol)
    self.form.removeFormRowWithTag(cell.to_s)
  elsif cell.is_a?(String)
    self.form.removeFormRowWithTag(cell)
  else
    self.form.removeFormRow(cell)
  end
end

#remove_section!(section_or_index) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 147

def remove_section!(section_or_index)
  if section_or_index.is_a?(XLFormSectionDescriptor)
    self.form.removeFormSection(section_or_index)
  else
    self.form.removeFormSectionAtIndex(section_or_index)
  end
end

#update_form_data ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 52

def update_form_data
  form_options = self.class.get_form_options
  title        = self.class.title
  required     = form_options[:required]

  @form_builder = PM::XLForm.new(self.form_data,
                                 {
                                     title:    title,
                                     required: required
                                 })
  @form_object  = @form_builder.build
  self.form     = @form_object
end

#valid? ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 78

def valid?
  validation_errors.empty?
end

#values ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 66

def values
  values = {}
  formValues.each do |key, value|
    if value.is_a? XLFormOptionsObject
      value = value.formValue
    end
    values[key] = value
  end

  values
end

#viewDidLoad ⇒ Object



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
40
41
42
43
44
45
# File 'lib/ProMotion/XLForm/xl_form_screen.rb', line 8

def viewDidLoad
  super
  update_form_data

  form_options = self.class.get_form_options

  if form_options[:on_cancel]
    on_cancel = form_options[:on_cancel]
    title     = NSLocalizedString('Cancel', nil)
    item      = :cancel
    if on_cancel.is_a? Hash
      title = on_cancel[:title] if on_cancel[:title]
      item  = on_cancel[:item] if on_cancel[:item]
    end

    set_nav_bar_button :left, {
                                system_item: item,
                                title:       title,
                                action:      'on_cancel:'
                            }
  end

  if form_options[:on_save]
    on_cancel = form_options[:on_save]
    title     = NSLocalizedString('Save', nil)
    item      = :save
    if on_cancel.is_a? Hash
      title = on_cancel[:title] if on_cancel[:title]
      item  = on_cancel[:item] if on_cancel[:item]
    end

    set_nav_bar_button :right, {
                                 system_item: item,
                                 title:       title,
                                 action:      'on_save:'
                             }
  end
end