Class: MotionForm::Base

Inherits:
UITableView
  • Object
show all
Defined in:
lib/project/form/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyboard_avoiding_delegateObject (readonly)

Returns the value of attribute keyboard_avoiding_delegate.



5
6
7
# File 'lib/project/form/base.rb', line 5

def keyboard_avoiding_delegate
  @keyboard_avoiding_delegate
end

Instance Method Details

#build_section(title) ⇒ Object



65
66
67
68
69
# File 'lib/project/form/base.rb', line 65

def build_section(title)
  MotionForm::Section.new(title).tap do |section|
    sections << section
  end
end

#did_begin_editing(notification) ⇒ Object



39
40
41
42
43
# File 'lib/project/form/base.rb', line 39

def did_begin_editing(notification)
  unless window.nil?
    keyboard_avoiding_delegate.textFieldDidBeginEditing(notification.userInfo[:text_field])
  end
end

#did_end_editing(notification) ⇒ Object



45
46
47
48
49
# File 'lib/project/form/base.rb', line 45

def did_end_editing(notification)
  unless window.nil?
    keyboard_avoiding_delegate.textFieldDidEndEditing(notification.userInfo[:text_field])
  end
end

#initObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/project/form/base.rb', line 7

def init
  initWithFrame(frame, style: UITableViewStylePlain).tap do |f|
    f.register_cells
    f.separatorStyle = UITableViewCellSeparatorStyleNone
    f.translatesAutoresizingMaskIntoConstraints = false

    f.dataSource = self
    f.delegate   = self

    f.addGestureRecognizer(tap_recognizer)

    @keyboard_avoiding_delegate = Motion::KeyboardAvoiding.new(f)

    listen
  end
end

#listenObject



34
35
36
37
# File 'lib/project/form/base.rb', line 34

def listen
  observers << notification_center.addObserver(self, selector: 'did_begin_editing:', name: 'FormCellDidBeginEditing', object: nil)
  observers << notification_center.addObserver(self, selector: 'did_end_editing:', name: 'FormCellDidEndEditing', object: nil)
end

#notification_centerObject



55
56
57
# File 'lib/project/form/base.rb', line 55

def notification_center
  NSNotificationCenter.defaultCenter
end

#numberOfSectionsInTableView(table_view) ⇒ Object



81
82
83
# File 'lib/project/form/base.rb', line 81

def numberOfSectionsInTableView(table_view)
  sections.count
end

#observersObject



51
52
53
# File 'lib/project/form/base.rb', line 51

def observers
  @observers ||= []
end

#register_cellsObject



75
76
77
78
79
# File 'lib/project/form/base.rb', line 75

def register_cells
  MotionForm.registered_cells.each do |klass|
    registerClass(klass, forCellReuseIdentifier: klass::IDENTIFIER)
  end
end

#renderObject



140
141
142
143
144
# File 'lib/project/form/base.rb', line 140

def render
  notification_center.postNotificationName('FormWillRender', object: self, userInfo: nil)

  Hash[render_values]
end

#render_valuesObject



146
147
148
# File 'lib/project/form/base.rb', line 146

def render_values
  value_rows.map { |row| [row.key, row.value] }
end

#rowsObject



121
122
123
# File 'lib/project/form/base.rb', line 121

def rows
  sections.map(&:rows).flatten
end

#section(title = '') ⇒ Object



59
60
61
62
63
# File 'lib/project/form/base.rb', line 59

def section(title = '')
  build_section(title).tap do |section|
    yield section
  end
end

#sectionsObject



71
72
73
# File 'lib/project/form/base.rb', line 71

def sections
  @sections ||= []
end

#tableView(table_view, cellForRowAtIndexPath: index_path) ⇒ Object



85
86
87
# File 'lib/project/form/base.rb', line 85

def tableView(table_view, numberOfRowsInSection: section_index)
  sections[section_index].rows.count
end

#tap_recognizerObject



24
25
26
27
28
# File 'lib/project/form/base.rb', line 24

def tap_recognizer
  @tap_recognizer ||= UITapGestureRecognizer.alloc.init.tap do |recognizer|
    recognizer.addTarget(self, action: 'tapped:')
  end
end

#tapped(recognizer) ⇒ Object



30
31
32
# File 'lib/project/form/base.rb', line 30

def tapped(recognizer)
  endEditing(true)
end

#valid?Boolean

Returns:

  • (Boolean)


125
126
127
128
129
# File 'lib/project/form/base.rb', line 125

def valid?
  notification_center.postNotificationName('FormWillValidate', object: self, userInfo: nil)

  rows.select { |row| row.is_a? TextInputRow }.all? { |row| row.valid? }
end

#value_rowsObject



150
151
152
# File 'lib/project/form/base.rb', line 150

def value_rows
  rows.select { |row| row.has_value? }
end