Skip to content

Commit

Permalink
Merge pull request #679 from clearsightstudio/extract-table-methods
Browse files Browse the repository at this point in the history
Extract methods from tablescreen for reuse in modules for redpotion.
  • Loading branch information
jamonholmgren committed May 12, 2015
2 parents 0047517 + a375c9d commit ef174d4
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 140 deletions.
7 changes: 6 additions & 1 deletion lib/ProMotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 1 addition & 28 deletions lib/ProMotion/table/data/table_data.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions lib/ProMotion/table/data/table_data_builder.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/ProMotion/table/grouped_table_screen.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
112 changes: 1 addition & 111 deletions lib/ProMotion/table/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions lib/ProMotion/table/table_builder.rb
Original file line number Diff line number Diff line change
@@ -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
78 changes: 78 additions & 0 deletions lib/ProMotion/table/table_class_methods.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/ProMotion/table/table_screen.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ProMotion
class TableScreen < TableViewController
include ProMotion::ScreenModule
include ProMotion::TableBuilder
include ProMotion::Table
end
end

0 comments on commit ef174d4

Please sign in to comment.