diff --git a/lib/ProMotion.rb b/lib/ProMotion.rb index 1abb5b39..6b097616 100644 --- a/lib/ProMotion.rb +++ b/lib/ProMotion.rb @@ -24,8 +24,13 @@ ], "#{core_lib}/screen/screen.rb" => [ "#{core_lib}/screen/screen_module.rb" ], "#{core_lib}/screen/screen_module.rb" => [ "#{core_lib}/screen/screen_navigation.rb" ], - "#{core_lib}/table/data/table_data.rb" => [ "#{core_lib}/table/table.rb" ], + "#{core_lib}/table/data/table_data.rb" => [ + "#{core_lib}/table/data/table_data_builder.rb", + "#{core_lib}/table/table.rb" + ], "#{core_lib}/table/table.rb" => [ + "#{core_lib}/table/table_class_methods.rb", + "#{core_lib}/table/table_builder.rb", "#{core_lib}/table/table_utils.rb", "#{core_lib}/table/extensions/searchable.rb", "#{core_lib}/table/extensions/refreshable.rb", diff --git a/lib/ProMotion/table/data/table_data.rb b/lib/ProMotion/table/data/table_data.rb index 417ad871..a4437bf2 100644 --- a/lib/ProMotion/table/data/table_data.rb +++ b/lib/ProMotion/table/data/table_data.rb @@ -1,6 +1,7 @@ module ProMotion class TableData include ProMotion::Table::Utils + include ProMotion::TableDataBuilder attr_accessor :data, :filtered_data, :search_string, :original_search_string, :filtered, :table_view, :search_params @@ -73,34 +74,6 @@ def stop_searching self.original_search_string = false end - def set_data_cell_defaults(data_cell) - data_cell[:cell_style] ||= begin - data_cell[:subtitle] ? UITableViewCellStyleSubtitle : UITableViewCellStyleDefault - end - data_cell[:cell_class] ||= PM::TableViewCell - data_cell[:cell_identifier] ||= build_cell_identifier(data_cell) - data_cell[:properties] ||= data_cell[:style] || data_cell[:styles] - - data_cell[:accessory] = { - view: data_cell[:accessory], - value: data_cell[:accessory_value], - action: data_cell[:accessory_action], - arguments: data_cell[:accessory_arguments] - } unless data_cell[:accessory].nil? || data_cell[:accessory].is_a?(Hash) - - data_cell - end - - def build_cell_identifier(data_cell) - ident = "#{data_cell[:cell_class].to_s}" - ident << "-#{data_cell[:stylename].to_s}" if data_cell[:stylename] # For Teacup - ident << "-accessory" if data_cell[:accessory] - ident << "-subtitle" if data_cell[:subtitle] - ident << "-remoteimage" if data_cell[:remote_image] - ident << "-image" if data_cell[:image] - ident - end - private def start_searching(search_string) diff --git a/lib/ProMotion/table/data/table_data_builder.rb b/lib/ProMotion/table/data/table_data_builder.rb new file mode 100644 index 00000000..af764cea --- /dev/null +++ b/lib/ProMotion/table/data/table_data_builder.rb @@ -0,0 +1,31 @@ +module ProMotion + module TableDataBuilder + def set_data_cell_defaults(data_cell) + data_cell[:cell_style] ||= begin + data_cell[:subtitle] ? UITableViewCellStyleSubtitle : UITableViewCellStyleDefault + end + data_cell[:cell_class] ||= PM::TableViewCell + data_cell[:cell_identifier] ||= build_cell_identifier(data_cell) + data_cell[:properties] ||= data_cell[:style] || data_cell[:styles] + + data_cell[:accessory] = { + view: data_cell[:accessory], + value: data_cell[:accessory_value], + action: data_cell[:accessory_action], + arguments: data_cell[:accessory_arguments] + } unless data_cell[:accessory].nil? || data_cell[:accessory].is_a?(Hash) + + data_cell + end + + def build_cell_identifier(data_cell) + ident = "#{data_cell[:cell_class].to_s}" + ident << "-#{data_cell[:stylename].to_s}" if data_cell[:stylename] # For Teacup + ident << "-accessory" if data_cell[:accessory] + ident << "-subtitle" if data_cell[:subtitle] + ident << "-remoteimage" if data_cell[:remote_image] + ident << "-image" if data_cell[:image] + ident + end + end +end diff --git a/lib/ProMotion/table/grouped_table_screen.rb b/lib/ProMotion/table/grouped_table_screen.rb index 0f816efb..5b396af0 100644 --- a/lib/ProMotion/table/grouped_table_screen.rb +++ b/lib/ProMotion/table/grouped_table_screen.rb @@ -1,6 +1,7 @@ module ProMotion class GroupedTableScreen < TableViewController include ProMotion::ScreenModule + include ProMotion::TableBuilder include ProMotion::Table include ProMotion::Table::Utils # Workaround until https://hipbyte.freshdesk.com/support/tickets/2086 is fixed. include ProMotion::GroupedTable diff --git a/lib/ProMotion/table/table.rb b/lib/ProMotion/table/table.rb index cc35c071..9abcd2f6 100644 --- a/lib/ProMotion/table/table.rb +++ b/lib/ProMotion/table/table.rb @@ -6,6 +6,7 @@ module Table include ProMotion::Table::Indexable include ProMotion::Table::Longpressable include ProMotion::Table::Utils + include ProMotion::TableBuilder attr_reader :promotion_table_data @@ -129,15 +130,6 @@ def update_table_view_data(data, args = {}) @table_search_display_controller.searchResultsTableView.reloadData if searching? end - def trigger_action(action, arguments, index_path) - return PM.logger.info "Action not implemented: #{action.to_s}" unless self.respond_to?(action) - case self.method(action).arity - when 0 then self.send(action) # Just call the method - when 2 then self.send(action, arguments, index_path) # Send arguments and index path - else self.send(action, arguments) # Send arguments - end - end - def accessory_toggled_switch(switch) table_cell = closest_parent(UITableViewCell, switch) index_path = closest_parent(UITableView, table_cell).indexPathForCell(table_cell) @@ -162,29 +154,6 @@ def delete_row(index_paths, animation = nil) table_view.deleteRowsAtIndexPaths(deletable_index_paths, withRowAnimation: map_row_animation_symbol(animation)) if deletable_index_paths.length > 0 end - def create_table_cell(data_cell) - new_cell = nil - table_cell = table_view.dequeueReusableCellWithIdentifier(data_cell[:cell_identifier]) || begin - new_cell = data_cell[:cell_class].alloc.initWithStyle(data_cell[:cell_style], reuseIdentifier:data_cell[:cell_identifier]) - new_cell.extend(PM::TableViewCellModule) unless new_cell.is_a?(PM::TableViewCellModule) - new_cell.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin - new_cell.clipsToBounds = true # fix for changed default in 7.1 - on_cell_created new_cell, data_cell - new_cell - end - table_cell.setup(data_cell, self) if table_cell.respond_to?(:setup) - on_cell_reused(table_cell, data_cell) if !new_cell - table_cell - end - - def on_cell_created(new_cell, data_cell) - new_cell.send(:on_load) if new_cell.respond_to?(:on_load) - end - - def on_cell_reused(cell, data) - cell.send(:on_reuse) if cell.respond_to?(:on_reuse) - end - def update_table_data(args = {}) args = { index_paths: args } unless args.is_a?(Hash) @@ -371,85 +340,6 @@ def map_row_animation_symbol(symbol) }[symbol] || symbol || UITableViewRowAnimationAutomatic end - module TableClassMethods - def table_style - UITableViewStylePlain - end - - def row_height(height, args={}) - if height == :auto - if UIDevice.currentDevice.systemVersion.to_f < 8.0 - height = args[:estimated] || 44.0 - PM.logger.warn "Using `row_height :auto` is not supported in iOS 7 apps. Setting to #{height}." - else - height = UITableViewAutomaticDimension - end - end - args[:estimated] ||= height unless height == UITableViewAutomaticDimension - @row_height = { height: height, estimated: args[:estimated] || 44.0 } - end - - def get_row_height - @row_height ||= nil - end - - # Searchable - def searchable(params={}) - @searchable_params = params - @searchable = true - end - - def get_searchable_params - @searchable_params ||= nil - end - - def get_searchable - @searchable ||= false - end - - # Refreshable - def refreshable(params = {}) - @refreshable_params = params - @refreshable = true - end - - def get_refreshable - @refreshable ||= false - end - - def get_refreshable_params - @refreshable_params ||= nil - end - - # Indexable - def indexable(params = {}) - @indexable_params = params - @indexable = true - end - - def get_indexable - @indexable ||= false - end - - def get_indexable_params - @indexable_params ||= nil - end - - # Longpressable - def longpressable(params = {}) - @longpressable_params = params - @longpressable = true - end - - def get_longpressable - @longpressable ||= false - end - - def get_longpressable_params - @longpressable_params ||= nil - end - end - def self.included(base) base.extend(TableClassMethods) end diff --git a/lib/ProMotion/table/table_builder.rb b/lib/ProMotion/table/table_builder.rb new file mode 100644 index 00000000..1d6e8d83 --- /dev/null +++ b/lib/ProMotion/table/table_builder.rb @@ -0,0 +1,35 @@ +module ProMotion + module TableBuilder + def trigger_action(action, arguments, index_path) + return PM.logger.info "Action not implemented: #{action.to_s}" unless self.respond_to?(action) + case self.method(action).arity + when 0 then self.send(action) # Just call the method + when 2 then self.send(action, arguments, index_path) # Send arguments and index path + else self.send(action, arguments) # Send arguments + end + end + + def create_table_cell(data_cell) + new_cell = nil + table_cell = table_view.dequeueReusableCellWithIdentifier(data_cell[:cell_identifier]) || begin + new_cell = data_cell[:cell_class].alloc.initWithStyle(data_cell[:cell_style], reuseIdentifier:data_cell[:cell_identifier]) + new_cell.extend(PM::TableViewCellModule) unless new_cell.is_a?(PM::TableViewCellModule) + new_cell.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin + new_cell.clipsToBounds = true # fix for changed default in 7.1 + on_cell_created new_cell, data_cell + new_cell + end + table_cell.setup(data_cell, self) if table_cell.respond_to?(:setup) + on_cell_reused(table_cell, data_cell) if !new_cell + table_cell + end + + def on_cell_created(new_cell, data_cell) + new_cell.send(:on_load) if new_cell.respond_to?(:on_load) + end + + def on_cell_reused(cell, data) + cell.send(:on_reuse) if cell.respond_to?(:on_reuse) + end + end +end diff --git a/lib/ProMotion/table/table_class_methods.rb b/lib/ProMotion/table/table_class_methods.rb new file mode 100644 index 00000000..723502dd --- /dev/null +++ b/lib/ProMotion/table/table_class_methods.rb @@ -0,0 +1,78 @@ +module TableClassMethods + def table_style + UITableViewStylePlain + end + + def row_height(height, args={}) + if height == :auto + if UIDevice.currentDevice.systemVersion.to_f < 8.0 + height = args[:estimated] || 44.0 + PM.logger.warn "Using `row_height :auto` is not supported in iOS 7 apps. Setting to #{height}." + else + height = UITableViewAutomaticDimension + end + end + args[:estimated] ||= height unless height == UITableViewAutomaticDimension + @row_height = { height: height, estimated: args[:estimated] || 44.0 } + end + + def get_row_height + @row_height ||= nil + end + + # Searchable + def searchable(params={}) + @searchable_params = params + @searchable = true + end + + def get_searchable_params + @searchable_params ||= nil + end + + def get_searchable + @searchable ||= false + end + + # Refreshable + def refreshable(params = {}) + @refreshable_params = params + @refreshable = true + end + + def get_refreshable + @refreshable ||= false + end + + def get_refreshable_params + @refreshable_params ||= nil + end + + # Indexable + def indexable(params = {}) + @indexable_params = params + @indexable = true + end + + def get_indexable + @indexable ||= false + end + + def get_indexable_params + @indexable_params ||= nil + end + + # Longpressable + def longpressable(params = {}) + @longpressable_params = params + @longpressable = true + end + + def get_longpressable + @longpressable ||= false + end + + def get_longpressable_params + @longpressable_params ||= nil + end +end diff --git a/lib/ProMotion/table/table_screen.rb b/lib/ProMotion/table/table_screen.rb index 441a7687..85170369 100644 --- a/lib/ProMotion/table/table_screen.rb +++ b/lib/ProMotion/table/table_screen.rb @@ -1,6 +1,7 @@ module ProMotion class TableScreen < TableViewController include ProMotion::ScreenModule + include ProMotion::TableBuilder include ProMotion::Table end end