Module: ProMotion::DataTable

Includes:
Styling, Table, Table::Indexable, Table::Longpressable, Table::Refreshable, Table::Searchable, Table::Utils, TableBuilder, TableDataBuilder, TableClassMethods
Included in:
DataTableScreen
Defined in:
lib/project/pro_motion/data_table.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Table::Searchable

#dt_searchDisplayController, #dt_searchDisplayControllerWillBeginSearch, #dt_searchDisplayControllerWillEndSearch, #make_data_table_searchable, #new_frc_with_search, #reset_search_frc, #search_delegate, #search_fetch_controller

Methods included from Table

#set_up_searchable

Class Method Details

.included(base) ⇒ Object



134
135
136
# File 'lib/project/pro_motion/data_table.rb', line 134

def self.included(base)
  base.extend(TableClassMethods)
end

Instance Method Details

#cell_at(args = {}) ⇒ Object



104
105
106
107
108
# File 'lib/project/pro_motion/data_table.rb', line 104

def cell_at(args = {})
  index_path = args.is_a?(Hash) ? args[:index_path] : args
  c = object_at_index(index_path).cell
  set_data_cell_defaults(c)
end

#controller(controller, didChangeObject: task, atIndexPath: index_path, forChangeType: change_type, newIndexPath: new_index_path) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/project/pro_motion/data_table.rb', line 179

def controller(controller, didChangeObject: task, atIndexPath: index_path, forChangeType: change_type, newIndexPath: new_index_path)
  unless searching?
    case change_type
    when NSFetchedResultsChangeInsert
      table_view.insertRowsAtIndexPaths([new_index_path], withRowAnimation: UITableViewRowAnimationAutomatic)
    when NSFetchedResultsChangeDelete
      table_view.deleteRowsAtIndexPaths([index_path], withRowAnimation: UITableViewRowAnimationAutomatic)
    when NSFetchedResultsChangeUpdate
      table_view.reloadRowsAtIndexPaths([index_path], withRowAnimation: UITableViewRowAnimationAutomatic)
    end
  end
end

#controllerDidChangeContent(controller) ⇒ Object



192
193
194
# File 'lib/project/pro_motion/data_table.rb', line 192

def controllerDidChangeContent(controller)
  table_view.endUpdates unless searching?
end

#controllerWillChangeContent(controller) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/project/pro_motion/data_table.rb', line 171

def controllerWillChangeContent(controller)
  # TODO - we should update the search results table when a new record is added
  # or deleted or changed. For now, when the data changes, the search doesn't
  # update. Closing the search will update the data and then searching again
  # will show the new or changed content.
  table_view.beginUpdates unless searching?
end

#data_modelObject



114
115
116
# File 'lib/project/pro_motion/data_table.rb', line 114

def data_model
  self.class.data_model
end

#data_scopeObject



118
119
120
# File 'lib/project/pro_motion/data_table.rb', line 118

def data_scope
  self.class.data_scope
end

#fetch_controllerObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/project/pro_motion/data_table.rb', line 86

def fetch_controller
  if searching?
    search_fetch_controller
  else
    @fetch_controller ||= NSFetchedResultsController.alloc.initWithFetchRequest(
      fetch_scope.fetch_request,
      managedObjectContext: fetch_scope.context,
      sectionNameKeyPath: nil,
      cacheName: nil
    )
  end
end

#fetch_scopeObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/project/pro_motion/data_table.rb', line 54

def fetch_scope
  @_fetch_scope ||= begin
    if respond_to?(:model_query)
      data_with_scope = model_query
    else
      data_with_scope = data_model.send(data_scope)
    end

    if data_with_scope.sort_descriptors.blank?
      # Try to be smart about how we sort things if a sort descriptor doesn't exist
      attributes = data_model.send(:attribute_names)
      sort_attribute = nil
      [:updated_at, :created_at, :id].each do |att|
        sort_attribute ||= att if attributes.include?(att.to_s)
      end

      if sort_attribute

        unless data_scope == :all
          mp "The `#{data_model}` model scope `#{data_scope}` needs a sort descriptor. Add sort_by(:property) to your scope. Currently sorting by :#{sort_attribute}.", force_color: :yellow
        end
        data_model.send(data_scope).sort_by(sort_attribute)
      else
        # This is where the application says goodbye and dies in a fiery crash.
        mp "The `#{data_model}` model scope `#{data_scope}` needs a sort descriptor. Add sort_by(:property) to your scope.", force_color: :yellow
      end
    else
      data_with_scope
    end
  end
end

#numberOfSectionsInTableView(_) ⇒ Object

UITableViewDelegate methods



139
140
141
# File 'lib/project/pro_motion/data_table.rb', line 139

def numberOfSectionsInTableView(_)
  fetch_controller.sections.count
end

#object_at_index(i) ⇒ Object



110
111
112
# File 'lib/project/pro_motion/data_table.rb', line 110

def object_at_index(i)
  fetch_controller.objectAtIndexPath(i)
end

#on_cell_created(cell, data) ⇒ Object



99
100
101
102
# File 'lib/project/pro_motion/data_table.rb', line 99

def on_cell_created(cell, data)
  # Do not call super here
  self.rmq.build(cell)
end

#original_search_stringObject



130
131
132
# File 'lib/project/pro_motion/data_table.rb', line 130

def original_search_string
  search_string
end

#screen_setupObject



25
26
27
28
29
30
31
32
33
# File 'lib/project/pro_motion/data_table.rb', line 25

def screen_setup
  set_up_fetch_controller

  set_up_header_footer_views
  set_up_searchable
  set_up_refreshable
  set_up_longpressable
  set_up_row_height
end

#search_stringObject



126
127
128
# File 'lib/project/pro_motion/data_table.rb', line 126

def search_string
  @_data_table_search_string
end

#searching?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/project/pro_motion/data_table.rb', line 122

def searching?
  @_data_table_searching || false
end

#set_up_fetch_controllerObject



35
36
37
38
39
40
41
42
# File 'lib/project/pro_motion/data_table.rb', line 35

def set_up_fetch_controller
  error_ptr = Pointer.new(:object)
  fetch_controller.delegate = self

  unless fetch_controller.performFetch(error_ptr)
    raise "Error performing fetch: #{error_ptr[2].description}"
  end
end

#table_dataObject

This has to be defined in order to inherit everything from TableScreen



21
22
23
# File 'lib/project/pro_motion/data_table.rb', line 21

def table_data
  [{cells:[]}]
end

#table_viewObject



16
17
18
# File 'lib/project/pro_motion/data_table.rb', line 16

def table_view
  self.view
end

#tableView(table_view, heightForRowAtIndexPath: index_path) ⇒ Object



143
144
145
# File 'lib/project/pro_motion/data_table.rb', line 143

def tableView(table_view, numberOfRowsInSection: section)
  fetch_controller.sections[section].numberOfObjects
end

#update_table_data(notification = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/project/pro_motion/data_table.rb', line 44

def update_table_data(notification = nil)
  if notification.nil?
    table_view.reloadData
  else
    Dispatch::Queue.main.async do
      fetch_controller.managedObjectContext.mergeChangesFromContextDidSaveNotification(notification)
    end
  end
end